home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / light.cpp < prev    next >
C/C++ Source or Header  |  2000-07-19  |  5KB  |  170 lines

  1. #include "light.h"
  2. #include "macros.h"
  3.  
  4. void Lights::GL(double time = 0.0)
  5. {
  6.   if(__entity_list.size()==0)
  7.     return;
  8.   glDisable(GL_LIGHT0);
  9.   glDisable(GL_LIGHT1);
  10.   glDisable(GL_LIGHT2);
  11.   glDisable(GL_LIGHT3);
  12.   glDisable(GL_LIGHT4);
  13.   glDisable(GL_LIGHT5);
  14.   glDisable(GL_LIGHT6);
  15.   glDisable(GL_LIGHT7);
  16.   __entity_list.rewind();
  17.   int i = (__entity_list.size()>8) ? 8 : __entity_list.size();
  18.   switch(i)
  19.   {
  20.     case 8:
  21.       (*__entity_list)->GL(GL_LIGHT7, time);
  22.       ++__entity_list;
  23.       glEnable(GL_LIGHT7);
  24.     case 7:
  25.       (*__entity_list)->GL(GL_LIGHT6, time);
  26.       ++__entity_list;
  27.       glEnable(GL_LIGHT6);
  28.     case 6:
  29.       (*__entity_list)->GL(GL_LIGHT5, time);
  30.       ++__entity_list;
  31.       glEnable(GL_LIGHT5);
  32.     case 5:
  33.       (*__entity_list)->GL(GL_LIGHT4, time);
  34.       ++__entity_list;
  35.       glEnable(GL_LIGHT4);
  36.     case 4:
  37.       (*__entity_list)->GL(GL_LIGHT3, time);
  38.       ++__entity_list;
  39.       glEnable(GL_LIGHT3);
  40.     case 3:
  41.       (*__entity_list)->GL(GL_LIGHT2, time);
  42.       ++__entity_list;
  43.       glEnable(GL_LIGHT2);
  44.     case 2:
  45.       (*__entity_list)->GL(GL_LIGHT1, time);
  46.       ++__entity_list;
  47.       glEnable(GL_LIGHT1);
  48.     case 1:
  49.       (*__entity_list)->GL(GL_LIGHT0, time);
  50.       ++__entity_list;
  51.       glEnable(GL_LIGHT0);
  52.       break;
  53.   }
  54. }
  55.  
  56. LightGL::LightGL () {
  57.   Reset();
  58. }
  59.  
  60. void LightGL::Reset ()
  61. {
  62.   ambient[0] = ambient[1] = ambient[2] = 0.0F; ambient[3] = 1.0F;
  63.   diffuse[0] = diffuse[1] = diffuse[2] = 0.0F; diffuse[3] = 1.0F;
  64.   specular[0] = specular[1] = specular[2] = 0.0F; specular[3] = 1.0F;
  65.   position[0] = position[1] = position[2] = 0.0F; position[4] = 1.0F;
  66.   spot_direction[0] = spot_direction[1] = 0.0; spot_direction[2] = -1.0F;
  67.  
  68.   spot_exponent = 0.0F;
  69.   spot_cutoff = 180.0F;
  70.   constant_attenuation = 1.0F;
  71.   linear_attenuation = 0.0F;
  72.   quadratic_attenuation = 0.0F;
  73. }
  74.  
  75. void LightGL::GL (GLenum light, double time = 0.0)
  76. {
  77.   glLightfv(light, GL_AMBIENT, ambient);
  78.   glLightfv(light, GL_DIFFUSE, diffuse);
  79.   glLightfv(light, GL_SPECULAR, specular);
  80.   glLightfv(light, GL_POSITION, position);
  81.   glLightfv(light, GL_SPOT_DIRECTION, spot_direction);
  82.  
  83.   glLightf(light, GL_SPOT_EXPONENT, spot_exponent);
  84.   glLightf(light, GL_SPOT_CUTOFF, spot_cutoff);
  85.   glLightf(light, GL_CONSTANT_ATTENUATION, constant_attenuation);
  86.   glLightf(light, GL_LINEAR_ATTENUATION, linear_attenuation);
  87.   glLightf(light, GL_QUADRATIC_ATTENUATION, quadratic_attenuation);
  88. }
  89.  
  90. void LightGL::Ambient (GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0F) {
  91.   SetVector4(ambient, r, g, b, a);
  92. }
  93. void LightGL::Diffuse (GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0F) {
  94.   SetVector4(diffuse, r, g, b, a);
  95. }
  96. void LightGL::Specular (GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0F) {
  97.   SetVector4(specular, r, g, b, a);
  98. }
  99. void LightGL::Ambient (GLfloat* v) {
  100.   SetVector4(ambient, *v, *(v+1), *(v+2), *(v+3));
  101. }
  102. void LightGL::Diffuse (GLfloat* v) {
  103.   SetVector4(diffuse, *v, *(v+1), *(v+2), *(v+3));
  104. }
  105. void LightGL::Specular (GLfloat* v) {
  106.   SetVector4(specular, *v, *(v+1), *(v+2), *(v+3));
  107. }
  108.  
  109. void LightGL::Position (GLfloat x, GLfloat y, GLfloat z, GLfloat w = 1.0F) {
  110.   SetVector4(position, x, y, z, w);
  111. }
  112. void LightGL::Position (GLfloat* v) {
  113.   SetVector4(position, *v, *(v+1), *(v+2), *(v+3));
  114. }
  115.  
  116. void LightGL::SpotDirection (GLfloat i, GLfloat j, GLfloat k) {
  117.   SetVector3(spot_direction, i, j, k);
  118. }
  119. void LightGL::SpotDirection (GLfloat* v) {
  120.   SetVector3(spot_direction, *v, *(v+1), *(v+2));
  121. }
  122.  
  123. void LightGL::SpotExponent (GLfloat f) {
  124.   spot_exponent = f;
  125. }
  126. void LightGL::SpotCutoff (GLfloat f) {
  127.   spot_cutoff = f;
  128. }
  129. void LightGL::ConstantAttenuation (GLfloat f) {
  130.   constant_attenuation = f;
  131. }
  132. void LightGL::LinearAttenuation (GLfloat f) {
  133.   linear_attenuation = f;
  134. }
  135. void LightGL::QuadraticAttenuation (GLfloat f) {
  136.   quadratic_attenuation = f;
  137. }
  138.  
  139. const GLfloat* LightGL::Ambient () {
  140.   return ambient;
  141. }
  142. const GLfloat* LightGL::Diffuse () {
  143.   return diffuse;
  144. }
  145. const GLfloat* LightGL::Specular () {
  146.   return specular;
  147. }
  148. const GLfloat* LightGL::Position () {
  149.   return position;
  150. }
  151. const GLfloat* LightGL::SpotDirection () {
  152.   return spot_direction;
  153. }
  154.  
  155. GLfloat LightGL::SpotExponent () {
  156.   return spot_exponent;
  157. }
  158. GLfloat LightGL::SpotCutoff () { 
  159.   return spot_cutoff;
  160. }
  161. GLfloat LightGL::ConstantAttenuation () {
  162.   return constant_attenuation;
  163. }
  164. GLfloat LightGL::LinearAttenuation () {
  165.   return linear_attenuation;
  166. }
  167. GLfloat LightGL::QuadraticAttenuation () {
  168.   return quadratic_attenuation;
  169. }
  170.