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