]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkyMaterial.hpp
Clouds3D crashes because there is no Light
[simgear.git] / simgear / scene / sky / clouds3d / SkyMaterial.hpp
1 //------------------------------------------------------------------------------
2 // File : SkyMaterial.hpp
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.hpp
20  * 
21  * Interface definition for class SkyMaterial, a meterial property object.
22  */
23 #ifndef __SKYMATERIAL_HPP__
24 #define __SKYMATERIAL_HPP__
25
26 // #pragma warning( disable : 4786)
27
28 #ifdef HAVE_CONFIG_H
29 #  include <simgear_config.h>
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #endif
35
36 #include <GL/gl.h>
37
38 #include "vec4f.hpp"
39 #include "SkyUtil.hpp"
40 #include "SkyTextureManager.hpp"
41 #include "SkyTextureState.hpp"
42
43 // forward
44 class SkyRenderable;
45
46
47 //------------------------------------------------------------------------------
48 /**
49  * @class SkyMaterial
50  * @brief A class for organizing and caching material state.
51  * 
52  * This class handles setting and querying material state.  By calling the Activate()
53  * method, the material's state can be made current in OpenGL.  The material will not
54  * set states that are currently active in the current OpenGL context.
55  */
56 class SkyMaterial
57 {
58 public:
59   SkyMaterial();
60   ~SkyMaterial();
61   
62   SKYRESULT     Activate();
63   SKYRESULT     Force();
64
65   // Getters for basic material properties
66
67   //! Returns the material identifier.
68   int           GetMaterialID() const                  { return _iMaterialID;     }
69   //! Returns the material diffuse color.
70   const Vec4f&  GetDiffuse() const                     { return _vecDiffuse;      }
71   //! Returns the material specular color.
72   const Vec4f&  GetSpecular() const                    { return _vecSpecular;     }
73   //! Returns the material ambient color.
74   const Vec4f&  GetAmbient() const                     { return _vecAmbient;      }
75   //! Returns the material emissive color.
76   const Vec4f&  GetEmissive() const                    { return _vecEmissive;     }
77   //! Returns the material specular power (shininess).
78   const float   GetSpecularPower() const               { return _rSpecularPower;  }
79
80   // lighting
81   //! Returns true if lighting is enabled for this material.
82   bool          IsLightingEnabled() const              { return _bLighting;       }
83
84   // color material (which material property tracks color calls)
85   //! Returns the face for which color material tracking is enabled.
86   GLenum        GetColorMaterialFace() const           { return _eColorMaterialFace; }
87   //! Returns the color material tracking mode.
88   GLenum        GetColorMaterialMode() const           { return _eColorMaterialMode; }
89   //! Returns true if color material tracking is enabled.
90   bool          IsColorMaterialEnabled() const         { return _bColorMaterial;     }  
91
92   //! Returns the fog density or start / end distance.
93   float         GetFogParameter(GLenum eParameter) const;
94   //! Returns the fog mode (exponential, linear, etc.)
95   GLenum        GetFogMode() const                     { return _eFogMode;        }
96   //! Returns the fog color.
97   const Vec4f&  GetFogColor() const                    { return _vecFogColor;     }
98   //! Returns true if fog is enabled for this material.
99   bool          IsFogEnabled() const                   { return _bFog;            }
100
101   // texturing
102   //! Returns the active texture target for texture unit @a iTextureUnit.
103   GLenum        GetActiveTarget(unsigned int iTextureUnit) const 
104                 { return _textureState.GetActiveTarget(iTextureUnit);   }
105   //! Returns the bound texture ID for texture unit @a iTextureUnit.
106   unsigned int  GetTextureID(unsigned int iTextureUnit) const    
107                 { return _textureState.GetTextureID(iTextureUnit);      }
108   //! Returns true if texturing is enabled for texture unit @a iTextureUnit.
109   bool          IsTextureEnabled(unsigned int iTextureUnit) const
110                 { return _textureState.IsTextureEnabled(iTextureUnit);  }
111   //! Returns the value of the texture parameter @a eParameter for texture unit @a iTextureUnit.
112   GLenum        GetTextureParameter(unsigned int iTextureUnit, GLenum eParameter) const
113                 { return _textureState.GetTextureParameter(iTextureUnit, eParameter); }
114   //! Returns the texture application mode of the texture environment.
115   GLenum        GetTextureApplicationMode() const      { return _eTextureEnvMode; }
116
117   //! Returns a reference to the texture state object owned by this materal.
118   SkyTextureState& GetTextureState()                   { return _textureState;    }
119
120   // depth test
121   //! Returns true if depth testing is enabled for this material.
122   bool          IsDepthTestEnabled() const             { return _bDepthTest;      }
123   //! Returns the depth test function for this material.
124   GLenum        GetDepthFunc() const                   { return _eDepthFunc;      }
125   //! Returns true if depth writes are enabled for this material, false if not.
126   bool          GetDepthMask() const                   { return _bDepthMask;      }
127   
128   // alpha test
129   //! Returns true if alpha testing is enabled for this material.
130   bool          IsAlphaTestEnabled() const             { return _bAlphaTest;      }
131   //! Returns the alpha test function for this material.
132   GLenum        GetAlphaFunc() const                   { return _eAlphaFunc;      }
133   //! Returns the reference value for alpha comparison.
134   float         GetAlphaRef() const                    { return _rAlphaRef;       }
135   
136   // blending
137   //! Returns true if blending is enabled for this material.
138   bool          IsBlendingEnabled() const              { return _bBlending;       }
139   //! Returns the source blending factor for this material.
140   GLenum        GetBlendingSourceFactor() const        { return _eBlendSrcFactor; }
141   //! Returns the destination blending factor for this material.
142   GLenum        GetBlendingDestFactor() const          { return _eBlendDstFactor; }
143     
144   //! Returns true if face culling enabled for this material.
145   bool          IsFaceCullingEnabled() const           { return _bFaceCulling;    }
146   //! Returns which faces are culled -- front-facing or back-facing.
147   GLenum        GetFaceCullingMode() const             { return _eFaceCullingMode; }
148
149   // Setters for basic material properties
150
151   //! Sets the material identifier.
152   void          SetMaterialID(int ID)                  { _iMaterialID = ID;       }  
153   //! Sets the diffuse material color.
154   void          SetDiffuse( const Vec4f& d)            { _vecDiffuse = d;         }
155   //! Sets the specular material color.
156   void          SetSpecular(const Vec4f& d)            { _vecSpecular = d;        }
157   //! Sets the ambient material color.
158   void          SetAmbient( const Vec4f& d)            { _vecAmbient = d;         }
159   //! Sets the emissive material color.
160   void          SetEmissive(const Vec4f& d)            { _vecEmissive = d;        }
161   //! Sets the material specular power (shininess).
162   void          SetSpecularPower(float power)          { _rSpecularPower = power; }
163
164   // lighting
165   //! Enables / Disables lighting for this material.
166   void          EnableLighting(bool bEnable)           { _bLighting = bEnable;    }
167
168   // color material (which material property tracks color calls)
169   //! Sets which faces (front or back) track color calls.
170   void          SetColorMaterialFace(GLenum eFace)     { _eColorMaterialFace = eFace; }
171   //! Sets which material property tracks color calls.
172   void          SetColorMaterialMode(GLenum eMode)     { _eColorMaterialMode = eMode; }
173   //! Enables / Disables material color tracking for this material.
174   void          EnableColorMaterial(bool bEnable)      { _bColorMaterial = bEnable;   }
175
176   //! Sets the fog density or start / end distance.
177   SKYRESULT     SetFogParameter(GLenum eParameter, float rValue);
178   //! Sets the fog mode (exponential, linear, etc.)
179   void          SetFogMode(GLenum eMode)               { _eFogMode = eMode;           }
180   //! Sets the fog color.
181   void          SetFogColor(const Vec4f& color)        { _vecFogColor = color;        }
182   //! Enables / Disables fog for this material.
183   void          EnableFog(bool bEnable)                { _bFog = bEnable;             }
184   
185   // texturing
186   //! Sets the bound texture and texture target for texture unit @a iTextureUnit.
187   SKYRESULT     SetTexture(unsigned int iTextureUnit, GLenum eTarget, SkyTexture& texture)
188                 { return _textureState.SetTexture(iTextureUnit, eTarget, texture);    }
189   //! Sets the bound texture and texture target for texture unit @a iTextureUnit.
190   SKYRESULT     SetTexture(unsigned int iTextureUnit, GLenum eTarget, unsigned int iTextureID)
191                 { return _textureState.SetTexture(iTextureUnit, eTarget, iTextureID); }
192   //! Enables / Disables texture unit @a iTextureUnit for this material.
193   SKYRESULT     EnableTexture(unsigned int iTextureUnit, bool bEnable)
194                 { return _textureState.EnableTexture(iTextureUnit, bEnable);          }
195   //! Sets the value of the texture parameter @a eParameter for texture unit @a iTextureUnit.
196   SKYRESULT     SetTextureParameter(unsigned int  iTextureUnit, GLenum eParameter, GLenum eMode)
197                 { return _textureState.SetTextureParameter(iTextureUnit, eParameter, eMode); }
198
199   //! Sets the texture application mode of the texture environment.
200   void          SetTextureApplicationMode(GLenum eMode){ _eTextureEnvMode = eMode;}
201
202   // depth test
203   //! Enables / Disables depth test for this material.
204   void          EnableDepthTest(bool bEnable)          { _bDepthTest = bEnable;   }
205   //! Sets the depth test function (greater, less than, equal, etc.).
206   void          SetDepthFunc(GLenum  eDepthFunc)       { _eDepthFunc = eDepthFunc;}
207   //! If @a bDepthMask is true, then depth writes are enabled, otherwise they are not.
208   void          SetDepthMask(bool    bDepthMask)       { _bDepthMask = bDepthMask;}
209   
210   // alpha test
211   //! Enables / Disables alpha test for this material.
212   void          EnableAlphaTest(bool bEnable)          { _bAlphaTest = bEnable;   }
213   //! Sets the alpha test function (greater, less than, equal, etc.).
214   void          SetAlphaFunc(GLenum  eAlphaFunc)       { _eAlphaFunc = eAlphaFunc;}
215   //! Sets the reference value against which fragment alpha values are compared.
216   void          SetAlphaRef(float    rAlphaRef)        { _rAlphaRef  = rAlphaRef; }
217   
218   // blending
219   //! Enables / Disables blending for this material.
220   void          EnableBlending(bool bEnable)           { _bBlending  = bEnable;   }
221   //! Sets the source and destination blending factors for this material.
222   void          SetBlendFunc(GLenum eSrcFactor, GLenum eDstFactor) 
223                 { _eBlendSrcFactor = eSrcFactor; _eBlendDstFactor = eDstFactor;   }
224
225   //! Enables / Disables face culling for this material.
226   void          EnableFaceCulling(bool bEnable)        { _bFaceCulling = bEnable; }
227   //! Sets which faces will be culled -- front facing or back facing.
228   void          SetFaceCullingMode(GLenum eMode)       { _eFaceCullingMode = eMode; }
229
230 protected:
231   int           _iMaterialID;  
232
233   Vec4f         _vecDiffuse;
234   Vec4f         _vecSpecular;
235   Vec4f         _vecAmbient;
236   Vec4f         _vecEmissive;
237   
238   float         _rSpecularPower;
239
240   bool          _bLighting;
241
242   GLenum        _eColorMaterialFace;
243   GLenum        _eColorMaterialMode;
244   bool          _bColorMaterial;
245
246   enum SkyFogParams
247   {
248     SKY_FOG_DENSITY,
249     SKY_FOG_START,
250     SKY_FOG_END,
251     SKY_FOG_NUM_PARAMS
252   };
253
254   Vec4f         _vecFogColor;
255   GLenum        _eFogMode;
256   float         _rFogParams[SKY_FOG_NUM_PARAMS];
257   bool          _bFog;
258
259   GLenum        _eDepthFunc;
260   bool          _bDepthMask;
261   bool          _bDepthTest;
262
263   GLenum        _eAlphaFunc;
264   float         _rAlphaRef;
265   bool          _bAlphaTest;
266
267   GLenum        _eBlendSrcFactor;
268   GLenum        _eBlendDstFactor;
269   bool          _bBlending;
270
271   bool          _bFaceCulling;
272   GLenum        _eFaceCullingMode;
273
274   SkyTextureState _textureState;
275   GLenum        _eTextureEnvMode;
276 };
277
278 #endif //__SKYMATERIAL_HPP__