00001 #include <iostream> 00002 #include <exception> 00003 #include <ctime> 00004 #include <string> 00005 #include <limits> 00006 #include "math/rcrtmath.h" 00007 #include "RGBAColor.h" 00008 #include "primitives/Triangle.h" 00009 #include "primitives/Sphere.h" 00010 #include "BIH.hpp" 00011 #include "loaders/ObjectLoader.h" 00012 #include "loaders/SceneLoader.h" 00013 #include "loaders/AnimLoader.h" 00014 #include "lights/lights.h" 00015 #include "lights/sampling/AreaLightSampler.h" 00016 #include "cameras/PerspectiveCamera.h" 00017 #include "Scene.h" 00018 #include "textures/Texture.hpp" 00019 #include "tracing/DebugTracer.h" 00020 #include "tracing/RayCaster.h" 00021 #include "tracing/SimpleTracer.h" 00022 #include "tracing/BIHTracer.h" 00023 #include "tracing/StereoMetaTracer.h" 00024 #include "tracing/ThreadedMetaTracer.h" 00025 #include "textures/Image.h" 00026 #include "materials/Material.h" 00027 #include "materials/BSDFMaterial.h" 00028 #include "materials/DebugMaterial.h" 00029 #include "materials/BlackMaterial.h" 00030 #include "materials/MirrorMaterial.h" 00031 #include "materials/DielectricMaterial.h" 00032 #include "bxdf/BSDF.h" 00033 #include "bxdf/Lambertian.h" 00034 #include "bxdf/Phong.h" 00035 #include "bxdf/CookTorrance.h" 00036 #include "primitives/ConvexQuad.h" 00037 #include "primitives/DisplacedTriangle.h" 00038 #include "textures/ImageTexture.h" 00039 00040 00041 using namespace rcrt; 00042 using namespace std; 00043 /* 00044 void testTracer2() 00045 { 00046 PerspectiveCamera cam(49.134342, Point3D(-0.145180,-13.305592,3.270175), 00047 Vec3D(0.016829,0.999128,-0.038211), Vec3D(-0.005218,0.038304,0.999252), 640, 480); 00048 cout << "cam" << endl; 00049 00050 Texture<Point2D>* tex; 00051 00052 vector<Traceable*>* list = new vector<Traceable*>(); 00053 clock_t start = clock(); 00054 ObjectLoader ol("mirror-s_Scene-o_Cone.xml"); 00055 clock_t end = clock(); 00056 double dif = end-start; 00057 cout << "loading and building time: " << dif/CLOCKS_PER_SEC << " s" << endl; 00058 Object* suzanne = ol.getObject(); 00059 BSDF lambert; 00060 BSDF phong; 00061 lambert.addBXDF(new Lambertian(RGBColor(0.6,0.5,0.9))); 00062 phong.addBXDF(new Phong(RGBColor(0.6,0.5,0.9),0.6,0.4,10)); 00063 BSDFMaterial matP = BSDFMaterial(phong); 00064 BSDFMaterial matL = BSDFMaterial(lambert); 00065 BSDF cooktorr; 00066 //Gold :: 0.370 :: 2.820 00067 //Silver :: 0.177 :: 3.638 00068 //Copper :: 0.617 :: 2.63 00069 //Steel :: 2.485 :: 3.433 00070 cooktorr.addBXDF(new CookTorrance(RGBColor(0.427, 0.278, 0), 0.40, 0.4, complex<float>(0.370,2.820))); 00071 BSDFMaterial mc(cooktorr); 00072 suzanne->setMaterial(&mc); 00073 list->push_back(suzanne); 00074 //plane->setMaterial(&matL); 00075 */ 00076 /*AffineObject spheres1("Spheres1"); 00077 AffineObject spheres2("Spheres2"); 00078 vector<Primitive*>* spList1 = new vector<Primitive*>(); 00079 vector<Primitive*>* spList2 = new vector<Primitive*>(); 00080 Sphere* sph1 = new Sphere(Point3D(1.5,0,0), 0.3, &spheres1); 00081 Sphere* sph2 = new Sphere(Point3D(-2.5,0,-0.5), 1.0, &spheres2); 00082 spList1->push_back(sph1); 00083 spList2->push_back(sph2); 00084 spheres1.setPrimitives(spList1); 00085 spheres2.setPrimitives(spList2); 00086 spheres1.setMaterial(&matL); 00087 spheres2.setMaterial(&matP); 00088 list->push_back(&spheres1);*/ 00089 //list->push_back(&spheres2); 00090 /*vector<Triangle*>* objList = ol.getTriangles(); 00091 vector<Primitive*> trList; 00092 for(unsigned int i = 0; i < objList->size(); i++) 00093 trList.push_back(objList->at(i)); 00094 */ 00095 /* 00096 try { 00097 PointLight* light1 = new PointLight(RGBColor(3,3,3),Point3D(3,0,0)); 00098 PointLight* light2 = new PointLight(RGBColor(3,3,3),Point3D(2,1,0)); 00099 PointLight* light3 = new PointLight(RGBColor(2,3,1),Point3D(0,2,0)); 00100 PointLight* light4 = new PointLight(RGBColor(3,3,1),Point3D(0,-2,0)); 00101 AreaLight* alight1 = new AreaLight(RGBColor(6,6,6), Point3D(3,0,0), Vec3D(0,0,2), Vec3D(0,2,0)); 00102 AreaLightSampler* alSampler = new AreaLightSampler(alight1); 00103 alight1->setSampler(alSampler); 00104 //list->push_back(alight1->getObject()); 00105 Scene s(list); 00106 s.setShadows(false); 00107 //s.addLight(light1); 00108 //s.addLight(light2); 00109 // s.addLight(light3); 00110 // s.addLight(light4); 00111 s.addLight(alight1); 00112 cout << "scene" << endl; 00113 //SimpleTracer rt(&s); 00114 //RayCaster rt(&s); 00115 //RayCaster tr(&s); 00116 //StereoMetaTracer rt(cam,0.05,tr); 00117 DebugTracer rt(&s); 00118 cout << "tracer" << endl; 00119 Image img = rt.trace(&cam); 00120 //Image img = rt.trace(cam, spList); 00121 cout << "tracing image" << endl; 00122 img.WritePNG("testBug.png"); 00123 cout << "fertsch" << endl; 00124 cout << "epsilon=" << numeric_limits<float>::epsilon() << endl; 00125 }catch(exception e){ 00126 cout << "exception: " << e.what() << endl; 00127 } 00128 00129 }*/ 00130 00131 void testLoaders() 00132 { 00133 SceneLoader sceneLoader("test-s_Scene.xml.bz2"); 00134 Scene* scene = sceneLoader.getScene(); 00135 Camera* cam = sceneLoader.getCamera(); 00136 cout << "Finished loading Scene: " << scene->getName() << endl; 00137 cout << "Tracing loaded Scene: " << scene->getName() << endl; 00138 RayCaster rt(scene); 00139 //DebugTracer rt(scene); 00140 Image img = rt.trace(cam); 00141 img.WritePNG("testscene.png"); 00142 cout << "Tracing finished" << endl; 00143 } 00144 /* 00145 void testMirror() 00146 { 00147 SceneLoader sceneLoader("boxrefract-s_Scene.xml"); 00148 Scene* scene = sceneLoader.getScene(); 00149 scene->setShadows(false); 00150 Camera* cam = sceneLoader.getCamera(); 00151 cout << "resX=" << cam->getResolutionX() << " resY=" << cam->getResolutionY() << endl; 00152 const vector<ObjectLoader*>& ols = sceneLoader.getObjectLoaders(); 00153 BSDF lambert; 00154 BSDF phong; 00155 lambert.addBXDF(new Lambertian(RGBColor(0.6,0.5,0.9))); 00156 phong.addBXDF(new Phong(RGBColor(0.6,0.5,0.9),0.6,0.4,10)); 00157 BSDF cooktorr; 00158 complex<float> goldIOR(0.370,2.820);//Gold :: 0.370 :: 2.820 00159 complex<float> silverIOR(0.177,3.638);//Silver :: 0.177 :: 3.638 00160 complex<float> copperIOR(0.617,2.63);//Copper :: 0.617 :: 2.63 00161 complex<float> steelIOR(2.485,3.433);//Steel :: 2.485 :: 3.433 00162 BSDFMaterial matP = BSDFMaterial(phong, steelIOR); 00163 BSDFMaterial matL = BSDFMaterial(lambert, steelIOR); 00164 cooktorr.addBXDF(new CookTorrance(RGBColor(0.427, 0.278, 0), 0.40, 0.4, goldIOR)); 00165 BSDFMaterial mc(cooktorr, goldIOR); 00166 BSDF cooktorr2; 00167 cooktorr2.addBXDF(new CookTorrance(RGBColor(0.67, 0.5, 0.5), 0.3, 0.5, silverIOR)); 00168 BSDFMaterial mc2(cooktorr2, silverIOR); 00169 DebugMaterial debug; 00170 BlackMaterial black; 00171 DielectricMaterial glass(1.519,RGBColor(1,1,0.5)); 00172 MirrorMaterial mirr(RGBColor(1,1,1)); 00173 ols[0]->getObject()->setMaterial(&mc); 00174 ols[1]->getObject()->setMaterial(&mc); 00175 ols[2]->getObject()->setMaterial(&mc); 00176 ols[3]->getObject()->setMaterial(&mc); 00177 ols[4]->getObject()->setMaterial(&mc); 00178 ols[5]->getObject()->setMaterial(&mc); 00179 ols[6]->getObject()->setMaterial(&glass); 00180 ols[7]->getObject()->setMaterial(&matL); 00181 ols[8]->getObject()->setMaterial(&matL); 00182 ols[9]->getObject()->setMaterial(&matL); 00183 ols[10]->getObject()->setMaterial(&matL); 00184 ols[11]->getObject()->setMaterial(&matL); 00185 ols[12]->getObject()->setMaterial(&matL); 00186 //RayCaster st(scene); 00187 SimpleTracer rt(scene); 00188 ThreadedMetaTracer st(&rt); 00189 //DebugTracer st(scene); 00190 //SimpleTracer st(scene); 00191 Image img = st.trace(cam); 00192 img.WritePNG("testrefract.png"); 00193 } 00194 */ 00195 void dummySBIH() { 00196 00197 ObjectLoader ol("boxrefract-s_Scene-o_Torus.xml.bz2"); 00198 00199 std::cout << "building KDTree" << std::endl; 00200 00201 //SAHKDtree<Triangle> blubb(ol.getTriangles(), 50, 2); 00202 } 00203 00204 void testQuad() 00205 { 00206 ConvexQuad quad(0,Point3D(-1,0,-1), Point3D(-1,0,1), Point3D(1,1,2), Point3D(1,1,-1)); 00207 vector<Traceable*>* list = new vector<Traceable*>(); 00208 list->push_back(&quad); 00209 Scene scene(list); 00210 std::cout << "debugtracer" << std::endl; 00211 DebugTracer rt(&scene); 00212 std::cout << "camera" << std::endl; 00213 PerspectiveCamera cam(60, Point3D(4,0.5,2), Vec3D(-1,-0.125,-0.5), Vec3D(0,1,0), 640, 480); 00214 std::cout << "tracing" << std::endl; 00215 Image img = rt.trace(&cam); 00216 img.WritePNG("quadtest.png"); 00217 //Ray r(Point3D(0,5,0),Vec3D(0,-1,0)) 00218 } 00219 /* 00220 void testBump() 00221 { 00222 //ConvexQuad quad(0,Point3D(-1,0,0), Point3D(-1,0,1), Point3D(1,0,1), Point3D(1,0,0)); 00223 vector<Traceable*>* list = new vector<Traceable*>(); 00224 //list->push_back(&quad); 00225 AffineObject bumpTest("hurz"); 00226 bumpTest.setWorldMatrix(Matrix4D::identity()); 00227 //DisplacedTriangle t(new Vertex(Point3D(-2,0,-2),Vec3D(0,1,0),Point2D(50,50)),new Vertex(Point3D(2,0,-2),Vec3D(0,1,0),Point2D(50,300)),new Vertex(Point3D(2,0,2),Vec3D(0,1,0),Point2D(300,300)),&bumpTest); 00228 Triangle t(new Vertex(Point3D(-2,0,-2),Vec3D(0,1,0),Point2D(0.1,0.1)),new Vertex(Point3D(2,0,-2),Vec3D(0,1,0),Point2D(0.1,0.9)),new Vertex(Point3D(2,-1,2),Vec3D(0,1,0),Point2D(0.9,0.9)),&bumpTest); 00229 vector<Primitive*>* prList = new vector<Primitive*>(); 00230 prList->push_back(&t); 00231 00232 Image texIm; 00233 00234 std::cout << "reading texture" << std::endl; 00235 bool success = texIm.ReadPNG("StoneWall5.png"); 00236 std::cout << "textureread " << success << std::endl; 00237 00238 ImageTexture tex(&texIm); 00239 00240 BSDF lambert; 00241 BSDF phong; 00242 lambert.addBXDF(new Lambertian(RGBColor(0.6,0.5,0.9))); 00243 phong.addBXDF(new Phong(RGBColor(0.6,0.5,0.9),0.6,0.4,10)); 00244 BSDFMaterial matP = BSDFMaterial(phong); 00245 BSDFMaterial matL = BSDFMaterial(lambert); 00246 00247 matP.setBumpMap(&tex); 00248 matP.setBumpFactor(0.1); 00249 00250 // bumpTest.setDisplDens(20); 00251 // bumpTest.setMaxDispl(0.4); 00252 // bumpTest.setDisplMap(&tex); 00253 00254 bumpTest.setMaterial(&matP); 00255 bumpTest.setPrimitives(prList); 00256 00257 list->push_back(&bumpTest); 00258 00259 Scene scene(list); 00260 00261 PointLight* light1 = new PointLight(RGBColor(15,15,15),Point3D(0.25,2,0)); 00262 scene.addLight(light1); 00263 00264 RayCaster rt(&scene); 00265 PerspectiveCamera cam(60, Point3D(0,4,0), Vec3D(0,-1,0), Vec3D(1,0,0), 640, 480); 00266 std::cout << "tracing" << std::endl; 00267 Image img = rt.trace(&cam); 00268 img.WritePNG("bump.png"); 00269 //Ray r(Point3D(0,5,0),Vec3D(0,-1,0)) 00270 } 00271 */ 00272 void testDispl1() 00273 { 00274 //ConvexQuad quad(0,Point3D(-1,0,0), Point3D(-1,0,1), Point3D(1,0,1), Point3D(1,0,0)); 00275 vector<Traceable*>* list = new vector<Traceable*>(); 00276 //list->push_back(&quad); 00277 00278 Image texIm; 00279 00280 std::cout << "reading texture" << std::endl; 00281 bool success = texIm.ReadPNG("StoneWall5.png"); 00282 std::cout << "textureread " << success << std::endl; 00283 00284 ImageTexture tex(&texIm); 00285 00286 AffineObject bumpTest("hurz"); 00287 00288 bumpTest.setDisplMap(&tex); 00289 bumpTest.setDisplDens(3); 00290 bumpTest.setMaxDispl(0.6); 00291 00292 bumpTest.setWorldMatrix(Matrix4D::identity()); 00293 00294 // Vec3D vn1 = Vec3D(-0.1,1,-0.1); 00295 // Vec3D vn2 = Vec3D(0.1,1,-0.1); 00296 // Vec3D vn3 = Vec3D(0.1,1,0.1); 00297 00298 Vec3D vn1 = Vec3D(0,1,0); 00299 Vec3D vn2 = Vec3D(0,1,0); 00300 Vec3D vn3 = Vec3D(0,1,0); 00301 00302 DisplacedTriangle t(new Vertex(Point3D(-2,0,-2),vn1,Point2D(0.1,0.1)),new Vertex(Point3D(2,0,-1),vn2,Point2D(0.1,0.9)),new Vertex(Point3D(1,0,2),vn3,Point2D(0.9,0.9)),&bumpTest); 00303 vector<Primitive*>* prList = new vector<Primitive*>(); 00304 prList->push_back(&t); 00305 00306 BSDF lambert; 00307 BSDF phong; 00308 lambert.addBXDF(new Lambertian(RGBColor(0.6,0.5,0.9))); 00309 phong.addBXDF(new Phong(RGBColor(0.6,0.5,0.9),RGBColor(0.4,0.5,0.1),10)); 00310 BSDFMaterial matP = BSDFMaterial(phong); 00311 BSDFMaterial matL = BSDFMaterial(lambert); 00312 00313 matP.setBumpMap(&tex); 00314 matP.setBumpFactor(0.1); 00315 00316 DebugMaterial dm; 00317 00318 // bumpTest.setDisplDens(20); 00319 // bumpTest.setMaxDispl(0.4); 00320 // bumpTest.setDisplMap(&tex); 00321 00322 bumpTest.setMaterial(&dm); 00323 bumpTest.setPrimitives(prList); 00324 00325 list->push_back(&bumpTest); 00326 00327 Scene scene(list); 00328 00329 PointLight* light1 = new PointLight(RGBColor(15,15,15),Point3D(0.25,2,0)); 00330 scene.addLight(light1); 00331 00332 DebugTracer rt(&scene); 00333 PerspectiveCamera cam(60, Point3D(0,4,0), Vec3D(0,-1,0), Vec3D(1,0,0), 640, 480); 00334 std::cout << "tracing" << std::endl; 00335 Ray r = cam.getRay(Point2D(540,360)); 00336 //rt.trace(r); 00337 Image img = rt.trace(&cam); 00338 img.WritePNG("displ.png"); 00339 //Ray r(Point3D(0,5,0),Vec3D(0,-1,0)) 00340 } 00341 00342 /* 00343 void testBug() 00344 { 00345 SceneLoader sceneLoader("glass-s_Scene.xml.bz2"); 00346 const vector<ObjectLoader*>& ols = sceneLoader.getObjectLoaders(); 00347 Scene* scene = sceneLoader.getScene(); 00348 scene->setShadows(false); 00349 BSDF lambert; 00350 BSDF phong; 00351 lambert.addBXDF(new Lambertian(RGBColor(0.6,0.5,0.9))); 00352 phong.addBXDF(new Phong(RGBColor(0.6,0.5,0.5),0.3,0.7,30)); 00353 BSDF cooktorr; 00354 complex<float> goldIOR(0.370,2.820);//Gold :: 0.370 :: 2.820 00355 complex<float> silverIOR(0.177,3.638);//Silver :: 0.177 :: 3.638 00356 complex<float> copperIOR(0.617,2.63);//Copper :: 0.617 :: 2.63 00357 complex<float> steelIOR(2.485,3.433);//Steel :: 2.485 :: 3.433 00358 BSDFMaterial matP = BSDFMaterial(phong, steelIOR); 00359 BSDFMaterial matL = BSDFMaterial(lambert, steelIOR); 00360 cooktorr.addBXDF(new CookTorrance(RGBColor(0.95294, 0.97254, 1), 0.08, 0.9, silverIOR)); 00361 BSDFMaterial mc(cooktorr, silverIOR); 00362 BSDF cooktorr2; 00363 cooktorr2.addBXDF(new CookTorrance(RGBColor(0.67, 0.5, 0.5), 0.3, 0.5, silverIOR)); 00364 BSDFMaterial mc2(cooktorr2, silverIOR); 00365 DebugMaterial debug; 00366 BlackMaterial black; 00367 DielectricMaterial glass(1.4,RGBColor(1,1,1)); 00368 MirrorMaterial mirr(RGBColor(1,1,1)); 00369 ols[0]->getObject()->setMaterial(&mc); 00370 ols[1]->getObject()->setMaterial(&mc); 00371 ols[2]->getObject()->setMaterial(&mc); 00372 ols[3]->getObject()->setMaterial(&mc); 00373 ols[4]->getObject()->setMaterial(&mc); 00374 ols[5]->getObject()->setMaterial(&glass); 00375 //ols[6]->getObject()->setMaterial(&mc); 00376 Camera* cam = sceneLoader.getCamera(); 00377 SimpleTracer rt(scene); 00378 ThreadedMetaTracer st(&rt); 00379 Image img = st.trace(cam); 00380 img.WritePNG("testShadows.png"); 00381 }*/