1 | #include "OgreItemBufferQueryManager.h" |
---|
2 | #include <OgreLogManager.h> |
---|
3 | #include <OgreStringConverter.h> |
---|
4 | #include <vector> |
---|
5 | #include <OgreSubEntity.h> |
---|
6 | |
---|
7 | namespace Ogre { |
---|
8 | //----------------------------------------------------------------------- |
---|
9 | ItemBufferQueryManager::ItemBufferQueryManager(PlatformHierarchyInterface *hierarchyInterface, Viewport *vp, |
---|
10 | const bool renderPatches): |
---|
11 | PlatformQueryManager(hierarchyInterface, vp), mRenderPatchesForItemBuffer(renderPatches) |
---|
12 | { |
---|
13 | } |
---|
14 | //----------------------------------------------------------------------- |
---|
15 | bool ItemBufferQueryManager::ShootRay(const Ray &ray, std::vector<Mesh *> *visibleMeshes, bool isGlobalLine) |
---|
16 | { |
---|
17 | // run OGRE ray shooting query |
---|
18 | return false; |
---|
19 | } |
---|
20 | //----------------------------------------------------------------------- |
---|
21 | void ItemBufferQueryManager::ComputeCameraVisibility(const Camera &camera, |
---|
22 | InfoContainer<GtpVisibility::NodeInfo> *visibleNodes, |
---|
23 | InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry, |
---|
24 | InfoContainer<GtpVisibility::PatchInfo> *visiblePatches, |
---|
25 | bool relativeVisibility) |
---|
26 | { |
---|
27 | // initialise item buffer (if not already initialised) |
---|
28 | InitItemBuffer(visibleNodes, visibleGeometry, visiblePatches); |
---|
29 | |
---|
30 | // we need access to the scene manager and the rendersystem |
---|
31 | PlatformHierarchyInterface *pfHierarchyInterface = |
---|
32 | dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface); |
---|
33 | |
---|
34 | SceneManager *sm = pfHierarchyInterface->GetSceneManager(); |
---|
35 | |
---|
36 | // --- Render scene with item buffer (i.e., objects with their id as color codes) |
---|
37 | |
---|
38 | // const_cast allowed because camera is not changed in renderScene |
---|
39 | Camera *pCam = const_cast<Camera *>(&camera); |
---|
40 | |
---|
41 | // disable overlays, reset them later |
---|
42 | bool overlayEnabled = mViewport->getOverlaysEnabled(); |
---|
43 | mViewport->setOverlaysEnabled(false); |
---|
44 | |
---|
45 | // set item buffer (must be provided by scene manager) |
---|
46 | bool useItemBuffer = true; |
---|
47 | sm->setOption("UseItemBuffer", &useItemBuffer); |
---|
48 | |
---|
49 | // clear background with black (i.e., not a valid item id) |
---|
50 | ColourValue bg = mViewport->getBackgroundColour(); |
---|
51 | mViewport->setBackgroundColour(ColourValue(0, 0, 0, 0)); |
---|
52 | |
---|
53 | |
---|
54 | // --- render item buffer |
---|
55 | pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false); |
---|
56 | |
---|
57 | |
---|
58 | //---- collect results |
---|
59 | int dimx = 0; |
---|
60 | int dimy = 0; |
---|
61 | |
---|
62 | // get frame buffer |
---|
63 | uchar *buf = mViewport->getTarget()->getBufferContents(dimx, dimy); |
---|
64 | |
---|
65 | int n = mRenderPatchesForItemBuffer ? (int)visiblePatches->size() : (int)visibleGeometry->size(); |
---|
66 | |
---|
67 | // loop through frame buffer & collect visible pixels |
---|
68 | for (int idx = 0; idx < dimy * dimx * 3; idx += 3) |
---|
69 | { |
---|
70 | // -- decode color code to receive id |
---|
71 | int id = buf[idx] << 16; |
---|
72 | id += buf[idx + 1] << 8; |
---|
73 | id += buf[idx + 2]; |
---|
74 | |
---|
75 | // if valid id <= add visibility (id values start at 1 |
---|
76 | if ((id > 0) && (id < n)) |
---|
77 | { |
---|
78 | if (mRenderPatchesForItemBuffer) |
---|
79 | { |
---|
80 | ((*visiblePatches)[id]).AddVisibility(1, 1); |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | ((*visibleGeometry)[id]).AddVisibility(1, 1); |
---|
85 | } |
---|
86 | } |
---|
87 | }
|
---|
88 |
|
---|
89 | //-- reset options
|
---|
90 |
|
---|
91 | // don't need item buffer anymore |
---|
92 | useItemBuffer = false; |
---|
93 | sm->setOption("UseItemBuffer", &useItemBuffer);
|
---|
94 | // reset initialised - flag
|
---|
95 | mWasInitialised = false;
|
---|
96 | // reset old overlay status |
---|
97 | mViewport->setOverlaysEnabled(overlayEnabled); |
---|
98 | // reset background color |
---|
99 | mViewport->setBackgroundColour(bg);
|
---|
100 |
|
---|
101 | // delete array storing the frame buffer
|
---|
102 | delete [] buf;
|
---|
103 | } |
---|
104 | //----------------------------------------------------------------------- |
---|
105 | void ItemBufferQueryManager::InitItemBuffer( |
---|
106 | InfoContainer<GtpVisibility::NodeInfo> *visibleNodes, |
---|
107 | InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry, |
---|
108 | InfoContainer<GtpVisibility::PatchInfo> *visiblePatches) |
---|
109 | { |
---|
110 | if (mWasInitialised) |
---|
111 | return; |
---|
112 | |
---|
113 | mWasInitialised = true; |
---|
114 | |
---|
115 | SceneManager *sm = |
---|
116 | dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface)->GetSceneManager(); |
---|
117 | |
---|
118 | SceneManager::EntityIterator it = sm->getEntityIterator(); |
---|
119 | |
---|
120 | // TODO: make this more efficient |
---|
121 | visibleGeometry->clear(); |
---|
122 | visibleNodes->clear(); |
---|
123 | visiblePatches->clear(); |
---|
124 | |
---|
125 | int id = 0; |
---|
126 | |
---|
127 | /* We can either use patches or meshes. If patches are used, an unique id must |
---|
128 | be given each patch. Otherwise the same id must be given to all patches belonging |
---|
129 | to the same mesh. |
---|
130 | */ |
---|
131 | while (it.hasMoreElements()) |
---|
132 | { |
---|
133 | Entity *ent = it.getNext(); |
---|
134 | |
---|
135 | for (int i = 0; i < (int)ent->getNumSubEntities(); ++i)
|
---|
136 | {
|
---|
137 | SubEntity *subEnt = ent->getSubEntity(i);
|
---|
138 |
|
---|
139 | if (mRenderPatchesForItemBuffer)
|
---|
140 | {
|
---|
141 | ++ id;
|
---|
142 | visiblePatches->push_back(GtpVisibility::PatchInfo(subEnt, 0, 0));
|
---|
143 | }
|
---|
144 |
|
---|
145 | subEnt->setId(id);
|
---|
146 | } |
---|
147 | |
---|
148 | if (!mRenderPatchesForItemBuffer) |
---|
149 | { |
---|
150 | visibleGeometry->push_back(GtpVisibility::MeshInfo(ent, 0, 0)); |
---|
151 | ++ id; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | /* |
---|
157 | //-----------------------------------------------------------------------
|
---|
158 | Entity* VisibilityOctreeSceneManager::createEntity(const String& entityName,
|
---|
159 | const String& meshName)
|
---|
160 | {
|
---|
161 | Entity *ent = SceneManager::createEntity(entityName, meshName);
|
---|
162 |
|
---|
163 | for (int i = 0; i < (int)ent->getNumSubEntities(); ++i)
|
---|
164 | {
|
---|
165 | ent->getSubEntity(i)->setId(mCurrentEntityId);
|
---|
166 | }
|
---|
167 |
|
---|
168 | // increase counter of entity id values
|
---|
169 | ++ mCurrentEntityId;
|
---|
170 |
|
---|
171 | return ent;
|
---|
172 | } |
---|
173 | */ |
---|
174 | } // namespace Ogre |
---|