source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp @ 3246

Revision 3246, 5.6 KB checked in by mattausch, 15 years ago (diff)
Line 
1#include "Visualization.h"
2#include "glInterface.h"
3#include "Camera.h"
4#include "SceneEntity.h"
5#include "Bvh.h"
6#include "RenderState.h"
7#include "ShadowMapping.h"
8#include "ViewCellsTree.h"
9#include "Material.h"
10
11
12#include <stack>
13
14
15using namespace std;
16
17
18namespace CHCDemoEngine
19{
20
21
22void Visualization::RenderBoxForViz(const AxisAlignedBox3 &box)
23{
24        glBegin(GL_LINE_LOOP);
25        glVertex3d(box.Min().x, box.Max().y, box.Min().z);
26        glVertex3d(box.Max().x, box.Max().y, box.Min().z);
27        glVertex3d(box.Max().x, box.Min().y, box.Min().z);
28        glVertex3d(box.Min().x, box.Min().y, box.Min().z);
29        glEnd();
30
31        glBegin(GL_LINE_LOOP);
32        glVertex3d(box.Min().x, box.Min().y, box.Max().z);
33        glVertex3d(box.Max().x, box.Min().y, box.Max().z);
34        glVertex3d(box.Max().x, box.Max().y, box.Max().z);
35        glVertex3d(box.Min().x, box.Max().y, box.Max().z);
36        glEnd();
37
38        glBegin(GL_LINE_LOOP);
39        glVertex3d(box.Max().x, box.Min().y, box.Min().z);
40        glVertex3d(box.Max().x, box.Min().y, box.Max().z);
41        glVertex3d(box.Max().x, box.Max().y, box.Max().z);
42        glVertex3d(box.Max().x, box.Max().y, box.Min().z);
43        glEnd();
44
45        glBegin(GL_LINE_LOOP);
46        glVertex3d(box.Min().x, box.Min().y, box.Min().z);
47        glVertex3d(box.Min().x, box.Min().y, box.Max().z);
48        glVertex3d(box.Min().x, box.Max().y, box.Max().z);
49        glVertex3d(box.Min().x, box.Max().y, box.Min().z);
50        glEnd();
51
52        glBegin(GL_LINE_LOOP);
53        glVertex3d(box.Min().x, box.Min().y, box.Min().z);
54        glVertex3d(box.Max().x, box.Min().y, box.Min().z);
55        glVertex3d(box.Max().x, box.Min().y, box.Max().z);
56        glVertex3d(box.Min().x, box.Min().y, box.Max().z);
57        glEnd();
58
59        glBegin(GL_LINE_LOOP);
60        glVertex3d(box.Min().x, box.Max().y, box.Min().z);
61        glVertex3d(box.Max().x, box.Max().y, box.Min().z);
62        glVertex3d(box.Max().x, box.Max().y, box.Max().z);
63        glVertex3d(box.Min().x, box.Max().y, box.Max().z);
64
65        glEnd();
66}
67
68
69/******************************************************/
70/*           Vizualization implementation             */
71/******************************************************/
72
73
74Visualization::Visualization(Bvh *bvh,
75                                                         PerspectiveCamera *camera,
76                                                         Camera *vizCamera,
77                                                         RenderState *renderState):
78mBvh(bvh),
79mCamera(camera),
80mVizCamera(vizCamera),
81mRenderState(renderState),
82mFrameId(0),
83mViewCell(NULL)
84{
85}
86
87
88Visualization::~Visualization()
89{
90}
91
92
93void Visualization::SetHierarchy(Bvh *bvh)
94{
95        mBvh = bvh;
96}
97
98
99void Visualization::SetRenderState(RenderState *state)
100{
101        mRenderState = state;
102}
103
104
105void Visualization::SetFrameId(int frameId)
106{
107        mFrameId = frameId;
108}
109
110
111void Visualization::SetViewCell(ViewCell *vc)
112{
113        mViewCell = vc;
114}
115
116
117static Technique GetVizTechnique()
118{
119        Technique tech;
120        tech.Init();
121
122        //tech.SetLightingEnabled(false);
123        //tech.SetDepthWriteEnabled(false);
124
125        tech.SetEmmisive(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
126        tech.SetDiffuse(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
127        tech.SetAmbient(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
128
129        return tech;
130}
131
132
133void Visualization::Render(bool showShadowFrustra)
134{
135        stack<BvhNode *> tStack;
136        tStack.push(mBvh->GetRoot());
137
138        glEnableClientState(GL_VERTEX_ARRAY);
139        glEnableClientState(GL_NORMAL_ARRAY);
140
141        while (!tStack.empty())
142        {
143                BvhNode *node = tStack.top();
144                tStack.pop();
145
146                if (!node->IsVirtualLeaf())
147                {
148                        BvhInterior *interior = static_cast<BvhInterior *>(node);
149
150                        tStack.push(interior->GetFront());
151                        tStack.push(interior->GetBack());
152                }
153                else
154                {
155                        if (node->GetLastRenderedFrame() == mFrameId)
156                        {
157                                int geometrySize;
158                                SceneEntity **entities = mBvh->GetGeometry(node, geometrySize);
159
160                                for (int i = 0; i < geometrySize; ++ i)
161                                {
162                                        SceneEntity *ent = entities[i];
163                                       
164                                        if (ent->IsVisible())
165                                                ent->Render(mRenderState);
166                                }               
167                        }
168                }
169        }
170
171        // render current view cell
172        static Technique vcTechnique = GetVizTechnique();
173
174        if (mViewCell)
175        {
176                vcTechnique.Render(mRenderState);
177                RenderBoxForViz(mViewCell->GetBox());
178        }
179
180        glDisableClientState(GL_VERTEX_ARRAY);
181        glDisableClientState(GL_NORMAL_ARRAY);
182
183
184        mRenderState->Reset();
185
186        glPushAttrib(GL_CURRENT_BIT);
187        glDisable(GL_LIGHTING);
188        glDisable(GL_DEPTH_TEST);
189        glDepthMask(GL_FALSE);
190
191        RenderFrustum();
192
193        if (showShadowFrustra)
194                ShadowMap::VisualizeFrustra();
195
196        Vector3 pos = mCamera->GetPosition();
197
198        // coordinates
199        glColor3f(0.0f, 1.0f, 0.0f);
200        glBegin(GL_LINES);
201        glVertex3d(pos.x, pos.y, pos.z);
202        glVertex3d(pos.x + 100, pos.y, pos.z);
203        glEnd();
204
205        glColor3f(0.0f, 0.0f, 1.0f);
206        glBegin(GL_LINES);
207        glVertex3d(pos.x, pos.y, pos.z);
208        glVertex3d(pos.x, pos.y + 100, pos.z);
209        glEnd();
210
211        //RenderBoxForViz(mBvh->GetBox());
212
213        glDepthMask(GL_TRUE);
214        glPopAttrib();
215}
216
217
218void Visualization::RenderFrustum()
219{
220        glColor3f(1.0f, 0.0f, 0.0f);
221
222        Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
223        mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
224
225        glLineWidth(2);
226
227        glBegin(GL_LINE_LOOP);
228        glVertex3d(fbl.x, fbl.y, fbl.z);
229        glVertex3d(ftl.x, ftl.y, ftl.z);
230        glVertex3d(ntl.x, ntl.y, ntl.z);
231        glVertex3d(nbl.x, nbl.y, nbl.z);
232        glEnd();
233
234        glBegin(GL_LINE_LOOP);
235        glVertex3d(fbr.x, fbr.y, fbr.z);
236        glVertex3d(ftr.x, ftr.y, ftr.z);
237        glVertex3d(ntr.x, ntr.y, ntr.z);
238        glVertex3d(nbr.x, nbr.y, nbr.z);
239        glEnd();
240
241        glBegin(GL_LINE_LOOP);
242        glVertex3d(fbr.x, fbr.y, fbr.z);
243        glVertex3d(fbl.x, fbl.y, fbl.z);
244        glVertex3d(nbl.x, nbl.y, nbl.z);
245        glVertex3d(nbr.x, nbr.y, nbr.z);
246        glEnd();
247
248        glBegin(GL_LINE_LOOP);
249        glVertex3d(ftr.x, ftr.y, ftr.z);
250        glVertex3d(ftl.x, ftl.y, ftl.z);
251        glVertex3d(ntl.x, ntl.y, ntl.z);
252        glVertex3d(ntr.x, ntr.y, ntr.z);
253        glEnd();
254}
255
256
257}
Note: See TracBrowser for help on using the repository browser.