Thread: OpenGL lighting problems

  1. #1
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154

    OpenGL lighting problems

    Well, after trying everything I could think of on this problem, I decided to try CBoard.

    I've been trying to learn OpenGL for a little while now, and this is about my fifth stab at it.

    All this program does is init OpenGL, load a cube model, print the cube, allow you to rotate the cube, and quit when <ESC> or <space> is pressed.

    Now, when I try to add in lights, the color of the cube changes. Also, when I rorate the cube, the color of the cube changes. And, when there are no lights, the sides of the cube are multi-coloured. When I add in the lights, they are a solid color. Any ideas?

    Thanks.
    Last edited by kawk; 04-14-2007 at 08:01 PM.

  2. #2
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    I think I'm having problems with the following:

    Code:
        pos4[0] = 0.5, pos4[1] = 0.5, pos4[2] = 0.5, pos4[3] = 1.0;
        glLightfv(GL_LIGHT1, GL_AMBIENT, pos4);
    
        pos4[0] = 1.0, pos4[1] = 1.0, pos4[2] = 1.0, pos4[3] = 1.0;
        glLightfv(GL_LIGHT1, GL_DIFFUSE, pos4);
    
        pos4[0] = 1.0, pos4[1] = 1.0, pos4[2] = -200.0, pos4[3] = 1.0;
        glLightfv(GL_LIGHT1, GL_POSITION, pos4);
    
        glEnable(GL_LIGHT1);
    
        glEnable(GL_LIGHTING);
    Also, uncomment out that section on the above section in the .zip, I uncommented it to get the screenshot.

    Thanks again.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    In my house
    Posts
    29
    I haven't done much with OpenGL lights, but what I can tell you is this, when the light is off, it is using the glcolor3dv which you set up in the gllib.c file, and when the light is off it uses a meterial which you define I think in the init_game() function, so that would explain why when the light is off the have different colours from when it is on, but I am not 100% and I can't say that I am very good either :P

  4. #4
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Just reading my OpenGL book again, perhaps I need to define a material? Would that make a difference?

  5. #5
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    OK, so now that I set a material, it works. However, the light is a bit bright, any more suggestions?

  6. #6
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Make its colour darker.

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    To organize it a little better instead of reusing the pos array you should make an array for each color you want and make them global

    ex
    Code:
    float squarePos[] = {3.0, 0.0, -4.0, 1.0};
    float squareAmbient[] = {0.1, 0.2, 0.3};
    float squareDiffuse[] = { 0.4, 0.4, 0.1};
    float squareSpecular[] = {0.4, .04, 0.4};
    also you need to at most position the light by
    Code:
    glLightfv{GL_LIGHT1, GL_POSITION, squarePos};
    The first three values in the squarePos array are the x, y, z coordinates, and the third indicating whether it's positional or not. 0 indicate's it's infinitely far away, while 1 indicate's it has a finite distance.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    In my house
    Posts
    29
    I can't remember the parameters passed to a meterial creation function, but one of the parameters defines the brightness . . . Diffuse I think may be it, but I can't be sure

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    glMaterial and glColorMaterial are only used on the object. for glMaterial you specify a face (GL_FRONT_AND_BACK ususally), the lighting attribute(specular, ambient, diffuse), and the vector specifying' it's color.

    A better way I think is to use glColorMaterial, which you have to enable. You specify it as such
    Code:
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
    glColor4f(1, 0, 0, .5);
    glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
    //..etc
    
    //draw your object with those attributes.

  10. #10
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Well, you see, when I rotate the cube, the _entire_ cube color changes. Not just the face facing the light. I'll post screenshots as soon as I can. I am working out of NeHe and a book called "OpenGL game development", by the creators of GameDev.

    Thanks again.

  11. #11
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I'm reading the same book.

    The thing is your light source is positioned 200 units in the negative z direction and it's a positional light. A positional light's effect on an object depends on its distance from the object. You indicated it was a positional light by setting the 4th value to 1 with the GL_POSITION value. Plus the fact that it's behind the object being rendered you will not see the effect. Try changing the z value to 10, which brings it 10 units in front of the object toward the screen.

    Code:
        pos4[0] = 1.0, pos4[1] = 1.0, pos4[2] = -200.0, pos4[3] = 1.0;
        glLightfv(GL_LIGHT1, GL_POSITION, pos4);
    you should change that to
    Code:
    float light1Pos[] = { 1.0, 1.0, 10, 1.0 };
    glLightfv(GL_LIGHT1, GL_POSITION, light1Pos);
    you should try using a less verbose method of initializing/re-initializing arrays, because it can get pretty confusing quickly.
    Last edited by indigo0086; 04-17-2007 at 09:35 PM.

  12. #12
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Thanks Indigo. I'll try that as soon as I can, probably later on today.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL lighting question...
    By psychopath in forum Game Programming
    Replies: 3
    Last Post: 07-31-2004, 03:28 PM
  2. Lighting in OpenGL
    By MathFan in forum Game Programming
    Replies: 5
    Last Post: 06-28-2004, 12:56 PM
  3. Problems with rotations with OpenGL and GLUT
    By darcome in forum Game Programming
    Replies: 13
    Last Post: 07-05-2002, 12:12 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. So.. what's the difference between DirectX and OpenGL?
    By QuestionC in forum Game Programming
    Replies: 6
    Last Post: 01-19-2002, 06:18 PM