C# OpenTK - Textured Quad

只谈情不闲聊 提交于 2019-12-19 04:49:29

问题


I've recently downloaded OpenTK. I've created a basic game class and a quad. I've tried rendering a texture in my quad but it doesn't work. Here's my code. This is the loading of the texture. (The texture class contains just an ID and a Bitmap. The GetWidth() and the GetHeight() just returns the Bitmap.Width and Bitmap.Height).

        Texture Texture = new Texture ();
        Texture.Bitmap = new Bitmap (Path);
        Texture.ID = GL.GenTexture ();
        GL.BindTexture (TextureTarget.Texture2D, Texture.ID);
        BitmapData data = Texture.Bitmap.LockBits (new Rectangle (0, 0, Texture.GetWidth (), Texture.GetHeight ()), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Texture.GetWidth(), Texture.GetHeight(), 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.Bitmap, data.Scan0);
        Texture.Bitmap.UnlockBits (data);
        GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
        GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
        return Texture;

This is the rendering method.

        GL.Enable (EnableCap.Texture2D);
        GL.BindTexture (TextureTarget.Texture2D, ID);
        GL.Begin (PrimitiveType.Quads);
        GL.TexCoord2 (0, 1); GL.Vertex2 (0, 32);
        GL.TexCoord2 (1, 1); GL.Vertex2 (32, 32);
        GL.TexCoord2 (1, 0); GL.Vertex2 (32, 0);
        GL.TexCoord2 (0, 0); GL.Vertex2 (0, 0);
        GL.End ();
        GL.Disable (EnableCap.Texture2D);

It renders just the quad and nothing else. Can someone please help me?


回答1:


Try replacing:

GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Texture.GetWidth(), Texture.GetHeight(), 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.Bitmap, data.Scan0);

with:

GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Texture.GetWidth(), Texture.GetHeight(), 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, TextureWrapMode.ClampToEdge);

This should solve it. In yours there are format issues where what you used is does not accurately represent how System.Drawing.Bitmap represents 32bpp Argb bitmaps.



来源:https://stackoverflow.com/questions/36535311/c-sharp-opentk-textured-quad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!