PDA

View Full Version : Light problems



AtZeuS
09-29-2005, 08:02 AM
I know this question has already been asked, but i've read all the other posts and searched the internet till my fingers bled. I'm one of those guys who cannot get his lighting right :) Anyway i still cannot get it right:

I want a constant light that does not move with the cube, i.e. the lighting on my cube remains the same as I rotate the cube.

Any ideas? Thanks!

init code:


glEnable (GL_DEPTH_TEST);
glShadeModel( GL_SMOOTH ) ;
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glEnable ( GL_LIGHTING ) ;
glEnable( GL_LIGHT0 ) ;

draw code:


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();



GLfloat light0_pos[] = {1.0 , 5.0f , 1.0f , 1.0f} ;
glLightfv(GL_LIGHT0, GL_POSITION , light0_pos ) ;
gluPerspective(60.0f,aspectRatio,0.2f,30.0f);

glTranslatef(0.0f,0.0f,-1.0f);
glRotatef( mouse_y * 180.0f,1.0f,0.0f,0.0f);
glRotatef( mouse_x * 180.0f,0.0f,1.0f,0.0f);
glScalef( 0.1f , 0.1f, 0.1f ) ;

drawCube();

glutSwapBuffers();
drawCube code:



void drawCube( ) {

point3 points[ 8 ] = { {-1.0f , -1.0f, -1.0f } ,
{-1.0f , 1.0f, -1.0f } ,
{1.0f , 1.0f, -1.0f } ,
{1.0f , -1.0f, -1.0f } ,
{-1.0f , -1.0f, 1.0f } ,
{-1.0f , 1.0f, 1.0f } ,
{1.0f , 1.0f, 1.0f } ,
{1.0f , -1.0f, 1.0f } } ;

point3 normal[ 6 ] = { { 0.0f , 0.0f , -1.0f } ,
{ 0.0f , 0.0f , 1.0f } ,
{ 0.0f , -1.0f , 0.0f } ,
{ 1.0f , 0.0f , 0.0f } ,
{ 0.0f , 1.0f , 0.0f } ,
{ -1.0f , 0.0f , 0.0f } } ;


glBegin( GL_QUADS ) ;
glColor3f( 0.0f, 1.0f , 1.0f );
quad( 0 , 1 , 2 , 3 , points , normal[ 0 ] ) ;
glColor3f( 0.0f, 0.0f , 1.0f );
quad( 4 , 5 , 6, 7 , points , normal[ 1 ] ) ;
glColor3f( 0.0f, 1.0f , 0.0f );
quad( 0 , 4 , 5 , 1 , points , normal[ 2 ] );
glColor3f( 0.3f, 0.4f , 1.0f );
quad( 1 , 5 , 6 , 2 , points , normal[ 3 ] );
glColor3f( 1.0f, 0.0f , 1.0f );
quad( 2 , 6 , 7 , 3 , points , normal[ 4 ] );
glColor3f( 0.0f, 0.6f , .6f );
quad( 3 , 7 , 4 , 0 , points , normal[ 5 ] );
glEnd();

}

void quad( int a, int b, int c, int d, point3* points , point3 normal) {
glNormal3fv( normal ) ;
glVertex3fv( (GLfloat * ) points[ a ] ) ;
glVertex3fv( (GLfloat * ) points[ b ] ) ;
glVertex3fv( (GLfloat * ) points[ c ] ) ;
glVertex3fv( (GLfloat * ) points[ d ] ) ;
}
Also, I don't think it matters, but i use these compiler directives to make it work under mingw :

#define DEV_CPP

#ifdef DEV_CPP // Dev-c++ related stuff comming up
#define _WCHAR_T_DEFINED
#define GLUT_DISABLE_ATEXIT_HACK
#endif

Ehsan Kamrani
09-29-2005, 10:04 AM
Here's your problem:

glLightfv(GL_LIGHT0, GL_POSITION , light0_pos ) ;
gluPerspective(60.0f,aspectRatio,0.2f,30.0f);

Use from the modelview matrix to specify the postition of your light. Use from the projection matrix to define the view volume. As an example:

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(60.0f,aspectRatio,0.2f,30.0f);

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION , light0_pos ) ;

GL_POSITION uses from the *current* modelview matrix to specify the position of your light. So as you know, before you transform the modelview matrix, you should specify the position of your light--as you have done in this code.
-Ehsan-
*Zehne Ziba* managing director.

AtZeuS
09-29-2005, 10:41 AM
Yay! It works! Thanks, i completely forgot about the matrix mode :)