1 | #include <OgreCamera.h>
|
---|
2 | #include <OgreLogManager.h>
|
---|
3 | #include <OgreSubEntity.h>
|
---|
4 | #include <OgreEntity.h>
|
---|
5 | #include <OgreMovableObject.h>
|
---|
6 | #include "OgreSolidBoundingBox.h"
|
---|
7 | #include "OgrePlatformHierarchyInterface.h"
|
---|
8 | #include "OgrePlatformOcclusionQuery.h"
|
---|
9 |
|
---|
10 | namespace Ogre {
|
---|
11 |
|
---|
12 | //-----------------------------------------------------------------------
|
---|
13 | PlatformHierarchyInterface::PlatformHierarchyInterface(SceneManager *sm, RenderSystem *rsys):
|
---|
14 | mSceneManager(sm), mRenderSystem(rsys), mSolidBoundingBox(NULL), mCamera(NULL),
|
---|
15 | mCullCamera(NULL), mOnlyShadowCasters(false), mLeavePassesInQueue(0),
|
---|
16 | mIsBoundingBoxQuery(false)
|
---|
17 | {
|
---|
18 | }
|
---|
19 | //-----------------------------------------------------------------------
|
---|
20 | void PlatformHierarchyInterface::CreateNodeVizMaterials()
|
---|
21 | {
|
---|
22 | // material for frustum culled nodes
|
---|
23 | MaterialPtr mat = MaterialManager::getSingleton().getByName("FrustumCulledNodesMaterial");
|
---|
24 |
|
---|
25 | if (mat.isNull())
|
---|
26 | {
|
---|
27 | mat = MaterialManager::getSingleton().create("FrustumCulledNodesMaterial",
|
---|
28 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
29 | mat->createTechnique()->createPass();
|
---|
30 |
|
---|
31 | mat->getTechnique(0)->getPass(0)->setAmbient(1, 0, 0);
|
---|
32 | mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
|
---|
33 | //mat->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
|
---|
34 | mat->load();
|
---|
35 | }
|
---|
36 |
|
---|
37 | // material for query culled nodes
|
---|
38 | mat = MaterialManager::getSingleton().getByName("QueryCulledNodesMaterial");
|
---|
39 |
|
---|
40 | if (mat.isNull())
|
---|
41 | {
|
---|
42 | mat = MaterialManager::getSingleton().create("QueryCulledNodesMaterial",
|
---|
43 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
44 | mat->createTechnique()->createPass();
|
---|
45 |
|
---|
46 | mat->getTechnique(0)->getPass(0)->setAmbient(0, 0, 1);
|
---|
47 | mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
|
---|
48 | mat->load();
|
---|
49 | }
|
---|
50 |
|
---|
51 | // material for scene nodes
|
---|
52 | /*mat = MaterialManager::getSingleton().getByName("SceneNodesMaterial");
|
---|
53 |
|
---|
54 | if (mat.isNull())
|
---|
55 | {
|
---|
56 | mat = MaterialManager::getSingleton().create("SceneNodesMaterial",
|
---|
57 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
58 | mat->createTechnique()->createPass();
|
---|
59 |
|
---|
60 | mat->getTechnique(0)->getPass(0)->setAmbient(1, 1, 0);
|
---|
61 | mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
|
---|
62 | mat->load();
|
---|
63 | }*/
|
---|
64 | }
|
---|
65 | //-----------------------------------------------------------------------
|
---|
66 | PlatformHierarchyInterface::~PlatformHierarchyInterface()
|
---|
67 | {
|
---|
68 | DeleteQueries();
|
---|
69 |
|
---|
70 | if (mSolidBoundingBox)
|
---|
71 | delete mSolidBoundingBox;
|
---|
72 | }
|
---|
73 | //-----------------------------------------------------------------------
|
---|
74 | void PlatformHierarchyInterface::DeleteQueries()
|
---|
75 | {
|
---|
76 | for (int i=0; i < (int)mOcclusionQueries.size(); ++i)
|
---|
77 | delete mOcclusionQueries[i];
|
---|
78 |
|
---|
79 | mOcclusionQueries.clear();
|
---|
80 | }
|
---|
81 | //-----------------------------------------------------------------------
|
---|
82 | void PlatformHierarchyInterface::RenderBoundingBox(AxisAlignedBox *box)
|
---|
83 | {
|
---|
84 | static RenderOperation ro;
|
---|
85 |
|
---|
86 | SolidBoundingBox *solidBox = GetSolidBoundingBox();
|
---|
87 |
|
---|
88 | mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
|
---|
89 | mSceneManager->useRenderableViewProjMode(solidBox);
|
---|
90 |
|
---|
91 | // set no depth write, no color, no lighting material
|
---|
92 | mSceneManager->setPass(solidBox->getTechnique()->getPass(0));
|
---|
93 | //SetOcclusionPass();
|
---|
94 |
|
---|
95 | solidBox->SetupBoundingBoxVertices(*box);
|
---|
96 |
|
---|
97 | solidBox->getRenderOperation(ro);
|
---|
98 | ro.srcRenderable = solidBox;
|
---|
99 | mRenderSystem->_render(ro);
|
---|
100 |
|
---|
101 | // Render two halfes of the bounding box (using triangle fans)
|
---|
102 | /*for(int halfIdx = 0; halfIdx < 2; ++halfIdx)
|
---|
103 | {
|
---|
104 | solidBox->SetupBoundingBoxVertices(*box, halfIdx == 1);
|
---|
105 |
|
---|
106 | solidBox->getRenderOperation(ro);
|
---|
107 | ro.srcRenderable = solidBox;
|
---|
108 | mRenderSystem->_render(ro);
|
---|
109 | }*/
|
---|
110 | }
|
---|
111 | //-----------------------------------------------------------------------
|
---|
112 | void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
|
---|
113 | {
|
---|
114 | mCamera = cam;
|
---|
115 | }
|
---|
116 | //-----------------------------------------------------------------------
|
---|
117 | void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
|
---|
118 | {
|
---|
119 | mCullCamera = cullCam;
|
---|
120 | }
|
---|
121 | //-----------------------------------------------------------------------
|
---|
122 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
|
---|
123 | {
|
---|
124 | if (mCurrentTestIdx == mOcclusionQueries.size())
|
---|
125 | {
|
---|
126 | mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
|
---|
127 | }
|
---|
128 |
|
---|
129 | return mOcclusionQueries[mCurrentTestIdx ++];
|
---|
130 | }
|
---|
131 | //-----------------------------------------------------------------------
|
---|
132 | void PlatformHierarchyInterface::InitTraversal(Camera *cam, Camera *cullCam, int leavePassesInQueue)
|
---|
133 | {
|
---|
134 | GtpVisibility::HierarchyInterface::InitTraversal();
|
---|
135 |
|
---|
136 | mSavedNode = NULL;
|
---|
137 | mLeavePassesInQueue = leavePassesInQueue;
|
---|
138 |
|
---|
139 | mCamera = cam;
|
---|
140 | mCullCamera = cullCam ? cullCam : cam;
|
---|
141 |
|
---|
142 | // create materials for node visualization
|
---|
143 | CreateNodeVizMaterials();
|
---|
144 | }
|
---|
145 | //-----------------------------------------------------------------------
|
---|
146 | void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
|
---|
147 | {
|
---|
148 | mSceneManager = sm;
|
---|
149 | }
|
---|
150 | //-----------------------------------------------------------------------
|
---|
151 | void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
|
---|
152 | {
|
---|
153 | mRenderSystem = rsys;
|
---|
154 | }
|
---|
155 | //-----------------------------------------------------------------------
|
---|
156 | bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
|
---|
157 | bool &intersects)
|
---|
158 | {
|
---|
159 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
160 | return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
|
---|
161 | #else
|
---|
162 | return true;
|
---|
163 | #endif
|
---|
164 | }
|
---|
165 | //-----------------------------------------------------------------------
|
---|
166 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueOcclusionQuery(
|
---|
167 | GtpVisibility::HierarchyNode *node, const bool wasVisible)
|
---|
168 | {
|
---|
169 | // get next available test id
|
---|
170 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
171 |
|
---|
172 | //-- the actual query test
|
---|
173 | query->BeginQuery();
|
---|
174 |
|
---|
175 | // if node is leaf and was visible => will be rendered anyway.
|
---|
176 | // In this case we can also test with the real geometry.
|
---|
177 | // If camera for culling is different from camera for rendering or only solids
|
---|
178 | // will be rendereded => cannot optimize
|
---|
179 | if (mTestGeometryForVisibleLeaves && (mCamera == mCullCamera) && wasVisible && IsLeaf(node))
|
---|
180 | {
|
---|
181 | //LogManager::getSingleton().logMessage("render node\n");
|
---|
182 | RenderNode(node);
|
---|
183 | }
|
---|
184 | else
|
---|
185 | {
|
---|
186 | // this information is used e.g., by the scene graph, because the bounding box
|
---|
187 | // must be treated differently to the scene geometry during rendering
|
---|
188 | mIsBoundingBoxQuery = true;
|
---|
189 |
|
---|
190 | //LogManager::getSingleton().logMessage("render box\n");
|
---|
191 | RenderBoundingBox(GetBoundingBox(node));
|
---|
192 |
|
---|
193 | mIsBoundingBoxQuery = false;
|
---|
194 | }
|
---|
195 |
|
---|
196 | query->EndQuery();
|
---|
197 |
|
---|
198 | return query;
|
---|
199 | }
|
---|
200 | //-----------------------------------------------------------------------
|
---|
201 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueOcclusionQuery(GtpVisibility::Mesh *mesh)
|
---|
202 | {
|
---|
203 | // get next available test id
|
---|
204 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
205 |
|
---|
206 | //-- the actual query test
|
---|
207 | query->BeginQuery();
|
---|
208 |
|
---|
209 | RenderGeometry(mesh);
|
---|
210 |
|
---|
211 | query->EndQuery();
|
---|
212 |
|
---|
213 | return query;
|
---|
214 | }
|
---|
215 | //-----------------------------------------------------------------------
|
---|
216 | /*void PlatformHierarchyInterface::SetOcclusionPass()
|
---|
217 | {
|
---|
218 | // disable vertex and fragment program
|
---|
219 | mRenderSystem->unbindGpuProgram(GPT_VERTEX_PROGRAM);
|
---|
220 | mRenderSystem->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
|
---|
221 |
|
---|
222 | // disable lighting
|
---|
223 | mRenderSystem->setLightingEnabled(false);
|
---|
224 |
|
---|
225 | // Disable remaining texture units
|
---|
226 | mRenderSystem->_disableTextureUnitsFrom(0);
|
---|
227 |
|
---|
228 | //--Set up non-texture related material settings
|
---|
229 |
|
---|
230 | // Depth buffer settings
|
---|
231 | mRenderSystem->_setDepthBufferParams(true, false, CMPF_LESS_EQUAL);
|
---|
232 | // Set colour write mode off
|
---|
233 | mRenderSystem->_setColourBufferWriteEnabled(false, false, false, false);
|
---|
234 | }*/
|
---|
235 | //-----------------------------------------------------------------------
|
---|
236 | SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
|
---|
237 | {
|
---|
238 | if (!mSolidBoundingBox)
|
---|
239 | mSolidBoundingBox = new SolidBoundingBox;
|
---|
240 |
|
---|
241 | return mSolidBoundingBox;
|
---|
242 | }
|
---|
243 | //-----------------------------------------------------------------------
|
---|
244 | void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
|
---|
245 | {
|
---|
246 | mOnlyShadowCasters = onlyShadowCasters;
|
---|
247 | }
|
---|
248 | //-----------------------------------------------------------------------
|
---|
249 | bool PlatformHierarchyInterface::GetOnlyShadowCasters()
|
---|
250 | {
|
---|
251 | return mOnlyShadowCasters;
|
---|
252 | }
|
---|
253 | //-----------------------------------------------------------------------
|
---|
254 | bool PlatformHierarchyInterface::GetTestGeometryForVisibleLeaves()
|
---|
255 | {
|
---|
256 | return mTestGeometryForVisibleLeaves;
|
---|
257 | }
|
---|
258 | //-----------------------------------------------------------------------
|
---|
259 | bool PlatformHierarchyInterface::IsBoundingBoxQuery()
|
---|
260 | {
|
---|
261 | return mIsBoundingBoxQuery;
|
---|
262 | }
|
---|
263 | //-----------------------------------------------------------------------
|
---|
264 | void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
|
---|
265 | {
|
---|
266 | mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
|
---|
267 | }
|
---|
268 | //-----------------------------------------------------------------------
|
---|
269 | SceneManager *PlatformHierarchyInterface::GetSceneManager()
|
---|
270 | {
|
---|
271 | return mSceneManager;
|
---|
272 | }
|
---|
273 | //-----------------------------------------------------------------------
|
---|
274 | RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
|
---|
275 | {
|
---|
276 | return mRenderSystem;
|
---|
277 | }
|
---|
278 | } // namespace Ogre
|
---|