예제 #1
0
        public void DrawSprite(int x, int y)
        {
            var quad = new QuadRecord();

            quad.X = x;
            quad.Y = y;
            PushQuad(quad);
        }
예제 #2
0
        private void PushQuad(QuadRecord record)
        {
            if (m_quadRecordCount == QUAD_BUFFER_SIZE)
            {
                Flush();
            }

            quadRecords[m_quadRecordCount] = record;
            m_quadRecordCount++;
        }
예제 #3
0
        public void DrawText(string text, Vector2 position, TextureFont font, Vector4 color, TextureUnits textureUnit = TextureUnits.GL_TEXTURE0, int layer = 0)
        {
            // TODO? Make a seperate FontBatcher?
            // See http://www.angelcode.com/products/bmfont/doc/render_text.html
            // To improve the rendering...

            var fontSheetWidth  = (float)font.Font.Common.ScaleW;
            var fontSheetHeight = (float)font.Font.Common.ScaleH;

            for (int i = 0; i < text.Length; i++)
            {
                var quad = new QuadRecord();
                quad.X = 32 + i * 32 + 16 * i + position.X;
                quad.Y = 32 + (2 * i) + position.Y;
                quad.W = 32;
                quad.H = 32;

                // Compute the U, V and UW, UH values
                var charId   = (byte)text[i];
                var fontChar = font.CharLookup[charId];

                quad.U = fontChar.X / fontSheetWidth;
                quad.V = fontChar.Y / fontSheetHeight;

                quad.UW = fontChar.Width / fontSheetWidth;
                quad.VH = fontChar.Height / fontSheetHeight;



                PushQuad(quad);
            }

            var device = GameWindow.GraphicsDevice;

            var sX = 2 / (float)GameWindow.ScreenWidth;
            var sY = 2 / (float)GameWindow.ScreenHeight;

            var oX = -1;
            var oY = 1;

            var quadCount = m_quadRecordCount;

            // Edit Vertex Data
            for (int i = 0; i < quadCount; i++)
            {
                var record = quadRecords[i];
                m_Verticies[i * 4 + 0].Position = new Vector3(oX + (record.X + record.W) * sX, oY - (record.Y + record.H) * sY, 0.0f);       // Top Right
                m_Verticies[i * 4 + 1].Position = new Vector3(oX + (record.X + record.W) * sX, oY - (record.Y * sY), 0.0f);                  // Bottom Right
                m_Verticies[i * 4 + 2].Position = new Vector3(oX + record.X * sX, oY - (record.Y * sY), 0.0f);                               // Bottom Left
                m_Verticies[i * 4 + 3].Position = new Vector3(oX + record.X * sX, oY - (record.Y + record.H) * sY, 0.0f);                    // Top Left


                m_Verticies[i * 4 + 0].Texture = new Vector2(record.U + record.UW, record.V + record.VH);                      // Note: Inverted Y
                m_Verticies[i * 4 + 1].Texture = new Vector2(record.U + record.UW, record.V);
                m_Verticies[i * 4 + 2].Texture = new Vector2(record.U, record.V);
                m_Verticies[i * 4 + 3].Texture = new Vector2(record.U, record.V + record.VH);
            }

            m_VertexArrayObject.UpdateVertexData <DefaultQuadBatchVertex>(m_Verticies, 0, quadCount * 6);


            // TODO Make a font.glsl shader
            //    GlBindings.PolygonMode(Face.GL_FRONT_AND_BACK, Mode.GL_LINE);

            GameWindow.GraphicsDevice.BindTexture2D(font.FontTexture.TextureId, OpenGL.TextureUnits.GL_TEXTURE0);
            GameWindow.GraphicsDevice.BindTexture2D(0, OpenGL.TextureUnits.GL_TEXTURE1);

            GameWindow.GraphicsDevice.BindShaderProgram(GameWindow.QuadBatchShader.ShaderProgramId);

            GameWindow.QuadBatchShader.SetUniform("texture1", 0);

            GlBindings.BindVertexArray(m_VertexArrayObject.VaoId);
            GlBindings.DrawElements(PrimitiveType.TriangleList, quadCount * 6, DrawElementsType.UnsignedInt, 0);

            GlBindings.BindVertexArray(0);

            m_quadRecordCount = 0;
        }