LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-23-2004, 07:00 AM   #1
sterrenkijker
Member
 
Registered: Mar 2004
Location: the Netherlands
Distribution: Debian Sarge
Posts: 302

Rep: Reputation: 30
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
 
Old 08-23-2004, 09:17 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.

Last edited by deiussum; 08-23-2004 at 09:22 AM.
 
Old 08-23-2004, 11:15 AM   #3
sterrenkijker
Member
 
Registered: Mar 2004
Location: the Netherlands
Distribution: Debian Sarge
Posts: 302

Original Poster
Rep: Reputation: 30
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
 
Old 08-23-2004, 11:46 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.
 
Old 08-24-2004, 04:20 AM   #5
sterrenkijker
Member
 
Registered: Mar 2004
Location: the Netherlands
Distribution: Debian Sarge
Posts: 302

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Control case lighting from linux Napalm Llama Linux - Hardware 2 11-30-2005 10:53 AM
Overwrite Mesa OpenGL with ATI OpenGL Carl-Fredrik Slackware 12 10-01-2004 04:33 PM
The lighting in Quake3 Arena demo and Linux couvier Linux - Software 5 07-21-2004 09:32 AM
Changing from MESA OpenGL to ATI OpenGL tillyoubreakit Linux - Hardware 19 10-07-2003 08:32 PM
OpenGL lighting with SDL MadCactus Programming 0 09-21-2003 12:52 PM


All times are GMT -5. The time now is 11:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration