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 "RenderTexture.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | static int texWidth = 2048;
|
---|
15 | static int texHeight = 2048;
|
---|
16 |
|
---|
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 | void startil()
|
---|
33 | {
|
---|
34 | ilInit();
|
---|
35 | assert(ilGetError() == IL_NO_ERROR);
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | void stopil()
|
---|
40 | {
|
---|
41 | ilShutDown();
|
---|
42 | assert(ilGetError() == IL_NO_ERROR);
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | namespace CHCDemoEngine
|
---|
47 | {
|
---|
48 |
|
---|
49 |
|
---|
50 | void GrabDepthBuffer(float *data, RenderTexture *rt)
|
---|
51 | {
|
---|
52 | rt->BindDepth();
|
---|
53 | rt->EnableTextureTarget();
|
---|
54 |
|
---|
55 | const int texFormat = GL_DEPTH_COMPONENT;
|
---|
56 | glGetTexImage(rt->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
|
---|
57 |
|
---|
58 | rt->DisableTextureTarget();
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | 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, RenderTraverser *renderer):
|
---|
91 | mSceneBox(sceneBox), mDepth(NULL)
|
---|
92 | {
|
---|
93 | Prepare(renderer);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | bool SceneQuery::CalcIntersection(Vector3 &pt)
|
---|
98 | {
|
---|
99 | const int px = (pt.x - mSceneBox.Min(0)) * (texWidth - 1) / mSceneBox.Size(0);
|
---|
100 | const int py = (pt.y - mSceneBox.Min(1)) * (texHeight - 1) / mSceneBox.Size(1);
|
---|
101 |
|
---|
102 | float d = mDepth[px + py * texHeight];
|
---|
103 |
|
---|
104 | static float depth = d;
|
---|
105 |
|
---|
106 | if (d > 0)
|
---|
107 | {
|
---|
108 | // temporal smoothing of depth values
|
---|
109 | const float x = 0.5f;
|
---|
110 | depth = d * x + depth * (1.0f - x);
|
---|
111 |
|
---|
112 | const float offs = mSceneBox.Size(2) * 5e-2f;
|
---|
113 | pt.z = mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
|
---|
114 |
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | void SceneQuery::Prepare(RenderTraverser *renderer)
|
---|
123 | {
|
---|
124 | cout << "Preparing scene queries" << endl;
|
---|
125 |
|
---|
126 | const float xlen = mSceneBox.Size().x * 0.5f;
|
---|
127 | const float ylen = mSceneBox.Size().y * 0.5f;
|
---|
128 |
|
---|
129 | Camera *orthoCam = new Camera(xlen, ylen);
|
---|
130 | orthoCam->SetOrtho(true);
|
---|
131 |
|
---|
132 | orthoCam->SetNear(0.0f);
|
---|
133 | orthoCam->Yaw(M_PI * 0.5f);
|
---|
134 |
|
---|
135 | Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
|
---|
136 | orthoCam->SetPosition(pos);
|
---|
137 |
|
---|
138 | RenderTexture *depthTexture = new RenderTexture(texWidth, texHeight, true, true);
|
---|
139 |
|
---|
140 | #ifdef ATI
|
---|
141 | depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
142 | #else
|
---|
143 | depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | PrintGLerror("Init");
|
---|
147 |
|
---|
148 | depthTexture->BeginCapture();
|
---|
149 | {
|
---|
150 | glViewport(0, 0, texWidth, texHeight);
|
---|
151 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
152 |
|
---|
153 | glClearColor(1, 1, 1, 1);
|
---|
154 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
155 |
|
---|
156 | glFrontFace(GL_CCW);
|
---|
157 | glCullFace(GL_BACK);
|
---|
158 |
|
---|
159 | glEnable(GL_CULL_FACE);
|
---|
160 |
|
---|
161 | glShadeModel(GL_FLAT);
|
---|
162 | glEnable(GL_DEPTH_TEST);
|
---|
163 |
|
---|
164 | glMatrixMode(GL_PROJECTION);
|
---|
165 | glPushMatrix();
|
---|
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->SetCamera(orthoCam);
|
---|
177 | renderer->RenderScene();
|
---|
178 |
|
---|
179 | glPopMatrix();
|
---|
180 | glPopMatrix();
|
---|
181 |
|
---|
182 | glPopAttrib();
|
---|
183 | }
|
---|
184 | depthTexture->EndCapture();
|
---|
185 |
|
---|
186 | GrabDepthBuffer(mDepth, depthTexture);
|
---|
187 | //ExportDepthBuffer(mDepth);
|
---|
188 | //PrintGLerror("grab");
|
---|
189 |
|
---|
190 | DEL_PTR(depthTexture);
|
---|
191 | DEL_PTR(orthoCam);
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 |
|
---|
196 |
|
---|
197 | } |
---|