繁体   English   中英

OpenTK OpenGL纹理未绘制

[英]OpenTK OpenGL texture not drawing

我在四边形上绘制纹理时遇到问题,但仍然保持白色。 我已经浏览了许多指南,但似乎并没有做任何不同的事情。

加载纹理:

        Bitmap bitmap = new Bitmap("Textures/Sprite_Can.png");

        GL.GenTextures(1, out textureID);
        GL.BindTexture(TextureTarget.Texture2D, textureID);

        BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.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);
        bitmap.UnlockBits(data);


        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

设置并应用正交投影:

        GL.MatrixMode(MatrixMode.Projection);

        GL.LoadIdentity();

        GL.Ortho(0, control.Width, 0, control.Height, -1, 1);
        GL.Viewport(0, 0, control.Width, control.Height);  

        GL.MatrixMode(MatrixMode.Modelview);

        GL.LoadIdentity();

        GL.ClearColor(Color4.CornflowerBlue);

最后是平局:

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);


        GL.LoadIdentity();

        GL.Translate(30, 30, 0);
        GL.BindTexture(TextureTarget.Texture2D, textureID);

        GL.Begin(BeginMode.Quads);
        GL.TexCoord2(0, 0);
        GL.Vertex2(-1 * width / 2, 1 * height / 2);

        GL.TexCoord2(1, 0);
        GL.Vertex2(1 * width / 2, 1 * height / 2);

        GL.TexCoord2(1, 1);
        GL.Vertex2(1 * width / 2, -1 * height / 2);

        GL.TexCoord2(0, 1);
        GL.Vertex2(-1 * width / 2, -1 * height / 2);
        GL.End();

        GL.Flush();
        control.SwapBuffers();

因此,基本上,四边形绘制就好了。 但是,不会渲染纹理。 结果,我所拥有的只是一个白色正方形。

在固定功能的OpenGL管道中,还必须Enable纹理单元,然后才能将绑定到一个纹理的纹理应用于绘制的任何对象。

对此的常规OpenGL API绑定是glEnable (GL_TEXTURE_2D) 等效的OpenTK将是: GL.Enable (EnableCap.Texture2D)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM