纹理无法正确显示C ++ opengl SOIL

时间:2013-03-31 23:41:06

标签: c++ opengl textures soil

这是我的问题;

将一个盒子或一个立方体想象成所有的右侧。

如果我们将盒子放在平坦的表面上,那么接触底部的面将是地板或底座。如果底座有一个内部,大多数盒子都是从外面做的,你将无法正确看到它。

不幸的是,在我的代码中你可以。而不是一个盒子,你有一个房间,我走出房间,看到地板,但我不应该。

发布问题和视频的图片,以及您可能要求的一些代码和任何代码。

http://www.youtube.com/watch?v=ml3-OBGNXXA&feature=youtu.be

http://www.flickr.com/photos/pkerkm/8607171993/

http://www.flickr.com/photos/pkerkm/8608276014/

//这是我如何管理纹理

void /*GraphicsEngine::*/drawWall(double a[3],double b[3], double c[3], double d[3], bool floor){
if(floor){
    glBindTexture(GL_TEXTURE_2D, texture[1]);
}else{
    glBindTexture(GL_TEXTURE_2D, texture[0]);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); // build our texture mipmaps
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
    glTexCoord2d(0.0,0.0);
    glVertex3dv(a);
    glTexCoord2d(-1.0,0.0);
    glVertex3dv(d);
    glTexCoord2d(-1.0,-1.0);
    glVertex3dv(c);
    glTexCoord2d(0.0,-1.0);
    glVertex3dv(b);
glEnd();
}

//这里是重塑函数

void reshape(int w, int h){
    //glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 200.0);
    glMatrixMode(GL_MODELVIEW);
    //glTranslatef(0.0, 0.0, -3.6);
   //glLoadIdentity();
}

//这是显示功能

void /*GraphicsEngine::*/display(){
 //glClearColor (0.0, 0.0, 0.0, 1.0);
 //glLoadIdentity ();
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 //glShadeModel(GL_FLAT);
 //glClearDepth(1.0);
 //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 setPos();
 gluLookAt(currentXPos,currentYPos,currentZPos,currentXFace,currentYFace,currentZFace,0,1,0);
 generateMap();
 graphicsFloor();
 int i = 0;
 if(projectiles.size()>0){
    for(i= 0; i< projectiles.size(); i++){
        arrow(projectiles.operator[](i).getXPos(),projectiles.operator[](i).getYPos(),projectiles.operator[](i).getZPos(),0.5,BLOCKWIDTH);
    }
    projectileMotion();
    player.advance();
    //glutPostRedisplay();
 /*}else if(player.advance()){
     glutPostRedisplay();
 }*/
 }
 //glFlush();
 glutSwapBuffers();
}

//这里是告诉在哪里绘制地板和墙壁

void graphicsFloor(){
int x,z;
//up

for(x=0;x<7;x+=7){
    for(z=0;z<105;z+=7){
        double a[3] = {x,0,z};
        double b[3] = {x+7,0,z};
        double c[3] = {x+7,0,z+7};
        double d[3] = {x,0,z+7};
        drawWall(d,a,b,c,true);
    }
}
}

//墙壁

void generateMap(){
int z;
int x;
for(z =0;z<105;z+=7){
      double a[3] = {0,0,z};
      double b[3] = {0,0,z+7};
      double c[3] = {0,MAPHEIGHT,z+7};
      double d[3] = {0,MAPHEIGHT,z};
      drawWall(d,a,b,c,false);
}
for(z =0;z<42;z+=7){
      double a[3] = {7,0,z};
      double b[3] = {7,0,z+7};
      double c[3] = {7,MAPHEIGHT,z+7};
      double d[3] = {7,MAPHEIGHT,z};
      drawWall(d,a,b,c,false);
}
}

//这就是我如何加载纹理

void genTex(){

texture[0]=SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture 
    (
        //"C:\\Users\\Eddy\\Desktop\\Senior Project\\Senior Project\\bloodwall2.png",
        "C:\\Users\\Pkerkm\\Documents\\Visual Studio 2010\\Projects\\files\\gameprojecgraphics\\bloodwall2.png",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
        //SOIL_FLAG_POWER_OF_TWO| SOIL_FLAG_MIPMAPS| SOIL_FLAG_MULTIPLY_ALPHA| SOIL_FLAG_COMPRESS_TO_DXT| SOIL_FLAG_DDS_LOAD_DIRECT| SOIL_FLAG_INVERT_Y

    );
glBindTexture(GL_TEXTURE_2D, texture[0]);
}

1 个答案:

答案 0 :(得分:1)

看起来深度测试已禁用。启用它:

glEnable(GL_DEPTH_TEST);

如果这不起作用,请确保您有深度缓冲区并重试。