PDA

View Full Version : Mutlitextring



enmaniac
11-29-2003, 12:32 AM
Hi!

I have created 2 textures. Inside rendering function I want to show 3 quads. First two should simply show loaded textures and the last one should show one texture on the other. This is a rendering code:
void RenderScene( HDC hDC )
{
static FLOAT wrap = 0;

glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();

glColor3ub( 255, 255, 255 );

glEnable( GL_TEXTURE_2D );

// Show FIRST texture - single

glBindTexture( GL_TEXTURE_2D, uiTextureIDs[ 0 ] );

glBegin( GL_QUADS );

glTexCoord2f( 0.0f, 0.0f );
glVertex3f( 1.0f, 0.0f, 0.0f );

glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 6.0f, 0.0f, 0.0f );

glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 6.0f, 5.0f, 0.0f );

glTexCoord2f( 0.0f, 1.0f );
glVertex3f( 1.0f, 5.0f, 0.0f );

glEnd();

// Show SECOND texture - single

glBindTexture( GL_TEXTURE_2D, uiTextureIDs[ 1 ] );

glBegin( GL_QUADS );

glTexCoord2f( 0.0f, 0.0f );
glVertex3f( 7.0f, 0.0f, 0.0f );

glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 12.0f, 0.0f, 0.0f );

glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 12.0f, 5.0f, 0.0f );

glTexCoord2f( 0.0f, 1.0f );
glVertex3f( 7.0f, 5.0f, 0.0f );

glEnd();

// Show FISRT texture on SECOND one - multitexture

pcEngine3D->glActiveTextureARB( GL_TEXTURE0_ARB );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, uiTextureIDs[ 1 ] );

pcEngine3D->glActiveTextureARB( GL_TEXTURE1_ARB );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, uiTextureIDs[ 0 ] );

glBegin(GL_QUADS);

pcEngine3D->glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 0.0f );
pcEngine3D->glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f-wrap, 0.0f );
glVertex3f( 3, 6, 0 );

pcEngine3D->glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 0.0f );
pcEngine3D->glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f-wrap, 0.0f );
glVertex3f( 10, 6, 0);

pcEngine3D->glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 1.0f );
pcEngine3D->glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f-wrap, 1.0f );
glVertex3f( 10, 11, 0);

pcEngine3D->glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 1.0f );
pcEngine3D->glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f-wrap, 1.0f );
glVertex3f( 3, 11, 0);

glEnd();

glDisable( GL_TEXTURE_2D );

wrap += 0.0015f;

SwapBuffers( hDC );
}
The problem is when I have added the code needed to show the last quad first two became corrupted and show the first texture ( that one connected with GL_TEXTURE0_ARB ). When I remove this code both textures are shown correctly
I have met this problem some time ago but I don't remember the solution

tayo
11-29-2003, 02:19 AM
this is a common error, because OpenGL is a state machine.
when you render the quad with two textures, you activate the texture unit 1 as the current texture unit
pcEngine3D->glActiveTextureARB( GL_TEXTURE1_ARB );
so at the next frame, the current texture unit will unit 1 not 0!

at the end of your frame, you have to set the current texture unit as the texture unit 0 to be correct at the next frame, something as :

glDisable(GL_TEXTURE_2D); // disable texturing on texture unit 1
pcEngine3D->glActiveTextureARB( GL_TEXTURE0_ARB );
glDisable(GL_TEXTURE_2D); // disable texturing on texture unit 0
wrap += 0.0015f;

SwapBuffers( hDC );

enmaniac
11-29-2003, 04:18 AM
Thx a LOT!!! It is works fine now! :-)

enmaniac
11-29-2003, 06:12 AM
So does it mean that texture unit 0 is a default unit ? I mean a unit which is used for 'normal' texturing.

tayo
11-30-2003, 12:18 AM
exactly, the default texture unit is 0.
so in your code, you don't need to call
pcEngine3D->glActiveTextureARB( GL_TEXTURE0_ARB );
glEnable( GL_TEXTURE_2D );

because you have already enable texturing on texture unit 0 by calling glEnable( GL_TEXTURE_2D ) before.