int App::start(const std::vector<std::string> &args) { clan::DisplayWindowDescription description; description.set_title("NightVision Shader"); description.set_size(clan::Size(1024, 768), true); clan::DisplayWindow window(description); clan::InputDevice keyboard = window.get_ic().get_keyboard(); clan::Canvas canvas(window); clan::SlotContainer cc; cc.connect(window.get_ic().get_keyboard().sig_key_up(), clan::bind_member(this, &App::on_input_up)); cc.connect(window.sig_window_close(), clan::bind_member(this, &App::window_close)); // Create offscreen texture clan::Texture2D texture_offscreen(canvas, canvas.get_width(), canvas.get_height()); texture_offscreen.set_min_filter(clan::filter_nearest); texture_offscreen.set_mag_filter(clan::filter_nearest); clan::Texture2D texture_mask(canvas, canvas.get_width(), canvas.get_height()); texture_mask.set_min_filter(clan::filter_nearest); texture_mask.set_mag_filter(clan::filter_nearest); // Create offscreen framebuffer clan::FrameBuffer framebuffer_offscreen(canvas); framebuffer_offscreen.attach_color(0, texture_offscreen); clan::Canvas canvas_offscreen(canvas, framebuffer_offscreen); clan::FrameBuffer framebuffer_mask(canvas); framebuffer_mask.attach_color(0, texture_mask); clan::Canvas canvas_mask(canvas, framebuffer_mask); clan::Image background(canvas, "../PostProcessing/Resources/background.png"); clan::Image ball(canvas, "../PostProcessing/Resources/ball.png"); ball.set_alignment(clan::origin_center); clan::Texture2D noise_texture(canvas, "Resources/noise_texture_0001.png"); noise_texture.set_wrap_mode(clan::wrap_repeat, clan::wrap_repeat); // Load and link shaders clan::ProgramObject shader = clan::ProgramObject::load(canvas, "Resources/vertex_shader.glsl", "Resources/fragment_shader.glsl"); shader.bind_attribute_location(0, "Position"); shader.bind_attribute_location(1, "TexCoord0"); shader.bind_frag_data_location(0, "cl_FragColor"); if (!shader.link()) throw clan::Exception("Unable to link shader program: Error:" + shader.get_info_log()); shader.set_uniform1i("sceneBuffer", 0); shader.set_uniform1i("noiseTex", 1); shader.set_uniform1i("maskTex", 2); quit = false; float amount = 0.0f; float timer = 0.0f; float scale = 1.0f; clan::Font font(canvas, "tahoma", 32); // Shader based on: http://www.geeks3d.com/20091009/shader-library-night-vision-post-processing-filter-glsl/ gpu_positions = clan::VertexArrayVector<clan::Vec2f>(canvas, 6); gpu_tex1_coords = clan::VertexArrayVector<clan::Vec2f>(canvas, 6); gpu_uniforms = clan::UniformVector<ProgramUniforms>(canvas, 1); gpu_primitives_array = clan::PrimitivesArray(canvas); gpu_primitives_array.set_attributes(0, gpu_positions); gpu_primitives_array.set_attributes(1, gpu_tex1_coords); uniforms.elapsedTime = 0.0f; // seconds uniforms.luminanceThreshold = 0.2f; uniforms.colorAmplification = 4.0f; uniforms.effectCoverage = 0.65f; // Render the mask canvas_mask.clear(); for (float offset=0.0f; offset<=1.0f; offset+=0.01f) { canvas_mask.fill_circle(canvas.get_width() / 2.0f, canvas.get_height() / 2.0f, 400.0f - offset * 64.0f, clan::Colorf(offset, offset, offset, 1.0f)); } canvas_mask.flush(); clan::ubyte64 startTime = clan::System::get_time(); while (!quit) { timer = (clan::System::get_time() - startTime) / 1000.0f; uniforms.elapsedTime = timer; // Render standard image to offscreen buffer background.draw(canvas_offscreen, 0, 0); ball.draw(canvas_offscreen, canvas.get_width() / 2 + 200 * sinf(timer / 2.0f), canvas.get_height() / 2 + 200 * cosf(timer / 2.0f)); canvas_offscreen.flush(); render_night_vision(canvas, texture_offscreen, texture_mask, noise_texture, shader); const int gap = 32; font.draw_text(canvas, 10, 64+gap*0, std::string("luminanceThreshold : " + clan::StringHelp::float_to_text(uniforms.luminanceThreshold) + " (Press Q,W)" )); font.draw_text(canvas, 10, 64+gap*1, std::string("colorAmplification : " + clan::StringHelp::float_to_text(uniforms.colorAmplification) + " (Press A,S)" )); font.draw_text(canvas, 10, 64+gap*2, std::string("effectCoverage : " + clan::StringHelp::float_to_text(uniforms.effectCoverage) + " (Press Z,X)" )); window.flip(); clan::System::sleep(10); clan::KeepAlive::process(); } return 0; }
int App::start(const std::vector<CL_String> &args) { CL_OpenGLWindowDescription description; description.set_title("NightVision Shader"); description.set_size(CL_Size(1024, 768), true); CL_DisplayWindow window(description); CL_InputDevice keyboard = window.get_ic().get_keyboard(); CL_GraphicContext gc = window.get_gc(); CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); CL_Slot slot_window_close = window.sig_window_close().connect(this, &App::window_close); // Create offscreen texture CL_Texture texture_offscreen(gc, gc.get_width(), gc.get_height()); texture_offscreen.set_min_filter(cl_filter_nearest); texture_offscreen.set_mag_filter(cl_filter_nearest); CL_Texture texture_mask(gc, gc.get_width(), gc.get_height()); texture_mask.set_min_filter(cl_filter_nearest); texture_mask.set_mag_filter(cl_filter_nearest); // Create offscreen framebuffer CL_FrameBuffer framebuffer_offscreen(gc); framebuffer_offscreen.attach_color_buffer(0, texture_offscreen); CL_FrameBuffer framebuffer_mask(gc); framebuffer_mask.attach_color_buffer(0, texture_mask); CL_Image background(gc, "../PostProcessing/Resources/background.png"); CL_Image ball(gc, "../PostProcessing/Resources/ball.png"); ball.set_alignment(origin_center); CL_Texture noise_texture(gc, "Resources/noise_texture_0001.png"); noise_texture.set_wrap_mode(cl_wrap_repeat, cl_wrap_repeat); // Load and link shaders CL_ProgramObject shader = CL_ProgramObject::load(gc, "Resources/vertex_shader.glsl", "Resources/fragment_shader.glsl"); shader.bind_attribute_location(0, "Position"); shader.bind_attribute_location(2, "TexCoord0"); if (!shader.link()) throw CL_Exception("Unable to link shader program: Error:" + shader.get_info_log()); quit = false; float amount = 0.0f; float timer = 0.0f; float scale = 1.0f; CL_Font font(gc, "tahoma", 32); // Shader based on: http://www.geeks3d.com/20091009/shader-library-night-vision-post-processing-filter-glsl/ elapsedTime = 0.0f; // seconds luminanceThreshold = 0.2f; colorAmplification = 4.0f; effectCoverage = 0.65f; // Render the mask gc.set_frame_buffer(framebuffer_mask); gc.clear(); for (float offset=0.0f; offset<=1.0f; offset+=0.01f) { CL_Draw::circle(gc, gc.get_width() / 2.0f, gc.get_height() / 2.0f, 400.0f - offset * 64.0f, CL_Colorf(offset, offset, offset, 1.0f)); } gc.reset_frame_buffer(); unsigned int startTime = CL_System::get_time(); while (!quit) { timer = (CL_System::get_time() - startTime) / 1000.0f; elapsedTime = timer; // Render standard image to offscreen buffer gc.set_frame_buffer(framebuffer_offscreen); background.draw(gc, 0, 0); ball.draw(gc, gc.get_width() / 2 + 200 * sinf(timer / 2.0f), gc.get_height() / 2 + 200 * cosf(timer / 2.0f)); gc.reset_frame_buffer(); render_night_vision(gc, texture_offscreen, texture_mask, noise_texture, shader); const int gap = 32; font.draw_text(gc, 10, 64+gap*0, CL_String("luminanceThreshold : " + CL_StringHelp::float_to_text(luminanceThreshold) + " (Press Q,W)" )); font.draw_text(gc, 10, 64+gap*1, CL_String("colorAmplification : " + CL_StringHelp::float_to_text(colorAmplification) + " (Press A,S)" )); font.draw_text(gc, 10, 64+gap*2, CL_String("effectCoverage : " + CL_StringHelp::float_to_text(effectCoverage) + " (Press Z,X)" )); window.flip(); CL_System::sleep(10); CL_KeepAlive::process(); } return 0; }