LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   lighting in OpenGL (with C language) (https://www.linuxquestions.org/questions/programming-9/lighting-in-opengl-with-c-language-221238/)

sterrenkijker 08-23-2004 06:00 AM

lighting in OpenGL (with C language)
 
Hello everybody,

I'm a beginner with OpenGL. I'm now writing a program to learn OpenGL, and I'm confused by the way lighting works.

I've programmed a sort of room without a ceiling, quite basic. There's also a cube in the room. I haven't used any stacks to make this.

I programmed moving around with the glTranslatef command, and rotating with glTranslatef (back to the center), glRotatef (rotating) and glTranslatef again(back to original position). Moving works really fine.

But now I started adding light to my scene, and ran in trouble: where-ever I put the light, the amount of light always seems dependent on your own position (not on the light's position). So when I am in the one side of the room, everything is dark. When I go to the other side, everything is light.

This code is a light that makes everything really bright, except when I'm near to the x-axis. The walls in the room are on the x-axis, z-axis, 50 units parallel from the x-axis and 50 units parallel from the z-axis. The floor is the x-z-plane.

Code:

    GLfloat light_position[] = { 0.0, -1.7, 2.0, 1.0 };
    GLfloat light_diffuse[] = {0.3, 0.3, 0.3, 1.0};
    GLfloat light_specular[] = {0.0, 0.0, 0.0, 1.0};
    GLfloat light_ambient[] = {0.3, 0.3, 0.3, 1.0};
    GLfloat spot_cutoff[] = {30.0};
    GLfloat spot_direction[] = {0.0, 0.2, 0.0};
    GLfloat spot_exponent[] = {0.1};
       
    glLightfv(GL_LIGHT1, GL_POSITION, light_position);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
   
    glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
    glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
    glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);
   
    glEnable(GL_LIGHT1);

Can anyone explain why the light is moving? I don't understand...

Thanks,

Corien

deiussum 08-23-2004 08:17 AM

The light position is affected by the modelview matrix. You don't show any of your modifications to that when you move, but try setting your light positions before or after your modelview transformations to see how it differs.

Also, lighting in OpenGL is vertex-based, so if you have a quad where the lighting doesn't affect any of the vertices much, it may seem darker than it should if the light should be affecting the middle of that quad. You can improve your lighting results by tessellating your quads more. (Break single quads up into multiple quads...)

Hope that helps somewhat.

sterrenkijker 08-23-2004 10:15 AM

Oh boy, apparently I haven't understood modelviewmatrices and stuff well. I'll hide myself in documentation and come back in a few weeks of so, lol :). Hard stuff, opengl...

I knew about the vertex-problem. Breaking up my walls and floor already helped something.

Thanks, now I have an idea what causes the problem.

Corien

deiussum 08-23-2004 10:46 AM

Here's an excerpt from the glLight man page, discussing setting the GL_POSITION property of a light:

Quote:

GL_POSITION params contains four integer or floating-point val-
ues that specify the position of the light in homo-
geneous object coordinates. Both integer and
floating-point values are mapped directly. Neither
integer nor floating-point values are clamped.

The position is transformed by the modelview matrix
when glLight is called (just as if it were a
point), and it is stored in eye coordinates. If
the w component of the position is 0, the light is
treated as a directional source. Diffuse and spec-
ular lighting calculations take the light's direc-
tion, but not its actual position, into account,
and attenuation is disabled. Otherwise, diffuse
and specular lighting calculations are based on the
actual location of the light in eye coordinates,
and attenuation is enabled. The initial position
is (0, 0, 1, 0); thus, the initial light source is
directional, parallel to, and in the direction of
the -z axis.

sterrenkijker 08-24-2004 03:20 AM

Good to know OpenGL has manpages!

I'm reading the first chapters of the red book again, and now I finally understand the story about the different matrices. I think I understand my problem: I made my world, and after that I did viewing transformations, and for every movement I just added more viewing transformations, so I moved the light with me as I noticed.

I'm gonna read on until I understand it all. Apparently OpenGL is not something to fool around with. Then I will have to rewrite big parts of my 'game'.

Thanks for the help!

Corien


All times are GMT -5. The time now is 02:50 PM.