[162] | 1 | #include <algorithm>
|
---|
[191] | 2 |
|
---|
| 3 | // the devil library
|
---|
| 4 | #include <IL/il.h>
|
---|
| 5 | #include <IL/ilu.h>
|
---|
| 6 | #include <IL/ilut.h>
|
---|
[2575] | 7 | #include <cassert>
|
---|
[191] | 8 |
|
---|
[2629] | 9 | #include "common.h"
|
---|
[162] | 10 | #include "Camera.h"
|
---|
| 11 | #include "Ray.h"
|
---|
[2575] | 12 | #include "SimpleRay.h"
|
---|
[162] | 13 | #include "KdTree.h"
|
---|
| 14 | #include "Mesh.h"
|
---|
| 15 | #include "Exporter.h"
|
---|
[492] | 16 | #include "SceneGraph.h"
|
---|
[2575] | 17 | #include "Preprocessor.h"
|
---|
| 18 | #include "RayCaster.h"
|
---|
[2629] | 19 | #include "tgaimg.h"
|
---|
[162] | 20 |
|
---|
[2635] | 21 |
|
---|
[2629] | 22 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
[2575] | 23 | #include "raypack.h"
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
[2176] | 26 | using namespace std;
|
---|
[191] | 27 |
|
---|
[863] | 28 | namespace GtpVisibilityPreprocessor {
|
---|
[191] | 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 |
|
---|
[162] | 47 | bool
|
---|
| 48 | Camera::SnapImage(string filename,
|
---|
[2575] | 49 | KdTree *tree,
|
---|
| 50 | SceneGraph *sceneGraph
|
---|
| 51 | )
|
---|
[162] | 52 | {
|
---|
| 53 | int x;
|
---|
| 54 | int y;
|
---|
| 55 |
|
---|
[1583] | 56 | bool exportRays = false;
|
---|
[2629] | 57 | CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
|
---|
[162] | 58 |
|
---|
[386] | 59 | vector<Ray *> rays;
|
---|
[162] | 60 |
|
---|
| 61 | long t1 = GetTime();
|
---|
| 62 |
|
---|
| 63 | Ray ray;
|
---|
[191] | 64 |
|
---|
[162] | 65 | for (y = 0; y < mHeight; y++) {
|
---|
[2629] | 66 | cout<<"+" << flush;
|
---|
[162] | 67 | for (x = 0; x < mWidth; x++) {
|
---|
[878] | 68 | SetupRay(ray, mWidth - (x + 1), mHeight - (y + 1));
|
---|
| 69 |
|
---|
[1583] | 70 | bool debug = true;
|
---|
[878] | 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 |
|
---|
[1583] | 79 | if (tree->CastRay(ray)) {
|
---|
| 80 | // cout<<"I1";
|
---|
| 81 |
|
---|
[492] | 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;
|
---|
[2629] | 88 |
|
---|
| 89 | tgaimg.SetPixel(x, y, color.r * 255.f, color.g * 255.f, color.b * 255.f);
|
---|
[492] | 90 | }
|
---|
[1583] | 91 | }
|
---|
[878] | 92 | if (debug) {
|
---|
[2629] | 93 | Ray *nray = new Ray(ray);
|
---|
| 94 | rays.push_back(nray);
|
---|
[492] | 95 | }
|
---|
[162] | 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | long t2 = GetTime();
|
---|
[176] | 100 | cout<<"#RAY_CAST_TIME\n";
|
---|
| 101 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
| 102 |
|
---|
[492] | 103 |
|
---|
[176] | 104 | cout<<"Saving image"<<endl;
|
---|
[2629] | 105 | tgaimg.SaveToFile(filename.c_str());
|
---|
[492] | 106 |
|
---|
| 107 | cout<<"done."<<endl<<flush;
|
---|
[191] | 108 |
|
---|
[162] | 109 | Exporter *exporter = NULL;
|
---|
[492] | 110 | if (1) {
|
---|
[162] | 111 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
[878] | 112 | //exporter->SetFilled();
|
---|
| 113 |
|
---|
| 114 | exporter->SetWireframe();
|
---|
| 115 |
|
---|
| 116 | if (sceneGraph)
|
---|
[1328] | 117 | exporter->ExportScene(sceneGraph->GetRoot());
|
---|
[492] | 118 |
|
---|
| 119 | //exporter->ExportKdTree(*tree);
|
---|
| 120 | //exporter->ExportBspTree(*bsptree);
|
---|
| 121 | exporter->ExportRays(rays, 2000);
|
---|
[162] | 122 | int k =0;
|
---|
| 123 | for (int j=0; j < rays.size(); j++)
|
---|
[386] | 124 | if (rays[j]->kdLeaves.size()) {
|
---|
[492] | 125 | Ray *ray = rays[j];
|
---|
| 126 | int i;
|
---|
[878] | 127 | exporter->SetWireframe();
|
---|
| 128 |
|
---|
| 129 | if (1)
|
---|
[492] | 130 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
| 131 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
[878] | 132 |
|
---|
| 133 | exporter->SetFilled();
|
---|
| 134 |
|
---|
| 135 | if (1)
|
---|
[492] | 136 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
| 137 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
[162] | 138 | }
|
---|
[492] | 139 |
|
---|
[162] | 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 |
|
---|
[466] | 154 | ray.Clear();
|
---|
[162] | 155 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
[878] | 156 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[162] | 157 | }
|
---|
[860] | 158 |
|
---|
[2575] | 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);
|
---|
[2629] | 165 | Vector3 dir = mDirection - xv + yv;
|
---|
[2575] | 166 | dir.Normalize();
|
---|
| 167 |
|
---|
| 168 | ray.Set(mPosition, dir, 0,
|
---|
| 169 | 1.0f, ~(SimpleRay::F_BIDIRECTIONAL));
|
---|
[878] | 170 | }
|
---|
[2575] | 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;
|
---|
[2629] | 183 | CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
|
---|
| 184 | tgaimg.FillInImage(0.0, 0.0f, 200.f);
|
---|
[2575] | 185 |
|
---|
| 186 | vector<Ray *> rays;
|
---|
| 187 |
|
---|
| 188 | long t1 = GetTime();
|
---|
| 189 |
|
---|
| 190 | SimpleRay ray;
|
---|
| 191 | VssRayContainer vssRays;
|
---|
| 192 |
|
---|
[2629] | 193 | bool doubleRays = true;
|
---|
| 194 | bool exploitDoubleRays = true;
|
---|
| 195 | if (!doubleRays)
|
---|
| 196 | exploitDoubleRays = false;
|
---|
| 197 |
|
---|
[2575] | 198 | //CTimer timer;
|
---|
| 199 | //timer.Start();
|
---|
| 200 |
|
---|
[2629] | 201 | //int ystart = 92;
|
---|
| 202 | int xstart = 0;
|
---|
| 203 | int ymax = mHeight;
|
---|
| 204 | for (y = 0; y < ymax; y++) {
|
---|
| 205 | cout<<"+" << flush;
|
---|
[2575] | 206 | for (x = 0; x < mWidth; x++) {
|
---|
[2629] | 207 | SetupRay(ray, x, y);
|
---|
| 208 | if (exploitDoubleRays)
|
---|
| 209 | ray.mDirection = - ray.mDirection;
|
---|
[2575] | 210 |
|
---|
[2629] | 211 | bool debug = false;
|
---|
[2575] | 212 | // (y == mHeight/2) && (x== mWidth/3);
|
---|
| 213 | // MeshDebug = debug;
|
---|
| 214 |
|
---|
| 215 | int res = raycaster->CastRay(ray,
|
---|
| 216 | vssRays,
|
---|
| 217 | bbox,
|
---|
[2629] | 218 | doubleRays, // castDoubleRay,
|
---|
[2575] | 219 | false); // pruneInvalidRays
|
---|
[2629] | 220 |
|
---|
| 221 | // cout << "End ------------------------ "
|
---|
| 222 | // << SimpleRay::IntersectionRes[0].intersectable
|
---|
| 223 | // << " t = " << SimpleRay::IntersectionRes[0].tdist
|
---|
| 224 | // << endl;
|
---|
[2575] | 225 |
|
---|
| 226 | if (res) {
|
---|
[2629] | 227 | Vector3 normal = SimpleRay::IntersectionRes[0].intersectable->GetNormal(0);
|
---|
[2575] | 228 | float v =
|
---|
| 229 | ray.mDirection.x * normal.x +
|
---|
| 230 | ray.mDirection.y * normal.y +
|
---|
| 231 | ray.mDirection.z * normal.z;
|
---|
[2629] | 232 | v = fabs(v);
|
---|
| 233 | v *= 200.0f;
|
---|
| 234 | if (v < 50.f) v = 50.f;
|
---|
| 235 | tgaimg.SetPixel(x, y, v, v, v);
|
---|
[2575] | 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;
|
---|
[2629] | 253 |
|
---|
| 254 | tgaimg.SaveToFile(filename.c_str());
|
---|
[2575] | 255 |
|
---|
[2629] | 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;
|
---|
[2575] | 260 |
|
---|
| 261 | cout<<"done."<<endl<<flush;
|
---|
| 262 |
|
---|
| 263 | return true;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[2629] | 266 |
|
---|
[2575] | 267 | bool
|
---|
[2629] | 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
|
---|
[2575] | 386 | Camera::SnapImagePacket(string filename,
|
---|
| 387 | RayCaster *raycaster,
|
---|
| 388 | AxisAlignedBox3 &bbox,
|
---|
| 389 | SceneGraph *sceneGraph
|
---|
| 390 | )
|
---|
| 391 | {
|
---|
[2629] | 392 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
| 393 | #ifdef _USE_HAVRAN_SSE
|
---|
[2575] | 394 |
|
---|
| 395 | int x;
|
---|
| 396 | int y;
|
---|
| 397 |
|
---|
[2629] | 398 | bool exportRays = false;
|
---|
[2575] | 399 |
|
---|
| 400 | // - components*mWidth;
|
---|
[2629] | 401 | CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
|
---|
| 402 | tgaimg.FillInImage(0.0, 0.0f, 200.f); // blue color image
|
---|
[2575] | 403 |
|
---|
| 404 | vector<Ray *> rays;
|
---|
| 405 |
|
---|
| 406 | long t1 = GetTime();
|
---|
| 407 |
|
---|
| 408 | SimpleRay ray;
|
---|
| 409 | VssRayContainer vssRays;
|
---|
| 410 |
|
---|
| 411 | //CTimer timer;
|
---|
| 412 | //timer.Start();
|
---|
[2629] | 413 |
|
---|
| 414 | bool doubleRays = false;
|
---|
| 415 | bool exploitDoubleRays = false;
|
---|
| 416 | if (!doubleRays)
|
---|
| 417 | exploitDoubleRays = false;
|
---|
[2603] | 418 |
|
---|
[2629] | 419 | GALIGN16 RayPacket2x2 rp;
|
---|
| 420 | //int ystart = 92;
|
---|
| 421 | //int xstart = 738;
|
---|
| 422 | int pixels = 0;;
|
---|
[2575] | 423 | for (y = 0; y < mHeight-1; y+=2) {
|
---|
[2629] | 424 | //for (y = ystart-2; y > 0; y-=2) {
|
---|
[2575] | 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++) {
|
---|
[2629] | 429 | SetupRay(ray, x+xi, y+yi);
|
---|
[2575] | 430 | rp.SetLoc(i, ray.mOrigin);
|
---|
| 431 | rp.SetDir(i, ray.mDirection);
|
---|
[2629] | 432 | if (exploitDoubleRays)
|
---|
| 433 | rp.SetDir(i, -ray.mDirection);
|
---|
[2575] | 434 | i++;
|
---|
| 435 | } // for xi
|
---|
| 436 | } // for yi
|
---|
[2629] | 437 | pixels += 4;
|
---|
| 438 | if (pixels > mWidth) {
|
---|
| 439 | cout << "+" << flush;
|
---|
| 440 | pixels = 0;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | raycaster->CastRaysPacket2x2(rp, doubleRays, false);
|
---|
[2575] | 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) {
|
---|
[2629] | 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);
|
---|
[2575] | 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;
|
---|
[2629] | 495 | tgaimg.SaveToFile(filename.c_str());
|
---|
| 496 |
|
---|
| 497 | #endif // _USE_HAVRAN_SSE
|
---|
| 498 | #endif // USE_HAVRAN_RAYCASTER
|
---|
[2575] | 499 | cout<<"done."<<endl<<flush;
|
---|
| 500 |
|
---|
| 501 | return true;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[2629] | 504 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 505 | {
|
---|
| 506 | return obj1->mId < obj2->mId;
|
---|
| 507 | }
|
---|
[2575] | 508 |
|
---|
[2629] | 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;
|
---|
[2575] | 629 | }
|
---|
[2629] | 630 |
|
---|
| 631 | }
|
---|
| 632 |
|
---|