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>
33 #include "glut_shapes.h"
37 # pragma warning( disable : 4786)
42 #include "SkyLight.hpp"
43 #include "SkyMaterial.hpp"
46 SkyMaterial* SkyLight::s_pMaterial = NULL;
48 //------------------------------------------------------------------------------
49 // Function : SkyLight::SkyLight
51 //------------------------------------------------------------------------------
53 * @fn SkyLight::SkyLight(SkyLightType eType)
54 * @brief @todo <WRITE BRIEF SkyLight::SkyLight DOCUMENTATION>
56 * @todo <WRITE EXTENDED SkyLight::SkyLight FUNCTION DOCUMENTATION>
58 SkyLight::SkyLight(SkyLightType eType)
63 _vecPosition(0, 0, 1, 1),
64 _vecDirection(0, 0, -1, 0),
65 _vecDiffuse(1, 1, 1, 1),
66 _vecAmbient(0, 0, 0, 0),
67 _vecSpecular(1, 1, 1, 1),
68 _vecAttenuation(1, 0, 0),
74 s_pMaterial = new SkyMaterial;
75 s_pMaterial->SetColorMaterialMode(GL_DIFFUSE);
76 s_pMaterial->EnableColorMaterial(true);
77 s_pMaterial->EnableLighting(false);
82 //------------------------------------------------------------------------------
83 // Function : SkyLight::~SkyLight
85 //------------------------------------------------------------------------------
87 * @fn SkyLight::~SkyLight()
88 * @brief @todo <WRITE BRIEF SkyLight::~SkyLight DOCUMENTATION>
90 * @todo <WRITE EXTENDED SkyLight::~SkyLight FUNCTION DOCUMENTATION>
96 //------------------------------------------------------------------------------
97 // Function : SkyLight::Display
99 //------------------------------------------------------------------------------
101 * @fn SkyLight::Display() const
102 * @brief Displays a wireframe representation of the light.
104 * This is useful for debugging.
106 void SkyLight::Display() const
108 s_pMaterial->Activate();
110 //glColor3fv(&(_vecDiffuse.x));
116 case SKY_LIGHT_POINT:
117 glMatrixMode(GL_MODELVIEW);
120 glTranslatef(_vecPosition.x, _vecPosition.y, _vecPosition.z);
121 glutWireSphere(4, 8, 8);
125 case SKY_LIGHT_DIRECTIONAL:
127 glMatrixMode(GL_MODELVIEW);
131 Vec3f vecPos(_vecPosition.x, _vecPosition.y, _vecPosition.z);
132 Vec3f vecDir(_vecDirection.x, _vecDirection.y, _vecDirection.z);
133 Vec3f vecUp(0, 1, 0);
134 if (fabs(vecDir * vecUp) - 1 < 1e-6) // check that the view and up directions are not parallel.
137 mat.invLookAt(vecPos, vecPos + 10 * vecDir, vecUp);
141 glTranslatef(-50 * vecDir.x, -50 * vecDir.y, -50 * vecDir.z);
143 glutWireCone(10, 10, 4, 1);
148 GLUquadric *pQuadric = gluNewQuadric();
149 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
150 gluCylinder(pQuadric, 4, 4, 50, 4, 4);
151 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
158 glMatrixMode(GL_MODELVIEW);
162 Vec3f vecUp = Vec3f(0, 1, 0);
163 Vec3f vecPos(_vecPosition.x, _vecPosition.y, _vecPosition.z);
164 Vec3f vecDir(_vecDirection.x, _vecDirection.y, _vecDirection.z);
165 if (_vecDirection == vecUp)
167 mat.invLookAt(vecPos + 50 * vecDir, vecPos + 51 * vecDir, vecUp);
170 float rAlpha= acos(pow(10, (-12 / _rSpotExponent)));
171 //glutWireCone(50 * tan(SKYDEGREESTORADS * rAlpha), 50, 16, 8);
172 glutWireCone(50 * tan(SKYDEGREESTORADS * _rSpotCutoff), 50, 16, 8);
182 //------------------------------------------------------------------------------
183 // Function : SkyLight::Activate
185 //------------------------------------------------------------------------------
187 * @fn SkyLight::Activate(int iLightID)
188 * @brief @todo <WRITE BRIEF SkyLight::Activate DOCUMENTATION>
190 * @todo <WRITE EXTENDED SkyLight::Activate FUNCTION DOCUMENTATION>
192 SKYRESULT SkyLight::Activate(int iLightID)
195 // set the position every frame
196 if (SKY_LIGHT_DIRECTIONAL != _eType)
197 glLightfv(GL_LIGHT0 + iLightID, GL_POSITION, &(_vecPosition.x));
199 glLightfv(GL_LIGHT0 + iLightID, GL_POSITION, &(_vecDirection.x));
201 if (SKY_LIGHT_SPOT == _eType)
202 glLightfv(GL_LIGHT0 + iLightID, GL_SPOT_DIRECTION, &(_vecDirection.x));
204 // set other light properties only when they change.
205 if (_bDirty || iLightID != _iLastGLID)
207 glLightfv(GL_LIGHT0 + iLightID, GL_DIFFUSE, &(_vecDiffuse.x));
208 glLightfv(GL_LIGHT0 + iLightID, GL_AMBIENT, &(_vecAmbient.x));
209 glLightfv(GL_LIGHT0 + iLightID, GL_SPECULAR, &(_vecSpecular.x));
210 glLightf(GL_LIGHT0 + iLightID, GL_CONSTANT_ATTENUATION, _vecAttenuation.x);
211 glLightf(GL_LIGHT0 + iLightID, GL_LINEAR_ATTENUATION, _vecAttenuation.y);
212 glLightf(GL_LIGHT0 + iLightID, GL_QUADRATIC_ATTENUATION, _vecAttenuation.z);
214 if (SKY_LIGHT_SPOT == _eType)
216 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_CUTOFF, _rSpotCutoff);
217 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_EXPONENT, _rSpotExponent);
221 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_CUTOFF, 180);
222 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_EXPONENT, 0);
226 glEnable(GL_LIGHT0 + iLightID);
229 glDisable(GL_LIGHT0 + iLightID);
232 _iLastGLID = iLightID;