]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkyRenderableInstance.hpp
Clouds3D crashes because there is no Light
[simgear.git] / simgear / scene / sky / clouds3d / SkyRenderableInstance.hpp
1 //------------------------------------------------------------------------------
2 // File : SkyRenderableInstance.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 SkyRenderableInstance.hpp
20  * 
21  * Interface definition for SkyRenderableInstance, an instance of a renderable object.
22  */
23 #ifndef __SKYRENDERABLEINSTANCE_HPP__
24 #define __SKYRENDERABLEINSTANCE_HPP__
25
26 #include <vector>
27 #include "mat33.hpp"
28 #include "mat44.hpp"
29 #include "SkyUtil.hpp"
30
31 // forward to reduce unnecessary dependencies
32 class Camera;
33 class SkyMinMaxBox;
34
35 // forward so we can make the following typedefs easily visible in the header.
36 // rather than buried under the class definition.
37 class SkyRenderableInstance;
38
39 //! A dynamic array of SkyRenderableInstance pointers.
40 typedef std::vector<SkyRenderableInstance*>       InstanceArray;
41 //! An instance array iterator.
42 typedef InstanceArray::iterator                   InstanceIterator;
43
44 //------------------------------------------------------------------------------
45 /**
46 * @class SkyRenderableInstance
47 * @brief An instance of a SkyRenderable object.
48
49 * An instance contains a pointer to a SkyRenderable object.  The
50 * instance contains attributes such as position, orientation,
51 * scale, etc. that vary between instances.
52 */
53 class SkyRenderableInstance
54 {
55 public:
56   //! Constructor.
57   SkyRenderableInstance() 
58   : _bCulled(false), _bAlive(true), _vecPosition(0, 0, 0), _rScale(1), _rSquareSortDistance(0)
59   { 
60     _matRotation.Identity(); _matInvRotation.Identity(); 
61   }
62
63   //! Constructor.
64   SkyRenderableInstance(const Vec3f   &position, 
65                         const Mat33f  &rotation, 
66                         const float   scale)
67   : _bCulled(false), _bAlive(true), _vecPosition(position), 
68     _matRotation(rotation), _rScale(scale), _rSquareSortDistance(0)
69   {
70     _matInvRotation = _matRotation;
71     _matInvRotation.Transpose();
72   }
73
74   //! Destructor
75   virtual ~SkyRenderableInstance() {}
76    
77   // Setters / Getters                                                      
78   //! Set the world space position of the instance.
79   virtual void SetPosition(const Vec3f  &position)     { _vecPosition = position; }
80   //! Set the world space rotation of the instance.
81   virtual void SetRotation(const Mat33f &rotation)     { _matRotation    = rotation;  
82                                                          _matInvRotation = rotation; 
83                                                          _matInvRotation.Transpose(); }
84   //! Set the world space scale of the instance.
85   virtual void SetScale(     const float  &scale)      { _rScale    = scale;  }
86   
87   //! Returns the world space position of the instance.
88   virtual const Vec3f&   GetPosition() const           { return _vecPosition; }
89   //! Returns the world space rotation matrix of the instance.
90   virtual const Mat33f&  GetRotation() const           { return _matRotation; }
91   //!  Returns the inverse of the world space rotation matrix of the instance.
92   virtual const Mat33f&  GetInverseRotation() const    { return _matInvRotation; }
93   //! Returns the world space scale of the instance.
94   virtual float          GetScale() const              { return _rScale;      }
95   
96   //! Update the instance based on the given camera, @a cam.
97   virtual SKYRESULT Update(const Camera &cam)          { return SKYRESULT_OK; }
98   //! Render the instance.
99   virtual SKYRESULT Display()                          { return SKYRESULT_OK; }
100   
101   //! Returns the transform matrix from model space to world space.
102   inline virtual void  GetModelToWorldTransform(Mat44f &mat) const;
103   
104   //! Returns the transform matrix from world space to model space.
105
106   inline virtual void  GetWorldToModelTransform(Mat44f &mat) const;
107
108   //! Returns the object-space bounding volume for this instance, or NULL if none is available.
109   virtual SkyMinMaxBox* GetBoundingVolume() const      { return NULL;         }
110    
111   //! Returns true if and only if the bounding volume of this instance lies entirely outside @a cam.  
112   virtual bool  ViewFrustumCull(const Camera &cam)     { return false;        }
113   //! Returns true if the instance was culled.
114   virtual bool  IsCulled()                             { return _bCulled;     }
115   //! Sets the culled state of the instance.
116   virtual void  SetCulled(bool bCulled)                { _bCulled = bCulled;  }
117   
118   //! Returns true if the instance is currently active.
119   virtual bool  IsAlive()                              { return _bAlive;      }
120   //! Activates or deactivates the instance.
121   virtual void  SetIsAlive(bool bAlive)                { _bAlive = bAlive;    }
122
123   //! Sets the distance of this object from the sort position.  Used to sort instances.
124   virtual void  SetSquareSortDistance(float rSqrDist)  { _rSquareSortDistance = rSqrDist; }
125   //! Returns the distance of this object from the sort position. (Set with SetSquareSortDistance())
126   virtual float GetSquareSortDistace() const           { return _rSquareSortDistance;     }
127   
128   //! This operator is used to sort instance arrays. 
129   bool operator<(const SkyRenderableInstance& instance) const
130   {
131     return (_rSquareSortDistance > instance._rSquareSortDistance);
132   }
133
134 protected:
135   bool          _bCulled;        // Culled flag
136   bool          _bAlive;         // Alive object flag  
137
138   Vec3f         _vecPosition;    // Position
139   Mat33f        _matRotation;    // Rotation
140   Mat33f        _matInvRotation; // inverse rotation
141   float         _rScale;         // Scale
142
143   // for sorting particles during shading
144   float         _rSquareSortDistance;
145 };
146
147 //------------------------------------------------------------------------------
148 // Function               : SkyRenderableInstance::GetModelToWorldTransform
149 // Description      : 
150 //------------------------------------------------------------------------------
151 /**
152  * @fn SkyRenderableInstance::GetModelToWorldTransform(Mat44f &mat) const
153  * @brief Returns the 4x4 transformation matrix from world to model space.
154  */ 
155 inline void SkyRenderableInstance::GetModelToWorldTransform(Mat44f &mat) const
156 {
157   mat[0]  = _matRotation.M[0]; mat[4]  = _matRotation.M[3]; 
158   mat[8]  = _matRotation.M[6]; mat[12] = 0;
159   mat[1]  = _matRotation.M[1]; mat[5]  = _matRotation.M[4]; 
160   mat[9]  = _matRotation.M[7]; mat[13] = 0;
161   mat[2]  = _matRotation.M[2]; mat[6]  = _matRotation.M[5]; 
162   mat[10] = _matRotation.M[8]; mat[14] = 0;
163   mat[3]  = 0; mat[7] = 0; mat[11] = 0; mat[15] = 0;
164
165   // Scale the matrix (we don't want to scale translation or mat[15] which is 1)
166   if (_rScale != 1)
167     mat *= _rScale;
168
169   // Set the translation and w coordinate after the potential scaling
170   mat[12] = _vecPosition.x; mat[13] = _vecPosition.y; mat[14] = _vecPosition.z;    
171   mat[15] = 1;
172 }
173
174
175 //------------------------------------------------------------------------------
176 // Function               : Mat44f& SkyRenderableInstance::GetWorldToModelTransform
177 // Description      : 
178 //------------------------------------------------------------------------------
179 /**
180  * @fn SkyRenderableInstance::GetWorldToModelTransform(Mat44f &mat) const
181  * @brief Returns the 4x4 transformation matrix from world to model space.
182  */ 
183 inline void SkyRenderableInstance::GetWorldToModelTransform(Mat44f &mat) const
184 {
185   mat[0]  = _matRotation.M[0]; mat[4]  = _matRotation.M[1]; 
186   mat[8]  = _matRotation.M[2]; mat[12] = 0;
187   mat[1]  = _matRotation.M[3]; mat[5]  = _matRotation.M[4]; 
188   mat[9]  = _matRotation.M[5]; mat[13] = 0;
189   mat[2]  = _matRotation.M[6]; mat[6]  = _matRotation.M[7]; 
190   mat[10] = _matRotation.M[8]; mat[14] = 0;
191   mat[3]  = 0; mat[7] = 0; mat[11] = 0; mat[15] = 0;
192
193   // Scale the matrix (we don't want to scale translation or mat[15] which is 1)
194   if (_rScale != 1)
195     mat *= (1 / _rScale);
196
197   // Set the translation and w coordinate after the potential scaling
198   mat[12] = -_vecPosition.x; mat[13] = -_vecPosition.y; mat[14] = -_vecPosition.z;    
199   mat[15] = 1;
200 }
201
202 #endif //__SKYRENDERABLEINSTANCE_HPP__