🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Lesson #06

Started by
3 comments, last by Miah-Avl 16 years, 6 months ago
Hi, I've been going through the lessons on the NeHe site, and I think they're great so far, but I have a question about lesson #06. I went through the lesson and got it working fine, however I decided to alter the code a little bit for practice before moving on. When I tried to add a simple shaded cube next to the textured cube, it did not color correctly. After some debugging, I found out that if you comment the line in the InitGL() function that calls the texture loading functions (and therefore don't load textures for the other cube), shading then appears to work correctly. The code segment I'm talking about is below (don't know if/how I'm supposed to put this in a code block somehow):

int InitGL(GLvoid)
{
	// Lesson #06: Enable texturing.
	if(!loadGLTexture()) return FALSE; // Commented this line out.
	glEnable(GL_TEXTURE_2D);
        ...
}
Does anyone know why this would cause smooth shading not to work? If I leave the line as is, the texture seems to load fine, but the colors are off when shading, and if I comment the line, shading works but textures won't load. Thanks. [Edited by - Miah-Avl on December 29, 2007 4:20:20 PM]
Advertisement
Posting your drawing code would help (surround the code with [code] and [/code] blocks to format it nicely, or [source] and [/source] blocks to give it its own scroll box and formatting). Without having seen the applicable code, though, my bet is that you're not disabling texturing before drawing the untextured cube. Basically:
// in draw functionglEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, TextureID);  //  should already be there somewhere//  draw texture cubeglDisable(GL_TEXTURE_2D);//  draw untextured cube

What's happening now is that your texture is (probably) being applied to both cubes, when you only want it applied to the first.

If that's not the problem, toss up more code and we'll see what we can do.
Cheers!
Thanks jouley,

I can now get both cubes to draw, one with texture and one shaded, however when I draw both, the one with texture appears sort of "dim." URLs to screenshots are below the code.

Here's my draw code:

int DrawGLScene(GLvoid){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer	glLoadIdentity();							// Reset The Current Modelview Matrix	glTranslatef(0.0f, 0.0f, -5.0f);	glRotatef(xr, 1.0f, 0.0f, 0.0f);	glRotatef(yr, 0.0f, 1.0f, 0.0f);	glRotatef(zr, 0.0f, 0.0f, 1.0f);	glEnable(GL_TEXTURE_2D);	glBindTexture(GL_TEXTURE_2D, texture[0]);	glBegin(GL_QUADS);		// Front:		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);		// Back:		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);		// Top:		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);		// Bottom:		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);		// Left:		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);		// Right:		glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);	glEnd();	// Exercise: Move to the right and draw a shaded cube:	glDisable(GL_TEXTURE_2D);	glLoadIdentity();	glTranslatef(1.0f, 0.0f, -6.0f);	glRotatef(cubeR, 0.0f, 1.0f, 0.0f);	// glColor3f(1.0f, 0.0f, 0.0f);	glBegin(GL_QUADS);		// Front:		glColor3f(1.0f, 0.0f, 0.0f);		glVertex3f(-1.0f, 1.0f, 1.0f);		glVertex3f(1.0f, 1.0f, 1.0f);		glVertex3f(1.0f, -1.0f, 1.0f);		glVertex3f(-1.0f, -1.0f, 1.0f);		// Back:		glColor3f(1.0f, 0.12f, 0.32f);		glVertex3f(-1.0f, 1.0f, -1.0f);		glVertex3f(1.0f, 1.0f, -1.0f);		glVertex3f(1.0f, -1.0f, -1.0f);		glVertex3f(-1.0f, -1.0f, -1.0f);		// Top:		glColor3f(0.80f, 0.12f, 0.32f);		glVertex3f(-1.0f, 1.0f, -1.0f);		glVertex3f(1.0f, 1.0f, -1.0f);		glVertex3f(1.0f, 1.0f, 1.0f);		glVertex3f(-1.0f, 1.0f, 1.0f);		// Bottom:		glColor3f(0.80f, 0.12f, 0.32f);		glVertex3f(-1.0f, -1.0f, -1.0f);		glVertex3f(1.0f, -1.0f, -1.0f);		glVertex3f(1.0f, -1.0f, 1.0f);		glVertex3f(-1.0f, -1.0f, 1.0f);		// Left:		glColor3f(0.80f, 0.12f, 0.32f);		glVertex3f(-1.0f, 1.0f, 1.0f);		glVertex3f(-1.0f, 1.0f, -1.0f);		glVertex3f(-1.0f, -1.0f, -1.0f);		glVertex3f(-1.0f, -1.0f, 1.0f);		// Right:		glColor3f(0.80f, 0.12f, 0.32f);		glVertex3f(1.0f, 1.0f, 1.0f);		glVertex3f(1.0f, 1.0f, -1.0f);		glVertex3f(1.0f, -1.0f, -1.0f);		glVertex3f(1.0f, -1.0f, 1.0f);	glEnd();	xr += 0.3f;	yr += 0.4f;	zr += 0.5f;	cubeR += 0.5f;	return TRUE;								// Everything Went OK}


Here's some screen grabs:

Just textured cube: http://babbage.missouri.edu/~jamzt8/cube_screen1.jpg
Textured and shaded: http://babbage.missouri.edu/~jamzt8/cube_screen2.jpg
Unless you specify otherwise (and I doubt that you have), the texture applied to a polygon is modulated (combined) with the polygon's color. In your case, it seems that you aren't setting the color for the textured cube, so the color for the untextured cube (reddish) is being applied to it after the first frame. So, simply add glColor3f(1.0f, 1.0f, 1.0f); before you draw the textured cube and you should be good to go.

If that doesn't do it, I'll take a closer look, but all signs point to forgetting to specify the color.
Thanks again Jouley, that seems to have done it.

Thanks for being patient, I'm pretty new to OpenGL programming and I'm not very familiar with how a lot of this stuff works yet, but I'm getting there.

This topic is closed to new replies.

Advertisement