source: trunk/VUT/GtpVisibilityPreprocessor/src/Camera.cpp @ 182

Revision 182, 2.3 KB checked in by mattausch, 19 years ago (diff)

the code is compiling under .net now

Line 
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
10bool
11Camera::SnapImage(string filename,
12                  KdTree *tree
13                  )
14{
15  int x;
16  int y;
17
18  bool exportRays = true;
19
20 
21#if QT_VERSION < 0x040000
22  QImage image(mWidth, mHeight, 32);
23#else
24  //QImage image(mWidth, mHeight, QImage::Format_RGB32);
25#endif
26 
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#if QT_VERSION < 0x040000
44          image.setPixel(x, y, qRgb(color.r*255,
45                                    color.g*255,
46                                    color.b*255
47                                    ));
48#endif
49
50        }
51     
52      if (exportRays && (x==222) && (y==97))
53        rays.push_back(ray);
54    }
55  }
56 
57  long t2 = GetTime();
58  cout<<"#RAY_CAST_TIME\n";
59  cout<<TimeDiff(t1, t2)<<"\n";
60 
61  cout<<"Saving image"<<endl;
62#if QT_VERSION < 0x040000
63  image.save(filename.c_str(), "PNG");
64#endif
65  Exporter *exporter = NULL;
66  if (exportRays) {
67    exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
68    exporter->SetWireframe();
69    exporter->ExportKdTree(*tree);
70    exporter->ExportRays(rays, 10000);
71    exporter->SetFilled();
72    int k =0;
73    for (int j=0; j < rays.size(); j++)
74      if (rays[j].leaves.size()) {
75        Ray *ray = &(rays[j]);
76        int i;
77        if (1)
78        for (i= 0; i < ray->leaves.size(); i++)
79          exporter->ExportBox(tree->GetBox(ray->leaves[i]));
80        if (0)
81        for (i= 0; i < ray->meshes.size(); i++)
82          exporter->ExportIntersectable(ray->meshes[i]);
83
84      }
85
86    delete exporter;
87  }
88 
89 
90  return true;
91}
92
93
94void
95Camera::SetupRay(Ray &ray, const int x, const int y)
96{
97  Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
98  Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
99  Vector3 target = xv + yv + mDirection;
100
101  ray.intersections.clear();
102  ray.leaves.clear();
103  ray.meshes.clear();
104 
105  ray.Init(mPosition, target, Ray::LOCAL_RAY);
106 
107}
Note: See TracBrowser for help on using the repository browser.