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

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

Large merge - viewcells seem not functional now

RevLine 
[162]1#include <algorithm>
[191]2
3// the devil library
4#include <IL/il.h>
5#include <IL/ilu.h>
6#include <IL/ilut.h>
7
[162]8#include "Camera.h"
9#include "Ray.h"
10#include "KdTree.h"
11#include "Mesh.h"
12#include "Exporter.h"
[492]13#include "SceneGraph.h"
[162]14
[191]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
[162]34bool
35Camera::SnapImage(string filename,
[492]36                                  KdTree *tree,
37                                  SceneGraph *sceneGraph
38                                  )
[162]39{
40  int x;
41  int y;
42
43  bool exportRays = true;
44
45 
[191]46  InitDevIl();
47  int components = 4;
48  float *buffer = new float[components*mWidth*mHeight];
49  float *pbuffer = buffer;
50  //  - components*mWidth;
[179]51 
[386]52  vector<Ray *> rays;
[162]53
54  long t1 = GetTime();
55
56  Ray ray;
[191]57
[162]58  for (y = 0; y < mHeight; y++) {
59    cout<<"+";
60    for (x = 0; x < mWidth; x++) {
[191]61      SetupRay(ray, x, mHeight - (y + 1));
[162]62      if (tree->CastRay(ray))
[492]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                }
[191]76      pbuffer+=components;
[162]77     
[492]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          }
[162]84    }
[191]85    //    pbuffer-=2*components*mWidth;
[162]86  }
87 
88  long t2 = GetTime();
[176]89  cout<<"#RAY_CAST_TIME\n";
90  cout<<TimeDiff(t1, t2)<<"\n";
91 
[492]92 
[176]93  cout<<"Saving image"<<endl;
[492]94 
[191]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;
[492]99
100  cout<<"done."<<endl<<flush;
[191]101 
[162]102  Exporter *exporter = NULL;
[492]103  if (1) {
[162]104    exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
105    exporter->SetFilled();
[492]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();
[162]113    int k =0;
114    for (int j=0; j < rays.size(); j++)
[386]115      if (rays[j]->kdLeaves.size()) {
[492]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               
[162]125      }
[492]126       
[162]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
[466]141  ray.Clear();
[162]142  ray.Init(mPosition, target, Ray::LOCAL_RAY);
143 
144}
Note: See TracBrowser for help on using the repository browser.