2012-02-24 264 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網站,可以幫助上的教程。