PDA

View Full Version : Please help me- light positions



alok47
07-19-2001, 03:41 AM
I want to place my POSITIONAL light at top, bottom, left, right, back and front of a cube and want to see its affect .Whatever position I specify the effect remains same. This is the code I am using:
// array of positions
position [][4] ={{top}, {bottom}..}

//light properties
glLight ( GL_LIGHT0, GL_DIFFUSE, COLOR ) ;

//saving the modelview matrix
glPushMatrix();
glLightfv ( GL_LIGHT0, GL_POSITION, position[i]) ;
glPopMatrix () ;

// drawing the Object here.

Of course normals are specified correctly for
the cube.Also I am not permitted to use any
glut function to change viewpoint.But I can use glFrustum.
Please help me out... R I ll go mad.

citizen
07-19-2001, 03:59 AM
Did you enabled lighting and light0?
glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);
Did you use glMaterial()(not glColor()) to specify the color of your cube?
Did you set the material (diffuse, ambient, specular)corectly?

(My common mistakes http://www.opengl.org/discussion_boards/ubb/smile.gif

Maybe you will have to call glLightModelf() somehow.

Sebb
07-19-2001, 04:01 AM
Perhaps I gonna say a stupid thing but where is your glEnable(GL_LIGTHING); line ? If you have not, it normal that nothing seems to change... If it isn't this problem, tell me, I'll try to help you more http://www.opengl.org/discussion_boards/ubb/smile.gif

Sebb
07-19-2001, 04:03 AM
Perhaps I gonna say a stupid thing but where is your glEnable(GL_LIGTHING); line ? If you have not, it normal that nothing seems to change... If it isn't this problem, tell me, I'll try to help you more http://www.opengl.org/discussion_boards/ubb/smile.gif

alok47
07-19-2001, 04:48 AM
Hi citizen and sebb,
Thanx for the early reply..
But I my problem is still unsolved .

I have enabled both the openGL lighting capability and GL_LIGHT0.
But I have not specified glLightModel assuming the default value which
takes the viewer at infinite position.
Also the material properties are specified correctly. Since normals are of unit length,
so there is no need to normalize them.
I ll be grateful if you kindly look at this code. Here it is.


void Test ()
{
/// different light positions
GLfloat position [][4] =
{
{ 0, 170, 0, 1 }, // top of the object
{ 0, -170, 0, 1 }, // bottom of the object
{ 0, 125, 50, 1 }, // front of the object
{ 0, -125, -50, 1 }, // back of the object
{ 0, 125, 0, 1 }, // left side of the object
{ 0, 125, 0, 1, } // right side of the object
} ;

glEnable ( GL_LIGHTING ) ;
glEnable ( GL_LIGHT0 ) ;
glEnable ( GL_DEPTH_TEST ) ;

// Settings for lighting
glShadeModel ( GL_SMOOTH ) ;
glPolygonMode ( GL_FRONT_AND_BACK, GL_FILL ) ;
glLightModeli ( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE ) ;

//setting light properties
glLightfv ( GL_LIGHT0, GL_AMBIENT, grey ) ;
glLightfv ( GL_LIGHT0, GL_DIFFUSE, red ) ;

//setting material properties
glMaterialfv ( GL_FRONT, GL_AMBIENT, white ) ;
glMaterialfv ( GL_FRONT, GL_DIFFUSE, white ) ;

//for different positions
for ( i=0 ; i<6 ; i++ )
{
// specifying light position
glPushMatrix () ;
glLoadIdentity () ;
glLightfv ( GL_LIGHT0, GL_POSITION, position [i] ) ;
glPopMatrix () ;

// rendering cube.The rotate operation is done to see the three faces of the cube.
glPushMatrix () ;
glLoadIdentity () ;
glTranslated ( 500 + x, 125, 0 ) ;
glRotated ( 45, 1, 1, 1 ) ;
drawCuboid ( -25, -25, 0, 50, 50, 50 ) ;// This is a user defined function that draws cube at (-25, -25, 0) of lenth =50, breadth=50 and width = 50
glPopMatrix () ;

x += 75 ;
}

glDisable ( GL_LIGHTING ) ;
glDisable ( GL_LIGHT0 ) ;
glDisable ( GL_DEPTH_TEST ) ;
}

citizen
07-19-2001, 05:54 AM
From the positions of your light i suppose that you wan't to use positional lights. Than you should translate the light too. But im not shure whether this will help.

Deiussum
07-19-2001, 11:23 AM
A couple of things I notice.. first..




// specifying light position
glPushMatrix () ;
glLoadIdentity () ;
glLightfv ( GL_LIGHT0, GL_POSITION, position [i] ) ;
glPopMatrix () ;


If you want the light to rotate with the cube, you should use glLight* after your glRotated call. If you want the light to stay in place just do it after your glLoadIdentity before drawing the cube. It's not necessary to push/pop the light positioning separately.

The other thing, I have to ask is how many vertices do you use for your cube? If you basically just use 1 quad for each face, your lighting isn't going to be very accurate. Lighting calculations are done on a per-vertex basis, so the more vertices you have, the better the lighting calculations will be. For instance if you have a huge single quad with a light that should shine right in the middle of it, the lighting will be very poor because the vertices of the quad will receive very little light.

alok47
07-20-2001, 02:04 AM
Hi Deiussum!
I am using only four vertices for each surface of the cube.Might be that is the reason of inaccurate lighting.