[153] | 1 | #include "OgreItemBufferQueryManager.h" |
---|
| 2 | #include <OgreLogManager.h> |
---|
| 3 | #include <OgreStringConverter.h> |
---|
| 4 | #include <vector> |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | namespace Ogre { |
---|
| 8 | //----------------------------------------------------------------------- |
---|
| 9 | ItemBufferQueryManager::ItemBufferQueryManager(PlatformHierarchyInterface *hierarchyInterface, Viewport *vp): |
---|
[154] | 10 | PlatformQueryManager(hierarchyInterface, vp) |
---|
[153] | 11 | { |
---|
| 12 | } |
---|
| 13 | //----------------------------------------------------------------------- |
---|
| 14 | bool ItemBufferQueryManager::ShootRay(const Ray &ray, std::vector<Mesh *> *visibleMeshes, bool isGlobalLine) |
---|
| 15 | { |
---|
| 16 | // run OGRE ray shooting query |
---|
| 17 | return false; |
---|
| 18 | } |
---|
| 19 | //----------------------------------------------------------------------- |
---|
| 20 | void ItemBufferQueryManager::ComputeCameraVisibility(const Camera &camera, |
---|
| 21 | InfoContainer<GtpVisibility::NodeInfo> *visibleNodes, |
---|
| 22 | InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry, |
---|
| 23 | bool relativeVisibility) |
---|
| 24 | { |
---|
[154] | 25 | // initialise item buffer (if not already initialised) |
---|
[153] | 26 | InitItemBuffer(visibleNodes, visibleGeometry); |
---|
| 27 | |
---|
| 28 | // we need access to the scene manager and the rendersystem |
---|
| 29 | PlatformHierarchyInterface *pfHierarchyInterface = |
---|
| 30 | dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface); |
---|
| 31 | |
---|
| 32 | SceneManager *sm = pfHierarchyInterface->GetSceneManager(); |
---|
| 33 | |
---|
[156] | 34 | // ---- Render scene with item buffer (i.e., objects with their id as color codes) |
---|
[153] | 35 | |
---|
| 36 | // const_cast allowed because camera is not changed in renderScene |
---|
| 37 | Camera *pCam = const_cast<Camera *>(&camera); |
---|
| 38 | |
---|
| 39 | // disable overlays, reset them later |
---|
| 40 | bool overlayEnabled = mViewport->getOverlaysEnabled(); |
---|
| 41 | mViewport->setOverlaysEnabled(false); |
---|
| 42 | |
---|
| 43 | // set item buffer (must be provided by scene manager) |
---|
| 44 | bool useItemBuffer = true; |
---|
| 45 | sm->setOption("UseItemBuffer", &useItemBuffer); |
---|
| 46 | |
---|
[154] | 47 | // clear background with black (i.e., not a valid item id) |
---|
[153] | 48 | ColourValue bg = mViewport->getBackgroundColour(); |
---|
| 49 | mViewport->setBackgroundColour(ColourValue(0, 0, 0, 0)); |
---|
| 50 | |
---|
[155] | 51 | |
---|
| 52 | // --- render item buffer |
---|
[153] | 53 | pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false); |
---|
[155] | 54 | |
---|
| 55 | |
---|
[153] | 56 | //---- collect results |
---|
| 57 | int dimx = 0; |
---|
| 58 | int dimy = 0; |
---|
| 59 | |
---|
| 60 | // get frame buffer |
---|
| 61 | uchar *buf = mViewport->getTarget()->getBufferContents(dimx, dimy); |
---|
| 62 | |
---|
[154] | 63 | |
---|
[153] | 64 | // loop through frame buffer & collect visible pixels |
---|
| 65 | for (int idx = 0; idx < dimy * dimx * 3; idx += 3) |
---|
| 66 | { |
---|
[154] | 67 | // -- decode color code to receive id |
---|
[153] | 68 | int id = buf[idx] << 16; |
---|
| 69 | id += buf[idx + 1] << 8; |
---|
| 70 | id += buf[idx + 2]; |
---|
| 71 | |
---|
[154] | 72 | // if valid id <= add visibility (id values start at 1 |
---|
| 73 | if ((id > 0) && (id < (int)visibleGeometry->size())) |
---|
[153] | 74 | { |
---|
| 75 | ((*visibleGeometry)[id]).AddVisibility(1, 1); |
---|
| 76 | } |
---|
| 77 | }
|
---|
| 78 |
|
---|
[158] | 79 | //-- reset options
|
---|
[156] | 80 |
|
---|
| 81 | // don't need item buffer anymore |
---|
| 82 | useItemBuffer = false; |
---|
[158] | 83 | sm->setOption("UseItemBuffer", &useItemBuffer);
|
---|
[153] | 84 | // reset initialised - flag
|
---|
| 85 | mWasInitialised = false;
|
---|
[156] | 86 | // reset old overlay status |
---|
| 87 | mViewport->setOverlaysEnabled(overlayEnabled); |
---|
[158] | 88 | // reset background color |
---|
[156] | 89 | mViewport->setBackgroundColour(bg);
|
---|
| 90 |
|
---|
[158] | 91 | // delete array storing the frame buffer
|
---|
[153] | 92 | delete [] buf;
|
---|
| 93 | } |
---|
| 94 | //----------------------------------------------------------------------- |
---|
| 95 | void ItemBufferQueryManager::InitItemBuffer(InfoContainer<GtpVisibility::NodeInfo> *visibleNodes, |
---|
| 96 | InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry) |
---|
| 97 | { |
---|
| 98 | if (mWasInitialised) |
---|
| 99 | return; |
---|
| 100 | |
---|
| 101 | mWasInitialised = true; |
---|
| 102 | |
---|
[155] | 103 | SceneManager *sm = |
---|
| 104 | dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface)->GetSceneManager(); |
---|
[153] | 105 | |
---|
[155] | 106 | SceneManager::EntityIterator it = sm->getEntityIterator(); |
---|
| 107 | |
---|
[153] | 108 | // TODO: make this more efficient |
---|
| 109 | visibleGeometry->clear(); |
---|
[154] | 110 | visibleNodes->clear(); |
---|
[153] | 111 | |
---|
| 112 | while (it.hasMoreElements()) |
---|
| 113 | { |
---|
| 114 | visibleGeometry->push_back(GtpVisibility::MeshInfo(it.getNext(), 0, 0)); |
---|
| 115 | } |
---|
| 116 | } |
---|
[155] | 117 | |
---|
[153] | 118 | } // namespace Ogre |
---|