1 | #include <algorithm>
|
---|
2 |
|
---|
3 | // the devil library
|
---|
4 | #include <IL/il.h>
|
---|
5 | #include <IL/ilu.h>
|
---|
6 | #include <IL/ilut.h>
|
---|
7 |
|
---|
8 | #include "Camera.h"
|
---|
9 | #include "Ray.h"
|
---|
10 | #include "KdTree.h"
|
---|
11 | #include "Mesh.h"
|
---|
12 | #include "Exporter.h"
|
---|
13 | #include "SceneGraph.h"
|
---|
14 |
|
---|
15 |
|
---|
16 | namespace GtpVisibilityPreprocessor {
|
---|
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 |
|
---|
35 | bool
|
---|
36 | Camera::SnapImage(string filename,
|
---|
37 | KdTree *tree,
|
---|
38 | SceneGraph *sceneGraph
|
---|
39 | )
|
---|
40 | {
|
---|
41 | int x;
|
---|
42 | int y;
|
---|
43 |
|
---|
44 | bool exportRays = true;
|
---|
45 |
|
---|
46 |
|
---|
47 | InitDevIl();
|
---|
48 | int components = 4;
|
---|
49 | float *buffer = new float[components*mWidth*mHeight];
|
---|
50 | float *pbuffer = buffer;
|
---|
51 | // - components*mWidth;
|
---|
52 |
|
---|
53 | vector<Ray *> rays;
|
---|
54 |
|
---|
55 | long t1 = GetTime();
|
---|
56 |
|
---|
57 | Ray ray;
|
---|
58 |
|
---|
59 | for (y = 0; y < mHeight; y++) {
|
---|
60 | cout<<"+";
|
---|
61 | for (x = 0; x < mWidth; x++) {
|
---|
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))
|
---|
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 | }
|
---|
87 | pbuffer+=components;
|
---|
88 |
|
---|
89 | // if (exportRays && ( 1||(x==222) && (y==97))) {
|
---|
90 | // if (exportRays && ((x%4==0) && (y%4==0))) {
|
---|
91 |
|
---|
92 | // if (exportRays && ray.intersections.empty()) {
|
---|
93 | if (debug) {
|
---|
94 | Ray *nray = new Ray(ray);
|
---|
95 | rays.push_back(nray);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | // pbuffer-=2*components*mWidth;
|
---|
99 | }
|
---|
100 |
|
---|
101 | long t2 = GetTime();
|
---|
102 | cout<<"#RAY_CAST_TIME\n";
|
---|
103 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
104 |
|
---|
105 |
|
---|
106 | cout<<"Saving image"<<endl;
|
---|
107 |
|
---|
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;
|
---|
112 |
|
---|
113 | cout<<"done."<<endl<<flush;
|
---|
114 |
|
---|
115 | Exporter *exporter = NULL;
|
---|
116 | if (1) {
|
---|
117 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
118 | //exporter->SetFilled();
|
---|
119 |
|
---|
120 | exporter->SetWireframe();
|
---|
121 |
|
---|
122 | if (sceneGraph)
|
---|
123 | exporter->ExportScene(sceneGraph->mRoot);
|
---|
124 |
|
---|
125 | //exporter->ExportKdTree(*tree);
|
---|
126 | //exporter->ExportBspTree(*bsptree);
|
---|
127 | exporter->ExportRays(rays, 2000);
|
---|
128 | int k =0;
|
---|
129 | for (int j=0; j < rays.size(); j++)
|
---|
130 | if (rays[j]->kdLeaves.size()) {
|
---|
131 | Ray *ray = rays[j];
|
---|
132 | int i;
|
---|
133 | exporter->SetWireframe();
|
---|
134 |
|
---|
135 | if (1)
|
---|
136 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
137 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
138 |
|
---|
139 | exporter->SetFilled();
|
---|
140 |
|
---|
141 | if (1)
|
---|
142 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
143 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
144 | }
|
---|
145 |
|
---|
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 |
|
---|
160 | ray.Clear();
|
---|
161 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
162 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
163 | }
|
---|
164 |
|
---|
165 | }
|
---|