source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgrePlatformQueryManager.cpp @ 925

Revision 925, 7.7 KB checked in by mattausch, 19 years ago (diff)

update for ogre 1.2
OcclusionCullingSceneManager? is the only scenemanager in the solution now

Line 
1#include "OgrePlatformQueryManager.h"
2#include "OcclusionQuery.h"
3#include <OgreSceneManager.h>
4#include <OgreLogManager.h>
5#include <OgreStringConverter.h>
6#include <vector>
7#include <OgreSubEntity.h>
8#include "OgrePlatformHierarchyInterface.h"
9
10namespace Ogre {
11
12//-----------------------------------------------------------------------
13PlatformQueryManager::PlatformQueryManager(GtpVisibility::HierarchyInterface *hierarchyInterface,
14                                                                                   Viewport *vp, int queryModes):
15QueryManager(hierarchyInterface, queryModes),
16mViewport(vp),
17mWasInitialised(false)
18{
19}
20//-----------------------------------------------------------------------
21bool PlatformQueryManager::ShootRay(const Ray &ray,
22                                                                        std::vector<Mesh *> *visibleMeshes,
23                                                                        bool isGlobalLine)
24{
25    // run OGRE ray shooting query
26    return false;
27}
28//-----------------------------------------------------------------------
29void PlatformQueryManager::ComputeFromPointVisibility(const Vector3 &point,
30                               GtpVisibility::NodeInfoContainer *visibleNodes,
31                               GtpVisibility::MeshInfoContainer *visibleGeometry,
32                                   GtpVisibility::PatchInfoContainer *visiblePatches,
33                               bool relativeVisibility)
34{
35        SceneManager *sm = //dynamic_cast<PlatformHierarchyInterface *>
36                static_cast<PlatformHierarchyInterface *>
37                (mHierarchyInterface)->GetSceneManager();
38
39        // create a camera for the point query
40        Camera *cam = sm->createCamera("PointQueryCam");       
41
42        //save old camera
43        Camera *savedCam = mViewport->getCamera();
44       
45        // --- initialise new camera
46        mViewport->setCamera(cam);
47        cam->setPosition(point);
48
49        cam->setNearClipDistance(savedCam->getNearClipDistance());
50        cam->setFarClipDistance(savedCam->getFarClipDistance());
51
52        // set frustum to 45 degrees so all the scene can be captured with 6 shots
53        cam->setAspectRatio(1.0);
54        cam->setFOVy(Radian(Math::HALF_PI));
55
56        int sign = -1;
57       
58        // ---- capture visibility from all 6 directions
59        for (int dir=0; dir < 6; dir++)
60        {
61                sign *= -1;
62               
63                // Print camera details
64        std::stringstream d;
65                d << "Point query camera: " + StringConverter::toString(cam->getDerivedPosition()) +
66                        " " + "O: " + StringConverter::toString(cam->getDerivedOrientation());
67                LogManager::getSingleton().logMessage(d.str());
68
69                // prevent from initialising geometry / node array again
70                if (dir > 0)
71                {
72                        mWasInitialised = true;
73                }
74
75                ComputeCameraVisibility(*cam, visibleNodes, visibleGeometry, visiblePatches,
76                                            relativeVisibility);
77                //mViewport->getTarget()->update(); for(int j=0; j<10000000; j++)       printf("wait");
78
79                // permute directions
80                Vector3 direction(0,0,0);
81                direction[dir/2] = sign;
82
83                cam->setDirection(direction);
84        }
85       
86        // reset camera
87        mViewport->setCamera(savedCam);
88}
89//-----------------------------------------------------------------------
90void PlatformQueryManager::ComputeCameraVisibility(const Camera &camera,
91                            GtpVisibility::NodeInfoContainer *visibleNodes,
92                            GtpVisibility::MeshInfoContainer *visibleGeometry,
93                                GtpVisibility::PatchInfoContainer *visiblePatches,
94                            bool relativeVisibility)
95{
96        // we need access to the scene manager and the rendersystem
97        PlatformHierarchyInterface *pfHierarchyInterface =
98                //dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
99                static_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
100
101        SceneManager *sm = pfHierarchyInterface->GetSceneManager();
102
103
104        // const_cast allowed because camera is not changed in renderScene
105        Camera *pCam = const_cast<Camera *>(&camera);
106
107        // disable overlays, reset them later
108        bool overlayEnabled = mViewport->getOverlaysEnabled();
109        mViewport->setOverlaysEnabled(false);
110       
111        // clear background with black (i.e., not a valid item id)
112        ColourValue bg = mViewport->getBackgroundColour();
113        mViewport->setBackgroundColour(ColourValue(0, 0, 0, 0));
114        //pfHierarchyInterface->GetRenderSystem()->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH);
115#ifdef ITEM_BUFFER
116        // initialise item buffer (if not already initialised)
117        InitItemBuffer(visibleGeometry, visiblePatches);
118
119        //-- render scene with item buffer (i.e., objects with their id as color codes)
120
121        // enable item buffer (must be provided by scene manager)
122        bool useItemBuffer = true;
123        sm->setOption("UseItemBuffer", &useItemBuffer);
124#endif // ITEM_BUFFER
125
126        sm->_renderScene(pCam, mViewport, false); // render item buffer
127   
128#ifdef ITEM_BUFFER
129        //-- collect results
130        CollectItemBufferResults(visibleGeometry, visiblePatches);
131
132        //-- reset options
133
134        useItemBuffer = false; // don't need item buffer anymore
135        sm->setOption("UseItemBuffer", &useItemBuffer);
136#endif // ITEM_BUFFER
137
138        mWasInitialised = false; // reset initialised - flag
139        mViewport->setOverlaysEnabled(overlayEnabled); // reset old overlay status
140        mViewport->setBackgroundColour(bg); // reset background color
141}
142#ifdef ITEM_BUFFER
143//-----------------------------------------------------------------------
144void PlatformQueryManager::CollectItemBufferResults(
145                                GtpVisibility::MeshInfoContainer *visibleGeometry,
146                                GtpVisibility::PatchInfoContainer *visiblePatches)
147{
148        int dimx = 0;
149        int dimy = 0;
150
151        // copy frame buffer
152        uchar *buf = mViewport->getTarget()->getBufferContents(dimx, dimy);
153
154        //std::stringstream d; d << "dimx: " << dimx << ", dimy: " << dimy; LogManager::getSingleton().logMessage(d.str());
155
156        // loop through frame buffer and collect visible pixels
157        for (int idx = 0; idx < dimy * dimx * 3; idx += 3)
158        {
159                //-- decode color code to receive id
160                int id = buf[idx] << 16;
161                id += buf[idx + 1] << 8;
162                id += buf[idx + 2];
163
164                //std::stringstream d; d << "myid: " << (int)buf[idx] << " " << (int)buf[idx + 1] << " " << (int)buf[idx + 2]; LogManager::getSingleton().logMessage(d.str());
165
166                // if valid id <= add visibility (id values start at 1)
167                if (mQueryModes == PATCH_VISIBILITY)
168                {       
169                        if ((id > 0) && (id < (int)visiblePatches->size()))
170                        {
171                                ((*visiblePatches)[id]).AddVisibility(1, 0);
172                        }
173                }
174                else if (mQueryModes == GEOMETRY_VISIBILITY)
175                {
176                        if ((id > 0) && (id < (int)visibleGeometry->size()))
177                        {
178                                ((*visibleGeometry)[id]).AddVisibility(1, 0);
179                        }
180                }
181        }
182
183        delete [] buf;
184}
185
186//-----------------------------------------------------------------------
187void PlatformQueryManager::InitItemBuffer(
188                                GtpVisibility::MeshInfoContainer *visibleGeometry,
189                                GtpVisibility::PatchInfoContainer *visiblePatches)
190{
191        if (mWasInitialised)
192                return;
193
194        mWasInitialised = true;
195
196        SceneManager *sm =
197                //dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface)->GetSceneManager();
198                static_cast<PlatformHierarchyInterface *>(mHierarchyInterface)->GetSceneManager();
199
200        SceneManager::EntityIterator it = sm->getEntityIterator();
201
202        // TODO: make the function more efficient
203
204        visibleGeometry->clear();
205        visiblePatches->clear();
206
207        int id = 0;
208
209        /* We can either use patches or meshes. If patches are used, an unique id must
210           be given each patch. Otherwise the same id must be given to all patches belonging
211           to the same mesh.
212    */
213        while (it.hasMoreElements())
214        {
215                Entity *ent = it.getNext();
216
217                for (int i = 0; i < (int)ent->getNumSubEntities(); ++i)
218                {
219                        SubEntity *subEnt = ent->getSubEntity(i);
220
221                        if (mQueryModes == PATCH_VISIBILITY)
222                        {
223                                ++ id;
224                                visiblePatches->push_back(GtpVisibility::PatchInfo(subEnt, 0, 0));
225                        }
226                               
227                        subEnt->setId(id);
228                        //subEnt->setId((41 << 16) + (4 << 8) + 60);
229                }
230                       
231                if (mQueryModes == GEOMETRY_VISIBILITY)
232                {
233                        visibleGeometry->push_back(GtpVisibility::MeshInfo(ent, 0, 0));
234                        ++ id;
235                }
236        }
237}
238#endif // ITEM_BUFFER
239//-----------------------------------------------------------------------
240void PlatformQueryManager::SetViewport(Viewport *vp)
241{
242        mViewport = vp;
243}
244} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.