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, RenderState *state):
|
---|
108 | mSceneBox(sceneBox), mDepth(NULL), mRenderState(state)
|
---|
109 | {
|
---|
110 | Prepare(renderer);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | bool SceneQuery::CalcIntersection(Vector3 &pt)
|
---|
115 | {
|
---|
116 | const int px = (mSceneBox.Max(0) - pt.x) * (texWidth - 1) / mSceneBox.Size(0);
|
---|
117 | const int py = (mSceneBox.Max(1) - pt.y) * (texHeight - 1) / mSceneBox.Size(1);
|
---|
118 |
|
---|
119 | const float d = mDepth[px + py * texHeight];
|
---|
120 |
|
---|
121 | static float depth = d;
|
---|
122 |
|
---|
123 | if (d > .0f)
|
---|
124 | {
|
---|
125 | // temporal smoothing of depth values
|
---|
126 | const float x = .5f;
|
---|
127 | depth = d * x + depth * (1.0f - x);
|
---|
128 |
|
---|
129 | const float offs = mSceneBox.Size(2) * 5e-2f;
|
---|
130 | pt.z = mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
|
---|
131 |
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 |
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | void SceneQuery::Prepare(RenderTraverser *renderer)
|
---|
140 | {
|
---|
141 | cout << "Preparing scene queries" << endl;
|
---|
142 |
|
---|
143 | const float xlen = mSceneBox.Size().x * .5f;
|
---|
144 | const float ylen = mSceneBox.Size().y * .5f;
|
---|
145 |
|
---|
146 | Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
|
---|
147 |
|
---|
148 | FrameBufferObject *fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32, true);
|
---|
149 | fbo->AddColorBuffer(ColorBufferObject::RGBA_UBYTE,
|
---|
150 | ColorBufferObject::WRAP_CLAMP_TO_EDGE,
|
---|
151 | ColorBufferObject::FILTER_NEAREST,
|
---|
152 | ColorBufferObject::FILTER_NEAREST);
|
---|
153 |
|
---|
154 |
|
---|
155 | fbo->Bind();
|
---|
156 |
|
---|
157 | glDrawBuffers(1, mrt);
|
---|
158 |
|
---|
159 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
160 | glViewport(0, 0, texWidth, texHeight);
|
---|
161 |
|
---|
162 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
163 |
|
---|
164 | glMatrixMode(GL_MODELVIEW);
|
---|
165 | glPushMatrix();
|
---|
166 | glLoadIdentity();
|
---|
167 |
|
---|
168 | glMatrixMode(GL_PROJECTION);
|
---|
169 | glPushMatrix();
|
---|
170 | glLoadIdentity();
|
---|
171 |
|
---|
172 | OrthoCamera *orthoCam = new OrthoCamera(xlen, -xlen, ylen, -ylen, .0f, mSceneBox.Size().z);
|
---|
173 |
|
---|
174 | orthoCam->SetDirection(Vector3(0, 0, -1));
|
---|
175 | orthoCam->SetPosition(pos);
|
---|
176 |
|
---|
177 | orthoCam->SetupViewProjection();
|
---|
178 |
|
---|
179 | //glOrtho(xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
|
---|
180 | glMatrixMode(GL_MODELVIEW);
|
---|
181 | //orthoCam->SetupCameraView();
|
---|
182 |
|
---|
183 | mDepth = new float[texHeight * texWidth];
|
---|
184 |
|
---|
185 | Camera *oldCam = renderer->GetCamera();
|
---|
186 | renderer->SetCamera(orthoCam);
|
---|
187 |
|
---|
188 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
189 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
190 |
|
---|
191 | // hack: use highest lod level for trees (billboards)
|
---|
192 | LODLevel::InitFrame(Vector3(1e6f));
|
---|
193 |
|
---|
194 | // forward rendering
|
---|
195 | mRenderState->SetRenderTechnique(0);
|
---|
196 | // temporarilly switch off
|
---|
197 | renderer->SetRenderDynamicObjects(false);
|
---|
198 |
|
---|
199 | renderer->RenderScene();
|
---|
200 |
|
---|
201 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
202 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
203 |
|
---|
204 | renderer->SetCamera(oldCam);
|
---|
205 | // temporarilly switch off
|
---|
206 | renderer->SetRenderDynamicObjects(true);
|
---|
207 |
|
---|
208 | glMatrixMode(GL_MODELVIEW);
|
---|
209 | glPopMatrix();
|
---|
210 |
|
---|
211 | glMatrixMode(GL_PROJECTION);
|
---|
212 | glPopMatrix();
|
---|
213 |
|
---|
214 | glPopAttrib();
|
---|
215 |
|
---|
216 | FrameBufferObject::Release();
|
---|
217 |
|
---|
218 | GrabDepthBuffer(mDepth, fbo->GetDepthTex());
|
---|
219 | ExportDepthBuffer(mDepth); PrintGLerror("grab depth");
|
---|
220 |
|
---|
221 | DEL_PTR(fbo);
|
---|
222 | DEL_PTR(orthoCam);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 |
|
---|
227 |
|
---|
228 | } |
---|