]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkyMaterial.cpp
Clouds3D crashes because there is no Light
[simgear.git] / simgear / scene / sky / clouds3d / SkyMaterial.cpp
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 
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 
17 // implied warranty.
18 /**
19  * @file SkyMaterial.cpp
20  * 
21  * Implementation of class SkyMaterial, a meterial property object.
22  */
23 #include "SkyMaterial.hpp"
24 #include "SkyContext.hpp"
25
26 //------------------------------------------------------------------------------
27 // Function               : SkyMaterial::SkyMaterial
28 // Description      : 
29 //------------------------------------------------------------------------------
30 /**
31  * @fn SkyMaterial::SkyMaterial()
32  * @brief Constructor.
33  */ 
34 SkyMaterial::SkyMaterial()
35 : _iMaterialID(-1),  
36   _vecDiffuse(Vec4f::ZERO),
37   _vecSpecular(Vec4f::ZERO),
38   _vecAmbient(Vec4f::ZERO),
39   _vecEmissive(Vec4f::ZERO),
40   _rSpecularPower(0),
41   _bLighting(true),
42   _eColorMaterialFace(GL_FRONT_AND_BACK),
43   _eColorMaterialMode(GL_AMBIENT_AND_DIFFUSE),
44   _bColorMaterial(false),
45   _vecFogColor(Vec4f::ZERO),
46   _eFogMode(GL_EXP),
47   _bFog(false),
48   _eDepthFunc(GL_LESS),
49   _bDepthMask(true),
50   _bDepthTest(true),
51   _eAlphaFunc(GL_ALWAYS),
52   _rAlphaRef(0),
53   _bAlphaTest(false), 
54   _eBlendSrcFactor(GL_ONE),
55   _eBlendDstFactor(GL_ZERO),
56   _bBlending(false),
57   _bFaceCulling(false),
58   _eFaceCullingMode(GL_BACK),
59   _eTextureEnvMode(GL_MODULATE)
60 {
61   _rFogParams[SKY_FOG_DENSITY]  = 1;
62   _rFogParams[SKY_FOG_START]    = 0;
63   _rFogParams[SKY_FOG_END]      = 1;
64 }
65
66
67 //------------------------------------------------------------------------------
68 // Function               : SkyMaterial::~SkyMaterial
69 // Description      : 
70 //------------------------------------------------------------------------------
71 /**
72  * @fn SkyMaterial::~SkyMaterial()
73  * @brief Destructor.
74  */ 
75 SkyMaterial::~SkyMaterial()
76 {
77 }
78
79
80 //------------------------------------------------------------------------------
81 // Function               : SkyMaterial::SetFogParameter
82 // Description      : 
83 //------------------------------------------------------------------------------
84 /**
85  * @fn SkyMaterial::SetFogParameter(GLenum eParameter, float rValue)
86  */ 
87 SKYRESULT SkyMaterial::SetFogParameter(GLenum eParameter, float rValue)
88 {
89   switch (eParameter)
90   {
91   case GL_FOG_DENSITY:
92     _rFogParams[SKY_FOG_DENSITY] = rValue;
93     break;
94   case GL_FOG_START:
95     _rFogParams[SKY_FOG_START] = rValue;
96     break;
97   case GL_FOG_END:
98     _rFogParams[SKY_FOG_END] = rValue;
99     break;
100   default:
101     FAIL_RETURN_MSG(SKYRESULT_FAIL, "SkyMaterial::SetFogParameter(): Invalid parameter.");
102     break;
103   }
104   
105   return SKYRESULT_OK;
106 }
107
108
109 //------------------------------------------------------------------------------
110 // Function               : SkyMaterial::GetFogParameter
111 // Description      : 
112 //------------------------------------------------------------------------------
113 /**
114  * @fn SkyMaterial::GetFogParameter(GLenum eParameter) const
115  */ 
116 float SkyMaterial::GetFogParameter(GLenum eParameter) const
117 {
118   switch (eParameter)
119   {
120   case GL_FOG_DENSITY:
121     return _rFogParams[SKY_FOG_DENSITY];
122     break;
123   case GL_FOG_START:
124     return _rFogParams[SKY_FOG_START];
125     break;
126   case GL_FOG_END:
127     return _rFogParams[SKY_FOG_END];
128     break;
129   default:
130     FAIL_RETURN_MSG(SKYRESULT_FAIL, "SkyMaterial::GetFogParameter(): Invalid parameter.");
131     break;
132   }
133   
134   return -1;
135 }
136
137
138 //------------------------------------------------------------------------------
139 // Function               : SkyMaterial::Activate
140 // Description      : 
141 //------------------------------------------------------------------------------
142 /**
143  * @fn SkyMaterial::Activate()
144  * @brief @todo <WRITE BRIEF SkyMaterial::SetMaterialPropertiesForDisplay DOCUMENTATION>
145  * 
146  * @todo <WRITE EXTENDED SkyMaterial::SetMaterialPropertiesForDisplay FUNCTION DOCUMENTATION>
147  */ 
148 SKYRESULT SkyMaterial::Activate()
149 {
150   // Update the cached current material, and only pass values that have changed to the GL.
151   
152   SkyMaterial *pCurrentMaterial = GraphicsContext::InstancePtr()->GetCurrentMaterial();
153   assert(NULL != pCurrentMaterial);
154
155   // basic material properties
156   if (pCurrentMaterial->GetDiffuse() != GetDiffuse())
157   {
158     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &(GetDiffuse().x));
159     pCurrentMaterial->SetDiffuse(GetDiffuse());
160   }
161   if (pCurrentMaterial->GetSpecular() != GetSpecular())
162   {
163     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &(GetSpecular().x));
164     pCurrentMaterial->SetSpecular(GetSpecular());
165   }  
166   if (pCurrentMaterial->GetAmbient() != GetAmbient())
167   {
168     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &(GetAmbient().x));
169     pCurrentMaterial->SetAmbient(GetAmbient());
170   }
171   if (pCurrentMaterial->GetEmissive() != GetEmissive())
172   {
173     glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &(GetEmissive().x));
174     pCurrentMaterial->SetEmissive(GetEmissive());
175   }
176   if (pCurrentMaterial->GetSpecularPower() != GetSpecularPower())
177   {
178     glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, GetSpecularPower());
179     pCurrentMaterial->SetSpecularPower(GetSpecularPower());
180   } 
181
182   // lighting
183   if (pCurrentMaterial->IsLightingEnabled() != IsLightingEnabled())
184   {
185     if (IsLightingEnabled())
186       glEnable(GL_LIGHTING);
187     else
188       glDisable(GL_LIGHTING);
189     pCurrentMaterial->EnableLighting(IsLightingEnabled());
190   }
191
192   // color material (which material property tracks color calls)
193   if (pCurrentMaterial->GetColorMaterialFace() != GetColorMaterialFace() ||
194       pCurrentMaterial->GetColorMaterialMode() != GetColorMaterialMode())
195   {
196     glColorMaterial(GetColorMaterialFace(), GetColorMaterialMode());
197     pCurrentMaterial->SetColorMaterialFace(GetColorMaterialFace());
198     pCurrentMaterial->SetColorMaterialMode(GetColorMaterialMode());
199   }
200   if (pCurrentMaterial->IsColorMaterialEnabled() != IsColorMaterialEnabled())
201   {
202     if (IsColorMaterialEnabled()) 
203       glEnable(GL_COLOR_MATERIAL);
204     else
205       glDisable(GL_COLOR_MATERIAL);
206     pCurrentMaterial->EnableColorMaterial(IsColorMaterialEnabled());
207   }
208
209   // fog
210   if (pCurrentMaterial->GetFogMode() != GetFogMode())
211   {
212     glFogf(GL_FOG_MODE, GetFogMode());
213     pCurrentMaterial->SetFogMode(GetFogMode());
214   }
215   if (pCurrentMaterial->GetFogColor() != GetFogColor())
216   {
217     glFogfv(GL_FOG_COLOR, GetFogColor());
218     pCurrentMaterial->SetFogColor(GetFogColor());
219   }
220   if (pCurrentMaterial->GetFogParameter(GL_FOG_DENSITY) != GetFogParameter(GL_FOG_DENSITY))
221   {
222     glFogf(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY));
223     pCurrentMaterial->SetFogParameter(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY));
224   }
225   if (pCurrentMaterial->GetFogParameter(GL_FOG_START) != GetFogParameter(GL_FOG_START))
226   {
227     glFogf(GL_FOG_START, GetFogParameter(GL_FOG_START));
228     pCurrentMaterial->SetFogParameter(GL_FOG_START, GetFogParameter(GL_FOG_START));
229   }
230   if (pCurrentMaterial->GetFogParameter(GL_FOG_END) != GetFogParameter(GL_FOG_END))
231   {
232     glFogf(GL_FOG_END, GetFogParameter(GL_FOG_END));
233     pCurrentMaterial->SetFogParameter(GL_FOG_END, GetFogParameter(GL_FOG_END));
234   }
235   if (pCurrentMaterial->IsFogEnabled() != IsFogEnabled())
236   {
237     if (IsFogEnabled())
238       glEnable(GL_FOG);
239     else
240       glDisable(GL_FOG);
241     pCurrentMaterial->EnableFog(IsFogEnabled());
242   }
243
244   // depth test
245   if (pCurrentMaterial->GetDepthFunc() != GetDepthFunc())
246   {
247     glDepthFunc(GetDepthFunc());
248     pCurrentMaterial->SetDepthFunc(GetDepthFunc());
249   }
250   if (pCurrentMaterial->GetDepthMask() != GetDepthMask())
251   {
252     glDepthMask(GetDepthMask());
253     pCurrentMaterial->SetDepthMask(GetDepthMask());
254   }
255   if (pCurrentMaterial->IsDepthTestEnabled() != IsDepthTestEnabled())
256   {
257     if (IsDepthTestEnabled()) 
258       glEnable(GL_DEPTH_TEST);
259     else 
260       glDisable(GL_DEPTH_TEST);
261     pCurrentMaterial->EnableDepthTest(IsDepthTestEnabled());
262   }
263
264   // alpha test
265   if (pCurrentMaterial->GetAlphaFunc() != GetAlphaFunc() ||
266       pCurrentMaterial->GetAlphaRef()  != GetAlphaRef())
267   {
268     glAlphaFunc(GetAlphaFunc(), GetAlphaRef());
269     pCurrentMaterial->SetAlphaFunc(GetAlphaFunc());
270     pCurrentMaterial->SetAlphaRef(GetAlphaRef());
271   }
272   if (pCurrentMaterial->IsAlphaTestEnabled() != IsAlphaTestEnabled())
273   {
274     if (IsAlphaTestEnabled()) 
275       glEnable(GL_ALPHA_TEST);
276     else
277       glDisable(GL_ALPHA_TEST);
278     pCurrentMaterial->EnableAlphaTest(IsAlphaTestEnabled());
279   }
280
281   // blending
282   if (pCurrentMaterial->GetBlendingSourceFactor() != GetBlendingSourceFactor() ||
283       pCurrentMaterial->GetBlendingDestFactor()   != GetBlendingDestFactor())
284   {
285     glBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor());
286     pCurrentMaterial->SetBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor());
287   }
288   if (pCurrentMaterial->IsBlendingEnabled() != IsBlendingEnabled())
289   {
290     if (IsBlendingEnabled())
291       glEnable(GL_BLEND);
292     else
293       glDisable(GL_BLEND);
294     pCurrentMaterial->EnableBlending(IsBlendingEnabled());
295   }
296
297   if (pCurrentMaterial->GetFaceCullingMode() != GetFaceCullingMode())
298   {
299     glCullFace(GetFaceCullingMode());
300     pCurrentMaterial->SetFaceCullingMode(GetFaceCullingMode());
301   }
302   if (pCurrentMaterial->IsFaceCullingEnabled() != IsFaceCullingEnabled())
303   {
304     if (IsFaceCullingEnabled())
305       glEnable(GL_CULL_FACE);
306     else
307       glDisable(GL_CULL_FACE);
308     pCurrentMaterial->EnableFaceCulling(IsFaceCullingEnabled());
309   }
310
311   // texturing
312   FAIL_RETURN(_textureState.Activate());
313   if (pCurrentMaterial->GetTextureApplicationMode() != GetTextureApplicationMode())
314   {
315     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GetTextureApplicationMode());
316     pCurrentMaterial->SetTextureApplicationMode(GetTextureApplicationMode());
317   }
318
319   return SKYRESULT_OK;
320 }
321
322
323 //------------------------------------------------------------------------------
324 // Function               : SkyMaterial::Force
325 // Description      : 
326 //------------------------------------------------------------------------------
327 /**
328  * @fn SkyMaterial::Force()
329  * @brief @todo <WRITE BRIEF SkyMaterial::SetMaterialPropertiesForDisplay DOCUMENTATION>
330  * 
331  * @todo <WRITE EXTENDED SkyMaterial::SetMaterialPropertiesForDisplay FUNCTION DOCUMENTATION>
332  */ 
333 SKYRESULT SkyMaterial::Force()
334 {
335   // Update the cached current material, and only pass values that have changed to the GL.
336   
337   SkyMaterial *pCurrentMaterial = GraphicsContext::InstancePtr()->GetCurrentMaterial();
338   assert(NULL != pCurrentMaterial);
339
340   // basic material properties
341   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &(GetDiffuse().x));
342   pCurrentMaterial->SetDiffuse(GetDiffuse());
343
344   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &(GetSpecular().x));
345   pCurrentMaterial->SetSpecular(GetSpecular());
346
347   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &(GetAmbient().x));
348   pCurrentMaterial->SetAmbient(GetAmbient());
349
350   glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &(GetEmissive().x));
351   pCurrentMaterial->SetEmissive(GetEmissive());
352
353   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, GetSpecularPower());
354   pCurrentMaterial->SetSpecularPower(GetSpecularPower());
355
356   // lighting
357   if (IsLightingEnabled())
358       glEnable(GL_LIGHTING);
359   else
360       glDisable(GL_LIGHTING);
361   pCurrentMaterial->EnableLighting(IsLightingEnabled());
362
363   // color material (which material property tracks color calls)
364   glColorMaterial(GetColorMaterialFace(), GetColorMaterialMode());
365   pCurrentMaterial->SetColorMaterialFace(GetColorMaterialFace());
366   pCurrentMaterial->SetColorMaterialMode(GetColorMaterialMode());
367
368   if (IsColorMaterialEnabled()) 
369       glEnable(GL_COLOR_MATERIAL);
370   else
371       glDisable(GL_COLOR_MATERIAL);
372   pCurrentMaterial->EnableColorMaterial(IsColorMaterialEnabled());
373
374   // fog
375   glFogf(GL_FOG_MODE, GetFogMode());
376   pCurrentMaterial->SetFogMode(GetFogMode());
377
378   glFogfv(GL_FOG_COLOR, GetFogColor());
379   pCurrentMaterial->SetFogColor(GetFogColor());
380
381   glFogf(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY));
382   pCurrentMaterial->SetFogParameter(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY));
383
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));
388
389   if (IsFogEnabled())
390       glEnable(GL_FOG);
391   else
392       glDisable(GL_FOG);
393   pCurrentMaterial->EnableFog(IsFogEnabled());
394
395   // depth test
396   glDepthFunc(GetDepthFunc());
397   pCurrentMaterial->SetDepthFunc(GetDepthFunc());
398
399   glDepthMask(GetDepthMask());
400   pCurrentMaterial->SetDepthMask(GetDepthMask());
401
402   if (IsDepthTestEnabled()) 
403       glEnable(GL_DEPTH_TEST);
404   else 
405       glDisable(GL_DEPTH_TEST);
406   pCurrentMaterial->EnableDepthTest(IsDepthTestEnabled());
407
408   // alpha test
409   glAlphaFunc(GetAlphaFunc(), GetAlphaRef());
410   pCurrentMaterial->SetAlphaFunc(GetAlphaFunc());
411   pCurrentMaterial->SetAlphaRef(GetAlphaRef());
412
413   if (IsAlphaTestEnabled()) 
414       glEnable(GL_ALPHA_TEST);
415   else
416       glDisable(GL_ALPHA_TEST);
417
418   // blending
419   glBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor());
420   pCurrentMaterial->SetBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor());
421
422   if (IsBlendingEnabled())
423       glEnable(GL_BLEND);
424   else
425       glDisable(GL_BLEND);
426   pCurrentMaterial->EnableBlending(IsBlendingEnabled());
427
428   glCullFace(GetFaceCullingMode());
429   pCurrentMaterial->SetFaceCullingMode(GetFaceCullingMode());
430
431   if (IsFaceCullingEnabled())
432       glEnable(GL_CULL_FACE);
433   else
434       glDisable(GL_CULL_FACE);
435   pCurrentMaterial->EnableFaceCulling(IsFaceCullingEnabled());
436
437   // texturing
438   FAIL_RETURN(_textureState.Force());
439   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GetTextureApplicationMode());
440   pCurrentMaterial->SetTextureApplicationMode(GetTextureApplicationMode());
441
442   return SKYRESULT_OK;
443 }
444
445