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