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 "common.h"
|
---|
10 | #include "Camera.h"
|
---|
11 | #include "Ray.h"
|
---|
12 | #include "SimpleRay.h"
|
---|
13 | #include "KdTree.h"
|
---|
14 | #include "Mesh.h"
|
---|
15 | #include "Exporter.h"
|
---|
16 | #include "SceneGraph.h"
|
---|
17 | #include "Preprocessor.h"
|
---|
18 | #include "RayCaster.h"
|
---|
19 | #include "tgaimg.h"
|
---|
20 |
|
---|
21 |
|
---|
22 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
23 | #include "raypack.h"
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | using namespace std;
|
---|
27 |
|
---|
28 | namespace GtpVisibilityPreprocessor {
|
---|
29 |
|
---|
30 |
|
---|
31 | void
|
---|
32 | InitDevIl()
|
---|
33 | {
|
---|
34 | ilInit();
|
---|
35 | ILuint ImageName;
|
---|
36 | ilGenImages(1, &ImageName);
|
---|
37 | ilBindImage(ImageName);
|
---|
38 | ilEnable(IL_FILE_OVERWRITE);
|
---|
39 |
|
---|
40 | // ilRegisterFormat(IL_RGBA);
|
---|
41 | // ilRegisterType(IL_FLOAT);
|
---|
42 |
|
---|
43 | // ilEnable(IL_ORIGIN_SET);
|
---|
44 | // ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
|
---|
45 | }
|
---|
46 |
|
---|
47 | bool
|
---|
48 | Camera::SnapImage(string filename,
|
---|
49 | KdTree *tree,
|
---|
50 | SceneGraph *sceneGraph
|
---|
51 | )
|
---|
52 | {
|
---|
53 | int x;
|
---|
54 | int y;
|
---|
55 |
|
---|
56 | bool exportRays = false;
|
---|
57 | CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
|
---|
58 |
|
---|
59 | vector<Ray *> rays;
|
---|
60 |
|
---|
61 | long t1 = GetTime();
|
---|
62 |
|
---|
63 | Ray ray;
|
---|
64 |
|
---|
65 | for (y = 0; y < mHeight; y++) {
|
---|
66 | cout<<"+" << flush;
|
---|
67 | for (x = 0; x < mWidth; x++) {
|
---|
68 | SetupRay(ray, mWidth - (x + 1), mHeight - (y + 1));
|
---|
69 |
|
---|
70 | bool debug = true;
|
---|
71 | // (y == mHeight/2) && (x== mWidth/3);
|
---|
72 | // MeshDebug = debug;
|
---|
73 |
|
---|
74 | if (debug)
|
---|
75 | ray.mFlags = (Ray::STORE_TESTED_OBJECTS | Ray::STORE_KDLEAVES);
|
---|
76 | else
|
---|
77 | ray.mFlags &= ~(Ray::STORE_TESTED_OBJECTS|Ray::STORE_KDLEAVES);
|
---|
78 |
|
---|
79 | if (tree->CastRay(ray)) {
|
---|
80 | // cout<<"I1";
|
---|
81 |
|
---|
82 | if (ray.intersections.size()) {
|
---|
83 | sort(ray.intersections.begin(), ray.intersections.end());
|
---|
84 | MeshInstance *mesh = (MeshInstance*)ray.intersections[0].mObject;
|
---|
85 | RgbColor color(1,1,1);
|
---|
86 | if (mesh->GetMesh()->mMaterial)
|
---|
87 | color = mesh->GetMesh()->mMaterial->mDiffuseColor;
|
---|
88 |
|
---|
89 | tgaimg.SetPixel(x, y, color.r * 255.f, color.g * 255.f, color.b * 255.f);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | if (debug) {
|
---|
93 | Ray *nray = new Ray(ray);
|
---|
94 | rays.push_back(nray);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | long t2 = GetTime();
|
---|
100 | cout<<"#RAY_CAST_TIME\n";
|
---|
101 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
102 |
|
---|
103 |
|
---|
104 | cout<<"Saving image"<<endl;
|
---|
105 | tgaimg.SaveToFile(filename.c_str());
|
---|
106 |
|
---|
107 | cout<<"done."<<endl<<flush;
|
---|
108 |
|
---|
109 | Exporter *exporter = NULL;
|
---|
110 | if (1) {
|
---|
111 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
112 | //exporter->SetFilled();
|
---|
113 |
|
---|
114 | exporter->SetWireframe();
|
---|
115 |
|
---|
116 | if (sceneGraph)
|
---|
117 | exporter->ExportScene(sceneGraph->GetRoot());
|
---|
118 |
|
---|
119 | //exporter->ExportKdTree(*tree);
|
---|
120 | //exporter->ExportBspTree(*bsptree);
|
---|
121 | exporter->ExportRays(rays, 2000);
|
---|
122 | int k =0;
|
---|
123 | for (int j=0; j < rays.size(); j++)
|
---|
124 | if (rays[j]->kdLeaves.size()) {
|
---|
125 | Ray *ray = rays[j];
|
---|
126 | int i;
|
---|
127 | exporter->SetWireframe();
|
---|
128 |
|
---|
129 | if (1)
|
---|
130 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
131 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
132 |
|
---|
133 | exporter->SetFilled();
|
---|
134 |
|
---|
135 | if (1)
|
---|
136 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
137 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
138 | }
|
---|
139 |
|
---|
140 | delete exporter;
|
---|
141 | }
|
---|
142 |
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | void
|
---|
148 | Camera::SetupRay(Ray &ray, const int x, const int y)
|
---|
149 | {
|
---|
150 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
151 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
152 | Vector3 target = xv + yv + mDirection;
|
---|
153 |
|
---|
154 | ray.Clear();
|
---|
155 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
156 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | void
|
---|
161 | Camera::SetupRay(SimpleRay &ray, const int x, const int y)
|
---|
162 | {
|
---|
163 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
164 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
165 | Vector3 dir = mDirection - xv + yv;
|
---|
166 | dir.Normalize();
|
---|
167 |
|
---|
168 | ray.Set(mPosition, dir, 0,
|
---|
169 | 1.0f, ~(SimpleRay::F_BIDIRECTIONAL));
|
---|
170 | }
|
---|
171 |
|
---|
172 | bool
|
---|
173 | Camera::SnapImage(string filename,
|
---|
174 | RayCaster *raycaster,
|
---|
175 | AxisAlignedBox3 &bbox,
|
---|
176 | SceneGraph *sceneGraph
|
---|
177 | )
|
---|
178 | {
|
---|
179 | int x;
|
---|
180 | int y;
|
---|
181 |
|
---|
182 | bool exportRays = false;
|
---|
183 | CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
|
---|
184 | tgaimg.FillInImage(0.0, 0.0f, 200.f);
|
---|
185 |
|
---|
186 | vector<Ray *> rays;
|
---|
187 |
|
---|
188 | long t1 = GetTime();
|
---|
189 |
|
---|
190 | SimpleRay ray;
|
---|
191 | VssRayContainer vssRays;
|
---|
192 |
|
---|
193 | bool doubleRays = true;
|
---|
194 | bool exploitDoubleRays = true;
|
---|
195 | if (!doubleRays)
|
---|
196 | exploitDoubleRays = false;
|
---|
197 |
|
---|
198 | //CTimer timer;
|
---|
199 | //timer.Start();
|
---|
200 |
|
---|
201 | //int ystart = 92;
|
---|
202 | int xstart = 0;
|
---|
203 | int ymax = mHeight;
|
---|
204 | for (y = 0; y < ymax; y++) {
|
---|
205 | cout<<"+" << flush;
|
---|
206 | for (x = 0; x < mWidth; x++) {
|
---|
207 | SetupRay(ray, x, y);
|
---|
208 | if (exploitDoubleRays)
|
---|
209 | ray.mDirection = - ray.mDirection;
|
---|
210 |
|
---|
211 | bool debug = false;
|
---|
212 | // (y == mHeight/2) && (x== mWidth/3);
|
---|
213 | // MeshDebug = debug;
|
---|
214 |
|
---|
215 | int res = raycaster->CastRay(ray,
|
---|
216 | vssRays,
|
---|
217 | bbox,
|
---|
218 | doubleRays, // castDoubleRay,
|
---|
219 | false); // pruneInvalidRays
|
---|
220 |
|
---|
221 | // cout << "End ------------------------ "
|
---|
222 | // << SimpleRay::IntersectionRes[0].intersectable
|
---|
223 | // << " t = " << SimpleRay::IntersectionRes[0].tdist
|
---|
224 | // << endl;
|
---|
225 |
|
---|
226 | if (res) {
|
---|
227 | Vector3 normal = SimpleRay::IntersectionRes[0].intersectable->GetNormal(0);
|
---|
228 | float v =
|
---|
229 | ray.mDirection.x * normal.x +
|
---|
230 | ray.mDirection.y * normal.y +
|
---|
231 | ray.mDirection.z * normal.z;
|
---|
232 | v = fabs(v);
|
---|
233 | v *= 200.0f;
|
---|
234 | if (v < 50.f) v = 50.f;
|
---|
235 | tgaimg.SetPixel(x, y, v, v, v);
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (debug) {
|
---|
239 | Ray *nray = new Ray(ray.mOrigin, ray.mDirection,
|
---|
240 | Ray::LOCAL_RAY);
|
---|
241 | rays.push_back(nray);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | } // for y
|
---|
245 |
|
---|
246 | //timer.Stop();
|
---|
247 |
|
---|
248 | long t2 = GetTime();
|
---|
249 | cout<<"\n#RAY_CAST_TIME = ";
|
---|
250 | cout << TimeDiff(t1, t2)<<" [mikrosec]\n";
|
---|
251 |
|
---|
252 | cout<<"Saving image"<<endl;
|
---|
253 |
|
---|
254 | tgaimg.SaveToFile(filename.c_str());
|
---|
255 |
|
---|
256 | //ilRegisterType(IL_FLOAT);
|
---|
257 | //ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
258 | //ilSaveImage((char *const)filename.c_str());
|
---|
259 | //delete buffer;
|
---|
260 |
|
---|
261 | cout<<"done."<<endl<<flush;
|
---|
262 |
|
---|
263 | return true;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | bool
|
---|
268 | Camera::SnapImage2(string filename,
|
---|
269 | RayCaster *raycaster,
|
---|
270 | AxisAlignedBox3 &bbox,
|
---|
271 | SceneGraph *sceneGraph)
|
---|
272 | {
|
---|
273 | int x;
|
---|
274 | int y;
|
---|
275 |
|
---|
276 | bool exportRays = false;
|
---|
277 | CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
|
---|
278 | tgaimg.FillInImage(0.0, 0.0f, 200.f);
|
---|
279 |
|
---|
280 | vector<Ray *> raysDebug;
|
---|
281 |
|
---|
282 | long t1 = GetTime();
|
---|
283 |
|
---|
284 | SimpleRay ray;
|
---|
285 | SimpleRayContainer rays;
|
---|
286 | VssRayContainer vssRays;
|
---|
287 |
|
---|
288 | bool doubleRays = false;
|
---|
289 | bool exploitDoubleRays = false;
|
---|
290 | int batchSize = 16;
|
---|
291 | if (!doubleRays)
|
---|
292 | exploitDoubleRays = false;
|
---|
293 | int shiftIndex = 0;
|
---|
294 | if (exploitDoubleRays)
|
---|
295 | shiftIndex = batchSize;
|
---|
296 |
|
---|
297 | //CTimer timer;
|
---|
298 | //timer.Start();
|
---|
299 |
|
---|
300 | //int ystart = 92;
|
---|
301 | int xstart = 0;
|
---|
302 | int ymax = mHeight;
|
---|
303 | for (y = 0; y < ymax; y++) {
|
---|
304 | if (y == 250) {
|
---|
305 | cout << "Debug y = " << y << endl;
|
---|
306 | }
|
---|
307 |
|
---|
308 | cout<<"+" << flush;
|
---|
309 | for (x = 0; x < mWidth; x += batchSize ) {
|
---|
310 | rays.erase(rays.begin(), rays.end());
|
---|
311 | int xi;
|
---|
312 | for (xi = 0; (xi < batchSize) && (x+xi < mWidth); xi++ ) {
|
---|
313 | SetupRay(ray, x + xi, y);
|
---|
314 | if (exploitDoubleRays)
|
---|
315 | ray.mDirection = -ray.mDirection;
|
---|
316 | rays.push_back(ray);
|
---|
317 | } // for
|
---|
318 |
|
---|
319 | bool debug = false;
|
---|
320 | // (y == mHeight/2) && (x== mWidth/3);
|
---|
321 | // MeshDebug = debug;
|
---|
322 |
|
---|
323 | #if 1
|
---|
324 | assert(batchSize == 16);
|
---|
325 | raycaster->CastRays16(rays,
|
---|
326 | vssRays,
|
---|
327 | bbox,
|
---|
328 | doubleRays, // castDoubleRay,
|
---|
329 | false); // pruneInvalidRays
|
---|
330 | #else
|
---|
331 | raycaster->CastSimpleForwardRays(rays, bbox);
|
---|
332 | #endif
|
---|
333 |
|
---|
334 | // cout << "End ------------------------ "
|
---|
335 | // << SimpleRay::IntersectionRes[0].intersectable
|
---|
336 | // << " t = " << SimpleRay::IntersectionRes[0].tdist
|
---|
337 | // << endl;
|
---|
338 |
|
---|
339 | for (xi = 0; (xi < batchSize) && (x+xi < mWidth); xi++ ) {
|
---|
340 | Intersectable* res =
|
---|
341 | SimpleRay::IntersectionRes[xi + shiftIndex].intersectable;
|
---|
342 |
|
---|
343 | if (res) {
|
---|
344 | Vector3 normal = res->GetNormal(0);
|
---|
345 | float v =
|
---|
346 | rays[xi].mDirection.x * normal.x +
|
---|
347 | rays[xi].mDirection.y * normal.y +
|
---|
348 | rays[xi].mDirection.z * normal.z;
|
---|
349 | v = fabs(v);
|
---|
350 | v *= 200.0f;
|
---|
351 | if (v < 50.f) v = 50.f;
|
---|
352 | tgaimg.SetPixel(x + xi, y, v, v, v);
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | if (debug) {
|
---|
357 | Ray *nray = new Ray(ray.mOrigin, ray.mDirection,
|
---|
358 | Ray::LOCAL_RAY);
|
---|
359 | raysDebug.push_back(nray);
|
---|
360 | }
|
---|
361 | } // for x
|
---|
362 | } // for y
|
---|
363 |
|
---|
364 | //timer.Stop();
|
---|
365 |
|
---|
366 | long t2 = GetTime();
|
---|
367 | cout<<"\n#RAY_CAST_TIME = ";
|
---|
368 | cout << TimeDiff(t1, t2)<<" [mikrosec]\n";
|
---|
369 |
|
---|
370 | cout<<"Saving image"<<endl;
|
---|
371 |
|
---|
372 | tgaimg.SaveToFile(filename.c_str());
|
---|
373 |
|
---|
374 | //ilRegisterType(IL_FLOAT);
|
---|
375 | //ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
376 | //ilSaveImage((char *const)filename.c_str());
|
---|
377 | //delete buffer;
|
---|
378 |
|
---|
379 | cout<<"done."<<endl<<flush;
|
---|
380 |
|
---|
381 | return true;
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | bool
|
---|
386 | Camera::SnapImagePacket(string filename,
|
---|
387 | RayCaster *raycaster,
|
---|
388 | AxisAlignedBox3 &bbox,
|
---|
389 | SceneGraph *sceneGraph
|
---|
390 | )
|
---|
391 | {
|
---|
392 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
393 | #ifdef _USE_HAVRAN_SSE
|
---|
394 |
|
---|
395 | int x;
|
---|
396 | int y;
|
---|
397 |
|
---|
398 | bool exportRays = false;
|
---|
399 |
|
---|
400 | // - components*mWidth;
|
---|
401 | CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
|
---|
402 | tgaimg.FillInImage(0.0, 0.0f, 200.f); // blue color image
|
---|
403 |
|
---|
404 | vector<Ray *> rays;
|
---|
405 |
|
---|
406 | long t1 = GetTime();
|
---|
407 |
|
---|
408 | SimpleRay ray;
|
---|
409 | VssRayContainer vssRays;
|
---|
410 |
|
---|
411 | //CTimer timer;
|
---|
412 | //timer.Start();
|
---|
413 |
|
---|
414 | bool doubleRays = false;
|
---|
415 | bool exploitDoubleRays = false;
|
---|
416 | if (!doubleRays)
|
---|
417 | exploitDoubleRays = false;
|
---|
418 |
|
---|
419 | GALIGN16 RayPacket2x2 rp;
|
---|
420 | //int ystart = 92;
|
---|
421 | //int xstart = 738;
|
---|
422 | int pixels = 0;;
|
---|
423 | for (y = 0; y < mHeight-1; y+=2) {
|
---|
424 | //for (y = ystart-2; y > 0; y-=2) {
|
---|
425 | for (x = 0; x < mWidth-1; x+=2) {
|
---|
426 | int i = 0;
|
---|
427 | for (int yi = 0; yi < 2; yi++) {
|
---|
428 | for (int xi = 0; xi < 2; xi++) {
|
---|
429 | SetupRay(ray, x+xi, y+yi);
|
---|
430 | rp.SetLoc(i, ray.mOrigin);
|
---|
431 | rp.SetDir(i, ray.mDirection);
|
---|
432 | if (exploitDoubleRays)
|
---|
433 | rp.SetDir(i, -ray.mDirection);
|
---|
434 | i++;
|
---|
435 | } // for xi
|
---|
436 | } // for yi
|
---|
437 | pixels += 4;
|
---|
438 | if (pixels > mWidth) {
|
---|
439 | cout << "+" << flush;
|
---|
440 | pixels = 0;
|
---|
441 | }
|
---|
442 |
|
---|
443 | raycaster->CastRaysPacket2x2(rp, doubleRays, false);
|
---|
444 |
|
---|
445 | i = 0;
|
---|
446 | for (int yi = 0; yi < 2; yi++) {
|
---|
447 | for (int xi = 0; xi < 2; xi++) {
|
---|
448 | Intersectable* res = rp.GetObject(i);
|
---|
449 | if (res) {
|
---|
450 | #if 0
|
---|
451 | // This is debugging code to find a bug
|
---|
452 | for (int j = 0; j < 4; j++) {
|
---|
453 | cout << " j = " << j << " obj = "
|
---|
454 | << rp.GetObject(j)
|
---|
455 | << " t = " << rp.GetT(j);
|
---|
456 | ray.Set(rp.GetLoc(j), rp.GetDir(j), 0, 1.0f, 0);
|
---|
457 | int res = raycaster->CastRay(ray,
|
---|
458 | vssRays,
|
---|
459 | bbox,
|
---|
460 | doubleRays, // castDoubleRay,
|
---|
461 | false); // pruneInvalidRays
|
---|
462 | cout << " correct result obj = "
|
---|
463 | << SimpleRay::IntersectionRes[0].intersectable
|
---|
464 | << " t = "
|
---|
465 | << SimpleRay::IntersectionRes[0].tdist
|
---|
466 | << endl;
|
---|
467 | } // for
|
---|
468 |
|
---|
469 | raycaster->CastRaysPacket2x2(rp, false, false);
|
---|
470 | #endif
|
---|
471 |
|
---|
472 | Vector3 normal = res->GetNormal(0);
|
---|
473 | float v =
|
---|
474 | ray.mDirection.x * normal.x +
|
---|
475 | ray.mDirection.y * normal.y +
|
---|
476 | ray.mDirection.z * normal.z;
|
---|
477 | v = fabs(v);
|
---|
478 | v *= 200.0f;
|
---|
479 | if (v < 50.f) v = 50.f;
|
---|
480 | tgaimg.SetPixel(x+xi, y+yi, v, v, v);
|
---|
481 | }
|
---|
482 | i++;
|
---|
483 | } // xi
|
---|
484 | } // yi
|
---|
485 | } // for x
|
---|
486 | } // for y
|
---|
487 |
|
---|
488 | //timer.Stop();
|
---|
489 |
|
---|
490 | long t2 = GetTime();
|
---|
491 | cout<<"\n#RAY_CAST_TIME = ";
|
---|
492 | cout << TimeDiff(t1, t2)<<" [mikrosec]\n";
|
---|
493 |
|
---|
494 | cout<<"Saving image"<<endl;
|
---|
495 | tgaimg.SaveToFile(filename.c_str());
|
---|
496 |
|
---|
497 | #endif // _USE_HAVRAN_SSE
|
---|
498 | #endif // USE_HAVRAN_RAYCASTER
|
---|
499 | cout<<"done."<<endl<<flush;
|
---|
500 |
|
---|
501 | return true;
|
---|
502 | }
|
---|
503 |
|
---|
504 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
505 | {
|
---|
506 | return obj1->mId < obj2->mId;
|
---|
507 | }
|
---|
508 |
|
---|
509 |
|
---|
510 | bool
|
---|
511 | Camera::SnapImagePacket2(string filename,
|
---|
512 | RayCaster *raycaster,
|
---|
513 | AxisAlignedBox3 &bbox,
|
---|
514 | SceneGraph *sceneGraph
|
---|
515 | )
|
---|
516 | {
|
---|
517 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
518 | #ifdef _USE_HAVRAN_SSE
|
---|
519 |
|
---|
520 | int x;
|
---|
521 | int y;
|
---|
522 |
|
---|
523 | bool exportRays = false;
|
---|
524 |
|
---|
525 | // - components*mWidth;
|
---|
526 | CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
|
---|
527 | tgaimg.FillInImage(0.0, 0.0f, 200.f); // blue color image
|
---|
528 |
|
---|
529 | vector<Ray *> rays;
|
---|
530 |
|
---|
531 | long t1 = GetTime();
|
---|
532 |
|
---|
533 | SimpleRay ray;
|
---|
534 | VssRayContainer vssRays;
|
---|
535 |
|
---|
536 | //CTimer timer;
|
---|
537 | //timer.Start();
|
---|
538 |
|
---|
539 | bool doubleRays = false;
|
---|
540 | bool exploitDoubleRays = false;
|
---|
541 | if (!doubleRays)
|
---|
542 | exploitDoubleRays = false;
|
---|
543 |
|
---|
544 | Vector3 origin4[4];
|
---|
545 | Vector3 direction4[4];
|
---|
546 | int result4[4];
|
---|
547 | float dist4[4];
|
---|
548 |
|
---|
549 | GALIGN16 RayPacket2x2 rp;
|
---|
550 | //int ystart = 92;
|
---|
551 | //int xstart = 738;
|
---|
552 | int pixels = 0;;
|
---|
553 | for (y = 0; y < mHeight-1; y+=2) {
|
---|
554 | //for (y = ystart-2; y > 0; y-=2) {
|
---|
555 | for (x = 0; x < mWidth-1; x+=2) {
|
---|
556 | int i = 0;
|
---|
557 | for (int yi = 0; yi < 2; yi++) {
|
---|
558 | for (int xi = 0; xi < 2; xi++) {
|
---|
559 | SetupRay(ray, x+xi, y+yi);
|
---|
560 | origin4[i] = ray.mOrigin;
|
---|
561 | direction4[i] = ray.mDirection;
|
---|
562 | if (exploitDoubleRays)
|
---|
563 | direction4[i] = -ray.mDirection;
|
---|
564 | i++;
|
---|
565 | } // for xi
|
---|
566 | } // for yi
|
---|
567 | pixels += 4;
|
---|
568 | if (pixels > mWidth) {
|
---|
569 | cout << "+" << flush;
|
---|
570 | pixels = 0;
|
---|
571 | }
|
---|
572 |
|
---|
573 | raycaster->CastRaysPacket4(bbox.Min(), bbox.Max(),
|
---|
574 | origin4,
|
---|
575 | direction4,
|
---|
576 | result4,
|
---|
577 | dist4);
|
---|
578 |
|
---|
579 | i = 0;
|
---|
580 | for (int yi = 0; yi < 2; yi++) {
|
---|
581 | for (int xi = 0; xi < 2; xi++) {
|
---|
582 | int objId = result4[i];
|
---|
583 | if (objId != -1) {
|
---|
584 | MeshInstance dummyInst(NULL);
|
---|
585 | dummyInst.SetId(objId);
|
---|
586 | ObjectContainer &objects = preprocessor->mObjects;
|
---|
587 | ObjectContainer::const_iterator oit =
|
---|
588 | lower_bound(objects.begin(), objects.end(),
|
---|
589 | (Intersectable *)&dummyInst, ilt);
|
---|
590 |
|
---|
591 | if ((oit != objects.end()) && ((*oit)->GetId() == objId)) {
|
---|
592 | Intersectable *res = *oit;
|
---|
593 |
|
---|
594 | Vector3 normal = res->GetNormal(0);
|
---|
595 | float v =
|
---|
596 | ray.mDirection.x * normal.x +
|
---|
597 | ray.mDirection.y * normal.y +
|
---|
598 | ray.mDirection.z * normal.z;
|
---|
599 | v = fabs(v);
|
---|
600 | v *= 200.0f;
|
---|
601 | if (v < 50.f) v = 50.f;
|
---|
602 | tgaimg.SetPixel(x+xi, y+yi, v, v, v);
|
---|
603 | }
|
---|
604 | else {
|
---|
605 | cout <<"*" << endl;
|
---|
606 | }
|
---|
607 |
|
---|
608 | } // object intersected
|
---|
609 | i++;
|
---|
610 | } // xi
|
---|
611 | } // yi
|
---|
612 | } // for x
|
---|
613 | } // for y
|
---|
614 |
|
---|
615 | //timer.Stop();
|
---|
616 |
|
---|
617 | long t2 = GetTime();
|
---|
618 | cout<<"\n#RAY_CAST_TIME = ";
|
---|
619 | cout << TimeDiff(t1, t2)<<" [mikrosec]\n";
|
---|
620 |
|
---|
621 | cout<<"Saving image"<<endl;
|
---|
622 | tgaimg.SaveToFile(filename.c_str());
|
---|
623 |
|
---|
624 | #endif // _USE_HAVRAN_SSE
|
---|
625 | #endif // USE_HAVRAN_RAYCASTER
|
---|
626 | cout<<"done."<<endl<<flush;
|
---|
627 |
|
---|
628 | return true;
|
---|
629 | }
|
---|
630 |
|
---|
631 | }
|
---|
632 |
|
---|