C# 不带GL.Begin()的OpenTK文本呈现

C# 不带GL.Begin()的OpenTK文本呈现,c#,opengl,vbo,opentk,vao,C#,Opengl,Vbo,Opentk,Vao,我正在用OpenTK编写一个应用程序,现在我想要呈现文本。从示例中,我拼凑了一个版本,使用Graphics.DrawString()创建了一个包含所需字符的位图。该版本工作得很好,但我很恼火,它依赖于GL.Begin(BeginMode.Quads)和GL.End()来渲染文本,这就是为什么我想从现在开始使用VAOs和VBOs 在我的程序中,我遇到了一个问题,因为我总是在文本应该出现的地方得到单色的正方形 _shader.Use(); GL.BindVertexArray(_VAO); GL.E

我正在用OpenTK编写一个应用程序,现在我想要呈现文本。从示例中,我拼凑了一个版本,使用
Graphics.DrawString()
创建了一个包含所需字符的位图。该版本工作得很好,但我很恼火,它依赖于
GL.Begin(BeginMode.Quads)
和GL.End()来渲染文本,这就是为什么我想从现在开始使用VAOs和VBOs

在我的程序中,我遇到了一个问题,因为我总是在文本应该出现的地方得到单色的正方形

_shader.Use(); GL.BindVertexArray(_VAO); GL.EnableVertexAttribArray(_texCoordLocation); GL.EnableVertexAttribArray(_vertexLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, _charSheet[87].VBO); GL.VertexAttribPointer(_vertexLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, _fontTextureID); GL.VertexAttribPointer(_texCoordLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2); GL.DrawArrays(PrimitiveType.Triangles, 0, 6); GL.BindVertexArray(0); _shader.StopUse(); 到目前为止,我更新函数所做的工作如下: 我像以前一样创建位图,我不明白为什么问题会出在那里。 之后,我创建一个“Char”对象列表,每个对象创建一个VBO,存储位置和纹理坐标,如下所示:

float u_step = (float)GlyphWidth / (float)TextureWidth; float v_step = (float)GlyphHeight / (float)TextureHeight; float u = (float)(character % GlyphsPerLine) * u_step; float v = (float)(character / GlyphsPerLine) * v_step; float x = -GlyphWidth / 2, y = 0; _vertices = new float[]{ x/rect.Width, -GlyphHeight/rect.Height, u, v, -x/rect.Width, -GlyphHeight/rect.Height, u + u_step, v, -x/rect.Width, y/rect.Height, u + u_step, v + v_step, x/rect.Width, -GlyphHeight/rect.Height, u, v, -x/rect.Width, y/rect.Height, u + u_step, v + v_step, x/rect.Width, y/rect.Height, u, v + v_step }; _VBO = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, _VBO); GL.BufferData<float>(BufferTarget.ArrayBuffer, (IntPtr)(sizeof(float) * _vertices.Length), _vertices, BufferUsageHint.DynamicDraw); 我现在创建一个VAO,绑定它,并将一个VBO绑定到它。然后,我设置VertexAttribute指针来解释VBO数据:

_VAO = GL.GenVertexArray(); GL.BindVertexArray(_VAO); GL.BindBuffer(BufferTarget.ArrayBuffer, _charSheet[87].VBO); GL.EnableVertexAttribArray(_vertexLocation); GL.VertexAttribPointer(_vertexLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0); GL.EnableVertexAttribArray(_texCoordLocation); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, _fontTextureID); GL.VertexAttribPointer(_texCoordLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2); GL.BindVertexArray(0); _shader.StopUse(); 片段着色器:

#版本330
输出vec4输出颜色;
在vec2 texCoord;
均匀采样2D纹理0;
void main()
{
outputColor=纹理(纹理0,texCoord);
}

如果已绑定命名缓冲区对象,则将
GL.VertexAttributePointer
的最后一个参数视为缓冲区对象数据存储的字节偏移量。
偏移量必须是
2*sizeof(float)
而不是
2

GL.VertexAttributePointer(_-texCoordLocation,2,VertexAttributePointerType.Float,false,4*sizeof(Float),2)

总账VertexAttributePointer(_texCoordLocation,2, VertexAttributePointerType.Float,false,4*sizeof(Float),2*sizeof(Float));
请参阅和。

感谢您提供了这个非常快速的答案,它也非常有效。您是否同意我的处理方式?@Pablo这是一个很好的开始,但您可能对存储库中的示例感兴趣。哇,非常感谢!我很高兴听到我走在正确的道路上。 _shader.Use(); GL.BindVertexArray(_VAO); GL.EnableVertexAttribArray(_texCoordLocation); GL.EnableVertexAttribArray(_vertexLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, _charSheet[87].VBO); GL.VertexAttribPointer(_vertexLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, _fontTextureID); GL.VertexAttribPointer(_texCoordLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2); GL.DrawArrays(PrimitiveType.Triangles, 0, 6); GL.BindVertexArray(0); _shader.StopUse(); GL.VertexAttribPointer(_texCoordLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2*sizeof(float));