Thread: OpenGL lighting

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    1

    Cool OpenGL lighting

    Hey

    I am going to be making a game soon but before i do i'm going to make a few little different opengl apps to make sure i have the hang on it. Now that's where the problem starts, I have this little app that rotates a polygon with a white triangle practically in the middle of the polygon, so you can see the edges of the polygon disappearing behind the white triangle as it rotates. (I once had the keyboard controling it but that codes not on this computer ). But my main problem is I have never been able to get lighting and shadows to work. My opengl code is run after windows messages are handled ect and is in a constant loop untill the window is closed. The opengl code is as follows:

    Code:
    			// OpenGL animation code goes here
    			
    			glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    			glClearDepth( -1 );
    			glClear( GL_COLOR_BUFFER_BIT );
    			glClear( GL_DEPTH_BUFFER_BIT );
    
    			glDepthRange( -5, 5);
    
    			glPushMatrix();
    			glRotatef( theta, 1.0f, 1.0f, 0.0f );
    			glEnable( GL_CULL_FACE );
    			glCullFace( GL_FRONT );
    			glEnable(GL_DEPTH_TEST);
    			glDepthFunc(GL_GEQUAL);
    			glBegin( GL_POLYGON );
    			glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 1.0f, 0.0f );
    			glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.87f, -0.5f, 0.0f );
    			glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( -0.87f, -0.5f, 0.0f );
    			glColor3f( 1.0f, 1.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, -1.0f );
    			glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.87f, -0.5f, 0.0f );
    			glEnd();
    			glBegin( GL_POLYGON );
    			glColor3f( 1.0f, 1.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, -1.0f );
    			glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( -0.87f, -0.5f, 0.0f );
    			glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.87f, -0.5f, 0.0f );
    			glEnd();
    			glPopMatrix();
    			
    			glPushMatrix();
    			glMapGrid1f( 0, 0, 0 );
    			glDisable( GL_CULL_FACE );
    			glBegin( GL_POLYGON );
    			glColor3f( 1.0f, 1.0f, 1.0f ); glVertex3f( 0.0f, 1.0f, -0.1f );
    			glColor3f( 1.0f, 1.0f, 1.0f ); glVertex3f( 0.87f, -0.5f, -0.1f );
    			glColor3f( 1.0f, 1.0f, 1.0f ); glVertex3f( -0.87f, -0.5f, -0.1f );
    			glEnd();
    			glPopMatrix();
    			
    			SwapBuffers( hDC );
    			
    			theta += 1.0f;
    Now first this is my first real opengl object, is that the cleanest way to do things? Second, I have tried playing around with glEnable and glLighti but havn't been able to get it to work . So I'm probably doing it all wrong. Can anyone help out and let me know what code to use for shadowing, how to set it up and where i should put it, maybe even an example and I can learn from there. Thanks guys.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    well, as for efficient ways of moving things. that is how I do it. As for lighting you kind of need a couple of variables defining all its properties. have you looked at chapter 5 of the opengl Programming guide? here

  3. #3
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    I had a similar problem occur and it was answered on here. The solution, if I remember correctly, was to set up normals. The below example should give you the general idea (it's a modified portion of your code).

    Code:
    glBegin( GL_POLYGON );
     glColor3f( 1.0f, 1.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, -1.0f ); glNormal3f( 0.0f, 0.0f, -1.0f);
     glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( -0.87f, -0.5f, 0.0f ); glNormal3f( -0.8f, -0.5f, 0.0f );
     glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.87f, -0.5f, 0.0f ); glNormal3f( 0.87f, -0.5f, 0.0f );
     glEnd();

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Frobozz
    I had a similar problem occur and it was answered on here. The solution, if I remember correctly, was to set up normals. The below example should give you the general idea (it's a modified portion of your code).

    Code:
    glBegin( GL_POLYGON );
     glColor3f( 1.0f, 1.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, -1.0f ); glNormal3f( 0.0f, 0.0f, -1.0f);
     glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( -0.87f, -0.5f, 0.0f ); glNormal3f( -0.8f, -0.5f, 0.0f );
     glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.87f, -0.5f, 0.0f ); glNormal3f( 0.87f, -0.5f, 0.0f );
     glEnd();

    you need to specify the normal before each vertex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shininess lighting GLUT OpenGL
    By Zishaan in forum Game Programming
    Replies: 1
    Last Post: 04-22-2007, 08:30 PM
  2. OpenGL lighting
    By Zishaan in forum Game Programming
    Replies: 6
    Last Post: 04-21-2007, 03:24 PM
  3. Lighting in OpenGL
    By MathFan in forum Game Programming
    Replies: 5
    Last Post: 06-28-2004, 12:56 PM
  4. OpenGL lighting problems
    By sirSolarius in forum Game Programming
    Replies: 0
    Last Post: 12-01-2003, 09:11 PM
  5. need help with opengl lighting!!
    By genghis37 in forum Game Programming
    Replies: 1
    Last Post: 06-22-2002, 12:28 AM