【问题标题】:OpenTK textures to display label text are showing up black显示标签文本的 OpenTK 纹理显示为黑色
【发布时间】:2015-07-20 03:15:00
【问题描述】:

我正在尝试使用 OpenTK 在 C# 中制作一个基本的绘图查看器。为了显示标题、x 和 y 轴标签,我从文本字符串中创建了每个标签的位图,并尝试将其用作指定位置的纹理。

位图的字符串在我已将它们保存到磁盘时起作用,并且它们按我想要的方式显示。然而,在将它们加载到纹理中并在屏幕上显示它们之间的某个地方出现了问题。所有显示的都是黑色矩形。它们看起来与我输入的文本的大小差不多,而且它们位于正确的位置,但我无法让它们显示正确的纹理。

将位图加载到纹理中的代码在这里:

public static int LoadTexture(Bitmap bitmap)
    {
        if (bitmap == null)
            throw new ArgumentException("Bitmap is null.");

        int id = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, id);

        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

        BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmpdata.Width, bmpdata.Height, 0,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmpdata.Scan0);

        bitmap.UnlockBits(bmpdata);

        return id;
    }

我尝试显示它的代码部分之一在这里:

                GL.Enable(EnableCap.Texture2D);
                GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (float)TextureEnvMode.Modulate);

                #region Title
                {
                    Bitmap title = Graphics.Utilities.DrawText(_plot2D.Title, _font, _colorScheme.AxesLabels, _colorScheme.BackGround);
                    title.Save(@"S:\Projects\Visual Studio\SCG\Code\Graphics\Ogle\TestFiles\titleTest.png", ImageFormat.Png);

                    int titleID = SCG.Code.Graphics.Utilities.LoadTexture(title);
                    labelTextures.Add(titleID);

                    GL.BindTexture(TextureTarget.Texture2D, titleID);

                    double pl = w * _dim.PlotLeft;
                    double pr = w * _dim.PlotRight;
                    double pb = h * _dim.PlotBottom;
                    double pt = h * _dim.PlotTop;
                    double l = pl + (pr - pl - title.Width) / 2;
                    double r = pl + (pr - pl + title.Width) / 2;
                    double b = pt + (h - pt - title.Height) / 2;
                    double t = pt + (h - pt + title.Height) / 2;

                    GL.Begin(PrimitiveType.Quads);

                    GL.TexCoord2(0, 1); GL.Vertex2(l, b);
                    GL.TexCoord2(1, 1); GL.Vertex2(r, b);
                    GL.TexCoord2(1, 0); GL.Vertex2(r, t);
                    GL.TexCoord2(0, 0); GL.Vertex2(l, t);

                    GL.End();
                }
                #endregion // title

我查看了其他答案,有些人说这与 GL.Enable(TextureCap.Texture2D) 有关,但我尝试将其移动到不同的位置,甚至在每次尝试显示纹理之前复制命令,但是结果没有任何改变。

非常感谢任何帮助。

请注意,我几乎没有 OpenTK 或 OpenGL 方面的经验。

【问题讨论】:

  • 如果您尝试使用透明纹理,请确保您正确设置了 GL 混合状态。
  • 我只是尝试在 OnPaint 方法的开头添加 GL.Enable(EnableCap.Blend) 并在结束时禁用它,它似乎没有任何区别。我还尝试将所有 PixelFormats 更改为 RGB,但这也没有什么不同。
  • 仅仅启用它是不够的。您需要设置适当的混合函数/方程。
  • 嗯,我打算让这些标签不透明。所有使用的颜色都是标准的 Color.Black、Color.White 等,因此它们没有任何透明度。您是否认为它不起作用,因为 PixelType 设置为可能是透明的颜色?这就是为什么我说我尝试将所有 PixelFormat 选项更改为 RGB 而不是 RGBA 但仍然没有用。
  • 嗯,混合/透明度只是您得到的结果的许多可能解释之一。使用 RGB 格式,您可能排除了您的问题。

标签: c# opengl textures opentk


【解决方案1】:

问题在于当前加载的颜色是黑色。要应用纹理,必须将颜色设置为 GL.Color3(Color.Transparent)。然后代码运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2012-07-14
    • 2020-04-17
    • 1970-01-01
    • 2023-02-03
    • 2016-02-22
    相关资源
    最近更新 更多