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