how to get multiple textures in lesson 6

Started by
2 comments, last by DDSquad 21 years, 8 months ago
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[0]))
		{
// Typical Texture Generation Using Data From The Bitmap
			glBindTexture(GL_TEXTURE_2D, texture);
			glTexImage2D(GL_TEXTURE_2D, 0, 3, 
				TextureImage->sizeX, 
				TextureImage->sizeY, 0, GL_RGB, 
				GL_UNSIGNED_BYTE, 
				TextureImage->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[0],"texture not found",MB_OK);
			Status = FALSE;
		}

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

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

     </pre>     

Then I did this when drawing the scene:

<pre>

	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();


     </pre>     

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.



<SPAN CLASS=editedby>[edited by - DDSquad on July 28, 2002 5:36:01 PM]</SPAN>  </i>   
-------------------------------------------------------BZZZZTT - CRASH - BANG"What was that?!""Captain, it appears that we have encountered a strange sub-space anomaly. I'm getting a high reading of tracheons beams. The anomaly seems to be some kind of rift in the space-time continuum. -- Never mind, Bones was just using the microwave again."
Advertisement
You can''t call glBindTexture between a glBegin/glEnd pair. You''ll have to do something like:


  // Front FaceglBindTexture(GL_TEXTURE_2D, texture[0]);glBegin(GL_QUADS);    glVertex(...) etc.glEnd();// Back FaceglBindTexture(GL_TEXTURE_2D, texture[1]);glBegin(GL_QUADS);    glVertex(...) etc.glEnd();and so on.  
Thank you Sibbo!

by the way, how did you get your code to come up in a text box? the code tags didn''t do that for me.
-------------------------------------------------------BZZZZTT - CRASH - BANG"What was that?!""Captain, it appears that we have encountered a strange sub-space anomaly. I'm getting a high reading of tracheons beams. The anomaly seems to be some kind of rift in the space-time continuum. -- Never mind, Bones was just using the microwave again."
No prob
For the box, use the tags [''source] and [''/source] (without the '')

This topic is closed to new replies.

Advertisement