// inicializar os shaders de texto e as variáveis correspondentes do OpenGL public TextRenderer(int width, int height) { TextShader = ResourceManager.LoadShader("shaders/text.vert", "shaders/text.frag", "text"); TextShader.Use(); TextShader.SetMatrix4("projection", Matrix4.CreateOrthographicOffCenter(0, width, height, 0, -1, 1)); TextShader.SetInteger("text", 0); VAO = GL.GenVertexArray(); VBO = GL.GenBuffer(); GL.BindVertexArray(VAO); GL.BindBuffer(BufferTarget.ArrayBuffer, VBO); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * 24, IntPtr.Zero, BufferUsageHint.DynamicDraw); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(0, 4, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0); GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
// desenhar cada caracter do texto utilizando a fonte carregada anteriormente // x e y denotam a posição inicial do texto, e o tamanho da fonte é utilizado // para determinar a posição do próximo caractér public void RenderText(string text, float x, float y, float scale, Vector3 color) { TextShader.Use(); TextShader.SetVector3f("textColor", color); GL.ActiveTexture(TextureUnit.Texture0); GL.BindVertexArray(VAO); foreach (char c in text) { Character ch = Characters[c]; float xpos = x + (ch.Bearing.X * scale); float ypos = y + ((Characters['X'].Bearing.Y - ch.Bearing.Y) * scale); float w = ch.Size.X * scale; float h = ch.Size.Y * scale; float[,] vertices = { { xpos, ypos + h, 0.0f, 1.0f }, { xpos + w, ypos, 1.0f, 0.0f }, { xpos, ypos, 0.0f, 0.0f }, { xpos, ypos + h, 0.0f, 1.0f }, { xpos + w, ypos + h, 1.0f, 1.0f }, { xpos + w, ypos, 1.0f, 0.0f } }; GL.BindTexture(TextureTarget.Texture2D, ch.TextureID); GL.BindBuffer(BufferTarget.ArrayBuffer, VBO); GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, sizeof(float) * vertices.Length, vertices); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DrawArrays(PrimitiveType.Triangles, 0, 6); x += ch.Advance * scale; } GL.BindVertexArray(0); GL.BindTexture(TextureTarget.Texture2D, 0); }