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