[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 |
|
---|
[1583] | 44 | bool exportRays = false;
|
---|
[162] | 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 |
|
---|
[1583] | 64 | bool debug = true;
|
---|
[878] | 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 |
|
---|
[1583] | 73 | if (tree->CastRay(ray)) {
|
---|
| 74 | // cout<<"I1";
|
---|
| 75 |
|
---|
[492] | 76 | if (ray.intersections.size()) {
|
---|
| 77 | sort(ray.intersections.begin(), ray.intersections.end());
|
---|
| 78 | MeshInstance *mesh = (MeshInstance*)ray.intersections[0].mObject;
|
---|
| 79 | RgbColor color(1,1,1);
|
---|
| 80 | if (mesh->GetMesh()->mMaterial)
|
---|
| 81 | color = mesh->GetMesh()->mMaterial->mDiffuseColor;
|
---|
| 82 |
|
---|
| 83 | pbuffer[0] = color.r;
|
---|
| 84 | pbuffer[1] = color.g;
|
---|
| 85 | pbuffer[2] = color.b;
|
---|
| 86 | pbuffer[3] = 1.0f;
|
---|
| 87 |
|
---|
| 88 | }
|
---|
[1583] | 89 | }
|
---|
[191] | 90 | pbuffer+=components;
|
---|
[162] | 91 |
|
---|
[492] | 92 | // if (exportRays && ( 1||(x==222) && (y==97))) {
|
---|
| 93 | // if (exportRays && ((x%4==0) && (y%4==0))) {
|
---|
[878] | 94 |
|
---|
| 95 | // if (exportRays && ray.intersections.empty()) {
|
---|
| 96 | if (debug) {
|
---|
[492] | 97 | Ray *nray = new Ray(ray);
|
---|
| 98 | rays.push_back(nray);
|
---|
| 99 | }
|
---|
[162] | 100 | }
|
---|
[191] | 101 | // pbuffer-=2*components*mWidth;
|
---|
[162] | 102 | }
|
---|
| 103 |
|
---|
| 104 | long t2 = GetTime();
|
---|
[176] | 105 | cout<<"#RAY_CAST_TIME\n";
|
---|
| 106 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
| 107 |
|
---|
[492] | 108 |
|
---|
[176] | 109 | cout<<"Saving image"<<endl;
|
---|
[492] | 110 |
|
---|
[191] | 111 | ilRegisterType(IL_FLOAT);
|
---|
| 112 | ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
| 113 | ilSaveImage((char *const)filename.c_str());
|
---|
| 114 | delete buffer;
|
---|
[492] | 115 |
|
---|
| 116 | cout<<"done."<<endl<<flush;
|
---|
[191] | 117 |
|
---|
[162] | 118 | Exporter *exporter = NULL;
|
---|
[492] | 119 | if (1) {
|
---|
[162] | 120 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
[878] | 121 | //exporter->SetFilled();
|
---|
| 122 |
|
---|
| 123 | exporter->SetWireframe();
|
---|
| 124 |
|
---|
| 125 | if (sceneGraph)
|
---|
[1328] | 126 | exporter->ExportScene(sceneGraph->GetRoot());
|
---|
[492] | 127 |
|
---|
| 128 | //exporter->ExportKdTree(*tree);
|
---|
| 129 | //exporter->ExportBspTree(*bsptree);
|
---|
| 130 | exporter->ExportRays(rays, 2000);
|
---|
[162] | 131 | int k =0;
|
---|
| 132 | for (int j=0; j < rays.size(); j++)
|
---|
[386] | 133 | if (rays[j]->kdLeaves.size()) {
|
---|
[492] | 134 | Ray *ray = rays[j];
|
---|
| 135 | int i;
|
---|
[878] | 136 | exporter->SetWireframe();
|
---|
| 137 |
|
---|
| 138 | if (1)
|
---|
[492] | 139 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
| 140 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
[878] | 141 |
|
---|
| 142 | exporter->SetFilled();
|
---|
| 143 |
|
---|
| 144 | if (1)
|
---|
[492] | 145 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
| 146 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
[162] | 147 | }
|
---|
[492] | 148 |
|
---|
[162] | 149 | delete exporter;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | return true;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | void
|
---|
| 157 | Camera::SetupRay(Ray &ray, const int x, const int y)
|
---|
| 158 | {
|
---|
| 159 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
| 160 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
| 161 | Vector3 target = xv + yv + mDirection;
|
---|
| 162 |
|
---|
[466] | 163 | ray.Clear();
|
---|
[162] | 164 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
[878] | 165 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[162] | 166 | }
|
---|
[860] | 167 |
|
---|
[878] | 168 | }
|
---|