Several textures with OpenGL
Hi,
I m learning OpenGL and i would use several textures in my scene but i didn t find tutos which explain it with more than a texture. I try by myself, but when i init a new texture, it cancel the others and at end i got only one texture and white faces on the others objects ....
If someone know why or have tutos on this, thx ;-)
++ juzam
"Tuez les tous, Dieu reconnaitra les siens ..."
"Tuez les tous, Dieu reconnaitra les siens ..."
Are you trying to load different textures or load different textures on one cube?
If you are loading different textures:
Then just load the textures before each construction of a quad.
Repeat the example code for each cube you want to append the texture on. You might want to make a function for this to avoid repeating most of this code.
Hope this helps
If you are loading different textures:
TextureImage[0]=LoadBMP("NEHE.bmp"); //First cube TextureImage[1]=LoadBMP("NEHE.bmp"); //Second cube TextureImage[2]=LoadBMP("NEHE.bmp"); //Third cube TextureImage[3]=LoadBMP("NEHE.bmp");//Fourth cube TextureImage[4]=LoadBMP("NEHE.bmp"); //Fifth cube TextureImage[5]=LoadBMP("NEHE.bmp"); //Sixth cube TextureImage[6]=LoadBMP("NEHE.bmp"); //Seventh cube TextureImage[6]=LoadBMP("NEHE.bmp"); //Eighth cube TextureImage[6]=LoadBMP("NEHE.bmp"); //Ninth cube TextureImage[7]=LoadBMP("NEHE.bmp"); //Introduction pic1 TextureImage[8]=LoadBMP("NEHE.bmp"); //Introduction pic2 TextureImage[9]=LoadBMP("NEHE.bmp"); //Introduction pic3//.... glGenTextures(17, &texture[0]); for (queue=0; queue<9; queue++) { glBindTexture(GL_TEXTURE_2D, texture[queue]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[queue]->sizeX, TextureImage[queue]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[queue]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); }
Then just load the textures before each construction of a quad.
glLoadIdentity(); glBindTexture(GL_TEXTURE_2D, texture[number goes here]); 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); // 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); // Top Face 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); // Bottom Face 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 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 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);
Repeat the example code for each cube you want to append the texture on. You might want to make a function for this to avoid repeating most of this code.
Hope this helps
The code above is easily optimizable - it is specially important in large scale projects with tons of textures.
When I firstly pointed out the problem some months ago the feedback let me think everyone knew it but looking at the code above, looks like it isn''t.
Basically, textures are saved in persistent SERVER-side memory. This means you can call delete[] to free the client side texture memory immediatly having called glTexImage.
This of curse will require to reload the textures from disk (look, exactly what most games do) every time the context is changed in some way but will halve the used memory.
By using the code above you''re having 2 textures: 1 in video card memory and one in ram (which is of curse, useless).
The trashing/swapping should not worry you - the driver takes care of that.
I feel the need to point this out because, as said in the previous thread, it is easy to get and gives very good results. BTW, continuing not understanding it means wasting a lot of memory.
I hope this post will get more attention than the previous one (looks like no one reads the old topics).
Bye
When I firstly pointed out the problem some months ago the feedback let me think everyone knew it but looking at the code above, looks like it isn''t.
Basically, textures are saved in persistent SERVER-side memory. This means you can call delete[] to free the client side texture memory immediatly having called glTexImage.
This of curse will require to reload the textures from disk (look, exactly what most games do) every time the context is changed in some way but will halve the used memory.
By using the code above you''re having 2 textures: 1 in video card memory and one in ram (which is of curse, useless).
The trashing/swapping should not worry you - the driver takes care of that.
I feel the need to point this out because, as said in the previous thread, it is easy to get and gives very good results. BTW, continuing not understanding it means wasting a lot of memory.
I hope this post will get more attention than the previous one (looks like no one reads the old topics).
Bye
Previously "Krohm"
Xiachunyi : Thank You very much it works fine now but
glGenTextures(17, &texture[0]);
should be
glGenTextures(1, &texture[0]);
glGenTextures(2, &texture[1]);
...
glGenTextures(10, &texture[9]);
No ?
Krohm : Freeing the memory is hat part of code => ?
if (TextureImage[0])
// If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure
}
"Tuez les tous, Dieu reconnaitra les siens ..."
glGenTextures(17, &texture[0]);
should be
glGenTextures(1, &texture[0]);
glGenTextures(2, &texture[1]);
...
glGenTextures(10, &texture[9]);
No ?
Krohm : Freeing the memory is hat part of code => ?
if (TextureImage[0])
// If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure
}
"Tuez les tous, Dieu reconnaitra les siens ..."
"Tuez les tous, Dieu reconnaitra les siens ..."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement