]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkyLight.cpp
Tweak lib name.
[simgear.git] / simgear / scene / sky / clouds3d / SkyLight.cpp
1 //------------------------------------------------------------------------------
2 // File : SkyLight.cpp
3 //------------------------------------------------------------------------------
4 // SkyWorks : Copyright 2002 Mark J. Harris and
5 //                                              The University of North Carolina at Chapel Hill
6 //------------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software and its 
8 // documentation for any purpose is hereby granted without fee, provided that 
9 // the above copyright notice appear in all copies and that both that copyright 
10 // notice and this permission notice appear in supporting documentation. 
11 // Binaries may be compiled with this software without any royalties or 
12 // restrictions. 
13 //
14 // The author(s) and The University of North Carolina at Chapel Hill make no 
15 // representations about the suitability of this software for any purpose. 
16 // It is provided "as is" without express or implied warranty.
17 /**
18  * @file SkyLight.cpp
19  * 
20  * Implementation of a class that maintains the state and operation of a light.
21  */
22 #pragma warning( disable : 4786)
23
24 #include "SkyLight.hpp"
25 #include "SkyMaterial.hpp"
26 #include "mat44.hpp"
27 #include <GL/glut.h>
28
29 SkyMaterial* SkyLight::s_pMaterial = NULL;
30
31 //------------------------------------------------------------------------------
32 // Function               : SkyLight::SkyLight
33 // Description      : 
34 //------------------------------------------------------------------------------
35 /**
36 * @fn SkyLight::SkyLight(SkyLightType eType)
37 * @brief @todo <WRITE BRIEF SkyLight::SkyLight DOCUMENTATION>
38
39 * @todo <WRITE EXTENDED SkyLight::SkyLight FUNCTION DOCUMENTATION>
40 */ 
41 SkyLight::SkyLight(SkyLightType eType)
42 : _bEnabled(true),
43 _bDirty(true),
44 _iLastGLID(-1),
45 _eType(eType),
46 _vecPosition(0, 0, 1, 1),
47 _vecDirection(0, 0, -1, 0),
48 _vecDiffuse(1, 1, 1, 1),
49 _vecAmbient(0, 0, 0, 0),
50 _vecSpecular(1, 1, 1, 1),
51 _vecAttenuation(1, 0, 0),
52 _rSpotExponent(0),
53 _rSpotCutoff(180)  
54 {
55   if (!s_pMaterial)
56   {
57     s_pMaterial = new SkyMaterial;
58     s_pMaterial->SetColorMaterialMode(GL_DIFFUSE);
59     s_pMaterial->EnableColorMaterial(true);
60     s_pMaterial->EnableLighting(false);
61   }
62 }
63
64
65 //------------------------------------------------------------------------------
66 // Function               : SkyLight::~SkyLight
67 // Description      : 
68 //------------------------------------------------------------------------------
69 /**
70 * @fn SkyLight::~SkyLight()
71 * @brief @todo <WRITE BRIEF SkyLight::~SkyLight DOCUMENTATION>
72
73 * @todo <WRITE EXTENDED SkyLight::~SkyLight FUNCTION DOCUMENTATION>
74 */ 
75 SkyLight::~SkyLight()
76 {
77 }
78
79 //------------------------------------------------------------------------------
80 // Function               : SkyLight::Display
81 // Description      : 
82 //------------------------------------------------------------------------------
83 /**
84 * @fn SkyLight::Display() const
85 * @brief Displays a wireframe representation of the light.
86
87 * This is useful for debugging.
88 */ 
89 void SkyLight::Display() const
90 {
91   s_pMaterial->Activate();
92   //if (_bEnabled)
93     //glColor3fv(&(_vecDiffuse.x));
94   //else
95     glColor3f(0, 0, 0);
96
97   switch(_eType)
98   {
99   case SKY_LIGHT_POINT:
100     glMatrixMode(GL_MODELVIEW);
101     glPushMatrix();
102     {
103       glTranslatef(_vecPosition.x, _vecPosition.y, _vecPosition.z);
104       glutWireSphere(4, 8, 8);
105     }
106     glPopMatrix();
107     break;
108   case SKY_LIGHT_DIRECTIONAL:
109     {
110       glMatrixMode(GL_MODELVIEW);
111       glPushMatrix();
112       {
113         Mat44f mat;
114         Vec3f vecPos(_vecPosition.x, _vecPosition.y, _vecPosition.z);
115         Vec3f vecDir(_vecDirection.x, _vecDirection.y, _vecDirection.z);
116         Vec3f vecUp(0, 1, 0);
117         if (fabs(vecDir * vecUp) - 1 < 1e-6) // check that the view and up directions are not parallel.
118           vecUp.Set(1, 0, 0);
119         
120         mat.invLookAt(vecPos, vecPos + 10 * vecDir, vecUp);
121         
122         glPushMatrix();
123         {
124           glTranslatef(-50 * vecDir.x, -50 * vecDir.y, -50 * vecDir.z);
125           glMultMatrixf(mat);
126           glutWireCone(10, 10, 4, 1);
127         }
128         glPopMatrix(); 
129         
130         glMultMatrixf(mat); 
131         GLUquadric *pQuadric = gluNewQuadric();
132         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
133         gluCylinder(pQuadric, 4, 4, 50, 4, 4);
134         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
135       }
136       glPopMatrix();
137     }
138     break;
139   case SKY_LIGHT_SPOT:
140     {
141       glMatrixMode(GL_MODELVIEW);
142       glPushMatrix();
143       {
144         Mat44f mat;
145         Vec3f vecUp = Vec3f(0, 1, 0);
146         Vec3f vecPos(_vecPosition.x, _vecPosition.y, _vecPosition.z);
147         Vec3f vecDir(_vecDirection.x, _vecDirection.y, _vecDirection.z);
148         if (_vecDirection == vecUp)
149           vecUp.Set(1, 0, 0);
150         mat.invLookAt(vecPos + 50 * vecDir, vecPos + 51 * vecDir, vecUp);
151
152         glMultMatrixf(mat);   
153         float rAlpha= acos(pow(10, (-12 / _rSpotExponent)));
154         //glutWireCone(50 * tan(SKYDEGREESTORADS * rAlpha), 50, 16, 8);
155         glutWireCone(50 * tan(SKYDEGREESTORADS * _rSpotCutoff), 50, 16, 8);
156       }
157       glPopMatrix();
158     }
159     break;
160   default:
161     break;
162   }  
163 }
164
165 //------------------------------------------------------------------------------
166 // Function               : SkyLight::Activate
167 // Description      : 
168 //------------------------------------------------------------------------------
169 /**
170  * @fn SkyLight::Activate(int iLightID)
171  * @brief @todo <WRITE BRIEF SkyLight::Activate DOCUMENTATION>
172  * 
173  * @todo <WRITE EXTENDED SkyLight::Activate FUNCTION DOCUMENTATION>
174  */ 
175 SKYRESULT SkyLight::Activate(int iLightID)
176 {
177   glPushMatrix();
178   // set the position every frame
179   if (SKY_LIGHT_DIRECTIONAL != _eType)
180     glLightfv(GL_LIGHT0 + iLightID, GL_POSITION,              &(_vecPosition.x));
181   else
182     glLightfv(GL_LIGHT0 + iLightID, GL_POSITION,              &(_vecDirection.x));
183   
184   if (SKY_LIGHT_SPOT == _eType)
185     glLightfv(GL_LIGHT0 + iLightID, GL_SPOT_DIRECTION,      &(_vecDirection.x));
186
187   // set other light properties only when they change.
188   if (_bDirty || iLightID != _iLastGLID)
189   {     
190     glLightfv(GL_LIGHT0 + iLightID,   GL_DIFFUSE,               &(_vecDiffuse.x));
191     glLightfv(GL_LIGHT0 + iLightID,   GL_AMBIENT,               &(_vecAmbient.x));
192     glLightfv(GL_LIGHT0 + iLightID,   GL_SPECULAR,              &(_vecSpecular.x));
193     glLightf(GL_LIGHT0 + iLightID,    GL_CONSTANT_ATTENUATION,  _vecAttenuation.x);
194     glLightf(GL_LIGHT0 + iLightID,    GL_LINEAR_ATTENUATION,    _vecAttenuation.y);
195     glLightf(GL_LIGHT0 + iLightID,    GL_QUADRATIC_ATTENUATION, _vecAttenuation.z);
196     
197     if (SKY_LIGHT_SPOT == _eType)
198     {
199       glLightf(GL_LIGHT0 + iLightID,  GL_SPOT_CUTOFF,         _rSpotCutoff);
200       glLightf(GL_LIGHT0 + iLightID,  GL_SPOT_EXPONENT,       _rSpotExponent);
201     }
202     else
203     {
204       glLightf(GL_LIGHT0 + iLightID,  GL_SPOT_CUTOFF,         180);
205       glLightf(GL_LIGHT0 + iLightID,  GL_SPOT_EXPONENT,       0);
206     }
207  
208     if (_bEnabled)
209       glEnable(GL_LIGHT0 + iLightID);
210     else
211     {
212       glDisable(GL_LIGHT0 + iLightID);
213     }
214     
215     _iLastGLID = iLightID;
216     _bDirty = false;    
217   }
218   glPopMatrix();
219  
220   return SKYRESULT_OK;
221 }