OpenTK OpenGL 绘制文本

发布于 2025-01-08 14:04:34 字数 1956 浏览 0 评论 0原文

我正在尝试学习如何使用 OpenTK 进行 OpenGL,到目前为止我可以成功绘制多边形、圆形和三角形,但我的下一个问题是如何绘制文本?我查看了他们主页上的 C# 示例,并将其翻译为 VB .NET。

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

绘画事件:

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

I am trying to learn how to do OpenGL using OpenTK and I can successfully draw polygons, circles, and triangles so far but my next question is how to draw text? I have looked at the example on their homepage which was in C# and I translated it to VB .NET.

It currently just draws a white rectangle so I was hoping that someone could spot an error in my code or suggest another way to draw text. I will just list my paint event.

Paint event:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆° 2025-01-15 14:04:34

如果您的卡不支持 NPOT(非二次幂)纹理大小,您将得到一个白色矩形。尝试通过将位图大小设置为 256x256 来进行测试。

You'll get a white rectangle if your card doesn't support NPOT (non-power-of-two) texture sizes. Try testing by setting the bitmap size to e.g. 256x256.

苏别ゝ 2025-01-15 14:04:34

这是一个不错的方法。如果您计划绘制大量甚至中等数量的文本,那绝对会破坏性能。你想要做的是查看一个名为 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 网站上有其他语言的教程,可能会有帮助。

That is an ok method. If you plan to draw lots of text or even a medium amount, that will absolutely destroy performance. What you want to do is look into a program called BMFont:

www.angelcode.com/products/bmfont/‎

What this does is create a texture atlas of text, along with an xml file with the positions, width and height and offsets of every letter. You start off by reading that xml file, and loading each character into a class, with the various values. Then you simply make a function that you pass a string which binds the atlas, than depending on the letters in the string, draws a quad with texture coordinates that vary on the xml data. So you might make a:

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

There are tutorials in other languages on the BMFont website which can be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文