1 | #include <algorithm>
|
---|
2 | #include <qstring.h>
|
---|
3 | #include <qimage.h>
|
---|
4 | #include "Camera.h"
|
---|
5 | #include "Ray.h"
|
---|
6 | #include "KdTree.h"
|
---|
7 | #include "Mesh.h"
|
---|
8 | #include "Exporter.h"
|
---|
9 |
|
---|
10 | bool
|
---|
11 | Camera::SnapImage(string filename,
|
---|
12 | KdTree *tree
|
---|
13 | )
|
---|
14 | {
|
---|
15 | int x;
|
---|
16 | int y;
|
---|
17 |
|
---|
18 | bool exportRays = true;
|
---|
19 |
|
---|
20 |
|
---|
21 |
|
---|
22 | // QImage image(mWidth, mHeight, 32); changed by matt
|
---|
23 | QImage image(mWidth, mHeight, QImage::Format_RGB32);
|
---|
24 |
|
---|
25 | vector<Ray> rays;
|
---|
26 |
|
---|
27 | long t1 = GetTime();
|
---|
28 |
|
---|
29 | Ray ray;
|
---|
30 | for (y = 0; y < mHeight; y++) {
|
---|
31 | cout<<"+";
|
---|
32 | for (x = 0; x < mWidth; x++) {
|
---|
33 | SetupRay(ray, x, y);
|
---|
34 | if (tree->CastRay(ray))
|
---|
35 | if (ray.intersections.size()) {
|
---|
36 | sort(ray.intersections.begin(), ray.intersections.end());
|
---|
37 | MeshInstance *mesh = (MeshInstance*)ray.intersections[0].mObject;
|
---|
38 | RgbColor color(1,1,1);
|
---|
39 | if (mesh->GetMesh()->mMaterial)
|
---|
40 | color = mesh->GetMesh()->mMaterial->mDiffuseColor;
|
---|
41 | image.setPixel(x, y, qRgb(color.r*255,
|
---|
42 | color.g*255,
|
---|
43 | color.b*255
|
---|
44 | ));
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (exportRays && (x==222) && (y==97))
|
---|
48 | rays.push_back(ray);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | long t2 = GetTime();
|
---|
53 | cout<<"#RAY_CAST_TIME\n";
|
---|
54 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
55 |
|
---|
56 | cout<<"Saving image"<<endl;
|
---|
57 | image.save(filename.c_str(), "PNG");
|
---|
58 |
|
---|
59 | Exporter *exporter = NULL;
|
---|
60 | if (exportRays) {
|
---|
61 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
62 | exporter->SetWireframe();
|
---|
63 | exporter->ExportKdTree(*tree);
|
---|
64 | exporter->ExportRays(rays, 10000);
|
---|
65 | exporter->SetFilled();
|
---|
66 | int k =0;
|
---|
67 | for (int j=0; j < rays.size(); j++)
|
---|
68 | if (rays[j].leaves.size()) {
|
---|
69 | Ray *ray = &(rays[j]);
|
---|
70 | int i;
|
---|
71 | if (1)
|
---|
72 | for (i= 0; i < ray->leaves.size(); i++)
|
---|
73 | exporter->ExportBox(tree->GetBox(ray->leaves[i]));
|
---|
74 | if (0)
|
---|
75 | for (i= 0; i < ray->meshes.size(); i++)
|
---|
76 | exporter->ExportIntersectable(ray->meshes[i]);
|
---|
77 |
|
---|
78 | }
|
---|
79 |
|
---|
80 | delete exporter;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | return true;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | void
|
---|
89 | Camera::SetupRay(Ray &ray, const int x, const int y)
|
---|
90 | {
|
---|
91 | Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
|
---|
92 | Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
|
---|
93 | Vector3 target = xv + yv + mDirection;
|
---|
94 |
|
---|
95 | ray.intersections.clear();
|
---|
96 | ray.leaves.clear();
|
---|
97 | ray.meshes.clear();
|
---|
98 |
|
---|
99 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
100 |
|
---|
101 | }
|
---|