high_flyer
31st May 2007, 11:40
Hi,
I thought to try and ask here, maybe someone here is fit with OpenGL and can help.
I am trying to do some multi texturing with OpenGL, and it "almost" works.
I have two rectangle surfaces covered by a texture, one faces parallel to the screen, and one "tilted" 45 degrees.
Each rectangle should be covered by another texture.
But the result is, that only the first texture gets painted, the second not.
Attached are two images.
The one with one textured rectangle and the other dark brown is the result of the below posted code.
The other, is when the GL_TEXTURE0 is used for both rectangles (instead of GL_TEXTURE1 for the second).
It looks like GL_TEXTURE1 doesn't get initialized or something similar, but I am rather new to Open GL so I don't know what I am doing wrong.
Thanks in advance!
void gl_display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
num_texture = 0;
//init first texture
glBindTexture(GL_TEXTURE_2D, g_textureNameArr[num_texture]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture[num_texture]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imageW, imageH, GL_RGBA, GL_UNSIGNED_BYTE, l_texture[num_texture]);
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_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
num_texture++;
//init second texutre
glBindTexture(GL_TEXTURE_2D, g_textureNameArr[num_texture]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture[num_texture]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imageW, imageH, GL_RGBA, GL_UNSIGNED_BYTE, l_texture[num_texture]);
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_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//pralell rectangle
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D,g_textureNameArr[0]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_TEXTURE_2D);
//draw the textures
glBegin(GL_QUADS);
//map texture 0 to first rectangle
glMultiTexCoord2f(GL_TEXTURE0,0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0,0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0,1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0,1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
//tilted rectangle
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D,g_textureNameArr[1]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
//map texture 1 to second rectangle
//if in this section GL_TEXTURE0 is used, opengl2.jpg (where both rectangles get textured) is the result
glMultiTexCoord2f(GL_TEXTURE1,0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1,0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1,1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glMultiTexCoord2f(GL_TEXTURE1,1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
I thought to try and ask here, maybe someone here is fit with OpenGL and can help.
I am trying to do some multi texturing with OpenGL, and it "almost" works.
I have two rectangle surfaces covered by a texture, one faces parallel to the screen, and one "tilted" 45 degrees.
Each rectangle should be covered by another texture.
But the result is, that only the first texture gets painted, the second not.
Attached are two images.
The one with one textured rectangle and the other dark brown is the result of the below posted code.
The other, is when the GL_TEXTURE0 is used for both rectangles (instead of GL_TEXTURE1 for the second).
It looks like GL_TEXTURE1 doesn't get initialized or something similar, but I am rather new to Open GL so I don't know what I am doing wrong.
Thanks in advance!
void gl_display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
num_texture = 0;
//init first texture
glBindTexture(GL_TEXTURE_2D, g_textureNameArr[num_texture]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture[num_texture]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imageW, imageH, GL_RGBA, GL_UNSIGNED_BYTE, l_texture[num_texture]);
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_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
num_texture++;
//init second texutre
glBindTexture(GL_TEXTURE_2D, g_textureNameArr[num_texture]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture[num_texture]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imageW, imageH, GL_RGBA, GL_UNSIGNED_BYTE, l_texture[num_texture]);
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_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//pralell rectangle
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D,g_textureNameArr[0]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_TEXTURE_2D);
//draw the textures
glBegin(GL_QUADS);
//map texture 0 to first rectangle
glMultiTexCoord2f(GL_TEXTURE0,0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0,0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0,1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0,1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
//tilted rectangle
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D,g_textureNameArr[1]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
//map texture 1 to second rectangle
//if in this section GL_TEXTURE0 is used, opengl2.jpg (where both rectangles get textured) is the result
glMultiTexCoord2f(GL_TEXTURE1,0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1,0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1,1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glMultiTexCoord2f(GL_TEXTURE1,1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}