Thread: OpenGL lighting

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    37

    OpenGL lighting

    Hey peeps,

    I have tried to make my game have simple lighting which i have seen on many tutorials on the web. However, i cant get mine to have the same effect, and i cant work out why. All i want is shininess to be in effect, which i have seen on many screen shots, and also makes the other half of the shape to be darker (shading?), i will show my environment setup:

    Code:
    const GLfloat shininess [] = { 100.0 };
    const GLfloat specular [] = { 1.0, 1.0, 1.0, 1.0 };
    
    const GLfloat LightPos[] = { 1.0, 1.0, 1.0, 0.0 };
    
    
    void glutInit(int argc, char ** argv) 
    
    {
    
      	// Setup the basic GLUT stuff
    
      	
    
    	glutInit(&argc, argv); 						// initialises environment
    
      	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 	// uses a double buffer - colour mode - 
    
    
    
      	// Create the window
    
      
    
    	glutInitWindowSize(200, 200); 					// creates the window size
    
      	glutInitWindowPosition(100, 150);  				// position of the window on the screen
    
      	glutCreateWindow("3D Draughts");
    
    
    
    	// Register the event callback functions
    
      
    
    	glutDisplayFunc(Display); 					// called everytime it needs to refresh the display
    
    	glutIdleFunc(idle); 						// called when it isnt doing anything
    
    
    
    
    
    	
    
      	glutKeyboardFunc(KeyboardHandler);         			// picks up keyboard strokes
    
    	glutSpecialFunc(SpecKeyHandler);
    
    	
    
    	glutMouseFunc(processMouse);					// mouse movement and clicks
    
    	glutMotionFunc(processMouseActiveMotion);
    
    
    
    
    
    
    
    	glutReshapeFunc(Reshape); 					// called everytime it needs to reshape something
    
    
    
    	orientMe();
    
    
    
    	glClearDepth(1.0f);
    
    	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    
    	glClearColor(0.0, 0.0, 0.0, 0.0);
    
    
    	glShadeModel(GL_SMOOTH);
    
    
    	glEnable(GL_DEPTH_TEST);					// Enabling depth test
    
    
    
    
    	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
    	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
    	glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
    	
    	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);	
    	glEnable(GL_COLOR_MATERIAL);
    
    	glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
    
      	glEnable(GL_LIGHTING);
    	glEnable(GL_LIGHT0);						// Enable lighting
    
    	
    
    									// At this point, control is relinquished to the GLUT event handler.
    
      	glutMainLoop();   						// Control is returned as events occur, via the callback functions.
    
       
    
    }
    If anybody could help, it would be a GREAT help, thank you very much.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You didn't draw an object. glColorMaterial sets the lighting attributes for an object drawn thereafter.

    Though I'm not sure, but you should enable before using them. Enable light0 then set its attributes, same with colormaterial. I think it makes it easier to keep track of.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    37
    Hey,

    Ive created a game, and call draw() in my game.cpp, but i added draw() to my environment setup as well, it doesn't do anything?? maybe i should delete the draw() from my game.cpp??

    Thanks.

  4. #4
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    glutDisplayFunc() looks like it calls Display() in your code, but you say your draw function is draw() ? The display function should be the one calling your rendering code. And why would you call the rendering function on the environment setup?
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    37
    sorry, ignore what i said before, you are right, it is the way you said it. I just don't understand why the shininess is not affecting the board and draughts on my game. Is their anything wrong with the setup??

  6. #6
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Try calling this:
    Code:
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
    In the rendering code, and not the setup code. Not sure if it'll matter in your case though. You also may want to try setting the specular componant of the light, like this:
    Code:
    glLightfv(GL_LIGHT0, GL_SPECULAR, youSpecularColour);
    Can't remember for sure if that's required for the specular of the material to take effect or not. It's been a while since I've used standard OpenGL lighting.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    37
    no luck :-( it just stays the same....

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 BabyG in forum Game Programming
    Replies: 3
    Last Post: 08-29-2004, 09:58 AM
  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