]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkyRenderableInstanceGroup.cpp
Initial revision.
[simgear.git] / simgear / scene / sky / clouds3d / SkyRenderableInstanceGroup.cpp
1 //------------------------------------------------------------------------------
2 // File : SkyRenderableInstanceGroup.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 SkyRenderableInstanceGroup.cpp
20  * 
21  * Implementation of class SkyRenderableInstanceGroup, an instance that groups
22  * other instances.
23  */
24
25 #include <GL/glut.h>
26 #include "SkyRenderableInstanceGroup.hpp"
27 #include "SkySceneManager.hpp"
28
29
30 //------------------------------------------------------------------------------
31 // Function               : SkyRenderableInstanceGroup::SkyRenderableInstanceGroup
32 // Description      : 
33 //------------------------------------------------------------------------------
34 /**
35  * @fn SkyRenderableInstanceGroup::SkyRenderableInstanceGroup()
36  * @brief Constructor.
37  */ 
38 SkyRenderableInstanceGroup::SkyRenderableInstanceGroup()
39 : SkyRenderableInstance(),
40   _pObjectSpaceBV(NULL) 
41 {
42 }
43
44
45 //------------------------------------------------------------------------------
46 // Function               : SkyRenderableInstanceGroup::~SkyRenderableInstanceGroup
47 // Description      : 
48 //------------------------------------------------------------------------------
49 /**
50  * @fn SkyRenderableInstanceGroup::~SkyRenderableInstanceGroup()
51  * @brief Destructor.
52  */ 
53 SkyRenderableInstanceGroup::~SkyRenderableInstanceGroup()
54 {
55 }
56
57
58 //------------------------------------------------------------------------------
59 // Function               : SkyRenderableInstanceGroup::Update
60 // Description      : 
61 //------------------------------------------------------------------------------
62 /**
63  * @fn SkyRenderableInstanceGroup::Update(const Camera &cam)
64  * @brief Processes any per frame updates the instance requires.
65  * 
66  * This method simply calls the SkyRenderableInstance::Update() method of each of
67  * its sub-instances.
68  */ 
69 SKYRESULT SkyRenderableInstanceGroup::Update(const Camera &cam)
70 {
71   InstanceIterator ii;
72   for (ii = _opaqueSubInstances.begin(); ii != _opaqueSubInstances.end(); ++ii)
73   {
74     FAIL_RETURN((*ii)->Update(cam));
75   }
76   for (ii = _transparentSubInstances.begin(); ii != _transparentSubInstances.end(); ++ii)
77   {
78     FAIL_RETURN((*ii)->Update(cam));
79   }
80
81   SkySceneManager::SortInstances(_transparentSubInstances, cam.Orig);
82   return SKYRESULT_OK;
83 }
84
85
86 //------------------------------------------------------------------------------
87 // Function               : SkyRenderableInstanceGroup::Display
88 // Description      : 
89 //------------------------------------------------------------------------------
90 /**
91  * @fn SkyRenderableInstanceGroup::Display()
92  * @brief Displays all sub-instances of this instance.
93  * 
94  * The object-to-world transform of this instance group will be applied to all sub-instances before
95  * their own object-to-world transforms are applied.
96  */ 
97 SKYRESULT SkyRenderableInstanceGroup::Display()
98 {
99   // Get and set the world space transformation
100   Mat44f mat;
101   GetModelToWorldTransform(mat);
102
103   glMatrixMode(GL_MODELVIEW);
104   glPushMatrix();
105   glMultMatrixf(mat.M);
106
107   InstanceIterator ii;
108   /***
109   for (ii = _opaqueSubInstances.begin(); ii != _opaqueSubInstances.end(); ++ii)
110   {
111     FAIL_RETURN((*ii)->Display());
112   }
113
114   for (ii = _transparentSubInstances.begin(); ii != _transparentSubInstances.end(); ++ii)
115   {
116     FAIL_RETURN((*ii)->Display());
117   }
118 \r***/
119   _pObjectSpaceBV->Display();
120
121   glMatrixMode(GL_MODELVIEW);
122   glPopMatrix();
123
124   return SKYRESULT_OK;
125 }
126
127
128 //------------------------------------------------------------------------------
129 // Function               : SkyRenderableInstanceGroup::ViewFrustumCull
130 // Description      : 
131 //------------------------------------------------------------------------------
132 /**
133  * @fn SkyRenderableInstanceGroup::ViewFrustumCull(const Camera& cam)
134  * @brief @todo <WRITE BRIEF SkyRenderableInstanceGroup::ViewFrustumCull DOCUMENTATION>
135  * 
136  * @todo <WRITE EXTENDED SkyRenderableInstanceGroup::ViewFrustumCull FUNCTION DOCUMENTATION>
137  */ 
138 bool SkyRenderableInstanceGroup::ViewFrustumCull(const Camera& cam)
139 {
140   Mat44f xform;
141   GetModelToWorldTransform(xform);
142   _bCulled = (_pObjectSpaceBV == NULL) ? false : _pObjectSpaceBV->ViewFrustumCull(cam, xform);
143   return _bCulled;
144 }
145
146
147 //------------------------------------------------------------------------------
148 // Function               : SkyRenderableInstanceGroup::AddSubInstance
149 // Description      : 
150 //------------------------------------------------------------------------------
151 /**
152  * @fn SkyRenderableInstanceGroup::AddSubInstance(SkyRenderableInstance *pInstance, bool bTransparent)
153  * @brief Adds a sub-instance to the group.
154  */ 
155 void SkyRenderableInstanceGroup::AddSubInstance(SkyRenderableInstance *pInstance, bool bTransparent)
156 {
157   if (!bTransparent)
158     _opaqueSubInstances.push_back(pInstance);
159   else
160     _transparentSubInstances.push_back(pInstance);
161
162   // update the bounds...
163   Mat44f xform;
164   pInstance->GetModelToWorldTransform(xform);
165
166   SkyMinMaxBox *pBV = pInstance->GetBoundingVolume();
167   if (pBV)
168   {
169     Vec3f min = pInstance->GetBoundingVolume()->GetMin();
170     Vec3f max = pInstance->GetBoundingVolume()->GetMax();
171
172     if (!_pObjectSpaceBV)
173       _pObjectSpaceBV = new SkyMinMaxBox;
174
175     _pObjectSpaceBV->AddPoint(xform * min);
176     _pObjectSpaceBV->AddPoint(xform * max);
177   } 
178 }