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 |
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 | void
|
---|
18 | InitDevIl()
|
---|
19 | {
|
---|
20 | ilInit();
|
---|
21 | ILuint ImageName;
|
---|
22 | ilGenImages(1, &ImageName);
|
---|
23 | ilBindImage(ImageName);
|
---|
24 | ilEnable(IL_FILE_OVERWRITE);
|
---|
25 |
|
---|
26 | // ilRegisterFormat(IL_RGBA);
|
---|
27 | // ilRegisterType(IL_FLOAT);
|
---|
28 |
|
---|
29 | // ilEnable(IL_ORIGIN_SET);
|
---|
30 | // ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
|
---|
31 | }
|
---|
32 |
|
---|
33 | bool
|
---|
34 | Camera::SnapImage(string filename,
|
---|
35 | KdTree *tree
|
---|
36 | )
|
---|
37 | {
|
---|
38 | int x;
|
---|
39 | int y;
|
---|
40 |
|
---|
41 | bool exportRays = true;
|
---|
42 |
|
---|
43 |
|
---|
44 | InitDevIl();
|
---|
45 | int components = 4;
|
---|
46 | float *buffer = new float[components*mWidth*mHeight];
|
---|
47 | float *pbuffer = buffer;
|
---|
48 | // - components*mWidth;
|
---|
49 |
|
---|
50 | vector<Ray *> rays;
|
---|
51 |
|
---|
52 | long t1 = GetTime();
|
---|
53 |
|
---|
54 | Ray ray;
|
---|
55 |
|
---|
56 | for (y = 0; y < mHeight; y++) {
|
---|
57 | cout<<"+";
|
---|
58 | for (x = 0; x < mWidth; x++) {
|
---|
59 | SetupRay(ray, x, mHeight - (y + 1));
|
---|
60 | if (tree->CastRay(ray))
|
---|
61 | if (ray.intersections.size()) {
|
---|
62 | sort(ray.intersections.begin(), ray.intersections.end());
|
---|
63 | MeshInstance *mesh = (MeshInstance*)ray.intersections[0].mObject;
|
---|
64 | RgbColor color(1,1,1);
|
---|
65 | if (mesh->GetMesh()->mMaterial)
|
---|
66 | color = mesh->GetMesh()->mMaterial->mDiffuseColor;
|
---|
67 |
|
---|
68 | pbuffer[0] = color.r;
|
---|
69 | pbuffer[1] = color.g;
|
---|
70 | pbuffer[2] = color.b;
|
---|
71 | pbuffer[3] = 1.0f;
|
---|
72 |
|
---|
73 | }
|
---|
74 | pbuffer+=components;
|
---|
75 |
|
---|
76 | if (exportRays && (x==222) && (y==97)) {
|
---|
77 | Ray *nray = new Ray(ray);
|
---|
78 | rays.push_back(nray);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | // pbuffer-=2*components*mWidth;
|
---|
82 | }
|
---|
83 |
|
---|
84 | long t2 = GetTime();
|
---|
85 | cout<<"#RAY_CAST_TIME\n";
|
---|
86 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
87 |
|
---|
88 |
|
---|
89 | cout<<"Saving image"<<endl;
|
---|
90 |
|
---|
91 | ilRegisterType(IL_FLOAT);
|
---|
92 | ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
93 | ilSaveImage((char *const)filename.c_str());
|
---|
94 | delete buffer;
|
---|
95 |
|
---|
96 | Exporter *exporter = NULL;
|
---|
97 | if (exportRays) {
|
---|
98 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
99 | exporter->SetWireframe();
|
---|
100 | exporter->ExportKdTree(*tree);
|
---|
101 | //exporter->ExportBspTree(*bsptree);
|
---|
102 | exporter->ExportRays(rays, 10000);
|
---|
103 | exporter->SetFilled();
|
---|
104 | int k =0;
|
---|
105 | for (int j=0; j < rays.size(); j++)
|
---|
106 | if (rays[j]->kdLeaves.size()) {
|
---|
107 | Ray *ray = rays[j];
|
---|
108 | int i;
|
---|
109 | if (1)
|
---|
110 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
111 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
112 | if (0)
|
---|
113 | for (i= 0; i < ray->testedObjects.size(); i++)
|
---|
114 | exporter->ExportIntersectable(ray->testedObjects[i]);
|
---|
115 |
|
---|
116 | }
|
---|
117 |
|
---|
118 | delete exporter;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | void
|
---|
127 | Camera::SetupRay(Ray &ray, const int x, const int y)
|
---|
128 | {
|
---|
129 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
130 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
131 | Vector3 target = xv + yv + mDirection;
|
---|
132 |
|
---|
133 | ray.Clear();
|
---|
134 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
135 |
|
---|
136 | }
|
---|