void ShaderImpl::end(CL_GraphicContext &p_gc) { G_ASSERT(m_initialized); G_ASSERT(m_began); // detach frame buffer p_gc.reset_frame_buffer(); m_frameBuffer.detach_color_buffer(0, m_texture); // prepare shader m_program.set_uniform1i("tex", 0); m_program.set_uniform1i("textureWidth", m_drawRect.get_width()); m_program.set_uniform1i("textureHeight", m_drawRect.get_height()); m_parent->setUniforms(m_program); // draw texture using shader p_gc.set_modelview(CL_Mat4f::identity()); p_gc.mult_translate(m_drawRect.left, m_drawRect.top); p_gc.mult_scale(m_drawRect.get_width(), m_drawRect.get_height()); p_gc.set_texture(0, m_texture); p_gc.set_program_object(m_program); p_gc.draw_primitives(cl_quads, 4, m_quad); p_gc.reset_program_object(); p_gc.reset_texture(0); #if defined(DRAW_WIREFRAME) CL_Draw::line(p_gc, 0, 0, 1, 0, CL_Colorf::red); CL_Draw::line(p_gc, 1, 0, 1, 1, CL_Colorf::red); CL_Draw::line(p_gc, 1, 1, 0, 1, CL_Colorf::red); CL_Draw::line(p_gc, 0, 1, 0, 0, CL_Colorf::red); #endif // DRAW_WIREFRAME // reset modelview matrix p_gc.pop_modelview(); m_began = false; }
void App::render_night_vision(CL_GraphicContext &gc, CL_Texture &source_texture, CL_Texture &mask_texture, CL_Texture &noise_texture, CL_ProgramObject &program_object) { program_object.set_uniform1i("sceneBuffer", 0); program_object.set_uniform1i("noiseTex", 1); program_object.set_uniform1i("maskTex", 2); program_object.set_uniform1f("elapsedTime", elapsedTime); program_object.set_uniform1f("luminanceThreshold", luminanceThreshold); program_object.set_uniform1f("colorAmplification", colorAmplification); program_object.set_uniform1f("effectCoverage", effectCoverage); gc.set_texture(0, source_texture); gc.set_texture(1, noise_texture); gc.set_texture(2, mask_texture); gc.set_program_object(program_object, cl_program_matrix_modelview_projection); draw_texture(gc, CL_Rectf(0,0,gc.get_width(),gc.get_height()), CL_Colorf::white, CL_Rectf(0.0f, 0.0f, 1.0f, 1.0f)); gc.reset_program_object(); gc.reset_texture(2); gc.reset_texture(1); gc.reset_texture(0); }
void App::render_gaussian_blur(CL_GraphicContext &gc, float blur_amount, CL_Texture &source_texture, CL_ProgramObject &program_object, float dx, float dy) { int sampleCount = 15; float *sampleWeights = new float[sampleCount]; CL_Vec2f *sampleOffsets = new CL_Vec2f[sampleCount]; sampleWeights[0] = compute_gaussian(0, blur_amount); sampleOffsets[0] = CL_Vec2f(0.0, 0.0); float totalWeights = sampleWeights[0]; for (int i = 0; i < sampleCount / 2; i++) { float weight = compute_gaussian(i + 1.0f, blur_amount); sampleWeights[i * 2 + 1] = weight; sampleWeights[i * 2 + 2] = weight; totalWeights += weight * 2; float sampleOffset = i * 2 + 1.5f; CL_Vec2f delta = CL_Vec2f(dx * sampleOffset, dy * sampleOffset); sampleOffsets[i * 2 + 1] = delta; sampleOffsets[i * 2 + 2] = CL_Vec2f(-delta.x, -delta.y); } for (int i = 0; i < sampleCount; i++) { sampleWeights[i] /= totalWeights; } program_object.set_uniform1i("SourceTexture", 0); program_object.set_uniformfv("SampleOffsets", 2, sampleCount, (float *)sampleOffsets); program_object.set_uniformfv("SampleWeights", 1, sampleCount, sampleWeights); gc.set_texture(0, source_texture); gc.set_program_object(program_object, cl_program_matrix_modelview_projection); draw_texture(gc, CL_Rectf(0,0,gc.get_width(),gc.get_height()), CL_Colorf::white, CL_Rectf(0.0f, 0.0f, 1.0f, 1.0f)); gc.reset_program_object(); gc.reset_texture(0); }
int App::start(const std::vector<CL_String> &args) { CL_OpenGLWindowDescription description; description.set_title("GLSL Test"); 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_window_close = window.sig_window_close().connect(this, &App::window_close); // 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"); if (!shader.link()) throw CL_Exception("Unable to link shader program: Error:" + shader.get_info_log()); quit = false; while (!keyboard.get_keycode(CL_KEY_ESCAPE) && !quit ) { gc.clear(); gc.set_program_object(shader, cl_program_matrix_modelview_projection); //shader.set_uniform1i("SourceTexture", 0); int xpos = 0; int size = 16; int gap = size + 2; //-------- Test 1 CL_Mat2f matrix2_a = CL_Mat2f(3, 1, 2, 4); CL_Mat2f matrix2_b = CL_Mat2f(-3, 7, 2, 5); CL_Mat2f matrix2_result = CL_Mat2f::multiply(matrix2_a, matrix2_b); CL_Mat2f matrix2_a_temp = matrix2_a; if (CL_Mat2i(matrix2_result) != CL_Mat2i(matrix2_a * matrix2_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 1); shader.set_uniform_matrix("matrix2_a", matrix2_a); shader.set_uniform_matrix("matrix2_b", matrix2_b); shader.set_uniform_matrix("matrix2_result", matrix2_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 2 CL_Mat3f matrix3_a = CL_Mat3f(3, 1, 2, 4, 5 ,6, 4, 2, 1); CL_Mat3f matrix3_b = CL_Mat3f(4, 7, 2, 5, 3, 5, 2, 9, 3); CL_Mat3f matrix3_result = CL_Mat3f::multiply(matrix3_a, matrix3_b); CL_Mat3f matrix3_a_temp = matrix3_a; if (CL_Mat3i(matrix3_result) != CL_Mat3i(matrix3_a * matrix3_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 2); shader.set_uniform_matrix("matrix3_a", matrix3_a); shader.set_uniform_matrix("matrix3_b", matrix3_b); shader.set_uniform_matrix("matrix3_result", matrix3_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 3 static float test_a_values[] = {3, 1, 2, 4, 5 ,6, 4, 2, 1, 4, 6, 7, 6, 3, 7, 2}; static float test_b_values[] = {4, 7, 2, 5, 3, 5, 2, 9, 3, 3, 6, 9, 2, 4, 6, 2}; CL_Mat4f matrix4_a = CL_Mat4f(test_a_values); CL_Mat4f matrix4_b = CL_Mat4f(test_b_values); CL_Mat4f matrix4_result = CL_Mat4f::multiply(matrix4_a, matrix4_b); CL_Mat4f matrix4_a_temp = matrix4_a; if (CL_Mat4i(matrix4_result) != CL_Mat4i(matrix4_a * matrix4_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 3); shader.set_uniform_matrix("matrix4_a", matrix4_a); shader.set_uniform_matrix("matrix4_b", matrix4_b); shader.set_uniform_matrix("matrix4_result", matrix4_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 4 matrix2_result = CL_Mat2f::subtract(matrix2_a, matrix2_b); matrix2_a_temp = matrix2_a; if (CL_Mat2i(matrix2_result) != CL_Mat2i(matrix2_a - matrix2_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 4); shader.set_uniform_matrix("matrix2_a", matrix2_a); shader.set_uniform_matrix("matrix2_b", matrix2_b); shader.set_uniform_matrix("matrix2_result", matrix2_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 5 matrix3_result = CL_Mat3f::subtract(matrix3_a, matrix3_b); matrix3_a_temp = matrix3_a; if (CL_Mat3i(matrix3_result) != CL_Mat3i(matrix3_a - matrix3_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 5); shader.set_uniform_matrix("matrix3_a", matrix3_a); shader.set_uniform_matrix("matrix3_b", matrix3_b); shader.set_uniform_matrix("matrix3_result", matrix3_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 6 matrix4_result = CL_Mat4f::subtract(matrix4_a, matrix4_b); matrix4_a_temp = matrix4_a; if (CL_Mat4i(matrix4_result) != CL_Mat4i(matrix4_a - matrix4_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 6); shader.set_uniform_matrix("matrix4_a", matrix4_a); shader.set_uniform_matrix("matrix4_b", matrix4_b); shader.set_uniform_matrix("matrix4_result", matrix4_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 7 CL_Vec2f vector2_a(2,3); CL_Vec2f vector2_result = vector2_a * matrix2_a; shader.set_uniform1i("test_id", 7); shader.set_uniform_matrix("matrix2_a", matrix2_a); shader.set_uniform2f("vector2_a", vector2_a); shader.set_uniform2f("vector2_result", vector2_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 8 vector2_result = matrix2_a * vector2_a; shader.set_uniform1i("test_id", 8); shader.set_uniform_matrix("matrix2_a", matrix2_a); shader.set_uniform2f("vector2_a", vector2_a); shader.set_uniform2f("vector2_result", vector2_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 9 CL_Vec3f vector3_a(3,5,6); CL_Vec3f vector3_result = vector3_a * matrix3_a; shader.set_uniform1i("test_id", 9); shader.set_uniform_matrix("matrix3_a", matrix3_a); shader.set_uniform3f("vector3_a", vector3_a); shader.set_uniform3f("vector3_result", vector3_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 10 vector3_result = matrix3_a * vector3_a; shader.set_uniform1i("test_id", 10); shader.set_uniform_matrix("matrix3_a", matrix3_a); shader.set_uniform3f("vector3_a", vector3_a); shader.set_uniform3f("vector3_result", vector3_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 11 CL_Vec4f vector4_a(4,3,5,6); CL_Vec4f vector4_result = vector4_a * matrix4_a; shader.set_uniform1i("test_id", 11); shader.set_uniform_matrix("matrix4_a", matrix4_a); shader.set_uniform4f("vector4_a", vector4_a); shader.set_uniform4f("vector4_result", vector4_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 12 vector4_result = matrix4_a * vector4_a; shader.set_uniform1i("test_id", 12); shader.set_uniform_matrix("matrix4_a", matrix4_a); shader.set_uniform4f("vector4_a", vector4_a); shader.set_uniform4f("vector4_result", vector4_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; gc.reset_program_object(); window.flip(); CL_System::sleep(10); CL_KeepAlive::process(); } return 0; }
void MotionBlurShaderImpl::setUniforms(CL_ProgramObject &p_program) { p_program.set_uniform1i("radius", m_radius); p_program.set_uniform1f("angle", m_angle.to_radians()); }