gpt4 book ai didi

c++ - 使用 C++ 和 OpenGL 显示图像的正确方法

转载 作者:太空狗 更新时间:2023-10-29 21:47:09 25 4
gpt4 key购买 nike

(我已经更新了新的更好的代码)

http://www.flickr.com/photos/pkerkm/8251188947/in/photostream好的方向

http://www.flickr.com/photos/pkerkm/8252258464/in/photostream错误的方向

我们正在使用 SOIL 库C++打开GL

我们在显示的其中一张图片上画了 N、S、W、E 的符号,因为我们认为图片被翻转或显示不正确。我们是对的,但不确定为什么以及如何解决它。我们的 W 在上,E 在下,S 在右,N 在左。这意味着,符号N在左侧,至于它应该在上部的位置。

这是我们生成 3 维数组和图片的代码。

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 );
}

我认为问题出在这里,但不确定如何解决

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 度。

关于c++ - 使用 C++ 和 OpenGL 显示图像的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13735202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com