简体   繁体   中英

Right way to display an image using C++ with OpenGL

(i've updated the code the new better one)

http://www.flickr.com/photos/pkerkm/8251188947/in/photostream good directions

http://www.flickr.com/photos/pkerkm/8252258464/in/photostream bad directions

We are using SOIL library C++ openGL

We drew the symbols of N, S, W, E on one of the images we are displaying, because we thought that the images are being flipped or displayed incorrectly. We were right, but not sure why and how to fix it. Our W is up, E is down, S is right and N is left. This means, that the symbol N is on the left side, as to where it should be on the up part.

Here is the code where we generate the 3-dimmentional array and our pictures.

void genTex(){
texture[0]=SOIL_load_OGL_texture // load an image file directly as a new OpenGL      texture 
    (
        "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
    );
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
}

void /*GraphicsEngine::*/generateMap(){
  int i, k, j;
  double coordinates[8][3];
  for(i=0; i<MAPWIDTH; i++){
    for(j=0; j<MAPHEIGHT; j++){
      for(k=0; k<MAPDEPTH; k++){
    if(stage[i][j][k]==0){
      //do nothing
    }
    else if (stage[i][j][k]==1){
      getWallCoordinates(coordinates, i, j, k);
  if(j==0 || stage[i][j-1][k]==0){
        drawWall(coordinates[0],coordinates[1],coordinates[2],coordinates[3],true);
  }
      if(j<MAPHEIGHT-1 && stage[i][j+1][k]==0){drawWall(coordinates[7],coordinates[6],coordinates[5],coordinates[4],false);
  }
  if(i==0 || stage[i-1][j][k]==0){drawWall(coordinates[7],coordinates[4],coordinates[0],coordinates[3],false);
  }
  if(k==MAPDEPTH-1 || stage[i][j][k+1]==0){drawWall(coordinates[7],coordinates[3],coordinates[2],coordinates[6],false);
  }
  if(i==MAPWIDTH-1 || stage[i+1][j][k]==0){drawWall(coordinates[6],coordinates[2],coordinates[1],coordinates[5],false);
  }
  if(k==0 || stage[i][j][k-1]==0){drawWall(coordinates[5],coordinates[1],coordinates[0],coordinates[4],false);
 }
    }
   }
  }
 }
} 
void /*GraphicsEngine::*/drawWall(double a[3],double b[3], double c[3], double d[3], bool floor){
//glColor3f(1.0,0.0,1.0);


if(floor){
    glBindTexture(GL_TEXTURE_2D, texture[1]);
}else{
    glBindTexture(GL_TEXTURE_2D, texture[0]);
}
//GLuint tex;
//glGenTextures(1, &tex);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//tex = LoadTexture("wallsprite.png");
//glBindTexture(GL_TEXTURE_2D, tex);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
    //glNormal3f(0, 1, 0);
    glTexCoord2d(0.0,0.0);
    glVertex3dv(a);
    glTexCoord2d(-1.0,0.0);
    //glVertex3dv(b);
    glVertex3dv(d);
    glTexCoord2d(-1.0,-1.0);
    glVertex3dv(c);
    glTexCoord2d(0.0,-1.0);
    //glVertex3iv(d);
    glVertex3dv(b);
glEnd();
/*glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
    glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
    glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
    glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);*/
glDisable( GL_TEXTURE_2D );
 }

}

I am thinking the problem is in here, but not sure how to fix it

void /*GraphicsEngine::*/getWallCoordinates(double coordinates[8][3], double x, double y, double z){
int i;
for(i=0;i<8; i++){
    if((i/4)>=1){
        coordinates[i][1]=BLOCKHEIGHT*(y+1);
    }else{
        coordinates[i][1]=BLOCKHEIGHT*y;
    }
    if((i/2)%2==0){
        coordinates[i][2]=BLOCKDEPTH*z;
    }else{
        coordinates[i][2]=BLOCKDEPTH*(z+1);
    }
    if((i%4)%3==0){
        coordinates[i][0]=BLOCKWIDTH*x;
    }else{
        coordinates[i][0]=BLOCKWIDTH*(x+1);
    }
}

}

只需切换glTexCoord2f指令即可将纹理旋转90度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM