PDA

View Full Version : Multiple Lights & Colors



HyPer
06-14-2000, 08:41 AM
I'm sure I'm missing something obvious, but what I want is simple. I looked for any example code or related questions but can't seem to find anything. I'm just trying to get the typical red green and blue lights illuminating an object. I can get a single light & color to work, but adding any other lights they all end up being the same color apparently.

The display function:

void Display() {

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (GLfloat) ww/(GLfloat) wh, 1, 700);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 3.0f, 1.0f };

glColor4f(1, 0, 0, 1);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT1);

LightPosition[2] = 0;
LightPosition[1] = 3;
glColor4f(0, 1, 0, 1);
glLightfv(GL_LIGHT2, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT2, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT2, GL_POSITION,LightPosition);
glEnable(GL_LIGHT2);

LightPosition[1] = 0;
LightPosition[0] = 3;
glColor4f(0, 0, 1, 1);
glLightfv(GL_LIGHT3, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT3, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT3, GL_POSITION,LightPosition);
glEnable(GL_LIGHT3);

glColor4f(1, 1, 1, 1);

// spin 'round some
rotate += 5;
glTranslatef(0, 0, -10);
glRotatef(rotate, 3, 1, 2);

sph.draw(); // draws the sphere
}

Siwko
06-14-2000, 09:39 AM
You don't use glColor for specifying lighting colors, just material/vertex colors. The quads you set up in your lighting materials (your 4 float array) for specular, ambient and diffuse specify the color of the light in RGBA color.

Thus, get rid of glColor in the above code and replace it with new lighting definition arrays.

You're defining a plain white light with your arrays. You need three seperate ones,
one with diffuse and ambient beging 1,0,0,1; the 2nd beging 0,1,0,1; and the last pair being 0,0,1,1 (or whatever combination you want)

Siwko

[This message has been edited by Siwko (edited 06-14-2000).]