Esempio n. 1
0
CL_ProgramObject HSV::create_shader_program(CL_GraphicContext &gc)
{
	CL_ProgramObject program = CL_ProgramObject::load(gc, "Resources/vertex.glsl", "Resources/fragment.glsl");
	program.bind_attribute_location(0, "Position");
	program.bind_attribute_location(1, "HueOffset0");
	program.bind_attribute_location(2, "TexCoord0");
	if (!program.link())
		throw CL_Exception("Unable to link program");
	return program;
}
Esempio n. 2
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);
}
Esempio n. 3
0
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;
}
Esempio n. 4
0
void ShaderImpl::initialize(CL_GraphicContext &p_gc)
{
	G_ASSERT(!m_initialized);

	CL_ResourceManager *resMgr = Gfx::Stage::getResourceManager();

	m_program = CL_ProgramObject(p_gc);

	// load vertex shader
	const CL_String &vertShaderName = m_parent->getVertexShaderResourceName();
	m_vertShader = CL_ShaderObject::load(p_gc, vertShaderName, resMgr);
	m_program.attach(m_vertShader);

	// load fragment shader
	const CL_String &fragShaderName = m_parent->getFragmentShaderResourceName();
	m_fragShader = CL_ShaderObject::load(p_gc, fragShaderName, resMgr);
	m_program.attach(m_fragShader);

	// bind common attributes
	m_program.bind_attribute_location(CL_PRIARR_VERTS, "Position");
	m_program.bind_attribute_location(CL_PRIARR_COLORS, "Color0");
	m_program.bind_attribute_location(CL_PRIARR_TEXCOORDS, "TexCoord0");

	// link the program
	if (!m_program.link()) {
		m_program.detach(m_vertShader);
		m_program.detach(m_fragShader);

		throw CL_Exception(
				cl_format(
						"error linking shader %1, %2: %3",
						vertShaderName, fragShaderName,
						m_program.get_info_log()
				)
		);
	}

	// build frame buffer
	m_frameBuffer = CL_FrameBuffer(p_gc);

	// build quad
	m_quad = CL_PrimitivesArray(p_gc);
	m_quad.set_attributes(CL_PRIARR_VERTS, m_quadVerts);
	m_quad.set_attributes(CL_PRIARR_COLORS, QUAD_COLORS);
	m_quad.set_attributes(CL_PRIARR_TEXCOORDS, QUAD_TEX_COORDS);

	m_initialized = true;
}
Esempio n. 5
0
void ShaderImpl::destroy()
{
	G_ASSERT(m_initialized);

	m_quad = CL_PrimitivesArray();
	m_frameBuffer = CL_FrameBuffer();

	m_program.detach(m_vertShader);
	m_program.detach(m_fragShader);

	m_vertShader = CL_ShaderObject();
	m_fragShader = CL_ShaderObject();

	m_program = CL_ProgramObject();

	m_initialized = false;
}
Esempio n. 6
0
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);
}
Esempio n. 7
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;
}
Esempio n. 8
0
int App::start(const std::vector<CL_String> &args)
{
	CL_OpenGLWindowDescription description;
	description.set_title("Guassian Blur 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_offscreen2(gc, gc.get_width(), gc.get_height());
	texture_offscreen2.set_min_filter(cl_filter_nearest);
	texture_offscreen2.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_offscreen2(gc);
	framebuffer_offscreen2.attach_color_buffer(0, texture_offscreen2);

	CL_Image background(gc, "../PostProcessing/Resources/background.png");
	CL_Image ball(gc, "../PostProcessing/Resources/ball.png");
	ball.set_alignment(origin_center);

	// 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);

	blur = 1.0f;
	unsigned int startTime = CL_System::get_time();

	while (!quit)
	{
		timer = (CL_System::get_time() - startTime) / 1000.0f;

		// 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();

		gc.set_frame_buffer(framebuffer_offscreen2);
		render_gaussian_blur(gc, blur, texture_offscreen, shader, 1.0f / texture_offscreen2.get_width(), 0.0f);
		gc.reset_frame_buffer();

		render_gaussian_blur(gc, blur, texture_offscreen2, shader, 0.0f, 1.0f / texture_offscreen2.get_height());

		CL_String text( "Press 1 to 9 to control blur amount. Currently it is :" + CL_StringHelp::float_to_text(blur) );
		font.draw_text(gc, 10, 64, text);

		window.flip();

		CL_System::sleep(10);

		CL_KeepAlive::process();
	}

	return 0;
}
Esempio n. 9
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;
}
Esempio n. 10
0
void MotionBlurShaderImpl::setUniforms(CL_ProgramObject &p_program)
{
	p_program.set_uniform1i("radius", m_radius);
	p_program.set_uniform1f("angle", m_angle.to_radians());
}