/// idle event handler void idle() { if(animating) { hud_fps_update.start(); if(animating_steps) animate_step(true); else animate_update(); hud_fps_update.stop(); if(not animating) hud_fps_update.reset(); glutPostRedisplay(); } #ifdef AUTORELOAD if(reload_auto) { static int tm = 0; struct stat st; stat(filename_scene.c_str(),&st); if(tm != st.st_mtime) reload(); tm = st.st_mtime; } #endif }
// uiloop void uiloop() { auto ok = glfwInit(); error_if_not(ok, "glfw init error"); // setting an error callback glfwSetErrorCallback([](int ecode, const char* msg){ return error(msg); }); // glfwWindowHint(GLFW_SAMPLES, scene->image_samples*scene->image_samples); auto window = glfwCreateWindow(scene->image_width, scene->image_height, "graphics13 | animate", NULL, NULL); error_if_not(window, "glfw window error"); glfwMakeContextCurrent(window); glfwSetCharCallback(window, [](GLFWwindow* window, unsigned int key) { switch (key) { case 's': scene->draw_captureimage = true; break; case ' ': scene->draw_animated = not scene->draw_animated; break; case '.': animate_update(scene); break; case 'g': scene->animation->gpu_skinning = not scene->animation->gpu_skinning; animate_reset(scene); break; case 'n': scene->draw_normals = not scene->draw_normals; break; case 'w': scene->draw_wireframe = not scene->draw_wireframe; break; } }); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); #ifdef _WIN32 auto ok1 = glewInit(); error_if_not(GLEW_OK == ok1, "glew init error"); #endif auto state = new ShadeState(); init_shaders(state); init_textures(scene,state); animate_reset(scene); auto mouse_last_x = -1.0; auto mouse_last_y = -1.0; auto last_update_time = glfwGetTime(); while(not glfwWindowShouldClose(window)) { auto title = tostring("graphics14 | animate | %03d", scene->animation->time); glfwSetWindowTitle(window, title.c_str()); if(scene->draw_animated) { if(glfwGetTime() - last_update_time > scene->animation->dt) { last_update_time = glfwGetTime(); animate_update(scene); } } glfwGetFramebufferSize(window, &scene->image_width, &scene->image_height); scene->camera->width = (scene->camera->height * scene->image_width) / scene->image_height; shade(scene,state); if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)) { double x, y; glfwGetCursorPos(window, &x, &y); if (mouse_last_x < 0 or mouse_last_y < 0) { mouse_last_x = x; mouse_last_y = y; } auto delta_x = x - mouse_last_x, delta_y = y - mouse_last_y; set_view_turntable(scene->camera, delta_x*0.01, -delta_y*0.01, 0, 0, 0); mouse_last_x = x; mouse_last_y = y; } else { mouse_last_x = -1; mouse_last_y = -1; } if(scene->draw_captureimage) { auto image = image3f(scene->image_width,scene->image_height); glReadPixels(0, 0, scene->image_width, scene->image_height, GL_RGB, GL_FLOAT, &image.at(0,0)); write_png(image_filename, image, true); scene->draw_captureimage = false; } glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); delete state; }