lighting basics

I am just starting to read about lighting. I don’t understand it all just yet but I do have a question. If I have an object that is multi-colored, how do I preserve the colors in my scene when using opengl lighting? If I just enable lighting without setting parameters for the other functions, glLight and glMaterial, I lose all of the original color of my object. But when setting parameters to the functions, I can never get back to the original color scheme. All I want is a simple stationary white light.

Thanks, Mike

Hi !

OpenGL does not preserve any information for you, every time you render a frame you have to setup everything again, light, colors, transformations and so on.

Mikael

So once I enable lighting the glColor() function calls made between glBegin(…) … glEnd() become invalid?

Mike

Yes

You have your choice.

OpenGL has the glColorMaterial call to allow you to use these colors as part of the material or to ignore them. To use colors as the attribute(s) specified by the glColorMaterial you must call:

glEnable(GL_COLOR_MATERIAL);

In general OpenGL is a state machine, any change you make is permanent unless you change it later. Starting a new frame (with swapbuffers) will not erase old state. The new frame will begin with the same state that existed at the end of the old frame except the contents of the backbuffer will be undefined. State is not remembered however there are mechanisms like stacks you can push that are useful for remembering & restoring state like matrix transformations, calls like glPushMatrix glPopMatrix glPushAttrib and glPopAttrib.

Display lists will also remember any state calls you send to them.

Sorry to use someone else’s topic, but i’m having this same problem.
I don’t get dorbie’s explanation… If i use glEnable(GL_COLOR_MATERIAL); it gets the colors again, but the light is gone…
Could you explain this better or link me to a tut that descibes lighting while affecting it by colors/textures?
Cheers.

That tut with source seems pretty good :
http://www.eecs.tulane.edu/www/Terry/OpenGL/Lighting.html

Mental, you need to enable lighting, have a light source on with an appropriate position and have

glColorMaterial(GL_AMBIENT_AND_DIFFUSE);

If you just want to test with no light source enable lighting and call

glColorMaterial(GL_EMISSION);

That should definitely give you something to look at but the color won’t really look like it’s affected by the light.

Well i’ve taken a look at that tut and i’ve fixed it now. :slight_smile:

mental would you post your code or send it to me?

thanks. mike

Put this in the global section:

GLfloat light_ambient[]= { 0.0f, 1.0f, 1.0f, 1.0f };
GLfloat light_diffuse[]= { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat light_specular[]= { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat light_position[]= { 1.0f, 0.0f, 0.0f, 0.0f };

GLfloat specular [] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat shininess [] = { 100.0 };
GLfloat position [] = { 1.0, 1.0, 1.0, 0.0 };

You can use all of the above for lighting, i find that the last three emit much less light.

Put this in the block that draws your gl scene:

 

   	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);

	// Set the GL_AMBIENT_AND_DIFFUSE color state variable to be the
	// one referred to by all following calls to glColor
	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	glEnable(GL_COLOR_MATERIAL);

	// Create a Directional Light Source
	glLightfv (GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT1, GL_SPECULAR, light_specular);
glLightfv (GL_LIGHT1, GL_POSITION, light_position);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT1);