View Full Version : OpenGL Lighting Problem

07-19-2008, 05:29 PM
There is a lighting problem I just cant get over with. To try to see where I was doing a mistake, I completely translated a C openGL light tutorial to Perl (which is the language I am using). And it still is not working. Can it be about a library or anything I have to install, or could it be about my graphics driver? Here is the translated code (@ stands for array variables, $ stands for non-array variables,
my ($w, $h) = @_; lines are for getting input to functions)

use OpenGL qw(:all);
use strict;

our $newQuad = gluNewQuadric ();
our $window;

sub init {

our @Light_Position = ( -3.0, -3.0, -3.0, 3.0 );
our @Specular_Material = (1.0, 1.0, 1.0, 1.0);
our $Shiny_Material = 50;

glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);

glMaterialfv_s(GL_FRONT, GL_SPECULAR, \@Specular_Material);
glMaterialfv_s(GL_FRONT, GL_SHININESS, $Shiny_Material);
glLightfv_p(GL_LIGHT1, GL_POSITION, @Light_Position);



sub ReSizeGLScene {
my ($w, $h) = @_;
glViewport (0, 0, $w, $h);
glMatrixMode (GL_PROJECTION);

if ($w <= $h)
glOrtho (-1.5, 1.5, -1.5*$h / $w, 1.5*$h/$w, -10.0, 10.0);

glOrtho (-1.5*$w/$h,1.5*$w/$h, -1.5, 1.5, -10.0, 10.0);


sub DrawGLScene {
gluSphere($newQuad,1, 18, 18);
glFlush ();

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
return 0;

07-19-2008, 06:08 PM
First of all, you should use the doublebuffering (GLUT_DOUBLE instead of GLUT_SINGLE and glutSwapBuffers where you have glFlush right now). Now, what exactly isn't working? Does the sphere render (if you disable lighting)?

07-19-2008, 06:14 PM
Sorry, I forgot to mention the problem (I changed the buffers as you told, I am going to read on them more as I dont know what their differences are).

The object is rendered. When the light is disabled, the ball appears white but when the light is enabled it appears dark. It suggested me that I placed the light wrong but I put it high up or in other places and it still didnt work. In fact changing the position of the light from the array does not change anything in the screen. I have also tried to input the location array both as itsself or as a reference to the function which does not create a difference too.

07-19-2008, 07:10 PM
The problem is with the syntax for some reason you cant give those arrays as references or direct arrays into those functions, doing those like

glMaterialfv_p(GL_FRONT, GL_SPECULAR, 1, 1, 1, 1);
glMaterialfv_p(GL_FRONT, GL_SHININESS, 50);
glLightfv_p (GL_LIGHT0, GL_POSITION, 1, 1, 1, 0);

did the trick

07-19-2008, 07:20 PM
Ok, I never really used OpenGL lighting so all suggestions will be just random guesses:

1. Try light position of (0, 0, 1, 0) at first - this will make the light a directional source (I am not sure about the z axis, try both)

if that does not help:

2. Set the diffuse material (maybe the driver provides an incorrect initial value)
3. Make sure that glu generates normals for the quadric object (via gluQuadricNormals)
4. Intialize the modelview matrix to identity prior to setting the light position (don't forget that light position is being transformed by the modelview matrix)

Have fun debugging :)

EDIT: Oh, I am too late... Still, glad you could resolve it :)

07-20-2008, 03:19 PM
Here is new question from me about lightining. The program is same as the above (or similar lets say) but I have added

glColor3f (0.0, 0.0, 0.8)

on the draw part to make the sphere blue. But it renders as gray. I tried to find what is wrong and it only renders blue when I disable the lights. Can't we give color to the object which we light? Do we need to load textures on them? Thanks

07-20-2008, 04:46 PM
it renders as gray

put this way before your glColor :

read the manual :

And check this "14. Careful Enabling Color Material" :

07-20-2008, 04:50 PM
Thanks, I will