more than 1 texture

Started by
10 comments, last by Kalidor 17 years, 11 months ago
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.
Advertisement
The array texture[] is holding your textures. When you create more than one, just use a different subscript. i.e. &texture[1], &texture[2].

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 hereglBindTexture(GL_TEXTURE_2D,texture2);drawSecondShape();//your other drawing code here


Hope that helps!
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]);//drawfirstshapeglBindTexture(GL_TEXTURE_2D,texture[1]);//drawsecondshape


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

glGenTextures(2,&texture[0]);

and only call that once at the start.
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]);//drawfirstshapeglBindTexture(GL_TEXTURE_2D,texture[1]);//drawsecondshape


Does not work.

Neither does:

glGenTextures(1, &texture[0]);orglGenTextures(2, &texture[0]);orglGenTextures(1, &texture[1]);orglGenTextures(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).
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.

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)
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? :(
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.

This topic is closed to new replies.

Advertisement