渲染器未使用开放 TK 中的纹理

本文关键字:TK 纹理 未使用 | 更新日期: 2023-09-27 17:55:24

嗨,我正在尝试使用Open TK在OpenGL中使用纹理。我从OpenTK框架的官方页面上得到了一个基本的例子,但没有工作。我在stackoverflow上搜索了一些帮助,并更改了代码的某些部分,但仍然不起作用。(发现并测试:OpenTk 纹理不显示图像)

到目前为止,我附带了这个片段,但这并没有显示纹理(而是显示白色):

已更新(附@The提琴手建议)

OpenTK.GLControl.Load事件处理程序中:

GL.ClearColor(Color.MidnightBlue);
GL.Enable(EnableCap.Texture2D);
GL.Viewport(0, 0, control.Width, control.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, 4, 0, 3, -1, 1);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out textureId);
GL.BindTexture(TextureTarget.Texture2D, textureId);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
var bmp = new Bitmap(@"C:'myfolder'texture.png");
var data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
    ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
    OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
System.Windows.Forms.MessageBox.Show("Erro: " + GL.GetError());
bmp.UnlockBits(data);

OpenTK.GLControl.Paint事件处理程序中:

GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.BindTexture(TextureTarget.Texture2D, textureId);
GL.Begin(BeginMode.Quads);
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
GL.TexCoord2(0, 1); GL.Vertex2(0, 1);
GL.TexCoord2(1, 1); GL.Vertex2(1, 1);
GL.TexCoord2(1, 0); GL.Vertex2(1, 0);
GL.End();
control.SwapBuffers();

第一部分得到的错误代码是:1281 = ErrorCode.InvalidValue 。有谁知道为什么它不渲染纹理?

渲染器未使用开放 TK 中的纹理

白色纹理通常意味着(a)不完整的纹理定义或(b)OpenGL错误状态弄乱了渲染。纹理定义看起来正确,因此请尝试调用GL.GetError()以检查是否出现任何 OpenGL 错误。

请注意,PixelInternalFormat.Rgb8需要OpenGL 3.x,因此在使用之前,您需要检查是否支持OpenGL 3.x。或者,尝试使用一直支持OpenGL 1.1的PixelInternalFormat.RgbPixelInternalFormat.Rgba

编辑:您可以在"纹理.cs"中找到完整的工作示例。您可以通过 OpenTK 安装中的示例浏览器(OpenGL -> 1.x -> 纹理)运行此操作。