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 = false;
|
---|
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 = true;
|
---|
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 | // cout<<"I1";
|
---|
75 |
|
---|
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 | }
|
---|
89 | }
|
---|
90 | pbuffer+=components;
|
---|
91 |
|
---|
92 | // if (exportRays && ( 1||(x==222) && (y==97))) {
|
---|
93 | // if (exportRays && ((x%4==0) && (y%4==0))) {
|
---|
94 |
|
---|
95 | // if (exportRays && ray.intersections.empty()) {
|
---|
96 | if (debug) {
|
---|
97 | Ray *nray = new Ray(ray);
|
---|
98 | rays.push_back(nray);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | // pbuffer-=2*components*mWidth;
|
---|
102 | }
|
---|
103 |
|
---|
104 | long t2 = GetTime();
|
---|
105 | cout<<"#RAY_CAST_TIME\n";
|
---|
106 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
107 |
|
---|
108 |
|
---|
109 | cout<<"Saving image"<<endl;
|
---|
110 |
|
---|
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;
|
---|
115 |
|
---|
116 | cout<<"done."<<endl<<flush;
|
---|
117 |
|
---|
118 | Exporter *exporter = NULL;
|
---|
119 | if (1) {
|
---|
120 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
121 | //exporter->SetFilled();
|
---|
122 |
|
---|
123 | exporter->SetWireframe();
|
---|
124 |
|
---|
125 | if (sceneGraph)
|
---|
126 | exporter->ExportScene(sceneGraph->GetRoot());
|
---|
127 |
|
---|
128 | //exporter->ExportKdTree(*tree);
|
---|
129 | //exporter->ExportBspTree(*bsptree);
|
---|
130 | exporter->ExportRays(rays, 2000);
|
---|
131 | int k =0;
|
---|
132 | for (int j=0; j < rays.size(); j++)
|
---|
133 | if (rays[j]->kdLeaves.size()) {
|
---|
134 | Ray *ray = rays[j];
|
---|
135 | int i;
|
---|
136 | exporter->SetWireframe();
|
---|
137 |
|
---|
138 | if (1)
|
---|
139 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
140 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
141 |
|
---|
142 | exporter->SetFilled();
|
---|
143 |
|
---|
144 | if (1)
|
---|
145 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
146 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
147 | }
|
---|
148 |
|
---|
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 |
|
---|
163 | ray.Clear();
|
---|
164 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
165 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
166 | }
|
---|
167 |
|
---|
168 | }
|
---|