GL_QUADS的Opengl纹理映射得到了奇怪的结果

时间:2013-08-07 15:58:26

标签: c++ opengl texture-mapping

纹理映射的结果非常奇怪。

我使用128 * 128 RGBA bmp图像进行GL_QUADS的纹理映射,然后我得到了以下奇怪的结果,用其他图像测试都没问题,但对于这个图像,我得到了非常奇怪的结果。 / p>

这里我想将叶子图像映射到GL_QUADS

以下是我的代码:

void init (void)
{

glClearColor(0.6, 0.6, 0.6, 0.0);
glClearDepth(1.0);
glShadeModel(GL_SMOOTH);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glEnable(GL_TEXTURE_2D);



glutSetCursor(GLUT_CURSOR_CROSSHAIR);
}

void display(){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);

GLuint texture[2];

unsigned int outWidth;
unsigned int outHeight;
unsigned int outDepth;

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);

unsigned char * data=loadbmp("leaftest.bmp", outWidth, outHeight, outWidth, true);

gluBuild2DMipmaps(GL_TEXTURE_2D, 4, outWidth, outHeight, GL_BGRA, GL_UNSIGNED_BYTE, data);

glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
    glTranslatef(0, 0, 2);
    glRotatef(90, 1, 0, 0);

    glBegin(GL_QUADS);
        glTexCoord2f(0.0,0.0);
        glVertex3f(0.0,-0.6,-0.3);

        glTexCoord2f(0.0,1.0);
        glVertex3f(0.0,0.0,-0.3);

        glTexCoord2f(1.0,1.0);
        glVertex3f(0.0,0.0,0.3);

        glTexCoord2f(1.0,0.0);
        glVertex3f(0.0,-0.6,0.3);

    glEnd();
glutSwapBuffers();
}

1 个答案:

答案 0 :(得分:1)

可能是因为你做了

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

在绑定任何纹理目标之前,这显然是错误的,我觉得为什么有些例子可以使用。

在您编码之前查看一些教程,例如this或购买书籍等。