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.
22 #pragma warning( disable : 4786)
24 #include "SkyLight.hpp"
25 #include "SkyMaterial.hpp"
29 SkyMaterial* SkyLight::s_pMaterial = NULL;
31 //------------------------------------------------------------------------------
32 // Function : SkyLight::SkyLight
34 //------------------------------------------------------------------------------
36 * @fn SkyLight::SkyLight(SkyLightType eType)
37 * @brief @todo <WRITE BRIEF SkyLight::SkyLight DOCUMENTATION>
39 * @todo <WRITE EXTENDED SkyLight::SkyLight FUNCTION DOCUMENTATION>
41 SkyLight::SkyLight(SkyLightType 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),
57 s_pMaterial = new SkyMaterial;
58 s_pMaterial->SetColorMaterialMode(GL_DIFFUSE);
59 s_pMaterial->EnableColorMaterial(true);
60 s_pMaterial->EnableLighting(false);
65 //------------------------------------------------------------------------------
66 // Function : SkyLight::~SkyLight
68 //------------------------------------------------------------------------------
70 * @fn SkyLight::~SkyLight()
71 * @brief @todo <WRITE BRIEF SkyLight::~SkyLight DOCUMENTATION>
73 * @todo <WRITE EXTENDED SkyLight::~SkyLight FUNCTION DOCUMENTATION>
79 //------------------------------------------------------------------------------
80 // Function : SkyLight::Display
82 //------------------------------------------------------------------------------
84 * @fn SkyLight::Display() const
85 * @brief Displays a wireframe representation of the light.
87 * This is useful for debugging.
89 void SkyLight::Display() const
91 s_pMaterial->Activate();
93 //glColor3fv(&(_vecDiffuse.x));
100 glMatrixMode(GL_MODELVIEW);
103 glTranslatef(_vecPosition.x, _vecPosition.y, _vecPosition.z);
104 glutWireSphere(4, 8, 8);
108 case SKY_LIGHT_DIRECTIONAL:
110 glMatrixMode(GL_MODELVIEW);
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.
120 mat.invLookAt(vecPos, vecPos + 10 * vecDir, vecUp);
124 glTranslatef(-50 * vecDir.x, -50 * vecDir.y, -50 * vecDir.z);
126 glutWireCone(10, 10, 4, 1);
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);
141 glMatrixMode(GL_MODELVIEW);
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)
150 mat.invLookAt(vecPos + 50 * vecDir, vecPos + 51 * vecDir, vecUp);
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);
165 //------------------------------------------------------------------------------
166 // Function : SkyLight::Activate
168 //------------------------------------------------------------------------------
170 * @fn SkyLight::Activate(int iLightID)
171 * @brief @todo <WRITE BRIEF SkyLight::Activate DOCUMENTATION>
173 * @todo <WRITE EXTENDED SkyLight::Activate FUNCTION DOCUMENTATION>
175 SKYRESULT SkyLight::Activate(int iLightID)
178 // set the position every frame
179 if (SKY_LIGHT_DIRECTIONAL != _eType)
180 glLightfv(GL_LIGHT0 + iLightID, GL_POSITION, &(_vecPosition.x));
182 glLightfv(GL_LIGHT0 + iLightID, GL_POSITION, &(_vecDirection.x));
184 if (SKY_LIGHT_SPOT == _eType)
185 glLightfv(GL_LIGHT0 + iLightID, GL_SPOT_DIRECTION, &(_vecDirection.x));
187 // set other light properties only when they change.
188 if (_bDirty || iLightID != _iLastGLID)
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);
197 if (SKY_LIGHT_SPOT == _eType)
199 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_CUTOFF, _rSpotCutoff);
200 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_EXPONENT, _rSpotExponent);
204 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_CUTOFF, 180);
205 glLightf(GL_LIGHT0 + iLightID, GL_SPOT_EXPONENT, 0);
209 glEnable(GL_LIGHT0 + iLightID);
212 glDisable(GL_LIGHT0 + iLightID);
215 _iLastGLID = iLightID;