]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkyLight.cpp
A first attempt at making the clouds3d endian aware. Almost there.
[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
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #ifdef HAVE_WINDOWS_H
28 #  include <windows.h>
29 #endif
30
31 #include GLUT_H
32
33 #ifdef WIN32
34 # ifdef _MSC_VER
35 #  pragma warning( disable : 4786)
36 # endif
37 # include "extgl.h"
38 #endif
39
40 #include "SkyLight.hpp"
41 #include "SkyMaterial.hpp"
42 #include "mat44.hpp"
43
44 SkyMaterial* SkyLight::s_pMaterial = NULL;
45
46 //------------------------------------------------------------------------------
47 // Function               : SkyLight::SkyLight
48 // Description      : 
49 //------------------------------------------------------------------------------
50 /**
51 * @fn SkyLight::SkyLight(SkyLightType eType)
52 * @brief @todo <WRITE BRIEF SkyLight::SkyLight DOCUMENTATION>
53
54 * @todo <WRITE EXTENDED SkyLight::SkyLight FUNCTION DOCUMENTATION>
55 */ 
56 SkyLight::SkyLight(SkyLightType eType)
57 : _bEnabled(true),
58 _bDirty(true),
59 _iLastGLID(-1),
60 _eType(eType),
61 _vecPosition(0, 0, 1, 1),
62 _vecDirection(0, 0, -1, 0),
63 _vecDiffuse(1, 1, 1, 1),
64 _vecAmbient(0, 0, 0, 0),
65 _vecSpecular(1, 1, 1, 1),
66 _vecAttenuation(1, 0, 0),
67 _rSpotExponent(0),
68 _rSpotCutoff(180)  
69 {
70   if (!s_pMaterial)
71   {
72     s_pMaterial = new SkyMaterial;
73     s_pMaterial->SetColorMaterialMode(GL_DIFFUSE);
74     s_pMaterial->EnableColorMaterial(true);
75     s_pMaterial->EnableLighting(false);
76   }
77 }
78
79
80 //------------------------------------------------------------------------------
81 // Function               : SkyLight::~SkyLight
82 // Description      : 
83 //------------------------------------------------------------------------------
84 /**
85 * @fn SkyLight::~SkyLight()
86 * @brief @todo <WRITE BRIEF SkyLight::~SkyLight DOCUMENTATION>
87
88 * @todo <WRITE EXTENDED SkyLight::~SkyLight FUNCTION DOCUMENTATION>
89 */ 
90 SkyLight::~SkyLight()
91 {
92 }
93
94 //------------------------------------------------------------------------------
95 // Function               : SkyLight::Display
96 // Description      : 
97 //------------------------------------------------------------------------------
98 /**
99 * @fn SkyLight::Display() const
100 * @brief Displays a wireframe representation of the light.
101
102 * This is useful for debugging.
103 */ 
104 void SkyLight::Display() const
105 {
106   s_pMaterial->Activate();
107   //if (_bEnabled)
108     //glColor3fv(&(_vecDiffuse.x));
109   //else
110     glColor3f(0, 0, 0);
111
112   switch(_eType)
113   {
114   case SKY_LIGHT_POINT:
115     glMatrixMode(GL_MODELVIEW);
116     glPushMatrix();
117     {
118       glTranslatef(_vecPosition.x, _vecPosition.y, _vecPosition.z);
119       glutWireSphere(4, 8, 8);
120     }
121     glPopMatrix();
122     break;
123   case SKY_LIGHT_DIRECTIONAL:
124     {
125       glMatrixMode(GL_MODELVIEW);
126       glPushMatrix();
127       {
128         Mat44f mat;
129         Vec3f vecPos(_vecPosition.x, _vecPosition.y, _vecPosition.z);
130         Vec3f vecDir(_vecDirection.x, _vecDirection.y, _vecDirection.z);
131         Vec3f vecUp(0, 1, 0);
132         if (fabs(vecDir * vecUp) - 1 < 1e-6) // check that the view and up directions are not parallel.
133           vecUp.Set(1, 0, 0);
134         
135         mat.invLookAt(vecPos, vecPos + 10 * vecDir, vecUp);
136         
137         glPushMatrix();
138         {
139           glTranslatef(-50 * vecDir.x, -50 * vecDir.y, -50 * vecDir.z);
140           glMultMatrixf(mat);
141           glutWireCone(10, 10, 4, 1);
142         }
143         glPopMatrix(); 
144         
145         glMultMatrixf(mat); 
146         GLUquadric *pQuadric = gluNewQuadric();
147         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
148         gluCylinder(pQuadric, 4, 4, 50, 4, 4);
149         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
150       }
151       glPopMatrix();
152     }
153     break;
154   case SKY_LIGHT_SPOT:
155     {
156       glMatrixMode(GL_MODELVIEW);
157       glPushMatrix();
158       {
159         Mat44f mat;
160         Vec3f vecUp = Vec3f(0, 1, 0);
161         Vec3f vecPos(_vecPosition.x, _vecPosition.y, _vecPosition.z);
162         Vec3f vecDir(_vecDirection.x, _vecDirection.y, _vecDirection.z);
163         if (_vecDirection == vecUp)
164           vecUp.Set(1, 0, 0);
165         mat.invLookAt(vecPos + 50 * vecDir, vecPos + 51 * vecDir, vecUp);
166
167         glMultMatrixf(mat);   
168         float rAlpha= acos(pow(10, (-12 / _rSpotExponent)));
169         //glutWireCone(50 * tan(SKYDEGREESTORADS * rAlpha), 50, 16, 8);
170         glutWireCone(50 * tan(SKYDEGREESTORADS * _rSpotCutoff), 50, 16, 8);
171       }
172       glPopMatrix();
173     }
174     break;
175   default:
176     break;
177   }  
178 }
179
180 //------------------------------------------------------------------------------
181 // Function               : SkyLight::Activate
182 // Description      : 
183 //------------------------------------------------------------------------------
184 /**
185  * @fn SkyLight::Activate(int iLightID)
186  * @brief @todo <WRITE BRIEF SkyLight::Activate DOCUMENTATION>
187  * 
188  * @todo <WRITE EXTENDED SkyLight::Activate FUNCTION DOCUMENTATION>
189  */ 
190 SKYRESULT SkyLight::Activate(int iLightID)
191 {
192   glPushMatrix();
193   // set the position every frame
194   if (SKY_LIGHT_DIRECTIONAL != _eType)
195     glLightfv(GL_LIGHT0 + iLightID, GL_POSITION,              &(_vecPosition.x));
196   else
197     glLightfv(GL_LIGHT0 + iLightID, GL_POSITION,              &(_vecDirection.x));
198   
199   if (SKY_LIGHT_SPOT == _eType)
200     glLightfv(GL_LIGHT0 + iLightID, GL_SPOT_DIRECTION,      &(_vecDirection.x));
201
202   // set other light properties only when they change.
203   if (_bDirty || iLightID != _iLastGLID)
204   {     
205     glLightfv(GL_LIGHT0 + iLightID,   GL_DIFFUSE,               &(_vecDiffuse.x));
206     glLightfv(GL_LIGHT0 + iLightID,   GL_AMBIENT,               &(_vecAmbient.x));
207     glLightfv(GL_LIGHT0 + iLightID,   GL_SPECULAR,              &(_vecSpecular.x));
208     glLightf(GL_LIGHT0 + iLightID,    GL_CONSTANT_ATTENUATION,  _vecAttenuation.x);
209     glLightf(GL_LIGHT0 + iLightID,    GL_LINEAR_ATTENUATION,    _vecAttenuation.y);
210     glLightf(GL_LIGHT0 + iLightID,    GL_QUADRATIC_ATTENUATION, _vecAttenuation.z);
211     
212     if (SKY_LIGHT_SPOT == _eType)
213     {
214       glLightf(GL_LIGHT0 + iLightID,  GL_SPOT_CUTOFF,         _rSpotCutoff);
215       glLightf(GL_LIGHT0 + iLightID,  GL_SPOT_EXPONENT,       _rSpotExponent);
216     }
217     else
218     {
219       glLightf(GL_LIGHT0 + iLightID,  GL_SPOT_CUTOFF,         180);
220       glLightf(GL_LIGHT0 + iLightID,  GL_SPOT_EXPONENT,       0);
221     }
222  
223     if (_bEnabled)
224       glEnable(GL_LIGHT0 + iLightID);
225     else
226     {
227       glDisable(GL_LIGHT0 + iLightID);
228     }
229     
230     _iLastGLID = iLightID;
231     _bDirty = false;    
232   }
233   glPopMatrix();
234  
235   return SKYRESULT_OK;
236 }