/* main function in embree namespace */ int main(int argc, char** argv) { /* create stream for parsing */ Ref<ParseStream> stream = new ParseStream(new CommandLineStream(argc, argv)); /* parse command line */ parseCommandLine(stream, FileName()); if (g_numThreads) g_rtcore += ",threads=" + std::stringOf(g_numThreads); if (g_numBenchmarkFrames) g_rtcore += ",benchmark=1"; g_rtcore += g_subdiv_mode; /* load scene */ if (strlwr(filename.ext()) == std::string("obj")) { if (g_subdiv_mode != "") { std::cout << "enabling subdiv mode" << std::endl; loadOBJ(filename,one,g_obj_scene,true); } else loadOBJ(filename,one,g_obj_scene); } else if (strlwr(filename.ext()) == std::string("xml")) loadXML(filename,one,g_obj_scene); else if (filename.ext() != "") THROW_RUNTIME_ERROR("invalid scene type: "+strlwr(filename.ext())); /* initialize ray tracing core */ init(g_rtcore.c_str()); /* convert triangle meshes to subdiv meshes */ if (g_only_subdivs) g_obj_scene.convert_to_subdiv(); /* send model */ set_scene(&g_obj_scene); /* benchmark mode */ if (g_numBenchmarkFrames) renderBenchmark(outFilename); /* render to disk */ if (outFilename.str() != "") renderToFile(outFilename); /* interactive mode */ if (g_interactive) { initWindowState(argc,argv,tutorialName, g_width, g_height, g_fullscreen); enterWindowRunLoop(g_anim_mode); } return 0; }
namespace embree { /* name of the tutorial */ const char* tutorialName = "tutorial06"; /* configuration */ static std::string g_rtcore = ""; static size_t g_numThreads = 0; static std::string g_subdiv_mode = ""; /* output settings */ static size_t g_width = 512; static size_t g_height = 512; static bool g_fullscreen = false; static FileName outFilename = ""; static int g_skipBenchmarkFrames = 0; static int g_numBenchmarkFrames = 0; static bool g_interactive = true; static bool g_only_subdivs = false; static bool g_anim_mode = false; /* scene */ OBJScene g_obj_scene; static FileName filename = ""; static void parseCommandLine(Ref<ParseStream> cin, const FileName& path) { while (true) { std::string tag = cin->getString(); if (tag == "") return; /* parse command line parameters from a file */ else if (tag == "-c") { FileName file = path + cin->getFileName(); parseCommandLine(new ParseStream(new LineCommentFilter(file, "#")), file.path()); } /* load OBJ model*/ else if (tag == "-i") { filename = path + cin->getFileName(); } /* parse camera parameters */ else if (tag == "-vp") g_camera.from = cin->getVec3fa(); else if (tag == "-vi") g_camera.to = cin->getVec3fa(); else if (tag == "-vd") g_camera.to = g_camera.from + cin->getVec3fa(); else if (tag == "-vu") g_camera.up = cin->getVec3fa(); else if (tag == "-fov") g_camera.fov = cin->getFloat(); /* frame buffer size */ else if (tag == "-size") { g_width = cin->getInt(); g_height = cin->getInt(); } /* full screen mode */ else if (tag == "-fullscreen") g_fullscreen = true; /* output filename */ else if (tag == "-o") { outFilename = cin->getFileName(); g_interactive = false; } /* subdivision mode */ else if (tag == "-cache") g_subdiv_mode = ",subdiv_accel=bvh4.subdivpatch1cached"; else if (tag == "-lazy") g_subdiv_mode = ",subdiv_accel=bvh4.grid.lazy"; else if (tag == "-pregenerate") g_subdiv_mode = ",subdiv_accel=bvh4.grid.eager"; else if (tag == "-anim") g_anim_mode = true; /* number of frames to render in benchmark mode */ else if (tag == "-benchmark") { g_skipBenchmarkFrames = cin->getInt(); g_numBenchmarkFrames = cin->getInt(); g_interactive = false; } /* rtcore configuration */ else if (tag == "-rtcore") g_rtcore = cin->getString(); /* number of threads to use */ else if (tag == "-threads") g_numThreads = cin->getInt(); /* ambient light source */ else if (tag == "-ambientlight") { const Vec3fa L = cin->getVec3fa(); g_obj_scene.ambientLights.push_back(OBJScene::AmbientLight(L)); } /* point light source */ else if (tag == "-pointlight") { const Vec3fa P = cin->getVec3fa(); const Vec3fa I = cin->getVec3fa(); g_obj_scene.pointLights.push_back(OBJScene::PointLight(P,I)); } /* directional light source */ else if (tag == "-directionallight" || tag == "-dirlight") { const Vec3fa D = cin->getVec3fa(); const Vec3fa E = cin->getVec3fa(); g_obj_scene.directionalLights.push_back(OBJScene::DirectionalLight(D,E)); } /* distant light source */ else if (tag == "-distantlight") { const Vec3fa D = cin->getVec3fa(); const Vec3fa L = cin->getVec3fa(); const float halfAngle = cin->getFloat(); g_obj_scene.distantLights.push_back(OBJScene::DistantLight(D,L,halfAngle)); } /* converts triangle meshes into subdiv meshes */ else if (tag == "-subdiv") { g_only_subdivs = true; } /* skip unknown command line parameter */ else { std::cerr << "unknown command line parameter: " << tag << " "; while (cin->peek() != "" && cin->peek()[0] != '-') std::cerr << cin->getString() << " "; std::cerr << std::endl; } } } void renderBenchmark(const FileName& fileName) { resize(g_width,g_height); AffineSpace3fa pixel2world = g_camera.pixel2world(g_width,g_height); double dt = 0.0f; size_t numTotalFrames = g_skipBenchmarkFrames + g_numBenchmarkFrames; for (size_t i=0; i<numTotalFrames; i++) { double t0 = getSeconds(); render(0.0f,pixel2world.l.vx,pixel2world.l.vy,pixel2world.l.vz,pixel2world.p); double t1 = getSeconds(); std::cout << "frame [" << i << " / " << numTotalFrames << "] "; std::cout << 1.0/(t1-t0) << "fps "; if (i < g_skipBenchmarkFrames) std::cout << "(skipped)"; std::cout << std::endl; if (i >= g_skipBenchmarkFrames) dt += t1-t0; } std::cout << "frame [" << g_skipBenchmarkFrames << " - " << numTotalFrames << "] " << std::flush; std::cout << double(g_numBenchmarkFrames)/dt << "fps " << std::endl; std::cout << "BENCHMARK_RENDER " << double(g_numBenchmarkFrames)/dt << std::endl; } void renderToFile(const FileName& fileName) { resize(g_width,g_height); AffineSpace3fa pixel2world = g_camera.pixel2world(g_width,g_height); render(0.0f,pixel2world.l.vx,pixel2world.l.vy,pixel2world.l.vz,pixel2world.p); void* ptr = map(); Ref<Image> image = new Image4c(g_width, g_height, (Col4c*)ptr); storeImage(image, fileName); unmap(); cleanup(); } /* main function in embree namespace */ int main(int argc, char** argv) { /* create stream for parsing */ Ref<ParseStream> stream = new ParseStream(new CommandLineStream(argc, argv)); /* parse command line */ parseCommandLine(stream, FileName()); if (g_numThreads) g_rtcore += ",threads=" + std::stringOf(g_numThreads); if (g_numBenchmarkFrames) g_rtcore += ",benchmark=1"; g_rtcore += g_subdiv_mode; /* load scene */ if (strlwr(filename.ext()) == std::string("obj")) { if (g_subdiv_mode != "") { std::cout << "enabling subdiv mode" << std::endl; loadOBJ(filename,one,g_obj_scene,true); } else loadOBJ(filename,one,g_obj_scene); } else if (strlwr(filename.ext()) == std::string("xml")) loadXML(filename,one,g_obj_scene); else if (filename.ext() != "") THROW_RUNTIME_ERROR("invalid scene type: "+strlwr(filename.ext())); /* initialize ray tracing core */ init(g_rtcore.c_str()); /* convert triangle meshes to subdiv meshes */ if (g_only_subdivs) g_obj_scene.convert_to_subdiv(); /* send model */ set_scene(&g_obj_scene); /* benchmark mode */ if (g_numBenchmarkFrames) renderBenchmark(outFilename); /* render to disk */ if (outFilename.str() != "") renderToFile(outFilename); /* interactive mode */ if (g_interactive) { initWindowState(argc,argv,tutorialName, g_width, g_height, g_fullscreen); enterWindowRunLoop(g_anim_mode); } return 0; } }
/* main function in embree namespace */ int main(int argc, char** argv) { /* for best performance set FTZ and DAZ flags in MXCSR control and status register */ _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); /* create stream for parsing */ Ref<ParseStream> stream = new ParseStream(new CommandLineStream(argc, argv)); /* parse command line */ parseCommandLine(stream, FileName()); /* load default scene if none specified */ if (filename.ext() == "") { FileName file = FileName::executableFolder() + FileName("models/cornell_box.ecs"); parseCommandLine(new ParseStream(new LineCommentFilter(file, "#")), file.path()); } /* configure number of threads */ if (g_numThreads) g_rtcore += ",threads=" + std::to_string((long long)g_numThreads); if (g_numBenchmarkFrames) g_rtcore += ",benchmark=1"; g_rtcore += g_subdiv_mode; /* load scene */ if (strlwr(filename.ext()) == std::string("obj")) { if (g_subdiv_mode != "") { std::cout << "enabling subdiv mode" << std::endl; loadOBJ(filename,one,g_obj_scene,true); } else loadOBJ(filename,one,g_obj_scene); } else if (strlwr(filename.ext()) == std::string("xml")) loadXML(filename,one,g_obj_scene); else if (filename.ext() != "") THROW_RUNTIME_ERROR("invalid scene type: "+strlwr(filename.ext())); /* load keyframes */ if (keyframeList.str() != "") loadKeyFrameAnimation(keyframeList); /* initialize ray tracing core */ init(g_rtcore.c_str()); /* set shader mode */ switch (g_shader) { case SHADER_EYELIGHT: key_pressed(GLUT_KEY_F2); break; case SHADER_UV : key_pressed(GLUT_KEY_F4); break; case SHADER_NG : key_pressed(GLUT_KEY_F5); break; case SHADER_GEOMID : key_pressed(GLUT_KEY_F6); break; case SHADER_GEOMID_PRIMID: key_pressed(GLUT_KEY_F7); break; }; /* convert triangle meshes to subdiv meshes */ if (g_only_subdivs) g_obj_scene.convert_to_subdiv(); /* send model */ set_scene(&g_obj_scene); /* send keyframes */ if (g_keyframes.size()) set_scene_keyframes(&*g_keyframes.begin(),g_keyframes.size()); /* benchmark mode */ if (g_numBenchmarkFrames) renderBenchmark(outFilename); /* render to disk */ if (outFilename.str() != "") renderToFile(outFilename); /* interactive mode */ if (g_interactive) { initWindowState(argc,argv,tutorialName, g_width, g_height, g_fullscreen); enterWindowRunLoop(g_anim_mode); } return 0; }
namespace embree { /* name of the tutorial */ const char* tutorialName = "pathtracer"; /* configuration */ static std::string g_rtcore = ""; static size_t g_numThreads = 0; static std::string g_subdiv_mode = ""; /* output settings */ static size_t g_width = 512; static size_t g_height = 512; static bool g_fullscreen = false; static FileName outFilename = ""; static int g_skipBenchmarkFrames = 0; static int g_numBenchmarkFrames = 0; static bool g_interactive = true; static bool g_only_subdivs = false; static bool g_anim_mode = false; static bool g_loop_mode = false; static FileName keyframeList = ""; static Shader g_shader = SHADER_DEFAULT; /* scene */ OBJScene g_obj_scene; static FileName filename = ""; std::vector<OBJScene*> g_keyframes; static void parseCommandLine(Ref<ParseStream> cin, const FileName& path) { while (true) { std::string tag = cin->getString(); if (tag == "") return; /* parse command line parameters from a file */ else if (tag == "-c") { FileName file = path + cin->getFileName(); parseCommandLine(new ParseStream(new LineCommentFilter(file, "#")), file.path()); } /* load OBJ model*/ else if (tag == "-i") { filename = path + cin->getFileName(); } /* parse camera parameters */ else if (tag == "-vp") g_camera.from = cin->getVec3fa(); else if (tag == "-vi") g_camera.to = cin->getVec3fa(); else if (tag == "-vd") g_camera.to = g_camera.from + cin->getVec3fa(); else if (tag == "-vu") g_camera.up = cin->getVec3fa(); else if (tag == "-fov") g_camera.fov = cin->getFloat(); /* frame buffer size */ else if (tag == "-size") { g_width = cin->getInt(); g_height = cin->getInt(); } /* full screen mode */ else if (tag == "-fullscreen") g_fullscreen = true; /* output filename */ else if (tag == "-o") { outFilename = cin->getFileName(); g_interactive = false; } else if (tag == "-objlist") { keyframeList = cin->getFileName(); } /* subdivision mode */ else if (tag == "-cache") g_subdiv_mode = ",subdiv_accel=bvh4.subdivpatch1cached"; else if (tag == "-lazy") g_subdiv_mode = ",subdiv_accel=bvh4.grid.lazy"; else if (tag == "-pregenerate") g_subdiv_mode = ",subdiv_accel=bvh4.grid.eager"; else if (tag == "-loop") g_loop_mode = true; else if (tag == "-anim") g_anim_mode = true; else if (tag == "-shader") { std::string mode = cin->getString(); if (mode == "default" ) g_shader = SHADER_DEFAULT; else if (mode == "eyelight") g_shader = SHADER_EYELIGHT; else if (mode == "uv" ) g_shader = SHADER_UV; else if (mode == "Ng" ) g_shader = SHADER_NG; else if (mode == "geomID" ) g_shader = SHADER_GEOMID; else if (mode == "primID" ) g_shader = SHADER_GEOMID_PRIMID; else throw std::runtime_error("invalid shader:" +mode); } /* number of frames to render in benchmark mode */ else if (tag == "-benchmark") { g_skipBenchmarkFrames = cin->getInt(); g_numBenchmarkFrames = cin->getInt(); g_interactive = false; } /* rtcore configuration */ else if (tag == "-rtcore") g_rtcore = cin->getString(); /* number of threads to use */ else if (tag == "-threads") g_numThreads = cin->getInt(); /* ambient light source */ else if (tag == "-ambientlight") { const Vec3fa L = cin->getVec3fa(); g_obj_scene.ambientLights.push_back(OBJScene::AmbientLight(L)); } /* point light source */ else if (tag == "-pointlight") { const Vec3fa P = cin->getVec3fa(); const Vec3fa I = cin->getVec3fa(); g_obj_scene.pointLights.push_back(OBJScene::PointLight(P,I)); } /* directional light source */ else if (tag == "-directionallight" || tag == "-dirlight") { const Vec3fa D = cin->getVec3fa(); const Vec3fa E = cin->getVec3fa(); g_obj_scene.directionalLights.push_back(OBJScene::DirectionalLight(D,E)); } /* distant light source */ else if (tag == "-distantlight") { const Vec3fa D = cin->getVec3fa(); const Vec3fa L = cin->getVec3fa(); const float halfAngle = cin->getFloat(); g_obj_scene.distantLights.push_back(OBJScene::DistantLight(D,L,halfAngle)); } /* converts triangle meshes into subdiv meshes */ else if (tag == "-subdiv") { g_only_subdivs = true; } /* skip unknown command line parameter */ else { std::cerr << "unknown command line parameter: " << tag << " "; while (cin->peek() != "" && cin->peek()[0] != '-') std::cerr << cin->getString() << " "; std::cerr << std::endl; } } } void renderBenchmark(const FileName& fileName) { resize(g_width,g_height); AffineSpace3fa pixel2world = g_camera.pixel2world(g_width,g_height); double dt = 0.0f; size_t numTotalFrames = g_skipBenchmarkFrames + g_numBenchmarkFrames; for (size_t i=0; i<numTotalFrames; i++) { double t0 = getSeconds(); render(0.0f,pixel2world.l.vx,pixel2world.l.vy,pixel2world.l.vz,pixel2world.p); double t1 = getSeconds(); std::cout << "frame [" << i << " / " << numTotalFrames << "] "; std::cout << 1.0/(t1-t0) << "fps "; if (i < g_skipBenchmarkFrames) std::cout << "(skipped)"; std::cout << std::endl; if (i >= g_skipBenchmarkFrames) dt += t1-t0; } std::cout << "frame [" << g_skipBenchmarkFrames << " - " << numTotalFrames << "] " << std::flush; std::cout << double(g_numBenchmarkFrames)/dt << "fps " << std::endl; std::cout << "BENCHMARK_RENDER " << double(g_numBenchmarkFrames)/dt << std::endl; } void renderToFile(const FileName& fileName) { resize(g_width,g_height); if (g_anim_mode) g_camera.anim = true; do { double msec = getSeconds(); AffineSpace3fa pixel2world = g_camera.pixel2world(g_width,g_height); render(0.0f,pixel2world.l.vx,pixel2world.l.vy,pixel2world.l.vz,pixel2world.p); msec = getSeconds() - msec; std::cout << "render time " << 1.0/msec << " fps" << std::endl; } while(g_loop_mode); void* ptr = map(); Ref<Image> image = new Image4uc(g_width, g_height, (Col4uc*)ptr); storeImage(image, fileName); unmap(); cleanup(); } void loadKeyFrameAnimation(FileName &fileName) { PRINT(fileName); std::ifstream cin; cin.open(fileName.c_str()); if (!cin.is_open()) { std::cerr << "cannot open " << fileName.str() << std::endl; return; } FileName path; path = fileName.path(); char line[10000]; memset(line, 0, sizeof(line)); int cur = 0; while (cin.peek() != -1) { /* load next multiline */ char* pline = line; while (true) { cin.getline(pline, sizeof(line) - (pline - line) - 16, '\n'); ssize_t last = strlen(pline) - 1; if (last < 0 || pline[last] != '\\') break; pline += last; *pline++ = ' '; } OBJScene *scene = new OBJScene; FileName keyframe = path + FileName(line); loadOBJ(keyframe,one,*scene,true); PRINT(keyframe); if (g_obj_scene.subdiv.size() != scene->subdiv.size()) FATAL("#subdiv meshes differ"); for (size_t i=0;i<g_obj_scene.subdiv.size();i++) if (g_obj_scene.subdiv[i]->positions.size() != scene->subdiv[i]->positions.size()) FATAL("#positions differ"); g_keyframes.push_back(scene); } cin.close(); } /* main function in embree namespace */ int main(int argc, char** argv) { /* for best performance set FTZ and DAZ flags in MXCSR control and status register */ _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); /* create stream for parsing */ Ref<ParseStream> stream = new ParseStream(new CommandLineStream(argc, argv)); /* parse command line */ parseCommandLine(stream, FileName()); /* load default scene if none specified */ if (filename.ext() == "") { FileName file = FileName::executableFolder() + FileName("models/cornell_box.ecs"); parseCommandLine(new ParseStream(new LineCommentFilter(file, "#")), file.path()); } /* configure number of threads */ if (g_numThreads) g_rtcore += ",threads=" + std::to_string((long long)g_numThreads); if (g_numBenchmarkFrames) g_rtcore += ",benchmark=1"; g_rtcore += g_subdiv_mode; /* load scene */ if (strlwr(filename.ext()) == std::string("obj")) { if (g_subdiv_mode != "") { std::cout << "enabling subdiv mode" << std::endl; loadOBJ(filename,one,g_obj_scene,true); } else loadOBJ(filename,one,g_obj_scene); } else if (strlwr(filename.ext()) == std::string("xml")) loadXML(filename,one,g_obj_scene); else if (filename.ext() != "") THROW_RUNTIME_ERROR("invalid scene type: "+strlwr(filename.ext())); /* load keyframes */ if (keyframeList.str() != "") loadKeyFrameAnimation(keyframeList); /* initialize ray tracing core */ init(g_rtcore.c_str()); /* set shader mode */ switch (g_shader) { case SHADER_EYELIGHT: key_pressed(GLUT_KEY_F2); break; case SHADER_UV : key_pressed(GLUT_KEY_F4); break; case SHADER_NG : key_pressed(GLUT_KEY_F5); break; case SHADER_GEOMID : key_pressed(GLUT_KEY_F6); break; case SHADER_GEOMID_PRIMID: key_pressed(GLUT_KEY_F7); break; }; /* convert triangle meshes to subdiv meshes */ if (g_only_subdivs) g_obj_scene.convert_to_subdiv(); /* send model */ set_scene(&g_obj_scene); /* send keyframes */ if (g_keyframes.size()) set_scene_keyframes(&*g_keyframes.begin(),g_keyframes.size()); /* benchmark mode */ if (g_numBenchmarkFrames) renderBenchmark(outFilename); /* render to disk */ if (outFilename.str() != "") renderToFile(outFilename); /* interactive mode */ if (g_interactive) { initWindowState(argc,argv,tutorialName, g_width, g_height, g_fullscreen); enterWindowRunLoop(g_anim_mode); } return 0; } }