I have problem using more than 1 texture

Hi !
I’m quite new to openGL and I want to apply 2 textures on a cube.
But it seems the second textures I load overwrite the first one and my cube is half white and half textured when I render it.

this is my code where I load the 2
textures :

if( !LoadTexture( “metal.bmp”, m_tabTexture[0] ) )
{
return false;
}

if( !LoadTexture( "space.bmp", m_tabTexture[1] ) )
{
	return false;
}

And this is my LoadTexture() Function :

bool C3DObject::LoadTexture( string strFileName, GLuint &objTexture )
{
int Status = FALSE;
AUX_RGBImageRec* ptrTextureImage = NULL;
FILE* ptrFile = NULL;

ptrFile = fopen( strFileName.c_str(), "r" );

if( ptrFile )
{
	fclose( ptrFile );
	ptrTextureImage = auxDIBImageLoad( strFileName.c_str() );
}

if( ptrTextureImage )
{
	glGenTextures(1, &objTexture);
	glBindTexture(GL_TEXTURE_2D, objTexture);

	glTexImage2D(GL_TEXTURE_2D, 0, 3, ptrTextureImage->sizeX, ptrTextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, ptrTextureImage->data);
}

if( ptrTextureImage )
{
	if( ptrTextureImage->data )
	{
		free( ptrTextureImage->data );
	}
	free( ptrTextureImage );
}

return true;

}

And I render my cube this way :

glBindTexture(GL_TEXTURE_2D, GetTexture(0));
glBegin(GL_QUADS);
// Front Face

	glNormal3f( 0.0f, 0.0f, 1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
	// Back Face
	glNormal3f( 0.0f, 0.0f,-1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
	// Top Face
	glNormal3f( 0.0f, 1.0f, 0.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
glEnd();

glBindTexture(GL_TEXTURE_2D, GetTexture(1));
glBegin(GL_QUADS);
	// Bottom Face
	glNormal3f( 0.0f,-1.0f, 0.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
	// Right face
	glNormal3f( 1.0f, 0.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
	// Left Face
	glNormal3f(-1.0f, 0.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
glEnd();

So as I said only the second texture ( space.bmp ) is rendered correctly. The other one is all white
I tried them alone and both of them is rendered correctly but If I try to put both texture on the cube it doesn’t work.
Help please. Hope My problem is clear.
Thanks alot !

u need to use multitexture or blend the second over the first.
a possible method is
glEnable( GL_BLEND )
glBlendFunc( GL_ONE, GL_ONE );

I don’t you need blending or multi-texturing, your just drawing a cube with 3 faces one texture and the other 3 with another texture. What happens if you only load the first texture? Have you tried using glGenTextures before loading your textures, ie outside of LoadTexture().

You’re right Brent Fogarty
I don’t want to do blending or multiple-texturing. Only draw faces with a textures and other faces with another texture.

  • If I load the first one only and draw my whole cube with it It works
  • If I load the second one only and draw the whole cube It works
  • If I load the second texture after the first one only the second one is rendered
  • If I load the first texture after the second only the first is rendered

It really seems like a texture-overwriting problem…
Please help !
If you can post some code that works it would help me !
Thanks Alot

I doubt this is technically a problem with OpenGL. Sounds more like a bug in your code to me. I take it that m_tabTexture is an array of GLuint pointers, correct? Also be aware that with your current settings, mip maps are necessary because GL_TEXTURE_MIN_FILTER defaults to GL_NEAREST_MIPMAP_LINEAR. You can either build the mipmaps yourself (or use gluBuild2DMipmaps) or simply change GL_TEXTURE_MIN_FILTER to GL_LINEAR before you call glTexImage2D.

I think what you said is right DFrey but I’m not sure I understand what you’Re saying hehehe
However I resolved my problem by doing this

glBindTexture(GL_TEXTURE_2D, iIDTexture);
glTexImage2D(GL_TEXTURE_2D, 0, 4, ptrTextureImage->sizeX, ptrTextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, ptrTextureImage->data);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

I forgot the array I prefer to work with texture IDs
for each ID I bind with glBindTexture I must specify the MIN and MAX FIlter as DFrey said I think cause I realized for each bind it was a new place in memory so we need to set up the texture parameters again !
Thanks guys ! :wink: