首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

OpenGL给立方纹理贴图

2012-08-13 
OpenGL给立方体纹理贴图下面是我的代码,实现的功能是绘制一个立方体,并给六个面贴上不同的纹理,(导入的六

OpenGL给立方体纹理贴图
下面是我的代码,实现的功能是绘制一个立方体,并给六个面贴上不同的纹理,(导入的六张bmp图片都是256*256大小的)。但是看到的效果是乱的,谁能帮我看看嘛?感激不尽~~

在这里我用到了SOIL(Simple OpenGL Image Library)库:http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/

我的代码:

C/C++ code
#include <GL/glut.h>#include <stdio.h>#include <stdlib.h>#include "SOIL.h"GLuint texture[6];//6 textures for 6 faces of the cubeGLfloat xRot,yRot,zRot;//control cube's rotation//load the bitmap and convert it into a textureint 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;} 



运行效果:




正常效果应该是这样子的:

可以看出,有的面上纹理可以看见,但是有的不能看见。所以就乱了~~

这是为什么啊?
求指导~~~~~

[解决办法]
刚开始研究OPENGL, 是不是需要打开glEnable(GL_DEPTH_TEST) 或者 glEnable(GL_CULL_FACE)?
[解决办法]
问题出在你没有调用glGenTextures(6,texture);在LoadGLTextures()中的循环前面加上它。并在main()函数的glutMainLoop()函数后面调用glDeleteTextures(6,texture);再看看吧。。。。

热点排行