Archived

This topic is now archived and is closed to further replies.

DDSquad

how to get multiple textures in lesson 6

Recommended Posts

I took a long break from learning OpenGL because of my sorry computer. Now that I have a new, I want to get back into it. I am trying to learn more about things I could hardly do before, such as textures. I want to modify lesson 6 to make each of the six sides of the cube a different image. I added this global variable:
char	filenames [6][12] = {"Data/01.bmp","Data/02.bmp","Data/03.bmp","Data/04.bmp","Data/05.bmp","Data/06.bmp"};
     
and changed the GLuint texture array to have 6 elements. Here's what I tried to do in the LoadGLTextures() function: EDIT: The line in the code below that tries to load the bitmap into TextureImage actually does try to load into the array at index of i, but the forum interprets that i in brackets as beginning italics, and hides it.
int LoadGLTextures()					// Load Bitmaps And Convert To Textures
{
	int Status=TRUE;						// Status Indicator

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

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

	glGenTextures(6, &texture[0]);		// Create The Texture

	for(int i=0; i<6; i++)
	{
	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
		if (TextureImage=LoadBMP(&filenames[i][0]))
		{
// Typical Texture Generation Using Data From The Bitmap
			glBindTexture(GL_TEXTURE_2D, texture[i]);
			glTexImage2D(GL_TEXTURE_2D, 0, 3, 
				TextureImage[i]->sizeX, 
				TextureImage[i]->sizeY, 0, GL_RGB, 
				GL_UNSIGNED_BYTE, 
				TextureImage[i]->data);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		}
		else
		{
			MessageBox(hWnd,&filenames[i][0],"texture not found",MB_OK);
			Status = FALSE;
		}

		if (TextureImage[i])					// If Texture Exists
		{
			if (TextureImage[i]->data)			// If Texture Image Exists
			{
				free(TextureImage[i]->data);		// Free The Texture Image Memory
			}

			free(TextureImage[i]);				// Free The Image Structure
		}
	}
	return Status;						// Return The Status
}

     
Then I did this when drawing the scene:

	glBindTexture(GL_TEXTURE_2D, texture[0]);

	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
		glBindTexture(GL_TEXTURE_2D, texture[1]);
		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
		glBindTexture(GL_TEXTURE_2D, texture[2]);
		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
		glBindTexture(GL_TEXTURE_2D, texture[3]);
		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
		glBindTexture(GL_TEXTURE_2D, texture[4]);
		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
		glBindTexture(GL_TEXTURE_2D, texture[5]);
		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();


     
When I run the program the texture of all six sides is 01.bmp. What am I doing wrong? Sorry, I am sure this has come up before in this forum, but when I tried to search for it, I got an error message on the search page. [edited by - DDSquad on July 28, 2002 5:36:01 PM]

Share this post


Link to post
Share on other sites
You can''t call glBindTexture between a glBegin/glEnd pair. You''ll have to do something like:


  

// Front Face

glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glVertex(...) etc.
glEnd();

// Back Face

glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glVertex(...) etc.
glEnd();

and so on.

Share this post


Link to post
Share on other sites