source: GTP/trunk/Lib/Vis/Preprocessing/src/Camera.cpp @ 492

Revision 492, 3.1 KB checked in by bittner, 19 years ago (diff)

Large merge - viewcells seem not functional now

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#include "SceneGraph.h"
14
15
16
17
18void
19InitDevIl()
20{
21  ilInit();
22  ILuint ImageName;
23  ilGenImages(1, &ImageName);
24  ilBindImage(ImageName);
25  ilEnable(IL_FILE_OVERWRITE);
26
27  //  ilRegisterFormat(IL_RGBA);
28  //  ilRegisterType(IL_FLOAT);
29
30  //  ilEnable(IL_ORIGIN_SET);
31  //  ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
32}
33
34bool
35Camera::SnapImage(string filename,
36                                  KdTree *tree,
37                                  SceneGraph *sceneGraph
38                                  )
39{
40  int x;
41  int y;
42
43  bool exportRays = true;
44
45 
46  InitDevIl();
47  int components = 4;
48  float *buffer = new float[components*mWidth*mHeight];
49  float *pbuffer = buffer;
50  //  - components*mWidth;
51 
52  vector<Ray *> rays;
53
54  long t1 = GetTime();
55
56  Ray ray;
57
58  for (y = 0; y < mHeight; y++) {
59    cout<<"+";
60    for (x = 0; x < mWidth; x++) {
61      SetupRay(ray, x, mHeight - (y + 1));
62      if (tree->CastRay(ray))
63                if (ray.intersections.size()) {
64                  sort(ray.intersections.begin(), ray.intersections.end());
65                  MeshInstance *mesh = (MeshInstance*)ray.intersections[0].mObject;
66                  RgbColor color(1,1,1);
67                  if (mesh->GetMesh()->mMaterial)
68                        color = mesh->GetMesh()->mMaterial->mDiffuseColor;
69                 
70                  pbuffer[0] = color.r;
71                  pbuffer[1] = color.g;
72                  pbuffer[2] = color.b;
73                  pbuffer[3] = 1.0f;
74                 
75                }
76      pbuffer+=components;
77     
78          //      if (exportRays && ( 1||(x==222) && (y==97))) {
79          //      if (exportRays && ((x%4==0) && (y%4==0))) {
80          if (exportRays && ray.intersections.empty()) {
81                Ray *nray = new Ray(ray);
82                rays.push_back(nray);
83          }
84    }
85    //    pbuffer-=2*components*mWidth;
86  }
87 
88  long t2 = GetTime();
89  cout<<"#RAY_CAST_TIME\n";
90  cout<<TimeDiff(t1, t2)<<"\n";
91 
92 
93  cout<<"Saving image"<<endl;
94 
95  ilRegisterType(IL_FLOAT);
96  ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
97  ilSaveImage((char *const)filename.c_str());
98  delete buffer;
99
100  cout<<"done."<<endl<<flush;
101 
102  Exporter *exporter = NULL;
103  if (1) {
104    exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
105    exporter->SetFilled();
106        if (sceneGraph)
107          exporter->ExportScene(sceneGraph->mRoot);
108       
109    //exporter->ExportKdTree(*tree);
110        //exporter->ExportBspTree(*bsptree);
111    exporter->ExportRays(rays, 2000);
112    exporter->SetFilled();
113    int k =0;
114    for (int j=0; j < rays.size(); j++)
115      if (rays[j]->kdLeaves.size()) {
116                Ray *ray = rays[j];
117                int i;
118                if (0)
119                  for (i= 0; i < ray->kdLeaves.size(); i++)
120                        exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
121                if (0)
122                  for (i= 0; i < ray->testedObjects.size(); i++)
123                        exporter->ExportIntersectable(ray->testedObjects[i]);
124               
125      }
126       
127    delete exporter;
128  }
129 
130  return true;
131}
132
133
134void
135Camera::SetupRay(Ray &ray, const int x, const int y)
136{
137  Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
138  Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
139  Vector3 target = xv + yv + mDirection;
140
141  ray.Clear();
142  ray.Init(mPosition, target, Ray::LOCAL_RAY);
143 
144}
Note: See TracBrowser for help on using the repository browser.