简体   繁体   中英

Texture Mapping bug in opengl (with multiple textures)

i'm having trouble in using more then one texture in Opengl. I've written a test program that draws two squares, one bigger then other. When i texturize just one of then, everything works fine, but when i texture both, thats the result:

基础正方形太大,无法完全显示

Thats the code i use to initialize the textures:

unsigned int grass_height, grass_width, wall_height, wall_width;
unsigned char *grass = loadBMP("./minegrama.bmp", &grass_height, &grass_width);
unsigned char *wall = loadBMP("./mineleaves.bmp", &wall_height, &wall_width);

glShadeModel(GL_SMOOTH);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

glGenTextures(2, tex);

glBindTexture(GL_TEXTURE_2D, tex[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, grass_width, grass_height, 0, GL_BGR, GL_UNSIGNED_BYTE, grass);
gluBuild2DMipmaps(tex[0], GL_RGB, grass_width, grass_height, GL_RGB, GL_UNSIGNED_BYTE, grass);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glBindTexture(GL_TEXTURE_2D, tex[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, wall_width, wall_height, 0, GL_BGR, GL_UNSIGNED_BYTE, wall);
gluBuild2DMipmaps(tex[1], GL_RGB, grass_width, grass_height, GL_RGB, GL_UNSIGNED_BYTE, wall);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

Thats the code of the drawing:

//Seta o MatrixMode pra usar texturas
glMatrixMode(GL_TEXTURE);
//INICIO DO CHAO
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex[0]);
//Desenhar o chao;
glColor3f(0, 0.39, 0);
glBegin(GL_QUADS);
glVertex3f(-30, -30, -0.001);
glTexCoord3f(-30, -30, -0.001);

glVertex3f(30, -30, -0.001);
glTexCoord3f(30, -30, -0.001);

glVertex3f(30, 30, -0.001);
glTexCoord3f(30, 30, -0.001);

glVertex3f(-30, 30, -0.001);
glTexCoord3f(-30, 30, -0.001);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
//FIM DO CHAO


glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex[1]);
glTranslatef(0, 0, 10);
glBegin(GL_QUADS);
glVertex3f(-1, -1, 0); glTexCoord3f(-1, -1, 0); //Especifica cada vértice
glVertex3f(1, -1, 0); glTexCoord3f(1, -1, 0);
glVertex3f(1, 1, 0); glTexCoord3f(1, 1, 0);
glVertex3f(-1, 1, 0); glTexCoord3f(-1, 1, 0);
glDisable(GL_TEXTURE_2D);
glEnd();
glPopMatrix();

The first parameter of gluBuild2DMipmaps is wrong. The documentation for the target paramter states:

Specifies the target texture. Must be GLU_TEXTURE_2D.

In addition, your texture coordinates don't look good. With the current values, the texture on the large rectangle will be repeated 60 times. In addition, the z-value of the texture coordinates should be 0, not 0.01. Even better: Use glTexCoord2f for 2-dimensional textures.

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