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

Revision 374, 2.9 KB checked in by bittner, 19 years ago (diff)
Line 
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
17void
18InitDevIl()
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
33bool
34Camera::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->ExportBspTree(*bsptree);
100    exporter->ExportRays(rays, 10000);
101    exporter->SetFilled();
102    int k =0;
103    for (int j=0; j < rays.size(); j++)
104      if (rays[j].kdLeaves.size()) {
105        Ray *ray = &(rays[j]);
106        int i;
107        if (1)
108        for (i= 0; i < ray->kdLeaves.size(); i++)
109          exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
110        if (0)
111                for (i= 0; i < ray->testedObjects.size(); i++)
112                        exporter->ExportIntersectable(ray->testedObjects[i]);
113
114      }
115
116    delete exporter;
117  }
118 
119 
120  return true;
121}
122
123
124void
125Camera::SetupRay(Ray &ray, const int x, const int y)
126{
127  Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
128  Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
129  Vector3 target = xv + yv + mDirection;
130
131  ray.intersections.clear();
132  ray.kdLeaves.clear();
133  ray.testedObjects.clear();
134  ray.bspIntersections.clear();
135
136  ray.Init(mPosition, target, Ray::LOCAL_RAY);
137 
138}
Note: See TracBrowser for help on using the repository browser.