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