[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>
|
---|
| 7 |
|
---|
[162] | 8 | #include "Camera.h"
|
---|
| 9 | #include "Ray.h"
|
---|
| 10 | #include "KdTree.h"
|
---|
| 11 | #include "Mesh.h"
|
---|
| 12 | #include "Exporter.h"
|
---|
[492] | 13 | #include "SceneGraph.h"
|
---|
[162] | 14 |
|
---|
[191] | 15 |
|
---|
[863] | 16 | namespace GtpVisibilityPreprocessor {
|
---|
[191] | 17 |
|
---|
| 18 |
|
---|
| 19 | void
|
---|
| 20 | InitDevIl()
|
---|
| 21 | {
|
---|
| 22 | ilInit();
|
---|
| 23 | ILuint ImageName;
|
---|
| 24 | ilGenImages(1, &ImageName);
|
---|
| 25 | ilBindImage(ImageName);
|
---|
| 26 | ilEnable(IL_FILE_OVERWRITE);
|
---|
| 27 |
|
---|
| 28 | // ilRegisterFormat(IL_RGBA);
|
---|
| 29 | // ilRegisterType(IL_FLOAT);
|
---|
| 30 |
|
---|
| 31 | // ilEnable(IL_ORIGIN_SET);
|
---|
| 32 | // ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[162] | 35 | bool
|
---|
| 36 | Camera::SnapImage(string filename,
|
---|
[492] | 37 | KdTree *tree,
|
---|
| 38 | SceneGraph *sceneGraph
|
---|
| 39 | )
|
---|
[162] | 40 | {
|
---|
| 41 | int x;
|
---|
| 42 | int y;
|
---|
| 43 |
|
---|
| 44 | bool exportRays = true;
|
---|
| 45 |
|
---|
| 46 |
|
---|
[191] | 47 | InitDevIl();
|
---|
| 48 | int components = 4;
|
---|
| 49 | float *buffer = new float[components*mWidth*mHeight];
|
---|
| 50 | float *pbuffer = buffer;
|
---|
| 51 | // - components*mWidth;
|
---|
[179] | 52 |
|
---|
[386] | 53 | vector<Ray *> rays;
|
---|
[162] | 54 |
|
---|
| 55 | long t1 = GetTime();
|
---|
| 56 |
|
---|
| 57 | Ray ray;
|
---|
[191] | 58 |
|
---|
[162] | 59 | for (y = 0; y < mHeight; y++) {
|
---|
| 60 | cout<<"+";
|
---|
| 61 | for (x = 0; x < mWidth; x++) {
|
---|
[878] | 62 | SetupRay(ray, mWidth - (x + 1), mHeight - (y + 1));
|
---|
| 63 |
|
---|
| 64 | bool debug = false;
|
---|
| 65 | // (y == mHeight/2) && (x== mWidth/3);
|
---|
| 66 | // MeshDebug = debug;
|
---|
| 67 |
|
---|
| 68 | if (debug)
|
---|
| 69 | ray.mFlags = (Ray::STORE_TESTED_OBJECTS | Ray::STORE_KDLEAVES);
|
---|
| 70 | else
|
---|
| 71 | ray.mFlags &= ~(Ray::STORE_TESTED_OBJECTS|Ray::STORE_KDLEAVES);
|
---|
| 72 |
|
---|
| 73 | if (tree->CastRay(ray))
|
---|
[492] | 74 | if (ray.intersections.size()) {
|
---|
| 75 | sort(ray.intersections.begin(), ray.intersections.end());
|
---|
| 76 | MeshInstance *mesh = (MeshInstance*)ray.intersections[0].mObject;
|
---|
| 77 | RgbColor color(1,1,1);
|
---|
| 78 | if (mesh->GetMesh()->mMaterial)
|
---|
| 79 | color = mesh->GetMesh()->mMaterial->mDiffuseColor;
|
---|
| 80 |
|
---|
| 81 | pbuffer[0] = color.r;
|
---|
| 82 | pbuffer[1] = color.g;
|
---|
| 83 | pbuffer[2] = color.b;
|
---|
| 84 | pbuffer[3] = 1.0f;
|
---|
| 85 |
|
---|
| 86 | }
|
---|
[191] | 87 | pbuffer+=components;
|
---|
[162] | 88 |
|
---|
[492] | 89 | // if (exportRays && ( 1||(x==222) && (y==97))) {
|
---|
| 90 | // if (exportRays && ((x%4==0) && (y%4==0))) {
|
---|
[878] | 91 |
|
---|
| 92 | // if (exportRays && ray.intersections.empty()) {
|
---|
| 93 | if (debug) {
|
---|
[492] | 94 | Ray *nray = new Ray(ray);
|
---|
| 95 | rays.push_back(nray);
|
---|
| 96 | }
|
---|
[162] | 97 | }
|
---|
[191] | 98 | // pbuffer-=2*components*mWidth;
|
---|
[162] | 99 | }
|
---|
| 100 |
|
---|
| 101 | long t2 = GetTime();
|
---|
[176] | 102 | cout<<"#RAY_CAST_TIME\n";
|
---|
| 103 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
| 104 |
|
---|
[492] | 105 |
|
---|
[176] | 106 | cout<<"Saving image"<<endl;
|
---|
[492] | 107 |
|
---|
[191] | 108 | ilRegisterType(IL_FLOAT);
|
---|
| 109 | ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
| 110 | ilSaveImage((char *const)filename.c_str());
|
---|
| 111 | delete buffer;
|
---|
[492] | 112 |
|
---|
| 113 | cout<<"done."<<endl<<flush;
|
---|
[191] | 114 |
|
---|
[162] | 115 | Exporter *exporter = NULL;
|
---|
[492] | 116 | if (1) {
|
---|
[162] | 117 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
[878] | 118 | //exporter->SetFilled();
|
---|
| 119 |
|
---|
| 120 | exporter->SetWireframe();
|
---|
| 121 |
|
---|
| 122 | if (sceneGraph)
|
---|
[1328] | 123 | exporter->ExportScene(sceneGraph->GetRoot());
|
---|
[492] | 124 |
|
---|
| 125 | //exporter->ExportKdTree(*tree);
|
---|
| 126 | //exporter->ExportBspTree(*bsptree);
|
---|
| 127 | exporter->ExportRays(rays, 2000);
|
---|
[162] | 128 | int k =0;
|
---|
| 129 | for (int j=0; j < rays.size(); j++)
|
---|
[386] | 130 | if (rays[j]->kdLeaves.size()) {
|
---|
[492] | 131 | Ray *ray = rays[j];
|
---|
| 132 | int i;
|
---|
[878] | 133 | exporter->SetWireframe();
|
---|
| 134 |
|
---|
| 135 | if (1)
|
---|
[492] | 136 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
| 137 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
[878] | 138 |
|
---|
| 139 | exporter->SetFilled();
|
---|
| 140 |
|
---|
| 141 | if (1)
|
---|
[492] | 142 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
| 143 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
[162] | 144 | }
|
---|
[492] | 145 |
|
---|
[162] | 146 | delete exporter;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | return true;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | void
|
---|
| 154 | Camera::SetupRay(Ray &ray, const int x, const int y)
|
---|
| 155 | {
|
---|
| 156 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
| 157 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
| 158 | Vector3 target = xv + yv + mDirection;
|
---|
| 159 |
|
---|
[466] | 160 | ray.Clear();
|
---|
[162] | 161 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
[878] | 162 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[162] | 163 | }
|
---|
[860] | 164 |
|
---|
[878] | 165 | }
|
---|