使用 openGL 和 C# 加载纹理

发布于 2024-10-09 20:46:06 字数 6875 浏览 8 评论 0原文

你好 我正在尝试加载纹理(.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2024-10-16 20:46:06

您应该检查一些事情。

纹理是否启用?

Gl.glEnable(Gl.GL_TEXTURE_2D);

您是否在 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?

Gl.glEnable(Gl.GL_TEXTURE_2D);

Are you calling Gl.glGenTextures(...) and Gl.glTexImage2D(...) inside Terrain.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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文