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

Revision 1582, 3.6 KB checked in by bittner, 18 years ago (diff)

ViewcellsManager? issue solved

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