void initCuda() { // First initialize OpenGL context, so we can properly set the GL // for CUDA. NVIDIA notes this is necessary in order to achieve // optimal performance with OpenGL/CUDA interop. use command-line // specified CUDA device, otherwise use device with highest Gflops/s int devCount= 0; cudaGetDeviceCount(&devCount); if( devCount < 1 ) { printf("No GPUS detected\n"); exit(EXIT_FAILURE); } cudaGLSetGLDevice( 0 ); //Set Up scenary setup_scene(); createPBO(&pbo); createTexture(&textureID,image_width,image_height); // Clean up on program exit atexit(cleanupCuda); runCuda(); }
void setup_scene(gua::SceneGraph& graph, std::shared_ptr<gua::node::Node> const& root_monkey, int depth_count) { if (depth_count > 0) { gua::TriMeshLoader loader; float offset(2.f); std::vector<gua::math::vec3> directions = { gua::math::vec3(0, offset, 0), gua::math::vec3(0, -offset, 0), gua::math::vec3(offset, 0, 0), gua::math::vec3(-offset, 0, 0), gua::math::vec3(0, 0, offset), gua::math::vec3(0, 0, -offset) }; for (auto direction: directions) { auto monkey_geometry(loader.create_geometry_from_file( "monkey", geometry, "data/materials/Stones.gmd" )); auto monkey = root_monkey->add_child(monkey_geometry); monkey->scale(0.5, 0.5, 0.5); monkey->translate(direction[0], direction[1], direction[2]); setup_scene(graph, monkey, depth_count - 1); } } }
void setup() { int ret, i; ret = info_scene(); for(i = 0; i < 60 * 3; ++i) { imagefilter_blurring({}, 0xF0, 0x100); delay_fps(60); } if(ret != 'y' && ret != 'Y') return; ret = getpath_scene(); for(i = 0; i < 60 * 1; ++i) { imagefilter_blurring({}, 0xF0, 0x100); delay_fps(60); } if(ret != 'y' && ret != 'Y') return; setup_scene(); for(i = 0; i < 60 * 1; ++i) { imagefilter_blurring({}, 0xF0, 0x100); delay_fps(60); } //for( ; kbhit() != -1; delay_fps(60)) { } }
int main(int argc, char** argv) { glutInit(&argc, argv); setup_app(); setup_menu(); setup_scene(); pi_setup(); glutTimerFunc(0, render_loop, 0); glutTimerFunc(0, logic_loop, 0); glutTimerFunc(0, ai_loop, 0); glutMainLoop(); return 0; }
int main(int argc, char** argv) { // initialize guacamole gua::init(argc, argv); gua::ShadingModelDatabase::load_shading_models_from("data/materials/"); gua::MaterialDatabase::load_materials_from("data/materials/"); // initialize Oculus SDK OVR::SensorFusion* oculus_sensor = init_oculus(); if (!oculus_sensor) return 1; // no oculus sensor found // setup scene gua::SceneGraph graph("main_scenegraph"); gua::TriMeshLoader loader; auto monkey_geometry(loader.create_geometry_from_file( "root_ape", geometry, "data/materials/Stones.gmd" )); auto root_monkey = graph.add_node("/", monkey_geometry); root_monkey->scale(0.5, 0.5, 0.5); // depth monkey cube car // 1 14.084 56 3.619.000 Vertices / 7 draw calls // 2 74.444 296 19.129.000 Vertices / 37 draw calls // 3 436.604 1.736 112.189.000 Vertices / 217 draw calls // 4 2.609.564 10.376 Vertices / 1.297 draw calls // 5 15.647.324 62.216 Vertices / 7.777 draw calls // 6 93.873.884 373.256 Vertices / 46.657 draw calls setup_scene(graph, root_monkey, 4); auto lights = add_lights(graph, 50); auto pos = graph.add_node<gua::node::TransformNode>("/", "pos"); pos->translate(0, 0, 2); auto nav = graph.add_node<gua::node::TransformNode>("/pos", "nav"); auto screen = graph.add_node<gua::node::ScreenNode>("/pos/nav", "screen_l"); screen->data.set_size(gua::math::vec2(0.08, 0.1)); screen->translate(-0.04, 0, -0.05f); screen = graph.add_node<gua::node::ScreenNode>("/pos/nav", "screen_r"); screen->data.set_size(gua::math::vec2(0.08, 0.1)); screen->translate(0.04, 0, -0.05f); auto eye = graph.add_node<gua::node::TransformNode>("/pos/nav", "eye_l"); eye->translate(-0.032, 0, 0); eye = graph.add_node<gua::node::TransformNode>("/pos/nav", "eye_r"); eye->translate(0.032, 0, 0); unsigned width = 1280/2; unsigned height = 800; auto pipe = new gua::Pipeline(); pipe->config.set_camera(gua::Camera("/pos/nav/eye_l", "/pos/nav/eye_r", "/pos/nav/screen_l", "/pos/nav/screen_r", "main_scenegraph")); pipe->config.set_left_resolution(gua::math::vec2ui(width, height)); pipe->config.set_right_resolution(gua::math::vec2ui(width, height)); pipe->config.set_enable_fps_display(true); pipe->config.set_enable_frustum_culling(true); pipe->config.set_enable_stereo(true); pipe->config.set_enable_ssao(true); pipe->config.set_ssao_intensity(2.f); pipe->config.set_enable_fxaa(true); pipe->config.set_enable_hdr(true); pipe->config.set_hdr_key(5.f); pipe->config.set_enable_bloom(true); pipe->config.set_bloom_radius(10.f); pipe->config.set_bloom_threshold(0.8f); pipe->config.set_bloom_intensity(0.8f); #if WIN32 auto oculus_window(new gua::OculusWindow("\\\\.\\DISPLAY1")); #else auto oculus_window(new gua::OculusWindow(":0.0")); #endif pipe->set_window(oculus_window); gua::Renderer renderer({ pipe }); gua::Timer timer; timer.start(); double time(0); float desired_frame_time(1.0 / 60.0); gua::events::MainLoop loop; // application loop gua::events::Ticker ticker(loop, desired_frame_time); ticker.on_tick.connect([&]() { double frame_time(timer.get_elapsed()); time += frame_time; timer.reset(); std::function<void (std::shared_ptr<gua::node::Node>, int)> rotate; rotate = [&](std::shared_ptr<gua::node::Node> node, int depth) { node->rotate(frame_time * (1+depth) * 0.5, 1, 1, 0); for (auto child: node->get_children()) { rotate(child, ++depth); } }; rotate(graph["/root_ape"], 1); for (int i = 0; i < lights.size(); ++i) { lights[i]->rotate( std::sin(time * (i * 0.1 + 0.5)) * frame_time * 2.5, 0, 1, 0); } graph["/root_ape"]->rotate(15 * frame_time, 0, 1, 0); //graph["/screen"]->rotate(20*frame_time, 0, 1, 0); nav->set_transform(get_oculus_transform(oculus_sensor)); renderer.queue_draw({&graph}); }); loop.start(); return 0; }
// MAIN int main(int argc, char ** argv) { int scene_id = 0; bool use_octree = false; char basename[256] = ""; if (argc > 1) scene_id = atoi(argv[1]); if (argc > 2) strcpy(basename, argv[2]); // scene_id 0, 1, 2, 3 is for tp 1 tests; switch (scene_id) { case 0: fill_red(output_image); break; case 1: fill(output_image, vector_init(0.7, 0.3, 0.1)); fill_rect(output_image, vector_init(0.2, 0.6, 0.7), vector_init(WIDTH / 4, HEIGHT / 5, 0), vector_init(WIDTH / 3, HEIGHT / 4, 0)); break; case 2: horizontal_gradient(output_image, vector_init(0, 0, 1), vector_init(1, 1, 1)); break; case 3: horizontal_gradient(output_image, vector_init(0, 0, 1), vector_init(1, 1, 1)); fill_circle(output_image, WIDTH / 20, vector_init(4 * WIDTH / 5, 4 * HEIGHT / 5, 0), vector_init(1, 1, 0)); fill_rect(output_image, vector_init(0.1, 0.8, 0.4), vector_init(0, 0, 0), vector_init(WIDTH, HEIGHT / 4, 0)); break; case 4: horizontal_gradient(output_image, vector_init(0.5, 0.8, 1), vector_init(1, 1, 1)); fill_circle(output_image, WIDTH / 20, vector_init(4 * WIDTH / 5, 4 * HEIGHT / 5, 0), vector_init(1, 1, 0)); fill_circle(output_image, WIDTH / 21, vector_init(3 * WIDTH / 5, 3 * HEIGHT / 5, 0), vector_init(1, 0.3, 0.1)); fill_rect(output_image, vector_init(0.85, 0.85, 0.3), vector_init(0, 0, 0), vector_init(WIDTH, HEIGHT / 4, 0)); break; default: setup_scene(scene_id - 5); raytrace(); break; } #ifdef USE_FREEIMAGE FreeImage_Initialise(); FIBITMAP *bitmap = FreeImage_Allocate(WIDTH, HEIGHT, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); if (bitmap) { // bitmap successfully created! // Calculate the number of bytes per pixel (3 for 24-bit or 4 for 32-bit) color *ptr = output_image; int bytespp = FreeImage_GetLine(bitmap) / FreeImage_GetWidth(bitmap); for(unsigned y = 0; y < FreeImage_GetHeight(bitmap); y++) { BYTE *bits = FreeImage_GetScanLine(bitmap, y); for(unsigned x = 0; x < FreeImage_GetWidth(bitmap); x++) { // Set pixel color to green with a transparency of 128 *ptr = vector_clamp(*ptr, 0.f, 1.f); bits[FI_RGBA_RED] = ptr->x*255; bits[FI_RGBA_GREEN] = ptr->y*255; bits[FI_RGBA_BLUE] = ptr->z*255; bits[FI_RGBA_ALPHA] = 255; // jump to next pixel bits += bytespp; ptr++; } } // FreeImage_Save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, const char *filename, int flags FI_DEFAULT(0)); char filename[256]; strcpy(filename, basename); strcat(filename, ".png"); if (FreeImage_Save(FIF_PNG, bitmap, filename, 0)) { // bitmap successfully saved! } FreeImage_Unload(bitmap); } FreeImage_DeInitialise(); #endif // le code ci dessous crée et sauvegarde une image sous le nom output.png dans le repertoire d'execution { FILE *fp = NULL; char filename[256]; strcpy(filename, basename); strcat(filename, ".ppm"); fp = fopen(filename, "w"); if (fp) { // création réussi fprintf(fp, "P3\n"); fprintf(fp, "%d %d\n255\n", WIDTH, HEIGHT); // La c'est pour faire la boucle for (unsigned y = 0; y < HEIGHT; y++) { color *ptr = &(output_image[(HEIGHT - y - 1) * WIDTH]); for (unsigned x = 0; x < WIDTH; x++) { *ptr = vector_clamp(*ptr, 0.f, 1.f); unsigned char r = ptr->x * 255; unsigned char g = ptr->y * 255; unsigned char b = ptr->z * 255; ptr++; fprintf(fp, "%d %d %d ", r, g, b); } fprintf(fp, "\n"); } fclose(fp); } } return 0; }
int Viewer::init(int argc, char *argv[]) { fmt::print("Start init\n"); for (auto i = 0; i < argc; i++) { std::string argv_str(argv[i]); if (argv_str == "-b1") { m_benchmark_mode = 1; m_max_frames = 500; } else if (argv_str == "-b2") { m_benchmark_mode = 2; m_max_frames = 250; } else if (argv_str == "-b3") { m_benchmark_mode = 3; m_max_frames = 125; } else if (argv_str == "-b4") { m_benchmark_mode = 4; m_max_frames = 50; } else if (argv_str == "-b5") { m_benchmark_mode = 5; m_max_frames = 25; } // Save image after n frames and quit auto found = argv_str.find("-f"); if (found != std::string::npos) { m_benchmark_mode = -1; m_max_frames = std::stoi(argv_str.substr(found + 2, argv_str.length())); } } if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) { std::cerr << "SDL_Init error: " << SDL_GetError() << std::endl; return 1; } m_window = SDL_CreateWindow("trac0r", 100, 100, m_screen_width, m_screen_height, SDL_WINDOW_SHOWN); if (m_window == nullptr) { std::cerr << "SDL_CreateWindow error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } // m_render = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED | // SDL_RENDERER_PRESENTVSYNC); m_render = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED); m_render_tex = SDL_CreateTexture(m_render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, m_screen_width, m_screen_height); m_pixels.resize(m_screen_width * m_screen_height, 0); if (m_render == nullptr) { SDL_DestroyWindow(m_window); std::cerr << "SDL_CreateRenderer error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } if (TTF_Init() != 0) { std::cerr << "SDL_ttf could not initialize! SDL_ttf error: " << SDL_GetError() << std::endl; return 1; } auto font_path = "res/DejaVuSansMono-Bold.ttf"; m_font = TTF_OpenFont(font_path, 14); if (m_font == nullptr) { std::cerr << "SDL_ttf could not open '" << font_path << "'" << std::endl; return 1; } // Setup scene setup_scene(); m_renderer = std::make_unique<trac0r::Renderer>(m_screen_width, m_screen_height, m_camera, m_scene, m_print_perf); m_renderer->print_sysinfo(); fmt::print("Finish init\n"); return 0; }