将不同的纹理应用于立方体的六个面,但有些问题是错误的

时间:2012-03-11 03:20:51

标签: opengl graphics

现在我在OpenGL中将六个不同的纹理应用于六个传真的多维数据集,但结果看起来并不正确。我从我的磁盘上加载了六张bmp图片,它们的大小都是256 * 256.我使用了SOIL(简单的OpenGL图像库:可以在这里下载:http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/)来加载bmp图片来构建纹理。 / p>

现在我在这里列出我的代码:

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>

#include "SOIL.h"

GLuint texture[6];//6 textures for 6 faces of the cube

GLfloat xRot,yRot,zRot;//control cube's rotation

//load the bitmap and convert it into a texture
int LoadGLTextures()
{
int Status = 0;
char *bmpFile[6] =          {"BmpFile/Ferrary.bmp","BmpFile/Honda.bmp","BmpFile/Hust.bmp",
    "BmpFile/Lamborghini.bmp","BmpFile/NeHe.bmp","BmpFile/Porsche.bmp"};
for (int i = 0;i < 6;++i)
{
    texture[i] = SOIL_load_OGL_texture(
        bmpFile[i],
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y);
    printf("texture[%d]: %d\n",i,texture[i]);
    if(texture[i] == 0)
        Status = 0;
    glBindTexture(GL_TEXTURE_2D,texture[i]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
}

return Status;
}

int init()
{
glEnable(GL_TEXTURE_2D);
if(!LoadGLTextures())
    return 0;
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
return 1;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f);
glRotatef(zRot,0.0f,0.0f,1.0f);

//glRotatef(45,1.0f,0.0f,0.0f);
//glRotatef(45,0.0f,1.0f,0.0f);

glBindTexture(GL_TEXTURE_2D,texture[0]);

glBegin(GL_QUADS); 
// Front Face 
// Bottom Left Of The Texture and Quad 
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f); 
// Bottom Right Of The Texture and Quad 
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f); 
// Top Right Of The Texture and Quad 
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f); 
// Top Left Of The Texture and Quad 
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f); 
glEnd(); 

glBindTexture(GL_TEXTURE_2D,texture[1]);
glBegin(GL_QUADS); 
// Back Face 
// Bottom Right Of The Texture and Quad 
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 
// Top Right Of The Texture and Quad 
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f); 
// Top Left Of The Texture and Quad 
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f); 
// Bottom Left Of The Texture and Quad 
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 
glEnd(); 

glBindTexture(GL_TEXTURE_2D,texture[2]);
glBegin(GL_QUADS); 
// Top Face 
// Top Left Of The Texture and Quad 
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f); 
// Bottom Left Of The Texture and Quad 
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f); 
// Bottom Right Of The Texture and Quad 
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f); 
// Top Right Of The Texture and Quad 
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f); 
glEnd(); 

glBindTexture(GL_TEXTURE_2D,texture[3]);
glBegin(GL_QUADS); 
// Bottom Face 
// Top Right Of The Texture and Quad 
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 
// Top Left Of The Texture and Quad 
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 
// Bottom Left Of The Texture and Quad 
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f); 
// Bottom Right Of The Texture and Quad 
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f); 
glEnd(); 

glBindTexture(GL_TEXTURE_2D,texture[4]);
glBegin(GL_QUADS); 
// Right face 
// Bottom Right Of The Texture and Quad 
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 
// Top Right Of The Texture and Quad 
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f); 
// Top Left Of The Texture and Quad 
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f); 
// Bottom Left Of The Texture and Quad 
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f); 
glEnd(); 

glBindTexture(GL_TEXTURE_2D,texture[5]);
glBegin(GL_QUADS); 
// Left Face 
// Bottom Left Of The Texture and Quad 
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 
// Bottom Right Of The Texture and Quad 
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f); 
// Top Right Of The Texture and Quad 
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f); 
// Top Left Of The Texture and Quad 
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f); 
glEnd(); 

/*xRot += 0.3f;
yRot += 0.4f;
zRot += 0.5f;*/

glutSwapBuffers();
}

void reshape(int w,int h)
{
if (0 == h)
    h = 1;

glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f,(GLfloat)w / (GLfloat)h,1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key,int x,int y)
{
switch(key){
    case 'x':
        xRot += 1.0f;
        glutPostRedisplay();
        break;
    case 'y':
        yRot += 1.0f;
        glutPostRedisplay();
        break;
    case 'z':
        zRot += 1.0f;
        glutPostRedisplay();
        break;
    default:
        break;
}
}

int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(100,100);
glutCreateWindow("Texture Map");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

这是运行后的结果:

http://hi.csdn.net/attachment/201203/11/2712336_1331434358OPyZ.jpg

正确的结果应该是:

http://hi.csdn.net/attachment/201203/11/2712336_1331434425901v.jpg

有人可以告诉我原因吗?我的代码有问题吗?

1 个答案:

答案 0 :(得分:5)

你实际上有几个错误。第一个错误是当你输入"Ferrary.bmp"时拼错了法拉利,这就是你的立方体缺少一张脸的原因。您的第二个错误是LoadGLTextures始终返回0。我认为您打算将Status初始化为1,而不是0

因此,由于Status已正确初始化并加载了错误的BMP,因此LoadGLTextures函数失败并返回0。这会导致init函数在调用LoadGLTextures之后退出glShadeModel。由于它会跳过所有其他初始化函数,因此您也会获得不正确的透视图。这导致了你的第三个错误,即你从未检查过init的返回值,从而错过了它失败的事实。

故事的道德:始终检查每一个返回值并打印诊断错误消息。