用openGL和C#加载纹理

时间:2011-01-02 14:29:09

标签: c# tao-framework

喜 我正在尝试加载纹理(.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();


    }

请帮帮我

1 个答案:

答案 0 :(得分:2)

你应该检查一些事情。

是否启用了纹理?

Gl.glEnable(Gl.GL_TEXTURE_2D);

您是否在Gl.glGenTextures(...)内呼叫Gl.glTexImage2D(...)Terrain.LoadGLTextures(...);

背部,右侧和后部的缠绕顺序底部四边形是错误的。绕组必须是逆时针方向。由于您正在启用剔除,因此不会从多维数据集卷中看到这些四边形。只需换掉第一个&第二个顶点,第三个顶点和第四。

确保您正在查看音量内的场景。

你真的想使用Tao Framework吗? Tao框架自2008年5月以来一直没有更新,并且自2010年7月起有效地解散。也许你应该看一下OpenTK框架:http://www.opentk.com/