• Advertisement
Sign in to follow this  

A very simple texture issue

This topic is 4519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there. I have begun going through the NeHe tutorials along with some other code samples. My last achievement was understanding the texture mapping tutorial (lesson 6). Applying a texture to a cube doesn't seem like a problem. However, once I was able to compile the lesson properly, I started asking questions. For instance, what if I had 2 textures and applied them alternatively to the sides of the cube? Well, the code that I am currently using doesn't seem to function properly. I have tinkered around with it quite a bit and it seems that it only recalls the last texture I load. I will post two code snippets and perhaps someone will point out the silly mistake I am making. This is the code where I load the textures.
	char *files[NUM_TEXTURES] = {"data/wehe.bmp", "data/nehe.bmp"};

	AUX_RGBImageRec *texture[NUM_TEXTURES];		

	memset(texture, 0, sizeof(void *)*NUM_TEXTURES);	

	for (int i = 0; i < NUM_TEXTURES; i++) {
		texture[i] = LoadBMP(files[i]);
		if(!texture[i]) return FALSE;
	}

	glGenTextures(NUM_TEXTURES, &g_texture[0]);
	for (int i = 0; i < NUM_TEXTURES; i++) {
		status = TRUE;

		glBindTexture(GL_TEXTURE_2D, g_texture[i]);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[i]->sizeX, texture[i]->sizeY, 0, GL_RGB, 
			GL_UNSIGNED_BYTE, texture[i]->data);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}

	for (int i = 0; i < NUM_TEXTURES; i++) {
		if(texture[i]) {
			if(texture[i]->data) free(texture[i]->data);
			free(texture[i]);
		}
	}




This is the code where I draw the quad (in the scene rendering function).
	glBegin(GL_QUADS);
		// Front Face
		glBindTexture(GL_TEXTURE_2D, g_texture[0]);
		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, g_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, g_texture[0]);

            // and so on....



WeHe.bmp is simply the original texture with a white background. The result of this code is that in spite of alternating textures, only the last one loaded (in this case NeHe.bmp) is actually applied to all the faces. I am quite sure it's probably an insignificant oversight - I am a master of those. Thanks for your time in advance!

Share this post


Link to post
Share on other sites
Advertisement
You cannot call glBindTexture between glBegin and glEnd.

Call glBindTexture before glBegin, then glEnd your primitive before changing the texture for the next polygon.

Share this post


Link to post
Share on other sites
ChemicalImbalance: One texture is effectively a clone of the other.

bpoint: That did the trick. Thanks a bunch!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement