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