Advertisement

Multiple Textures

Started by February 19, 2003 07:18 PM
8 comments, last by Eric_003 22 years ago
If i had a 3D cube and wanted to have different textures on each side of the cube how would i do this? I know that the variable i define should be changed to texture[6] for 6 textures. I just not sure how I would import all the bitmap files and covert them into textures. An also had to choose what texture to display for each bind of the cube. [edited by - Eric_003 on February 19, 2003 8:20:41 PM]
to bind the texture call:

glBindTexture(GL_TEXTURE_2D, texture[0]);
draw a side
glBindTexture(GL_TEXTURE_2D, texture[1]);
draw a side

Use NeHe''s lesson #6 it shows you how to load a bitmap to a GLuint

then just modify it to take the filename through a paramater like LoadBitmap("pic.bmp")

and make it return a local texture as a GLuint rather than using a global texture directly.

then you would simply call something like:
bitmap[0]=LoadBitmap("pic.bmp");
bitmap[1]=LoadBitmap("pic2.bmp");
to load a bitmap.

Try doing it yourself, you''ll learn more that way than just copying someone elses code.
Advertisement
I understand the glBindTexture between the side now but i am still have problems with the loading of the bitmaps and converting it to a texture. I am not sure what you mean by local and global textures. Here is the code i was trying to use but if i set my loop to anything higher then 1 openGL chrashed when i run it.

int LoadGLTextures()
{
int Status=FALSE;

AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);

int i;
char texName[10];
char texNum[3];

for(i=0;i<2;i++)
{
memset(texName, 0, 10);
memset(texNum, 0, 3);

itoa(i, texNum, 10);

strcat(texName, "tex");
strcat(texName, texNum);
strcat(texName, ".bmp");


if (TextureImage=LoadBMP(texName))
{
Status=TRUE;
glGenTextures(2, &texture); <br><br> glBindTexture(GL_TEXTURE_2D, texture);<br><br> glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage>sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);<br><br> glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);<br> glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);<br> }<br><br> if (TextureImage) {<br> if (TextureImage->data) <br> {<br> free(TextureImage->data); <br> }<br><br> free(TextureImage); <br> }<br><br> }<br> return Status; <br>} </i>
Ok i fixed the problem with openGL crashing the problem was where it frees up space was set to 1, so all i did was set it to the number of textures i have which is 2 for now.
AUX_RGBImageRec *TextureImage[2];

When i try to bind and display the texture though the first quad works with either texture, but the second one doesnt. The first texture is used for the whole cube. Can you see what im doing wrong?


glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, texture[1]);
// Front Face
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);

glBindTexture(GL_TEXTURE_2D, texture[0]);
// Back Face
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);

[edited by - Eric_003 on February 20, 2003 1:06:59 PM]
Do NOT call glBindTexture between glBegin and glEnd
Ok that works but does that mean i have to draw each side with a begin() and end() like this...

glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
// Front Face
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();

glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
// Back Face
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);
glEnd();
Advertisement
Yes. There is no other solution (except multitexturing but that''s recommended for this case).

Otherwise, for the case of cube, you could use a cubemap. It''s a bit heavy to manage but you have a single texture object for the whole model. All cards since GeForce and Radeon support cubemaps in hardware I think.
Ok thanks for your help. Should i use multitexturing if I make a 3D world? example: wall & floor textures etc...
Multitexturing is an extionsion, and it''s only real use is to "blend" two textures together. Good for shadow maps and reflective things, but that''s about it.
-~-The Cow of Darkness-~-
Cows are not evil, they''re right.
What you ask don''t need mutitexturing. just repeat the above trick and that''s ok.
Multitexturing could be used for shadowmapping/lightmapping, bumpmapping, or even decals, but not for polygons that have a single texture PER POLYGON.

This topic is closed to new replies.

Advertisement