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