2015-07-19 163 views
1

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

位图字符串的工作原理是将它们保存到磁盘,它们显示为我想要的。然而,在将它们加载到纹理中并在屏幕上显示它们之间的某处出错了。所有显示的是黑色矩形。他们看起来大小正好适合我输入的文字,并且它们位于正确的位置,但我无法让它们显示正确的纹理。

View of application

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

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的经验。

+0

如果您尝试使用透明纹理,请确保您确实设置了GL混合状态。 – derhass

+0

我刚刚尝试在开始处添加GL.Enable(EnableCap.Blend),并在OnPaint方法结束时禁用它,但似乎没有任何区别。 我也尝试将所有的PixelFormats改为RGB,这也没有改变。 –

+0

只是启用它是不够的。您需要设置适当的混合函数/方程式。 – derhass

回答

0

问题是当前加载的颜色是黑色的。要应用纹理,必须将颜色设置为GL.Color3(Color.Transparent)。然后代码工作正常。