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

Revision 386, 3.0 KB checked in by bittner, 19 years ago (diff)

VssPreprocessor? updates - directional rays, x3dexporter updates - removed duplicated code for ray exports

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"
13
[191]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
[162]33bool
34Camera::SnapImage(string filename,
35                  KdTree *tree
36                  )
37{
38  int x;
39  int y;
40
41  bool exportRays = true;
42
43 
[191]44  InitDevIl();
45  int components = 4;
46  float *buffer = new float[components*mWidth*mHeight];
47  float *pbuffer = buffer;
48  //  - components*mWidth;
[179]49 
[386]50  vector<Ray *> rays;
[162]51
52  long t1 = GetTime();
53
54  Ray ray;
[191]55
[162]56  for (y = 0; y < mHeight; y++) {
57    cout<<"+";
58    for (x = 0; x < mWidth; x++) {
[191]59      SetupRay(ray, x, mHeight - (y + 1));
[162]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;
[182]67
[191]68          pbuffer[0] = color.r;
69          pbuffer[1] = color.g;
70          pbuffer[2] = color.b;
71          pbuffer[3] = 1.0f;
72
[162]73        }
[191]74      pbuffer+=components;
[162]75     
[386]76      if (exportRays && (x==222) && (y==97)) {
77                                Ray *nray = new Ray(ray);
78                                rays.push_back(nray);
79                        }
[162]80    }
[191]81    //    pbuffer-=2*components*mWidth;
[162]82  }
83 
84  long t2 = GetTime();
[176]85  cout<<"#RAY_CAST_TIME\n";
86  cout<<TimeDiff(t1, t2)<<"\n";
[191]87
[176]88 
89  cout<<"Saving image"<<endl;
[191]90
91  ilRegisterType(IL_FLOAT);
92  ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
93  ilSaveImage((char *const)filename.c_str());
94  delete buffer;
95 
[162]96  Exporter *exporter = NULL;
97  if (exportRays) {
98    exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
99    exporter->SetWireframe();
100    exporter->ExportKdTree(*tree);
[386]101                //exporter->ExportBspTree(*bsptree);
[162]102    exporter->ExportRays(rays, 10000);
103    exporter->SetFilled();
104    int k =0;
105    for (int j=0; j < rays.size(); j++)
[386]106      if (rays[j]->kdLeaves.size()) {
107                                Ray *ray = rays[j];
108                                int i;
109                                if (1)
110                                        for (i= 0; i < ray->kdLeaves.size(); i++)
111                                                exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
112                                if (0)
113                                        for (i= 0; i < ray->testedObjects.size(); i++)
114                                                exporter->ExportIntersectable(ray->testedObjects[i]);
115                               
[162]116      }
[386]117               
[162]118    delete exporter;
119  }
120 
121 
122  return true;
123}
124
125
126void
127Camera::SetupRay(Ray &ray, const int x, const int y)
128{
129  Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
130  Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
131  Vector3 target = xv + yv + mDirection;
132
133  ray.intersections.clear();
[362]134  ray.kdLeaves.clear();
[374]135  ray.testedObjects.clear();
[370]136  ray.bspIntersections.clear();
[362]137
[162]138  ray.Init(mPosition, target, Ray::LOCAL_RAY);
139 
140}
Note: See TracBrowser for help on using the repository browser.