C#OpenTK-纹理四边形 [英] C# OpenTK - Textured Quad

查看:354
本文介绍了C#OpenTK-纹理四边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近下载了OpenTK.我创建了一个基本的游戏课程和一个四边形.我尝试在四边形中渲染纹理,但是它不起作用.这是我的代码.这是纹理的加载. (纹理类仅包含一个ID和一个位图.GetWidth()和GetHeight()仅返回Bitmap.Width和Bitmap.Height).

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;

这是渲染方法.

        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?

推荐答案

尝试替换:

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

具有:

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);

这应该解决它.在您的格式问题中,您使用的格式不能准确地表示System.Drawing.Bitmap如何表示32bpp Argb位图.

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.

这篇关于C#OpenTK-纹理四边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆