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): |
---|
10 | PlatformQueryManager(hierarchyInterface, vp) |
---|
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 | { |
---|
25 | // initialise item buffer (if not already initialised) |
---|
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 | |
---|
34 | // ---- Render scene with item buffer (i.e., objects with their id as color codes) |
---|
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 | |
---|
47 | // clear background with black (i.e., not a valid item id) |
---|
48 | ColourValue bg = mViewport->getBackgroundColour(); |
---|
49 | mViewport->setBackgroundColour(ColourValue(0, 0, 0, 0)); |
---|
50 | |
---|
51 | |
---|
52 | // --- render item buffer |
---|
53 | pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false); |
---|
54 | |
---|
55 | |
---|
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 | |
---|
63 | |
---|
64 | // loop through frame buffer & collect visible pixels |
---|
65 | for (int idx = 0; idx < dimy * dimx * 3; idx += 3) |
---|
66 | { |
---|
67 | // -- decode color code to receive id |
---|
68 | int id = buf[idx] << 16; |
---|
69 | id += buf[idx + 1] << 8; |
---|
70 | id += buf[idx + 2]; |
---|
71 | |
---|
72 | // if valid id <= add visibility (id values start at 1 |
---|
73 | if ((id > 0) && (id < (int)visibleGeometry->size())) |
---|
74 | { |
---|
75 | ((*visibleGeometry)[id]).AddVisibility(1, 1); |
---|
76 | } |
---|
77 | }
|
---|
78 |
|
---|
79 | //-- reset options
|
---|
80 |
|
---|
81 | // don't need item buffer anymore |
---|
82 | useItemBuffer = false; |
---|
83 | sm->setOption("UseItemBuffer", &useItemBuffer);
|
---|
84 | // reset initialised - flag
|
---|
85 | mWasInitialised = false;
|
---|
86 | // reset old overlay status |
---|
87 | mViewport->setOverlaysEnabled(overlayEnabled); |
---|
88 | // reset background color |
---|
89 | mViewport->setBackgroundColour(bg);
|
---|
90 |
|
---|
91 | // delete array storing the frame buffer
|
---|
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 | |
---|
103 | SceneManager *sm = |
---|
104 | dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface)->GetSceneManager(); |
---|
105 | |
---|
106 | SceneManager::EntityIterator it = sm->getEntityIterator(); |
---|
107 | |
---|
108 | // TODO: make this more efficient |
---|
109 | visibleGeometry->clear(); |
---|
110 | visibleNodes->clear(); |
---|
111 | |
---|
112 | while (it.hasMoreElements()) |
---|
113 | { |
---|
114 | visibleGeometry->push_back(GtpVisibility::MeshInfo(it.getNext(), 0, 0)); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | } // namespace Ogre |
---|