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

Revision 162, 2.2 KB checked in by bittner, 19 years ago (diff)

functional raycasting version

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