Sign in to follow this  
BackwardsDown

more than 1 texture

Recommended Posts

BackwardsDown    122
I am in the beginning stage of learning open gl, and now I have done lesson 6 from NeHe. Everything works perfectly, but now I want to create more textures. The problem lies with creating and generating the texture. When I have 1 texture I use this code:
glGenTextures(1, &texture[0]);					// Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Generate The Texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
But what if I want more textures? I dont seem to get any result.

Share this post


Link to post
Share on other sites
CodeZero    138
The array texture[] is holding your textures. When you create more than one, just use a different subscript. i.e. &texture[1], &texture[2].

Share this post


Link to post
Share on other sites
Ezbez    1164
You're going to have to use the above code for the second texture, too(a different texture[] variable, though). Then, to use a texture, you have to call glBindTexture() like you do in the loading code. So, here's an example:

//Load in texture1
//Load in texture2 (like how you already do it)

glBindTexture(GL_TEXTURE_2D,texture1);
drawFirstShape();//your drawing code here

glBindTexture(GL_TEXTURE_2D,texture2);
drawSecondShape();//your other drawing code here



Hope that helps!

Share this post


Link to post
Share on other sites
BackwardsDown    122
I dont seem to get it right, when I try this code:

glGenTextures(1, &texture[0]);					
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glGenTextures(1, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);

glBindTexture(GL_TEXTURE_2D,texture[1]);
//drawfirstshape

glBindTexture(GL_TEXTURE_2D,texture[1]);
//drawsecondshape


When I execute this code, the first texture is empty (white) and the second is normal.

Share this post


Link to post
Share on other sites
BackwardsDown    122
glGenTextures(1, &texture[0]);					

glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glBindTexture(GL_TEXTURE_2D, texture[1]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);



glBindTexture(GL_TEXTURE_2D,texture[1]);

//drawfirstshape



glBindTexture(GL_TEXTURE_2D,texture[1]);

//drawsecondshape


Does not work.

Neither does:

glGenTextures(1, &texture[0]);
or
glGenTextures(2, &texture[0]);
or
glGenTextures(1, &texture[1]);
or
glGenTextures(2, &texture[1]);


Maybe this quote from NeHe helps:
Quote:
The first line glGenTextures(1, &texture[0]) tells OpenGL we want to generate one texture name (increase the number if you load more than one texture).

Share this post


Link to post
Share on other sites
CodeZero    138
You can create the actual textures one at a time as long as you address them correctly. If this is the exact code you have then add a line to it:

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glGenTextures(1, &texture[1]); <--------------- Added line
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);



When you draw the two shapes, remember to bind to the texture you want to use:

glBindTexture(GL_TEXTURE_2D,texture[0]); <-------- Note the 0
//drawfirstshape

glBindTexture(GL_TEXTURE_2D,texture[1]); <-------- And the 1
//drawsecondshape

If one shows up white, then you might want to check and make sure that TextureImage[0]->data and TextureImage[1]->data both have data in them.

Share this post


Link to post
Share on other sites
nmi    978
You have to call this for every texture you create:

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);



(at least this made my texture mapping work)

Share this post


Link to post
Share on other sites
BackwardsDown    122
Your code is exactly the same as my first post :(

This is my loadtexture function:


int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator

AUX_RGBImageRec *TextureImage[2]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if ( (TextureImage[0]=LoadBMP("Data/1.bmp")) && (TextureImage[1]=LoadBMP("Data/2.bmp")) )
{
Status=TRUE; // Set The Status To TRUE

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glGenTextures(1, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}

if ((TextureImage[0])&&(TextureImage[1])) // If Texture Exists
{
if ((TextureImage[0]->data)&&(TextureImage[0]->data)) // If Texture Image Exists
{
MessageBox(NULL,"They have data!","Not an error",MB_OK | MB_ICONINFORMATION);
free(TextureImage[0]->data); // Free The Texture Image Memory
free(TextureImage[1]->data);

}

free(TextureImage[0]); // Free The Image Structure
free(TextureImage[1]);
}

return Status; // Return The Status
}


When I execute the program, the window pops up that they do have got data.
Texture 1 does not work (its white).
Texture 2 does it perfectly.

Hope you can make my code work? :(

Share this post


Link to post
Share on other sites
nmi    978
As I said before, you have to set the paramters for EVERY texture. So instead of

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glGenTextures(1, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);



you should try:

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glGenTextures(1, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);




Also read here (point 5):
http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

Edit:
Also try to use source tags if possible.

Share this post


Link to post
Share on other sites
BackwardsDown    122
It works, nmi gave me the right hint :) :
edit: nmi said it the post before me too :)


glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glGenTextures(1, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

Share this post


Link to post
Share on other sites
Kalidor    1087
EDIT: Whoops... I'm too slow. Nevermind.

Like nmi said, you need to change EACH texture's minification filter. The initial minification filter for a texture is GL_NEAREST_MIPMAP_LINEAR. This means that OpenGL is expecting the entire mipmap chain to be complete, but you are only giving data for the 0th level. So to fix it, either create all mipmap levels or use a non-mipmapped minification filter.

So the easiest way to fix this is to add this line...
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
somewhere after you bind the first texture and before you bind the second texture when creating them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this