Vb.net OpenTK OpenGL绘图文本
我正在尝试学习如何使用OpenTK进行OpenGL,到目前为止,我可以成功地绘制多边形、圆和三角形,但我的下一个问题是如何绘制文本?我在他们的主页上看到了一个C语言的例子,我把它翻译成VB.NET 它目前只画了一个白色的矩形,所以我希望有人能发现我代码中的错误,或者建议另一种方式来画文本。我将只列出我的绘画活动 绘画活动:Vb.net OpenTK OpenGL绘图文本,vb.net,opengl,opentk,Vb.net,Opengl,Opentk,我正在尝试学习如何使用OpenTK进行OpenGL,到目前为止,我可以成功地绘制多边形、圆和三角形,但我的下一个问题是如何绘制文本?我在他们的主页上看到了一个C语言的例子,我把它翻译成VB.NET 它目前只画了一个白色的矩形,所以我希望有人能发现我代码中的错误,或者建议另一种方式来画文本。我将只列出我的绘画活动 绘画活动: GL.Clear(ClearBufferMask.ColorBufferBit) GL.Clear(ClearBufferMask.DepthBufferBi
GL.Clear(ClearBufferMask.ColorBufferBit)
GL.Clear(ClearBufferMask.DepthBufferBit)
Dim text_bmp As Bitmap
Dim text_texture As Integer
text_bmp = New Bitmap(ClientSize.Width, ClientSize.Height)
text_texture = GL.GenTexture()
GL.BindTexture(TextureTarget.Texture2D, text_texture)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, All.Linear)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, All.Linear)
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, text_bmp.Width, text_bmp.Height, 0 _
, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero)
Dim gfx As Graphics
gfx = Graphics.FromImage(text_bmp)
gfx.DrawString("TEST", Me.Font, Brushes.Red, 0, 0)
Dim data As Imaging.BitmapData
data = text_bmp.LockBits(New Rectangle(0, 0, text_bmp.Width, text_bmp.Height), Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0)
text_bmp.UnlockBits(data)
GL.MatrixMode(MatrixMode.Projection)
GL.LoadIdentity()
GL.Ortho(0, width, Height, 0, -1, 1)
GL.Enable(EnableCap.Texture2D)
GL.Enable(EnableCap.Blend)
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha)
GL.Begin(BeginMode.Quads)
GL.TexCoord2(0.0F, 1.0F)
GL.Vertex2(0.0F, 0.0F)
GL.TexCoord2(1.0F, 1.0F)
GL.Vertex2(1.0F, 0.0F)
GL.TexCoord2(1.0F, 0.0F)
GL.Vertex2(1.0F, 1.0F)
GL.TexCoord2(0.0F, 0.0F)
GL.Vertex2(0.0F, 1.0F)
GL.End()
GlControl1.SwapBuffers()
如果您的卡不支持两种纹理大小的NPOT非幂,您将得到一个白色矩形。尝试通过将位图大小设置为256x256来进行测试。这是一种正常的方法。如果你计划画大量的文字,甚至是中等数量的文字,那绝对会破坏性能。您要做的是查看一个名为BMFont的程序: www.angelcode.com/products/bmfont/ 这样做的目的是创建一个文本纹理图谱,以及一个xml文件,其中包含每个字母的位置、宽度、高度和偏移量。首先读取该xml文件,然后将每个字符加载到一个类中,并使用不同的值。然后,您只需创建一个函数,传递一个绑定atlas的字符串,而不是根据字符串中的字母,绘制一个具有纹理坐标的四边形,纹理坐标随xml数据而变化。所以你可以做一个:
for each _char in string
create quad according to xml size
assign texture coordinates relative to xml position
increase position so letters don't draw on top of each other
BMFont网站上有其他语言的教程,可能会有所帮助