home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / lights.c < prev    next >
C/C++ Source or Header  |  1999-08-19  |  3KB  |  142 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include "orbit.h"
  23.  
  24. static float ambient[]         =   { 0.0, 0.0, 0.0, 1.0 };
  25. static float diffuse[]         =   { 1.0, 1.0, 0.0, 1.0 };
  26. static float position0[]       =   { 0.0, 0.0, 0.0, 1.0 };
  27. static float lmodel_ambient[]  =   { 0.01, 0.01, 0.01, 1.0 };
  28. static float lmodel_twoside[]  =   {GL_TRUE};
  29. static float front_shininess[] =   {50.0};
  30. static float front_specular[]  =   { 1.0, 1.0, 1.0, 1.0 }; 
  31. /* static float front_specular[]  =   { 0.0, 0.7, 0.0, 1.0 }; */
  32.  
  33. Lights()
  34. {
  35.     int l;
  36.     double v[3];
  37.     float f[3];
  38.  
  39.     /* Enable depth buffer (why is this *here*?) */
  40.     glDepthFunc (GL_LEQUAL);
  41.     glEnable (GL_DEPTH_TEST);
  42.  
  43.     /* Turn off ambient light */
  44.     glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  45.     glLightfv (GL_LIGHT0, GL_AMBIENT, lmodel_ambient);
  46.  
  47.     /* Define light0, the big, main light */
  48. /*    glLightfv (GL_LIGHT0, GL_POSITION, position0);    */
  49.     Vsub (v, planet[0].pos, player.pos);
  50.     f[0] = (float) v[0];
  51.     f[1] = (float) v[1];
  52.     f[2] = (float) v[2];
  53.     glLightfv (GL_LIGHT0, GL_POSITION, f);
  54.  
  55.     /* Turn on lighting in general */
  56.     glEnable (GL_LIGHTING);
  57.  
  58.     /* Turn on individual lights */
  59.     glEnable (GL_LIGHT0);
  60.  
  61.     /* Check each non-permanent light */
  62.     for (l=0; l<NLIGHTS; l++)
  63.     {
  64.         if (light[l].age > 0.0)
  65.         {
  66.             light[l].age += deltaT;
  67.             glLightfv (light[l].gl_num, GL_DIFFUSE, light[l].color);
  68.             glLightfv (light[l].gl_num, GL_SPECULAR, light[l].color);
  69. /*            glLightfv (light[l].gl_num, GL_POSITION, light[l].pos);    */
  70.             f[0] = light[l].pos[0] - (float) player.pos[0];
  71.             f[1] = light[l].pos[1] - (float) player.pos[1];
  72.             f[2] = light[l].pos[2] - (float) player.pos[2];
  73.             glLightfv (light[l].gl_num, GL_POSITION, f);
  74.             glEnable (light[l].gl_num);
  75.         }
  76.         else
  77.         {
  78. /*            glDisable (light[l].gl_num);    */
  79.         }
  80.     }
  81.  
  82.     /* Pick smooth or flat shading */
  83.     glShadeModel (GL_SMOOTH);
  84. /*    glShadeModel (GL_FLAT);        */
  85. }
  86.  
  87. InitLights()
  88. /*
  89.  *  Set up non-permanent light sources
  90.  */
  91. {
  92.     int i;
  93.  
  94.     for (i=0; i<NLIGHTS; i++)
  95.     {
  96.         light[i].age = 0.0;
  97.         light[i].pos[3] = 1.0;
  98.         light[i].gl_num = GL_LIGHT1 + i;
  99.     }
  100. }
  101.  
  102. int FindLight()
  103. /*
  104.  *  Return free or oldest light
  105.  */
  106. {
  107.     int i, oldest;
  108.     double maxage;
  109.  
  110.     for (i=0; i<NLIGHTS; i++)
  111.     {
  112.         if (light[i].age == 0.0) return (i);
  113.  
  114.         if (i == 0)
  115.         {
  116.             oldest = i;
  117.             maxage = light[i].age;
  118.         }
  119.         else
  120.         {
  121.             if (light[i].age > maxage)
  122.             {
  123.                 maxage = light[i].age;
  124.                 oldest = i;
  125.             }
  126.         }
  127.     }
  128.  
  129.     return (oldest);
  130. }
  131.  
  132. DestroyLight (l)
  133. int l;
  134. /*
  135.  *  Destroy specified light
  136.  */
  137. {
  138.     light[l].age = 0.0;
  139.     glDisable (light[l].gl_num);
  140. }
  141.  
  142.