Jump to content
  • Advertisement
Sign in to follow this  
ITGER

Lighting a colored cube?

This topic is 4742 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

I was looking around at Nehe's lesson #7, the one in which you light up a textured cube. I tried modifying it so that the cube is not texture mapped, but rather has solid color faces. I disabled texture mapping, and basically replaced the cube drawing code with the code for a colored cube from an earlier lesson. (I kept the normals intact, of course.) However, when I turn on lighting, then the cube just looks grey! I thought that the the lighting would just affect the intensity of the colors that you see. How do I achieve this behavior instead? Here are a few snippets of what I have:
GLfloat LightAmbient[]=		{ 0.2f, 0.2f, 0.2f, 0.0f };
GLfloat LightDiffuse[]=		{ 0.5f, 0.5f, 0.5f, 0.0f };
GLfloat LightPosition[]=	{ 0.0f, 0.0f, 2.0f, 0.0f };
And here's the drawing function:
int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	glLoadIdentity();									// Reset The View
	glTranslatef(0.0f,0.0f,z);

	glRotatef(xrot,1.0f,0.0f,0.0f);
	glRotatef(yrot,0.0f,1.0f,0.0f);

        glBegin(GL_QUADS);
            glNormal3f( 0.0f, 0.0f, 1.0f);
            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 Face
            glNormal3f( 0.0f, 0.0f,-1.0f);
            glColor3f(1.0f, 1.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);
            // Top Face
            glNormal3f( 0.0f, 1.0f, 0.0f);
            glColor3f(1.0f, 0.0f, 1.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);
            // Bottom Face
            glNormal3f( 0.0f,-1.0f, 0.0f);
            glColor3f(0.0f, 1.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);
            // Right face
            glNormal3f( 1.0f, 0.0f, 0.0f);
            glColor3f(0.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);
            glVertex3f( 1.0f, -1.0f,  1.0f);
            // Left Face
            glNormal3f(-1.0f, 0.0f, 0.0f);
            glColor3f(1.0f, 0.2f, 1.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);
        glEnd();
    
	

	xrot+=xspeed;
	yrot+=yspeed;
	return TRUE;							
}

Share this post


Link to post
Share on other sites
Advertisement
Really? Oh well, at least I won't have to spend any more time figuring why it won't work, since it's uh impossible...

When do you see a colored cube anyway...

Share this post


Link to post
Share on other sites
Quote:
Original post by zedzeek
look up material's eg colormaterial lets u use colorX commands with lighting


I think it would be glEnable(GL_COLOR_MATERIAL);
If you include that, then the states current color is also used for calculating lighting.

Share this post


Link to post
Share on other sites
To elaborate a bit, you can set several colors for a vertex:
- The color to use when lighting is not enabled. This can be set with glColor
- The ambient color to use when lighting is enabled. This can be set with glMaterial, or with glColor if glColorMaterial has been set to GL_AMBIENT or GL_AMBIENT_AND_DIFFUSE and GL_COLOR_MATERIAL is enabled.
- The diffuse color to use when lighting is enabled. This can be set with glMaterial, or with glColor if glColorMaterial has been set to GL_DIFFUSE or GL_AMBIENT_AND_DIFFUSE
- The specular color for lighting. glMaterial or (if GL_COLOR_MATERIAL is enabled and glColorMaterial is set to GL_SPECULAR) glColor.

(There are more material settings you can use, but that should be enough for now. Check out glMaterial for more info.)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!