c# 定义四边形,C#OpenTK-纹理四边形

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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值