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