Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Multiple Lights & Colors

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2000
    Posts
    3

    Multiple Lights & Colors

    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
    }

  2. #2
    Member Regular Contributor
    Join Date
    May 2000
    Location
    Batavia, NY, 14020
    Posts
    259

    Re: Multiple Lights & Colors

    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).]
    - I am not opensource! -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •