1 | #include "SceneQuery.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 | #include "Vector3.h"
|
---|
4 | #include "Camera.h"
|
---|
5 | #include "SceneQuery.h"
|
---|
6 | #include "RenderTraverser.h"
|
---|
7 | #include "FrameBufferObject.h"
|
---|
8 | #include "RenderState.h"
|
---|
9 |
|
---|
10 | #include <IL/il.h>
|
---|
11 | #include <assert.h>
|
---|
12 |
|
---|
13 |
|
---|
14 | #ifdef _CRT_SET
|
---|
15 | #define _CRTDBG_MAP_ALLOC
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <crtdbg.h>
|
---|
18 |
|
---|
19 | // redefine new operator
|
---|
20 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
21 | #define new DEBUG_NEW
|
---|
22 | #endif
|
---|
23 |
|
---|
24 |
|
---|
25 | using namespace std;
|
---|
26 |
|
---|
27 | const static int texWidth = 2048;
|
---|
28 | const static int texHeight = 2048;
|
---|
29 |
|
---|
30 |
|
---|
31 | static void PrintGLerror(char *msg)
|
---|
32 | {
|
---|
33 | GLenum errCode;
|
---|
34 | const GLubyte *errStr;
|
---|
35 |
|
---|
36 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
37 | {
|
---|
38 | errStr = gluErrorString(errCode);
|
---|
39 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | static void startil()
|
---|
45 | {
|
---|
46 | ilInit();
|
---|
47 | assert(ilGetError() == IL_NO_ERROR);
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | static void stopil()
|
---|
52 | {
|
---|
53 | ilShutDown();
|
---|
54 | assert(ilGetError() == IL_NO_ERROR);
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | namespace CHCDemoEngine
|
---|
59 | {
|
---|
60 |
|
---|
61 |
|
---|
62 | static void GrabDepthBuffer(float *data, GLuint depthTexture)
|
---|
63 | {
|
---|
64 | glEnable(GL_TEXTURE_2D);
|
---|
65 | glBindTexture(GL_TEXTURE_2D, depthTexture);
|
---|
66 |
|
---|
67 | glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, data);
|
---|
68 |
|
---|
69 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
70 | glDisable(GL_TEXTURE_2D);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | static void ExportDepthBuffer(float *data)
|
---|
75 | {
|
---|
76 | startil();
|
---|
77 |
|
---|
78 | ILstring filename = ILstring("sceneQuery.tga");
|
---|
79 | ilRegisterType(IL_FLOAT);
|
---|
80 |
|
---|
81 | const int depth = 1;
|
---|
82 | const int bpp = 1;
|
---|
83 |
|
---|
84 | if (!ilTexImage(texWidth, texHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data))
|
---|
85 | {
|
---|
86 | cerr << "IL error " << ilGetError() << endl;
|
---|
87 | stopil();
|
---|
88 | return;
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (!ilSaveImage(filename))
|
---|
92 | {
|
---|
93 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
94 | }
|
---|
95 |
|
---|
96 | stopil();
|
---|
97 |
|
---|
98 | cout << "exported depth buffer" << endl;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox,
|
---|
103 | RenderTraverser *renderer, RenderState *state):
|
---|
104 | mSceneBox(sceneBox), mDepth(NULL), mRenderState(state)
|
---|
105 | {
|
---|
106 | Prepare(renderer);
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | bool SceneQuery::CalcIntersection(Vector3 &pt)
|
---|
111 | {
|
---|
112 | const int px = (mSceneBox.Max(0) - pt.x) * (texWidth - 1) / mSceneBox.Size(0);
|
---|
113 | const int py = (mSceneBox.Max(1) - pt.y) * (texHeight - 1) / mSceneBox.Size(1);
|
---|
114 |
|
---|
115 | const float d = mDepth[px + py * texHeight];
|
---|
116 |
|
---|
117 | static float depth = d;
|
---|
118 |
|
---|
119 | if (d > .0f)
|
---|
120 | {
|
---|
121 | // temporal smoothing of depth values
|
---|
122 | const float x = .5f;
|
---|
123 | depth = d * x + depth * (1.0f - x);
|
---|
124 |
|
---|
125 | const float offs = mSceneBox.Size(2) * 5e-2f;
|
---|
126 | pt.z = mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
|
---|
127 |
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | void SceneQuery::Prepare(RenderTraverser *renderer)
|
---|
136 | {
|
---|
137 | cout << "Preparing scene queries" << endl;
|
---|
138 |
|
---|
139 | const float xlen = mSceneBox.Size().x * .5f;
|
---|
140 | const float ylen = mSceneBox.Size().y * .5f;
|
---|
141 |
|
---|
142 | PerspectiveCamera *orthoCam = new PerspectiveCamera(xlen / ylen);
|
---|
143 | //orthoCam->SetOrtho(true);
|
---|
144 |
|
---|
145 | orthoCam->SetNear(0.0f);
|
---|
146 | orthoCam->Yaw(.5 * M_PI);
|
---|
147 | orthoCam->SetDirection(Vector3(0, 0, -1));
|
---|
148 |
|
---|
149 | Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
|
---|
150 | orthoCam->SetPosition(pos);
|
---|
151 |
|
---|
152 | FrameBufferObject *fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32, true);
|
---|
153 | fbo->AddColorBuffer(ColorBufferObject::RGBA_UBYTE,
|
---|
154 | ColorBufferObject::WRAP_CLAMP_TO_EDGE,
|
---|
155 | ColorBufferObject::FILTER_NEAREST,
|
---|
156 | ColorBufferObject::FILTER_NEAREST);
|
---|
157 |
|
---|
158 |
|
---|
159 | fbo->Bind();
|
---|
160 |
|
---|
161 | glDrawBuffers(1, mrt);
|
---|
162 |
|
---|
163 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
164 | glViewport(0, 0, texWidth, texHeight);
|
---|
165 |
|
---|
166 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
167 |
|
---|
168 | // hack: disable cull face because of alpha textured balconies
|
---|
169 | glDisable(GL_CULL_FACE);
|
---|
170 | mRenderState->LockCullFaceEnabled(true);
|
---|
171 |
|
---|
172 | //glDisable(GL_CULL_FACE);
|
---|
173 | //glEnable(GL_DEPTH_TEST);
|
---|
174 |
|
---|
175 | glMatrixMode(GL_MODELVIEW);
|
---|
176 | glPushMatrix();
|
---|
177 | glLoadIdentity();
|
---|
178 |
|
---|
179 | glMatrixMode(GL_PROJECTION);
|
---|
180 | glPushMatrix();
|
---|
181 | glLoadIdentity();
|
---|
182 |
|
---|
183 | glOrtho(xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
|
---|
184 | //glOrtho(xlen, .0f, ylen, .0f, .0f, mSceneBox.Size().z);
|
---|
185 |
|
---|
186 | glMatrixMode(GL_MODELVIEW);
|
---|
187 |
|
---|
188 | orthoCam->SetupCameraView();
|
---|
189 |
|
---|
190 | mDepth = new float[texHeight * texWidth];
|
---|
191 |
|
---|
192 | Camera *oldCam = renderer->GetCamera();
|
---|
193 | renderer->SetCamera(orthoCam);
|
---|
194 |
|
---|
195 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
196 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
197 |
|
---|
198 | // forward rendering
|
---|
199 | mRenderState->SetRenderTechnique(0);
|
---|
200 |
|
---|
201 | renderer->RenderScene();
|
---|
202 |
|
---|
203 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
204 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
205 |
|
---|
206 | renderer->SetCamera(oldCam);
|
---|
207 |
|
---|
208 | mRenderState->LockCullFaceEnabled(false);
|
---|
209 | //glEnable(GL_CULL_FACE);
|
---|
210 |
|
---|
211 | glMatrixMode(GL_MODELVIEW);
|
---|
212 | glPopMatrix();
|
---|
213 |
|
---|
214 | glMatrixMode(GL_PROJECTION);
|
---|
215 | glPopMatrix();
|
---|
216 |
|
---|
217 | glPopAttrib();
|
---|
218 |
|
---|
219 | FrameBufferObject::Release();
|
---|
220 |
|
---|
221 | GrabDepthBuffer(mDepth, fbo->GetDepthTex());
|
---|
222 | ExportDepthBuffer(mDepth); PrintGLerror("grab depth");
|
---|
223 |
|
---|
224 | DEL_PTR(fbo);
|
---|
225 | DEL_PTR(orthoCam);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 |
|
---|
230 |
|
---|
231 | } |
---|