1
我想在C#中使用OpenTK製作基本圖形查看器。爲了顯示標題,x和y軸標籤,我正在爲一個文本字符串創建一個位圖,並試圖將其用作指定位置的紋理。OpenTK紋理顯示標籤文本顯示爲黑色
位圖字符串的工作原理是將它們保存到磁盤,它們顯示爲我想要的。然而,在將它們加載到紋理中並在屏幕上顯示它們之間的某處出錯了。所有顯示的是黑色矩形。他們看起來大小正好適合我輸入的文字,並且它們位於正確的位置,但我無法讓它們顯示正確的紋理。
加載位圖到紋理的代碼是在這裏:
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混合狀態。 – derhass
我剛剛嘗試在開始處添加GL.Enable(EnableCap.Blend),並在OnPaint方法結束時禁用它,但似乎沒有任何區別。 我也嘗試將所有的PixelFormats改爲RGB,這也沒有改變。 –
只是啓用它是不夠的。您需要設置適當的混合函數/方程式。 – derhass