1 //------------------------------------------------------------------------------
2 // File : SkyMaterial.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
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
19 * @file SkyMaterial.cpp
21 * Implementation of class SkyMaterial, a meterial property object.
23 #include "SkyMaterial.hpp"
24 #include "SkyContext.hpp"
26 //------------------------------------------------------------------------------
27 // Function : SkyMaterial::SkyMaterial
29 //------------------------------------------------------------------------------
31 * @fn SkyMaterial::SkyMaterial()
34 SkyMaterial::SkyMaterial()
36 _vecDiffuse(Vec4f::ZERO),
37 _vecSpecular(Vec4f::ZERO),
38 _vecAmbient(Vec4f::ZERO),
39 _vecEmissive(Vec4f::ZERO),
42 _eColorMaterialFace(GL_FRONT_AND_BACK),
43 _eColorMaterialMode(GL_AMBIENT_AND_DIFFUSE),
44 _bColorMaterial(false),
45 _vecFogColor(Vec4f::ZERO),
51 _eAlphaFunc(GL_ALWAYS),
54 _eBlendSrcFactor(GL_ONE),
55 _eBlendDstFactor(GL_ZERO),
58 _eFaceCullingMode(GL_BACK),
59 _eTextureEnvMode(GL_MODULATE)
61 _rFogParams[SKY_FOG_DENSITY] = 1;
62 _rFogParams[SKY_FOG_START] = 0;
63 _rFogParams[SKY_FOG_END] = 1;
67 //------------------------------------------------------------------------------
68 // Function : SkyMaterial::~SkyMaterial
70 //------------------------------------------------------------------------------
72 * @fn SkyMaterial::~SkyMaterial()
75 SkyMaterial::~SkyMaterial()
80 //------------------------------------------------------------------------------
81 // Function : SkyMaterial::SetFogParameter
83 //------------------------------------------------------------------------------
85 * @fn SkyMaterial::SetFogParameter(GLenum eParameter, float rValue)
87 SKYRESULT SkyMaterial::SetFogParameter(GLenum eParameter, float rValue)
92 _rFogParams[SKY_FOG_DENSITY] = rValue;
95 _rFogParams[SKY_FOG_START] = rValue;
98 _rFogParams[SKY_FOG_END] = rValue;
101 FAIL_RETURN_MSG(SKYRESULT_FAIL, "SkyMaterial::SetFogParameter(): Invalid parameter.");
109 //------------------------------------------------------------------------------
110 // Function : SkyMaterial::GetFogParameter
112 //------------------------------------------------------------------------------
114 * @fn SkyMaterial::GetFogParameter(GLenum eParameter) const
116 float SkyMaterial::GetFogParameter(GLenum eParameter) const
121 return _rFogParams[SKY_FOG_DENSITY];
124 return _rFogParams[SKY_FOG_START];
127 return _rFogParams[SKY_FOG_END];
130 FAIL_RETURN_MSG(SKYRESULT_FAIL, "SkyMaterial::GetFogParameter(): Invalid parameter.");
138 //------------------------------------------------------------------------------
139 // Function : SkyMaterial::Activate
141 //------------------------------------------------------------------------------
143 * @fn SkyMaterial::Activate()
144 * @brief @todo <WRITE BRIEF SkyMaterial::SetMaterialPropertiesForDisplay DOCUMENTATION>
146 * @todo <WRITE EXTENDED SkyMaterial::SetMaterialPropertiesForDisplay FUNCTION DOCUMENTATION>
148 SKYRESULT SkyMaterial::Activate()
150 // Update the cached current material, and only pass values that have changed to the GL.
152 SkyMaterial *pCurrentMaterial = GraphicsContext::InstancePtr()->GetCurrentMaterial();
153 assert(NULL != pCurrentMaterial);
155 // basic material properties
156 if (pCurrentMaterial->GetDiffuse() != GetDiffuse())
158 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &(GetDiffuse().x));
159 pCurrentMaterial->SetDiffuse(GetDiffuse());
161 if (pCurrentMaterial->GetSpecular() != GetSpecular())
163 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &(GetSpecular().x));
164 pCurrentMaterial->SetSpecular(GetSpecular());
166 if (pCurrentMaterial->GetAmbient() != GetAmbient())
168 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &(GetAmbient().x));
169 pCurrentMaterial->SetAmbient(GetAmbient());
171 if (pCurrentMaterial->GetEmissive() != GetEmissive())
173 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &(GetEmissive().x));
174 pCurrentMaterial->SetEmissive(GetEmissive());
176 if (pCurrentMaterial->GetSpecularPower() != GetSpecularPower())
178 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, GetSpecularPower());
179 pCurrentMaterial->SetSpecularPower(GetSpecularPower());
183 if (pCurrentMaterial->IsLightingEnabled() != IsLightingEnabled())
185 if (IsLightingEnabled())
186 glEnable(GL_LIGHTING);
188 glDisable(GL_LIGHTING);
189 pCurrentMaterial->EnableLighting(IsLightingEnabled());
192 // color material (which material property tracks color calls)
193 if (pCurrentMaterial->GetColorMaterialFace() != GetColorMaterialFace() ||
194 pCurrentMaterial->GetColorMaterialMode() != GetColorMaterialMode())
196 glColorMaterial(GetColorMaterialFace(), GetColorMaterialMode());
197 pCurrentMaterial->SetColorMaterialFace(GetColorMaterialFace());
198 pCurrentMaterial->SetColorMaterialMode(GetColorMaterialMode());
200 if (pCurrentMaterial->IsColorMaterialEnabled() != IsColorMaterialEnabled())
202 if (IsColorMaterialEnabled())
203 glEnable(GL_COLOR_MATERIAL);
205 glDisable(GL_COLOR_MATERIAL);
206 pCurrentMaterial->EnableColorMaterial(IsColorMaterialEnabled());
210 if (pCurrentMaterial->GetFogMode() != GetFogMode())
212 glFogf(GL_FOG_MODE, GetFogMode());
213 pCurrentMaterial->SetFogMode(GetFogMode());
215 if (pCurrentMaterial->GetFogColor() != GetFogColor())
217 glFogfv(GL_FOG_COLOR, GetFogColor());
218 pCurrentMaterial->SetFogColor(GetFogColor());
220 if (pCurrentMaterial->GetFogParameter(GL_FOG_DENSITY) != GetFogParameter(GL_FOG_DENSITY))
222 glFogf(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY));
223 pCurrentMaterial->SetFogParameter(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY));
225 if (pCurrentMaterial->GetFogParameter(GL_FOG_START) != GetFogParameter(GL_FOG_START))
227 glFogf(GL_FOG_START, GetFogParameter(GL_FOG_START));
228 pCurrentMaterial->SetFogParameter(GL_FOG_START, GetFogParameter(GL_FOG_START));
230 if (pCurrentMaterial->GetFogParameter(GL_FOG_END) != GetFogParameter(GL_FOG_END))
232 glFogf(GL_FOG_END, GetFogParameter(GL_FOG_END));
233 pCurrentMaterial->SetFogParameter(GL_FOG_END, GetFogParameter(GL_FOG_END));
235 if (pCurrentMaterial->IsFogEnabled() != IsFogEnabled())
241 pCurrentMaterial->EnableFog(IsFogEnabled());
245 if (pCurrentMaterial->GetDepthFunc() != GetDepthFunc())
247 glDepthFunc(GetDepthFunc());
248 pCurrentMaterial->SetDepthFunc(GetDepthFunc());
250 if (pCurrentMaterial->GetDepthMask() != GetDepthMask())
252 glDepthMask(GetDepthMask());
253 pCurrentMaterial->SetDepthMask(GetDepthMask());
255 if (pCurrentMaterial->IsDepthTestEnabled() != IsDepthTestEnabled())
257 if (IsDepthTestEnabled())
258 glEnable(GL_DEPTH_TEST);
260 glDisable(GL_DEPTH_TEST);
261 pCurrentMaterial->EnableDepthTest(IsDepthTestEnabled());
265 if (pCurrentMaterial->GetAlphaFunc() != GetAlphaFunc() ||
266 pCurrentMaterial->GetAlphaRef() != GetAlphaRef())
268 glAlphaFunc(GetAlphaFunc(), GetAlphaRef());
269 pCurrentMaterial->SetAlphaFunc(GetAlphaFunc());
270 pCurrentMaterial->SetAlphaRef(GetAlphaRef());
272 if (pCurrentMaterial->IsAlphaTestEnabled() != IsAlphaTestEnabled())
274 if (IsAlphaTestEnabled())
275 glEnable(GL_ALPHA_TEST);
277 glDisable(GL_ALPHA_TEST);
278 pCurrentMaterial->EnableAlphaTest(IsAlphaTestEnabled());
282 if (pCurrentMaterial->GetBlendingSourceFactor() != GetBlendingSourceFactor() ||
283 pCurrentMaterial->GetBlendingDestFactor() != GetBlendingDestFactor())
285 glBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor());
286 pCurrentMaterial->SetBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor());
288 if (pCurrentMaterial->IsBlendingEnabled() != IsBlendingEnabled())
290 if (IsBlendingEnabled())
294 pCurrentMaterial->EnableBlending(IsBlendingEnabled());
297 if (pCurrentMaterial->GetFaceCullingMode() != GetFaceCullingMode())
299 glCullFace(GetFaceCullingMode());
300 pCurrentMaterial->SetFaceCullingMode(GetFaceCullingMode());
302 if (pCurrentMaterial->IsFaceCullingEnabled() != IsFaceCullingEnabled())
304 if (IsFaceCullingEnabled())
305 glEnable(GL_CULL_FACE);
307 glDisable(GL_CULL_FACE);
308 pCurrentMaterial->EnableFaceCulling(IsFaceCullingEnabled());
312 FAIL_RETURN(_textureState.Activate());
313 if (pCurrentMaterial->GetTextureApplicationMode() != GetTextureApplicationMode())
315 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GetTextureApplicationMode());
316 pCurrentMaterial->SetTextureApplicationMode(GetTextureApplicationMode());
323 //------------------------------------------------------------------------------
324 // Function : SkyMaterial::Force
326 //------------------------------------------------------------------------------
328 * @fn SkyMaterial::Force()
329 * @brief @todo <WRITE BRIEF SkyMaterial::SetMaterialPropertiesForDisplay DOCUMENTATION>
331 * @todo <WRITE EXTENDED SkyMaterial::SetMaterialPropertiesForDisplay FUNCTION DOCUMENTATION>
333 SKYRESULT SkyMaterial::Force()
335 // Update the cached current material, and only pass values that have changed to the GL.
337 SkyMaterial *pCurrentMaterial = GraphicsContext::InstancePtr()->GetCurrentMaterial();
338 assert(NULL != pCurrentMaterial);
340 // basic material properties
341 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &(GetDiffuse().x));
342 pCurrentMaterial->SetDiffuse(GetDiffuse());
344 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &(GetSpecular().x));
345 pCurrentMaterial->SetSpecular(GetSpecular());
347 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &(GetAmbient().x));
348 pCurrentMaterial->SetAmbient(GetAmbient());
350 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &(GetEmissive().x));
351 pCurrentMaterial->SetEmissive(GetEmissive());
353 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, GetSpecularPower());
354 pCurrentMaterial->SetSpecularPower(GetSpecularPower());
357 if (IsLightingEnabled())
358 glEnable(GL_LIGHTING);
360 glDisable(GL_LIGHTING);
361 pCurrentMaterial->EnableLighting(IsLightingEnabled());
363 // color material (which material property tracks color calls)
364 glColorMaterial(GetColorMaterialFace(), GetColorMaterialMode());
365 pCurrentMaterial->SetColorMaterialFace(GetColorMaterialFace());
366 pCurrentMaterial->SetColorMaterialMode(GetColorMaterialMode());
368 if (IsColorMaterialEnabled())
369 glEnable(GL_COLOR_MATERIAL);
371 glDisable(GL_COLOR_MATERIAL);
372 pCurrentMaterial->EnableColorMaterial(IsColorMaterialEnabled());
375 glFogf(GL_FOG_MODE, GetFogMode());
376 pCurrentMaterial->SetFogMode(GetFogMode());
378 glFogfv(GL_FOG_COLOR, GetFogColor());
379 pCurrentMaterial->SetFogColor(GetFogColor());
381 glFogf(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY));
382 pCurrentMaterial->SetFogParameter(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY));
384 glFogf(GL_FOG_START, GetFogParameter(GL_FOG_START));
385 pCurrentMaterial->SetFogParameter(GL_FOG_START, GetFogParameter(GL_FOG_START));
386 glFogf(GL_FOG_END, GetFogParameter(GL_FOG_END));
387 pCurrentMaterial->SetFogParameter(GL_FOG_END, GetFogParameter(GL_FOG_END));
393 pCurrentMaterial->EnableFog(IsFogEnabled());
396 glDepthFunc(GetDepthFunc());
397 pCurrentMaterial->SetDepthFunc(GetDepthFunc());
399 glDepthMask(GetDepthMask());
400 pCurrentMaterial->SetDepthMask(GetDepthMask());
402 if (IsDepthTestEnabled())
403 glEnable(GL_DEPTH_TEST);
405 glDisable(GL_DEPTH_TEST);
406 pCurrentMaterial->EnableDepthTest(IsDepthTestEnabled());
409 glAlphaFunc(GetAlphaFunc(), GetAlphaRef());
410 pCurrentMaterial->SetAlphaFunc(GetAlphaFunc());
411 pCurrentMaterial->SetAlphaRef(GetAlphaRef());
413 if (IsAlphaTestEnabled())
414 glEnable(GL_ALPHA_TEST);
416 glDisable(GL_ALPHA_TEST);
419 glBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor());
420 pCurrentMaterial->SetBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor());
422 if (IsBlendingEnabled())
426 pCurrentMaterial->EnableBlending(IsBlendingEnabled());
428 glCullFace(GetFaceCullingMode());
429 pCurrentMaterial->SetFaceCullingMode(GetFaceCullingMode());
431 if (IsFaceCullingEnabled())
432 glEnable(GL_CULL_FACE);
434 glDisable(GL_CULL_FACE);
435 pCurrentMaterial->EnableFaceCulling(IsFaceCullingEnabled());
438 FAIL_RETURN(_textureState.Force());
439 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GetTextureApplicationMode());
440 pCurrentMaterial->SetTextureApplicationMode(GetTextureApplicationMode());