[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 |
|
---|
| 17 | void
|
---|
| 18 | InitDevIl()
|
---|
| 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] | 33 | bool
|
---|
| 34 | Camera::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 |
|
---|
[162] | 50 | vector<Ray> rays;
|
---|
| 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 |
|
---|
| 76 | if (exportRays && (x==222) && (y==97))
|
---|
| 77 | rays.push_back(ray);
|
---|
| 78 | }
|
---|
[191] | 79 | // pbuffer-=2*components*mWidth;
|
---|
[162] | 80 | }
|
---|
| 81 |
|
---|
| 82 | long t2 = GetTime();
|
---|
[176] | 83 | cout<<"#RAY_CAST_TIME\n";
|
---|
| 84 | cout<<TimeDiff(t1, t2)<<"\n";
|
---|
[191] | 85 |
|
---|
[176] | 86 |
|
---|
| 87 | cout<<"Saving image"<<endl;
|
---|
[191] | 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 |
|
---|
[162] | 94 | Exporter *exporter = NULL;
|
---|
| 95 | if (exportRays) {
|
---|
| 96 | exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
|
---|
| 97 | exporter->SetWireframe();
|
---|
| 98 | exporter->ExportKdTree(*tree);
|
---|
[263] | 99 | //exporter->ExportBspTree(*bsptree);
|
---|
[162] | 100 | exporter->ExportRays(rays, 10000);
|
---|
| 101 | exporter->SetFilled();
|
---|
| 102 | int k =0;
|
---|
| 103 | for (int j=0; j < rays.size(); j++)
|
---|
[362] | 104 | if (rays[j].kdLeaves.size()) {
|
---|
[162] | 105 | Ray *ray = &(rays[j]);
|
---|
| 106 | int i;
|
---|
| 107 | if (1)
|
---|
[362] | 108 | for (i= 0; i < ray->kdLeaves.size(); i++)
|
---|
| 109 | exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
|
---|
[162] | 110 | if (0)
|
---|
| 111 | for (i= 0; i < ray->meshes.size(); i++)
|
---|
[176] | 112 | exporter->ExportIntersectable(ray->meshes[i]);
|
---|
[162] | 113 |
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | delete exporter;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | return true;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | void
|
---|
| 125 | Camera::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();
|
---|
[362] | 132 | ray.kdLeaves.clear();
|
---|
[162] | 133 | ray.meshes.clear();
|
---|
[362] | 134 | ray.bspLeaves.clear();
|
---|
| 135 |
|
---|
[162] | 136 | ray.Init(mPosition, target, Ray::LOCAL_RAY);
|
---|
| 137 |
|
---|
| 138 | }
|
---|