Jump to content
  • Advertisement
Sign in to follow this  
mbanghart

Lighting - better way?

This topic is 3684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, My code works but I was wondering if there is a better way to do this. I have up to eight light sources (read from a file). If the user presses 1 - 8 the lights are toggled on/off. The user can also turn all the lights on with a menu.
void EnableLights(int id)
{
     if (all_on == true)
     {
       switch (id)
       {
       case 0 :
                 glEnable(GL_LIGHTING);   
                 glEnable(GL_LIGHT0);
                 glLightfv(GL_LIGHT0,GL_POSITION,light_pos[0]);
                 glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient[0]);
                 glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse[0]);                                                               
                 break;
       case 1 :
                 glEnable(GL_LIGHTING);               
                 glEnable(GL_LIGHT1);                               
                 glLightfv(GL_LIGHT1,GL_POSITION,light_pos[1]);
                 glLightfv(GL_LIGHT1,GL_AMBIENT,light_ambient[1]);
                 glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse[1]);                                                               
             
                 break;
       case 2 :
                 glEnable(GL_LIGHTING);               
                 glEnable(GL_LIGHT2);                               
                 glLightfv(GL_LIGHT2,GL_POSITION,light_pos[2]);
                 glLightfv(GL_LIGHT2,GL_AMBIENT,light_ambient[2]);
                 glLightfv(GL_LIGHT2,GL_DIFFUSE,light_diffuse[2]);                                                               
             
                 break;
       case 3 :
                 glEnable(GL_LIGHTING);   
                 glEnable(GL_LIGHT3);                               
                 glLightfv(GL_LIGHT3,GL_POSITION,light_pos[3]);
                 glLightfv(GL_LIGHT3,GL_AMBIENT,light_ambient[3]);
                 glLightfv(GL_LIGHT3,GL_DIFFUSE,light_diffuse[3]);                                                               
            
                 break;                                     
       case 4 :
                 glEnable(GL_LIGHTING);               
                 glEnable(GL_LIGHT4);                               
                 glLightfv(GL_LIGHT4,GL_POSITION,light_pos[4]);
                 glLightfv(GL_LIGHT4,GL_AMBIENT,light_ambient[4]);
                 glLightfv(GL_LIGHT4,GL_DIFFUSE,light_diffuse[4]);                                                               
              
                 break;
       case 5 :
                 glEnable(GL_LIGHTING);               
                 glEnable(GL_LIGHT5);                               
                 glLightfv(GL_LIGHT5,GL_POSITION,light_pos[5]);
                 glLightfv(GL_LIGHT5,GL_AMBIENT,light_ambient[5]);
                 glLightfv(GL_LIGHT5,GL_DIFFUSE,light_diffuse[5]);                                                               
             
                 break;
       case 6 :
                 glEnable(GL_LIGHTING);               
                 glEnable(GL_LIGHT6);                               
                 glLightfv(GL_LIGHT6,GL_POSITION,light_pos[6]);
                 glLightfv(GL_LIGHT6,GL_AMBIENT,light_ambient[6]);
                 glLightfv(GL_LIGHT6,GL_DIFFUSE,light_diffuse[6]);                                                               
           
                 break;
       case 7 :
                 glEnable(GL_LIGHTING);               
                 glEnable(GL_LIGHT7);                               
                 glLightfv(GL_LIGHT7,GL_POSITION,light_pos[7]);
                 glLightfv(GL_LIGHT7,GL_AMBIENT,light_ambient[7]);
                 glLightfv(GL_LIGHT7,GL_DIFFUSE,light_diffuse[7]);                                                               
              
                 break;
       case 9 :  
                 glEnable(GL_LIGHTING);   
                 glEnable(GL_LIGHT0);    
                 glEnable(GL_LIGHT1);    
                 glEnable(GL_LIGHT3);    
                 glEnable(GL_LIGHT4);                                                                        
                 glEnable(GL_LIGHT5);    
                 glEnable(GL_LIGHT6);    
                 glEnable(GL_LIGHT7);    
                 glLightfv(GL_LIGHT0,GL_POSITION,light_pos[0]);
                 glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient[0]);
                 glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse[0]);  
                 glLightfv(GL_LIGHT1,GL_POSITION,light_pos[1]);
                 glLightfv(GL_LIGHT1,GL_AMBIENT,light_ambient[1]);
                 glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse[1]);  
                 glLightfv(GL_LIGHT2,GL_POSITION,light_pos[2]);
                 glLightfv(GL_LIGHT2,GL_AMBIENT,light_ambient[2]);
                 glLightfv(GL_LIGHT2,GL_DIFFUSE,light_diffuse[2]);   
                 glLightfv(GL_LIGHT3,GL_POSITION,light_pos[3]);
                 glLightfv(GL_LIGHT3,GL_AMBIENT,light_ambient[3]);
                 glLightfv(GL_LIGHT3,GL_DIFFUSE,light_diffuse[3]);    
                 glLightfv(GL_LIGHT4,GL_POSITION,light_pos[4]);
                 glLightfv(GL_LIGHT4,GL_AMBIENT,light_ambient[4]);
                 glLightfv(GL_LIGHT4,GL_DIFFUSE,light_diffuse[4]);  
                 glLightfv(GL_LIGHT5,GL_POSITION,light_pos[5]);
                 glLightfv(GL_LIGHT5,GL_AMBIENT,light_ambient[5]);
                 glLightfv(GL_LIGHT5,GL_DIFFUSE,light_diffuse[5]);    
                 glLightfv(GL_LIGHT6,GL_POSITION,light_pos[6]);
                 glLightfv(GL_LIGHT6,GL_AMBIENT,light_ambient[6]);
                 glLightfv(GL_LIGHT6,GL_DIFFUSE,light_diffuse[6]);  
                 glLightfv(GL_LIGHT7,GL_POSITION,light_pos[7]);
                 glLightfv(GL_LIGHT7,GL_AMBIENT,light_ambient[7]);
                 glLightfv(GL_LIGHT7,GL_DIFFUSE,light_diffuse[7]);                                                                                                                                                                                                           
                 
                 break;
                 
       }//switch
       }//if
     
     
     
}//function
Is it possible to use variables in the glLightfv(GL_LIGHT7,GL_POSITION,light_pos[7]); call? Something like: glLightfv(which_light_string,GL_POSITION,light_pos[id]); Thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by mbanghart
Is it possible to use variables in the glLightfv(GL_LIGHT7,GL_POSITION,light_pos[7]); call?

Something like:

glLightfv(which_light_string,GL_POSITION,light_pos[id]);

Sure is:

GLenum light = GL_LIGHT0 + id;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!