使用 openGL 和 C# 加载纹理
你好 我正在尝试加载纹理(.bmp 图像)...
通过调试我看到它已加载...但屏幕上没有出现任何内容! 这是我的代码:
int front_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int back_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int right_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int left_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int top_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int down_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
Terrain.draw_skybox(520, front_ID, back_ID, right_ID, left_ID, top_ID, down_ID);
和方法(draw_skybox):
public static void draw_skybox(float size, int front, int back, int right, int left, int up, int bottom)
{
float scale = 1; // important
Gl.glPushMatrix();
Gl.glScalef(size, size, size);
//Front Face
//Gl.glEnable(Gl.GL_CULL_FACE);
Gl.glCullFace(Gl.GL_BACK);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, front);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, 1);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glEnd();
//Gl.glCullFace(Gl.GL_BACK);
// Gl.glDisable(Gl.GL_CULL_FACE);
//Back Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, back);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, -1);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glEnd();
//right face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, right);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(1, 0, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glEnd();
//Left Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, left);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(-1, 0, 0);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glEnd();
//Top Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, up);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 1, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glEnd();
//Bottom Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, bottom);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, -1, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glEnd();
Gl.glPopMatrix();
}
public static void draw_skybox(float size, int front, int back, int right, int left, int up, int bottom)
{
float scale = 1; // important
Gl.glPushMatrix();
Gl.glScalef(size, size, size);
//Front Face
//Gl.glEnable(Gl.GL_CULL_FACE);
//Gl.glCullFace(Gl.GL_BACK);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, front);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, 1);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glEnd();
//Gl.glCullFace(Gl.GL_BACK);
// Gl.glDisable(Gl.GL_CULL_FACE);
//Back Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, back);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, -1);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glEnd();
//right face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, right);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(1, 0, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glEnd();
//Left Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, left);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(-1, 0, 0);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glEnd();
//Top Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, up);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 1, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glEnd();
//Bottom Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, bottom);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, -1, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glEnd();
Gl.glPopMatrix();
}
请帮助我
hi
i'm trying to load a texture (.bmp image) ...
with debugging i see it has been loaded ... but on the screen nothing appear!
here is my code :
int front_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int back_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int right_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int left_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int top_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
int down_ID = Terrain.LoadGLTextures(@"C:\Users\Ruba\Desktop\our project graphic2\our project graphic\images\top.bmp");
Terrain.draw_skybox(520, front_ID, back_ID, right_ID, left_ID, top_ID, down_ID);
and the method (draw_skybox) :
public static void draw_skybox(float size, int front, int back, int right, int left, int up, int bottom)
{
float scale = 1; // important
Gl.glPushMatrix();
Gl.glScalef(size, size, size);
//Front Face
//Gl.glEnable(Gl.GL_CULL_FACE);
Gl.glCullFace(Gl.GL_BACK);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, front);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, 1);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glEnd();
//Gl.glCullFace(Gl.GL_BACK);
// Gl.glDisable(Gl.GL_CULL_FACE);
//Back Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, back);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, -1);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glEnd();
//right face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, right);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(1, 0, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glEnd();
//Left Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, left);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(-1, 0, 0);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glEnd();
//Top Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, up);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 1, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glEnd();
//Bottom Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, bottom);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, -1, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glEnd();
Gl.glPopMatrix();
}
public static void draw_skybox(float size, int front, int back, int right, int left, int up, int bottom)
{
float scale = 1; // important
Gl.glPushMatrix();
Gl.glScalef(size, size, size);
//Front Face
//Gl.glEnable(Gl.GL_CULL_FACE);
//Gl.glCullFace(Gl.GL_BACK);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, front);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, 1);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glEnd();
//Gl.glCullFace(Gl.GL_BACK);
// Gl.glDisable(Gl.GL_CULL_FACE);
//Back Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, back);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, -1);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glEnd();
//right face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, right);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(1, 0, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glEnd();
//Left Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, left);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(-1, 0, 0);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glEnd();
//Top Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, up);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 1, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, 0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, 0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, 0.5f, 0.5f);
Gl.glEnd();
//Bottom Face
Gl.glBindTexture(Gl.GL_TEXTURE_2D, bottom);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, -1, 0);
Gl.glTexCoord2d(0, 0); Gl.glVertex3f(-0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 0); Gl.glVertex3f(0.5f, -0.5f, -0.5f);
Gl.glTexCoord2d(1, 1); Gl.glVertex3f(0.5f, -0.5f, 0.5f);
Gl.glTexCoord2d(0, 1); Gl.glVertex3f(-0.5f, -0.5f, 0.5f);
Gl.glEnd();
Gl.glPopMatrix();
}
please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该检查一些事情。
纹理是否启用?
您是否在
Terrain.LoadGLTextures(...);
内调用Gl.glGenTextures(...)
和Gl.glTexImage2D(...)
> ?后、右、右绕线顺序底部四边形是错误的。绕线必须是逆时针方向。由于您启用了剔除,因此从立方体体积内部看不到这些四边形。只需交换第一个和第二个即可第二个顶点,以及第三个顶点第四。
确保您是从体积内部查看场景。
你真的想使用Tao框架吗?自 2008 年 5 月以来,Tao 框架就没有更新过,并且自 2010 年 7 月起实际上已经失效。也许您应该看一下 OpenTK 框架:http://www.opentk.com/
A few things you should check.
Is Texturing enabled?
Are you calling
Gl.glGenTextures(...)
andGl.glTexImage2D(...)
insideTerrain.LoadGLTextures(...);
?The winding order of the back, right & bottom quads is wrong. The winding must be counter clock wise. Since you're enabling culling, those quads won't be seen from inside the cube volume. Just swap the first & second vertices, and the third & fourth.
Make sure you're viewing the scene from inside the volume.
Do you really want to use the Tao Framework? The Tao Framework hasn't been updated since may 2008 and is effectivaly defunct since july 2010. Perhaps you should take a look at the OpenTK Framework: http://www.opentk.com/