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