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 | using namespace std;
|
---|
16 |
|
---|
17 | namespace GtpVisibilityPreprocessor {
|
---|
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 |
|
---|
36 | bool
|
---|
37 | Camera::SnapImage(string filename,
|
---|
38 | KdTree *tree,
|
---|
39 | SceneGraph *sceneGraph
|
---|
40 | )
|
---|
41 | {
|
---|
42 | int x;
|
---|
43 | int y;
|
---|
44 |
|
---|
45 | bool exportRays = false;
|
---|
46 |
|
---|
47 |
|
---|
48 | InitDevIl();
|
---|
49 | int components = 4;
|
---|
50 | float *buffer = new float[components*mWidth*mHeight];
|
---|
51 | float *pbuffer = buffer;
|
---|
52 | // - components*mWidth;
|
---|
53 |
|
---|
54 | vector<Ray *> rays;
|
---|
55 |
|
---|
56 | long t1 = GetTime();
|
---|
57 |
|
---|
58 | Ray ray;
|
---|
59 |
|
---|
60 | for (y = 0; y < mHeight; y++) {
|
---|
61 | cout<<"+";
|
---|
62 | for (x = 0; x < mWidth; x++) {
|
---|
63 | SetupRay(ray, mWidth - (x + 1), mHeight - (y + 1));
|
---|
64 |
|
---|
65 | bool debug = true;
|
---|
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 |
|
---|
74 | if (tree->CastRay(ray)) {
|
---|
75 | // cout<<"I1";
|
---|
76 |
|
---|
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 | }
|
---|
90 | }
|
---|
91 | pbuffer+=components;
|
---|
92 |
|
---|
93 | // if (exportRays && ( 1||(x==222) && (y==97))) {
|
---|
94 | // if (exportRays && ((x%4==0) && (y%4==0))) {
|
---|
95 |
|
---|
96 | // if (exportRays && ray.intersections.empty()) {
|
---|
97 | if (debug) {
|
---|
98 | Ray *nray = new Ray(ray);
|
---|
99 | rays.push_back(nray);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | // pbuffer-=2*components*mWidth;
|
---|
103 | }
|
---|
104 |
|
---|
105 | long t2 = GetTime();
|
---|
106 | cout<<"#RAY_CAST_TIME\n";
|
---|
107 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
108 |
|
---|
109 |
|
---|
110 | cout<<"Saving image"<<endl;
|
---|
111 |
|
---|
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;
|
---|
116 |
|
---|
117 | cout<<"done."<<endl<<flush;
|
---|
118 |
|
---|
119 | Exporter *exporter = NULL;
|
---|
120 | if (1) {
|
---|
121 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
122 | //exporter->SetFilled();
|
---|
123 |
|
---|
124 | exporter->SetWireframe();
|
---|
125 |
|
---|
126 | if (sceneGraph)
|
---|
127 | exporter->ExportScene(sceneGraph->GetRoot());
|
---|
128 |
|
---|
129 | //exporter->ExportKdTree(*tree);
|
---|
130 | //exporter->ExportBspTree(*bsptree);
|
---|
131 | exporter->ExportRays(rays, 2000);
|
---|
132 | int k =0;
|
---|
133 | for (int j=0; j < rays.size(); j++)
|
---|
134 | if (rays[j]->kdLeaves.size()) {
|
---|
135 | Ray *ray = rays[j];
|
---|
136 | int i;
|
---|
137 | exporter->SetWireframe();
|
---|
138 |
|
---|
139 | if (1)
|
---|
140 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
141 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
142 |
|
---|
143 | exporter->SetFilled();
|
---|
144 |
|
---|
145 | if (1)
|
---|
146 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
147 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
148 | }
|
---|
149 |
|
---|
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 |
|
---|
164 | ray.Clear();
|
---|
165 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
166 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
167 | }
|
---|
168 |
|
---|
169 | }
|
---|