2012-02-24 274 views
0

我想学习如何使用OpenTK来做OpenGL,并且到目前为止我可以成功地绘制多边形,圆形和三角形,但是我的下一个问题是如何绘制文本?我已经看过他们在C#中的主页上的例子,并将它翻译成VB .NET。OpenTK OpenGL绘图文本

它目前只是绘制一个白色的矩形,所以我希望有人可以发现我的代码中的错误或建议另一种绘制文本的方式。我将列出我的绘画事件。

Paint事件:

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() 

回答

0

你会得到一个白色矩形,如果你的显卡不支持NPOT(非电源的两),纹理尺寸。尝试将位图大小设置为例如256×256。

0

这是一个好方法。如果你打算画很多文字甚至是中等数量,那绝对会破坏表演。你想要做的是寻找到一个名为BMFont程序:

www.angelcode.com/products/bmfont/

这样做是创建文本的纹理图集,随用随一个XML文件一起位置,宽度和高度以及每个字母的偏移量。首先阅读该XML文件,并将每个字符加载到具有各种值的类中。然后,您只需制作一个函数,您可以传递一个绑定地图集的字符串,而不是根据字符串中的字母,绘制一个具有不同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网站,可以帮助上的教程。