1 | #include <algorithm>
|
---|
2 |
|
---|
3 | // the devil library
|
---|
4 | #include <IL/il.h>
|
---|
5 | #include <IL/ilu.h>
|
---|
6 | #include <IL/ilut.h>
|
---|
7 | #include <cassert>
|
---|
8 |
|
---|
9 | #include "Camera.h"
|
---|
10 | #include "Ray.h"
|
---|
11 | #include "SimpleRay.h"
|
---|
12 | #include "KdTree.h"
|
---|
13 | #include "Mesh.h"
|
---|
14 | #include "Exporter.h"
|
---|
15 | #include "SceneGraph.h"
|
---|
16 | #include "Preprocessor.h"
|
---|
17 | #include "RayCaster.h"
|
---|
18 |
|
---|
19 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
20 | //#include "timer.h"
|
---|
21 | #include "raypack.h"
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | using namespace std;
|
---|
25 |
|
---|
26 | namespace GtpVisibilityPreprocessor {
|
---|
27 |
|
---|
28 |
|
---|
29 | void
|
---|
30 | InitDevIl()
|
---|
31 | {
|
---|
32 | ilInit();
|
---|
33 | ILuint ImageName;
|
---|
34 | ilGenImages(1, &ImageName);
|
---|
35 | ilBindImage(ImageName);
|
---|
36 | ilEnable(IL_FILE_OVERWRITE);
|
---|
37 |
|
---|
38 | // ilRegisterFormat(IL_RGBA);
|
---|
39 | // ilRegisterType(IL_FLOAT);
|
---|
40 |
|
---|
41 | // ilEnable(IL_ORIGIN_SET);
|
---|
42 | // ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
|
---|
43 | }
|
---|
44 |
|
---|
45 | bool
|
---|
46 | Camera::SnapImage(string filename,
|
---|
47 | KdTree *tree,
|
---|
48 | SceneGraph *sceneGraph
|
---|
49 | )
|
---|
50 | {
|
---|
51 | int x;
|
---|
52 | int y;
|
---|
53 |
|
---|
54 | bool exportRays = false;
|
---|
55 |
|
---|
56 |
|
---|
57 | InitDevIl();
|
---|
58 | int components = 4;
|
---|
59 | float *buffer = new float[components*mWidth*mHeight];
|
---|
60 | float *pbuffer = buffer;
|
---|
61 | // - components*mWidth;
|
---|
62 |
|
---|
63 | vector<Ray *> rays;
|
---|
64 |
|
---|
65 | long t1 = GetTime();
|
---|
66 |
|
---|
67 | Ray ray;
|
---|
68 |
|
---|
69 | for (y = 0; y < mHeight; y++) {
|
---|
70 | cout<<"+";
|
---|
71 | for (x = 0; x < mWidth; x++) {
|
---|
72 | SetupRay(ray, mWidth - (x + 1), mHeight - (y + 1));
|
---|
73 |
|
---|
74 | bool debug = true;
|
---|
75 | // (y == mHeight/2) && (x== mWidth/3);
|
---|
76 | // MeshDebug = debug;
|
---|
77 |
|
---|
78 | if (debug)
|
---|
79 | ray.mFlags = (Ray::STORE_TESTED_OBJECTS | Ray::STORE_KDLEAVES);
|
---|
80 | else
|
---|
81 | ray.mFlags &= ~(Ray::STORE_TESTED_OBJECTS|Ray::STORE_KDLEAVES);
|
---|
82 |
|
---|
83 | if (tree->CastRay(ray)) {
|
---|
84 | // cout<<"I1";
|
---|
85 |
|
---|
86 | if (ray.intersections.size()) {
|
---|
87 | sort(ray.intersections.begin(), ray.intersections.end());
|
---|
88 | MeshInstance *mesh = (MeshInstance*)ray.intersections[0].mObject;
|
---|
89 | RgbColor color(1,1,1);
|
---|
90 | if (mesh->GetMesh()->mMaterial)
|
---|
91 | color = mesh->GetMesh()->mMaterial->mDiffuseColor;
|
---|
92 |
|
---|
93 | pbuffer[0] = color.r;
|
---|
94 | pbuffer[1] = color.g;
|
---|
95 | pbuffer[2] = color.b;
|
---|
96 | pbuffer[3] = 1.0f;
|
---|
97 |
|
---|
98 | }
|
---|
99 | }
|
---|
100 | pbuffer+=components;
|
---|
101 |
|
---|
102 | // if (exportRays && ( 1||(x==222) && (y==97))) {
|
---|
103 | // if (exportRays && ((x%4==0) && (y%4==0))) {
|
---|
104 |
|
---|
105 | // if (exportRays && ray.intersections.empty()) {
|
---|
106 | if (debug) {
|
---|
107 | Ray *nray = new Ray(ray);
|
---|
108 | rays.push_back(nray);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | // pbuffer-=2*components*mWidth;
|
---|
112 | }
|
---|
113 |
|
---|
114 | long t2 = GetTime();
|
---|
115 | cout<<"#RAY_CAST_TIME\n";
|
---|
116 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
117 |
|
---|
118 |
|
---|
119 | cout<<"Saving image"<<endl;
|
---|
120 |
|
---|
121 | ilRegisterType(IL_FLOAT);
|
---|
122 | ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
123 | ilSaveImage((char *const)filename.c_str());
|
---|
124 | delete buffer;
|
---|
125 |
|
---|
126 | cout<<"done."<<endl<<flush;
|
---|
127 |
|
---|
128 | Exporter *exporter = NULL;
|
---|
129 | if (1) {
|
---|
130 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
131 | //exporter->SetFilled();
|
---|
132 |
|
---|
133 | exporter->SetWireframe();
|
---|
134 |
|
---|
135 | if (sceneGraph)
|
---|
136 | exporter->ExportScene(sceneGraph->GetRoot());
|
---|
137 |
|
---|
138 | //exporter->ExportKdTree(*tree);
|
---|
139 | //exporter->ExportBspTree(*bsptree);
|
---|
140 | exporter->ExportRays(rays, 2000);
|
---|
141 | int k =0;
|
---|
142 | for (int j=0; j < rays.size(); j++)
|
---|
143 | if (rays[j]->kdLeaves.size()) {
|
---|
144 | Ray *ray = rays[j];
|
---|
145 | int i;
|
---|
146 | exporter->SetWireframe();
|
---|
147 |
|
---|
148 | if (1)
|
---|
149 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
150 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
151 |
|
---|
152 | exporter->SetFilled();
|
---|
153 |
|
---|
154 | if (1)
|
---|
155 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
156 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
157 | }
|
---|
158 |
|
---|
159 | delete exporter;
|
---|
160 | }
|
---|
161 |
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | void
|
---|
167 | Camera::SetupRay(Ray &ray, const int x, const int y)
|
---|
168 | {
|
---|
169 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
170 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
171 | Vector3 target = xv + yv + mDirection;
|
---|
172 |
|
---|
173 | ray.Clear();
|
---|
174 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
175 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | void
|
---|
180 | Camera::SetupRay(SimpleRay &ray, const int x, const int y)
|
---|
181 | {
|
---|
182 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
183 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
184 | Vector3 target = xv + yv + mDirection;
|
---|
185 | Vector3 dir = target - mPosition;
|
---|
186 | dir.Normalize();
|
---|
187 |
|
---|
188 | ray.Set(mPosition, dir, 0,
|
---|
189 | 1.0f, ~(SimpleRay::F_BIDIRECTIONAL));
|
---|
190 | }
|
---|
191 |
|
---|
192 | bool
|
---|
193 | Camera::SnapImage(string filename,
|
---|
194 | RayCaster *raycaster,
|
---|
195 | AxisAlignedBox3 &bbox,
|
---|
196 | SceneGraph *sceneGraph
|
---|
197 | )
|
---|
198 | {
|
---|
199 | int x;
|
---|
200 | int y;
|
---|
201 |
|
---|
202 | bool exportRays = false;
|
---|
203 |
|
---|
204 |
|
---|
205 | InitDevIl();
|
---|
206 | int components = 4;
|
---|
207 | float *buffer = new float[components*mWidth*mHeight];
|
---|
208 | assert(buffer);
|
---|
209 | float *pbuffer = buffer;
|
---|
210 | // - components*mWidth;
|
---|
211 |
|
---|
212 | vector<Ray *> rays;
|
---|
213 |
|
---|
214 | long t1 = GetTime();
|
---|
215 |
|
---|
216 | SimpleRay ray;
|
---|
217 | VssRayContainer vssRays;
|
---|
218 |
|
---|
219 | //CTimer timer;
|
---|
220 | //timer.Start();
|
---|
221 |
|
---|
222 | for (y = 0; y < mHeight; y++) {
|
---|
223 | cout<<"+";
|
---|
224 | for (x = 0; x < mWidth; x++) {
|
---|
225 | SetupRay(ray, mWidth - (x + 1), mHeight - (y + 1));
|
---|
226 |
|
---|
227 | bool debug = true;
|
---|
228 | // (y == mHeight/2) && (x== mWidth/3);
|
---|
229 | // MeshDebug = debug;
|
---|
230 |
|
---|
231 |
|
---|
232 | int res = raycaster->CastRay(ray,
|
---|
233 | vssRays,
|
---|
234 | bbox,
|
---|
235 | false, // castDoubleRay,
|
---|
236 | false); // pruneInvalidRays
|
---|
237 |
|
---|
238 | if (res) {
|
---|
239 | Vector3 normal = raycaster->intersect.mNormal;
|
---|
240 | float v =
|
---|
241 | ray.mDirection.x * normal.x +
|
---|
242 | ray.mDirection.y * normal.y +
|
---|
243 | ray.mDirection.z * normal.z;
|
---|
244 | v *= 1.0f;
|
---|
245 | pbuffer[0] = v;
|
---|
246 | pbuffer[1] = v;
|
---|
247 | pbuffer[2] = v;
|
---|
248 | pbuffer[3] = 1.0f;
|
---|
249 | }
|
---|
250 | else {
|
---|
251 | pbuffer[0] = 0.0;
|
---|
252 | pbuffer[1] = 0.0;
|
---|
253 | pbuffer[2] = 0.0;
|
---|
254 | pbuffer[3] = 1.0f;
|
---|
255 | }
|
---|
256 | pbuffer+=components;
|
---|
257 |
|
---|
258 | if (debug) {
|
---|
259 | Ray *nray = new Ray(ray.mOrigin, ray.mDirection,
|
---|
260 | Ray::LOCAL_RAY);
|
---|
261 | rays.push_back(nray);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | // pbuffer-=2*components*mWidth;
|
---|
265 | } // for y
|
---|
266 |
|
---|
267 | //timer.Stop();
|
---|
268 |
|
---|
269 | long t2 = GetTime();
|
---|
270 | cout<<"\n#RAY_CAST_TIME = ";
|
---|
271 | cout << TimeDiff(t1, t2)<<" [mikrosec]\n";
|
---|
272 |
|
---|
273 | cout<<"Saving image"<<endl;
|
---|
274 |
|
---|
275 | ilRegisterType(IL_FLOAT);
|
---|
276 | ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
277 | ilSaveImage((char *const)filename.c_str());
|
---|
278 | delete buffer;
|
---|
279 |
|
---|
280 | cout<<"done."<<endl<<flush;
|
---|
281 |
|
---|
282 | return true;
|
---|
283 | }
|
---|
284 |
|
---|
285 | bool
|
---|
286 | Camera::SnapImagePacket(string filename,
|
---|
287 | RayCaster *raycaster,
|
---|
288 | AxisAlignedBox3 &bbox,
|
---|
289 | SceneGraph *sceneGraph
|
---|
290 | )
|
---|
291 | {
|
---|
292 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
293 |
|
---|
294 | int x;
|
---|
295 | int y;
|
---|
296 |
|
---|
297 | bool exportRays = false;
|
---|
298 |
|
---|
299 |
|
---|
300 | InitDevIl();
|
---|
301 | int components = 4;
|
---|
302 | float *buffer = new float[components*mWidth*mHeight];
|
---|
303 | assert(buffer);
|
---|
304 | float *pbuffer = buffer;
|
---|
305 | // - components*mWidth;
|
---|
306 |
|
---|
307 | vector<Ray *> rays;
|
---|
308 |
|
---|
309 | long t1 = GetTime();
|
---|
310 |
|
---|
311 | SimpleRay ray;
|
---|
312 | VssRayContainer vssRays;
|
---|
313 |
|
---|
314 | //CTimer timer;
|
---|
315 | //timer.Start();
|
---|
316 |
|
---|
317 | RayPacket2x2 rp;
|
---|
318 | for (y = 0; y < mHeight-1; y+=2) {
|
---|
319 | float *pppbuffer = pbuffer;
|
---|
320 | cout<<"+";
|
---|
321 | for (x = 0; x < mWidth-1; x+=2) {
|
---|
322 | int i = 0;
|
---|
323 | for (int yi = 0; yi < 2; yi++) {
|
---|
324 | for (int xi = 0; xi < 2; xi++) {
|
---|
325 | SetupRay(ray, mWidth - (x+xi + 1), mHeight - (y+yi + 1));
|
---|
326 | rp.SetLoc(i, ray.mOrigin);
|
---|
327 | rp.SetDir(i, ray.mDirection);
|
---|
328 | i++;
|
---|
329 | } // for xi
|
---|
330 | } // for yi
|
---|
331 |
|
---|
332 | raycaster->CastRaysPacket2x2(rp, false, false);
|
---|
333 |
|
---|
334 | i = 0;
|
---|
335 | float *ppbuffer = pbuffer;
|
---|
336 | for (int yi = 0; yi < 2; yi++) {
|
---|
337 | for (int xi = 0; xi < 2; xi++) {
|
---|
338 | Intersectable* res = rp.GetObject(i);
|
---|
339 | if (res) {
|
---|
340 | pbuffer[0] = 1.0f;
|
---|
341 | pbuffer[1] = 1.0f;
|
---|
342 | pbuffer[2] = 1.0f;
|
---|
343 | pbuffer[3] = 1.0f;
|
---|
344 | }
|
---|
345 | else {
|
---|
346 | pbuffer[0] = 0.0;
|
---|
347 | pbuffer[1] = 0.0;
|
---|
348 | pbuffer[2] = 0.0;
|
---|
349 | pbuffer[3] = 1.0f;
|
---|
350 | }
|
---|
351 | i++;
|
---|
352 | ppbuffer += components;
|
---|
353 | } // xi
|
---|
354 | ppbuffer += components * (mWidth-2);
|
---|
355 | } // yi
|
---|
356 |
|
---|
357 | pbuffer = ppbuffer + 2 * components;
|
---|
358 | } // for x
|
---|
359 | pbuffer = pppbuffer + mWidth * 2 * components;
|
---|
360 | pppbuffer = pbuffer; // for the next time;
|
---|
361 | } // for y
|
---|
362 |
|
---|
363 | //timer.Stop();
|
---|
364 |
|
---|
365 | long t2 = GetTime();
|
---|
366 | cout<<"\n#RAY_CAST_TIME = ";
|
---|
367 | cout << TimeDiff(t1, t2)<<" [mikrosec]\n";
|
---|
368 |
|
---|
369 | cout<<"Saving image"<<endl;
|
---|
370 |
|
---|
371 | ilRegisterType(IL_FLOAT);
|
---|
372 | ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
373 | ilSaveImage((char *const)filename.c_str());
|
---|
374 | delete buffer;
|
---|
375 | #endif
|
---|
376 | cout<<"done."<<endl<<flush;
|
---|
377 |
|
---|
378 | return true;
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | }
|
---|