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 | rays.push_back(ray);
|
---|
78 | }
|
---|
79 | // pbuffer-=2*components*mWidth;
|
---|
80 | }
|
---|
81 |
|
---|
82 | long t2 = GetTime();
|
---|
83 | cout<<"#RAY_CAST_TIME\n";
|
---|
84 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
85 |
|
---|
86 |
|
---|
87 | cout<<"Saving image"<<endl;
|
---|
88 |
|
---|
89 | ilRegisterType(IL_FLOAT);
|
---|
90 | ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
|
---|
91 | ilSaveImage((char *const)filename.c_str());
|
---|
92 | delete buffer;
|
---|
93 |
|
---|
94 | Exporter *exporter = NULL;
|
---|
95 | if (exportRays) {
|
---|
96 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
97 | exporter->SetWireframe();
|
---|
98 | exporter->ExportKdTree(*tree);
|
---|
99 | exporter->ExportRays(rays, 10000);
|
---|
100 | exporter->SetFilled();
|
---|
101 | int k =0;
|
---|
102 | for (int j=0; j < rays.size(); j++)
|
---|
103 | if (rays[j].leaves.size()) {
|
---|
104 | Ray *ray = &(rays[j]);
|
---|
105 | int i;
|
---|
106 | if (1)
|
---|
107 | for (i= 0; i < ray->leaves.size(); i++)
|
---|
108 | exporter->ExportBox(tree->GetBox(ray->leaves[i]));
|
---|
109 | if (0)
|
---|
110 | for (i= 0; i < ray->meshes.size(); i++)
|
---|
111 | exporter->ExportIntersectable(ray->meshes[i]);
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | delete exporter;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | return true;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | void
|
---|
124 | Camera::SetupRay(Ray &ray, const int x, const int y)
|
---|
125 | {
|
---|
126 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
127 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
128 | Vector3 target = xv + yv + mDirection;
|
---|
129 |
|
---|
130 | ray.intersections.clear();
|
---|
131 | ray.leaves.clear();
|
---|
132 | ray.meshes.clear();
|
---|
133 |
|
---|
134 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
135 |
|
---|
136 | }
|
---|