1 //------------------------------------------------------------------------------
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
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.
20 * Implementation of a class that maintains the state and operation of a light.
24 # include <simgear_config.h>
35 # pragma warning( disable : 4786)
40 #include "SkyLight.hpp"
41 #include "SkyMaterial.hpp"
44 SkyMaterial* SkyLight::s_pMaterial = NULL;
46 //------------------------------------------------------------------------------
47 // Function : SkyLight::SkyLight
49 //------------------------------------------------------------------------------
51 * @fn SkyLight::SkyLight(SkyLightType eType)
52 * @brief @todo <WRITE BRIEF SkyLight::SkyLight DOCUMENTATION>
54 * @todo <WRITE EXTENDED SkyLight::SkyLight FUNCTION DOCUMENTATION>
56 SkyLight::SkyLight(SkyLightType 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),
72 s_pMaterial = new SkyMaterial;
73 s_pMaterial->SetColorMaterialMode(GL_DIFFUSE);
74 s_pMaterial->EnableColorMaterial(true);
75 s_pMaterial->EnableLighting(false);
80 //------------------------------------------------------------------------------
81 // Function : SkyLight::~SkyLight
83 //------------------------------------------------------------------------------
85 * @fn SkyLight::~SkyLight()
86 * @brief @todo <WRITE BRIEF SkyLight::~SkyLight DOCUMENTATION>
88 * @todo <WRITE EXTENDED SkyLight::~SkyLight FUNCTION DOCUMENTATION>
94 //------------------------------------------------------------------------------
95 // Function : SkyLight::Display
97 //------------------------------------------------------------------------------
99 * @fn SkyLight::Display() const
100 * @brief Displays a wireframe representation of the light.
102 * This is useful for debugging.
104 void SkyLight::Display() const
106 s_pMaterial->Activate();
108 //glColor3fv(&(_vecDiffuse.x));
114 case SKY_LIGHT_POINT:
115 glMatrixMode(GL_MODELVIEW);
118 glTranslatef(_vecPosition.x, _vecPosition.y, _vecPosition.z);
119 glutWireSphere(4, 8, 8);
123 case SKY_LIGHT_DIRECTIONAL:
125 glMatrixMode(GL_MODELVIEW);
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.
135 mat.invLookAt(vecPos, vecPos + 10 * vecDir, vecUp);
139 glTranslatef(-50 * vecDir.x, -50 * vecDir.y, -50 * vecDir.z);
141 glutWireCone(10, 10, 4, 1);
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);
156 glMatrixMode(GL_MODELVIEW);
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)
165 mat.invLookAt(vecPos + 50 * vecDir, vecPos + 51 * vecDir, vecUp);
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);
180 //------------------------------------------------------------------------------
181 // Function : SkyLight::Activate
183 //------------------------------------------------------------------------------
185 * @fn SkyLight::Activate(int iLightID)
186 * @brief @todo <WRITE BRIEF SkyLight::Activate DOCUMENTATION>
188 * @todo <WRITE EXTENDED SkyLight::Activate FUNCTION DOCUMENTATION>
190 SKYRESULT SkyLight::Activate(int iLightID)
193 // set the position every frame
194 if (SKY_LIGHT_DIRECTIONAL != _eType)
195 glLightfv(GL_LIGHT0 + iLightID, GL_POSITION, &(_vecPosition.x));
197 glLightfv(GL_LIGHT0 + iLightID, GL_POSITION, &(_vecDirection.x));
199 if (SKY_LIGHT_SPOT == _eType)
200 glLightfv(GL_LIGHT0 + iLightID, GL_SPOT_DIRECTION, &(_vecDirection.x));
202 // set other light properties only when they change.
203 if (_bDirty || iLightID != _iLastGLID)
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);
212 if (SKY_LIGHT_SPOT == _eType)
214 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_CUTOFF, _rSpotCutoff);
215 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_EXPONENT, _rSpotExponent);
219 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_CUTOFF, 180);
220 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_EXPONENT, 0);
224 glEnable(GL_LIGHT0 + iLightID);
227 glDisable(GL_LIGHT0 + iLightID);
230 _iLastGLID = iLightID;