ShaderDepth::ShaderDepth(CL_GraphicContext &gc)
{
	CL_ShaderObject vertex_shader(gc, cl_shadertype_vertex, vertex);
	if(!vertex_shader.compile())
	{
		throw CL_Exception(cl_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log()));
	}

	CL_ShaderObject fragment_shader(gc, cl_shadertype_fragment, fragment);
	if(!fragment_shader.compile())
	{
		throw CL_Exception(cl_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log()));
	}

	program_object = CL_ProgramObject(gc);
	program_object.attach(vertex_shader);
	program_object.attach(fragment_shader);
	program_object.bind_attribute_location(0, "InPosition");
	if (!program_object.link())
	{
		throw CL_Exception(cl_format("Unable to link program object: %1", program_object.get_info_log()));
	}

	material_updated = false;

	material_ambient =  CL_Vec4f(0.9f, 0.2f, 0.2f, 1.0f);

}
ShaderColorInstanced::ShaderColorInstanced(CL_GraphicContext &gc)
{
	CL_ShaderObject vertex_shader(gc, cl_shadertype_vertex, vertex);
	if(!vertex_shader.compile())
	{
		throw CL_Exception(cl_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log()));
	}

	CL_ShaderObject fragment_shader(gc, cl_shadertype_fragment, fragment);
	if(!fragment_shader.compile())
	{
		throw CL_Exception(cl_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log()));
	}

	program_object = CL_ProgramObject(gc);
	program_object.attach(vertex_shader);
	program_object.attach(fragment_shader);
	program_object.bind_attribute_location(0, "InPosition");
	program_object.bind_attribute_location(1, "InNormal");
	if (!program_object.link())
	{
		throw CL_Exception(cl_format("Unable to link program object: %1", program_object.get_info_log()));
	}

	material_updated = false;
	light_updated = false;

	material_shininess = 64.0f;
	material_emission = CL_Vec4f(0.0f, 0.0f, 0.0f, 1.0f);

	light_vector = CL_Vec4f(0.0f, 0.0f, 1.0f, 0.0f);
	light_specular = CL_Vec4f(0.7f, 0.7f, 0.7f, 1.0f);
	light_diffuse = CL_Vec4f(0.7f, 0.7f, 0.7f, 1.0f);
}
Beispiel #3
0
	RenderData() : program_object(create_program(vertex_shader(), fragment_shader()))
	{
		position_loc = glGetAttribLocation(program_object, "a_position");
		tex_coord_loc = glGetAttribLocation(program_object, "a_texCoord");
		sampler_loc = glGetUniformLocation(program_object, "s_texture");
		matrix_loc = glGetUniformLocation(program_object, "m_texMatrix");
	}
Beispiel #4
0
std::string Video3DUberShader::_warp_pass_fragment_shader() const
{
  std::string fragment_shader(
    Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_video3d_warp_pass_frag)
    );

  return fragment_shader;
}
    void Projection::render_crosshairs (const Point<>& focus) const
    {
      if (!crosshairs_VB || !crosshairs_VAO) {
        crosshairs_VB.gen();
        crosshairs_VAO.gen();

        crosshairs_VB.bind (gl::ARRAY_BUFFER);
        crosshairs_VAO.bind();

        gl::EnableVertexAttribArray (0);
        gl::VertexAttribPointer (0, 2, gl::FLOAT, gl::FALSE_, 0, (void*)0);
      }
      else {
        crosshairs_VB.bind (gl::ARRAY_BUFFER);
        crosshairs_VAO.bind();
      }

      if (!crosshairs_program) {
        GL::Shader::Vertex vertex_shader (
            "layout(location=0) in vec2 pos;\n"
            "void main () {\n"
            "  gl_Position = vec4 (pos, 0.0, 1.0);\n"
            "}\n");
        GL::Shader::Fragment fragment_shader (
            "out vec4 color;\n"
            "void main () {\n"
            "  color = vec4 (0.5, 0.5, 0.0, 1.0);\n"
            "}\n");
        crosshairs_program.attach (vertex_shader);
        crosshairs_program.attach (fragment_shader);
        crosshairs_program.link();
      }

      Point<> F = model_to_screen (focus);
      F[0] = std::round (F[0] - x_position()) - 0.5f;
      F[1] = std::round (F[1] - y_position()) + 0.5f;

      F[0] = 2.0f * F[0] / width() - 1.0f;
      F[1] = 2.0f * F[1] / height() - 1.0f;

      GLfloat data [] = {
        F[0], -1.0f,
        F[0], 1.0f,
        -1.0f, F[1],
        1.0f, F[1]
      };
      gl::BufferData (gl::ARRAY_BUFFER, sizeof(data), data, gl::STATIC_DRAW);

      gl::DepthMask (gl::TRUE_);
      gl::Disable (gl::BLEND);
      gl::LineWidth (1.0);

      crosshairs_program.start();
      gl::DrawArrays (gl::LINES, 0, 4);
      crosshairs_program.stop();
    }
Beispiel #6
0
void LightingUberShader::
create(std::set<std::string> const& material_names,
       std::vector<LayerMapping const*> const& inputs) {

  fshader_factory_ = gua::make_unique<UberShaderFactory>(
    ShadingModel::LIGHTING_STAGE, material_names
    );

  fshader_factory_->add_inputs_to_main_functions(inputs, ShadingModel::GBUFFER_FRAGMENT_STAGE);

  UberShader::set_uniform_mapping(fshader_factory_->get_uniform_mapping());
  UberShader::set_output_mapping(fshader_factory_->get_output_mapping());

  // VERTEX SHADER -------------------------------------------------------------
  std::string vertex_shader(
    Resources::lookup_shader(Resources::shaders_uber_shaders_lighting_lighting_vert)
  );

  // FRAGMENT SHADER -----------------------------------------------------------
  std::string fragment_shader(
    Resources::lookup_shader(Resources::shaders_uber_shaders_lighting_lighting_frag)
  );

  // input from gbuffer
  std::stringstream s;
  for (unsigned i(0); i<inputs.size(); ++i)
    s << inputs[i]->get_gbuffer_input_definition(ShadingModel::StageID(i+1));

  string_utils::replace(fragment_shader, "@input_definition", s.str());

  // material specific uniforms
  string_utils::replace(fragment_shader, "@uniform_definition",
    get_uniform_mapping()->get_uniform_definition());

  // outputs
  string_utils::replace(fragment_shader, "@output_definition",
    get_gbuffer_mapping()->get_gbuffer_output_definition(false, false));

  // print material specific methods
  string_utils::replace(fragment_shader, "@material_methods",
    UberShader::print_material_methods(*fshader_factory_));

  // print main switch(es)
  string_utils::replace(fragment_shader, "@material_switch",
    UberShader::print_material_switch(*fshader_factory_));

  auto lighting_program = std::make_shared<ShaderProgram>();
  lighting_program->create_from_sources(vertex_shader, fragment_shader);
  add_program(lighting_program);
}
ProgramObject StandardPrograms::compile(GraphicContext &gc, const void *vertex_code, int vertex_code_size, const void *fragment_code, int fragment_code_size)
{
	ShaderObject vertex_shader(gc, shadertype_vertex, vertex_code, vertex_code_size);
	if (!vertex_shader.compile())
		throw Exception(string_format("Unable to compile standard vertex shader: %1", vertex_shader.get_info_log()));

	ShaderObject fragment_shader(gc, shadertype_fragment, fragment_code, fragment_code_size);
	if (!fragment_shader.compile())
		throw Exception(string_format("Unable to compile standard fragment shader: %1", fragment_shader.get_info_log()));

	ProgramObject program(gc);
	program.attach(vertex_shader);
	program.attach(fragment_shader);
	return program;
}
Program Display::Prepare() {
    Program program_;
    Shader vertex_shader(GL_VERTEX_SHADER);
    Shader fragment_shader(GL_FRAGMENT_SHADER);
    
    vertex_shader.LoadSourceFromFile("Shaders/vertex_shader.glsl");
    fragment_shader.LoadSourceFromFile("Shaders/fragment_shader.glsl");
    
    program_.AttachShader(vertex_shader);
    program_.AttachShader(fragment_shader);
    
    program_.Link();
    program_.Validate();
    program_.Use();
    
    return program_;
}
ShaderColorGeometry::ShaderColorGeometry(GraphicContext &gc)
{
	ShaderObject vertex_shader(gc, shadertype_vertex, vertex);
	if(!vertex_shader.compile())
	{
		throw Exception(string_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log()));
	}

	ShaderObject geometry_shader(gc, shadertype_geometry, geometry);
	if(!geometry_shader.compile())
	{
		throw Exception(string_format("Unable to compile geometry shader object: %1", geometry_shader.get_info_log()));
	}

	if (!glProgramParameteri)
		throw Exception("Geomtry shader is not available");

	ShaderObject fragment_shader(gc, shadertype_fragment, fragment);
	if(!fragment_shader.compile())
	{
		throw Exception(string_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log()));
	}

	program_object = ProgramObject(gc);
	program_object.attach(vertex_shader);
	program_object.attach(geometry_shader);
	program_object.attach(fragment_shader);
	program_object.bind_attribute_location(0, "InPosition");
	program_object.bind_attribute_location(1, "InColor");
	program_object.bind_frag_data_location(0, "cl_FragColor");

	if (!program_object.link())
	{
		throw Exception(string_format("Unable to link program object: %1", program_object.get_info_log()));
	}
	
	program_object.set_uniform1i("Texture0", 0);

	gpu_uniforms = clan::UniformVector<ProgramUniforms>(gc, 1);


}
Beispiel #10
0
Shader::Shader(GraphicContext &gc)
{
	ShaderLanguage shader_language = gc.get_shader_language();
	ShaderObject vertex_shader(gc, shadertype_vertex, shader_language==shader_glsl ? vertex_glsl : vertex_hlsl);
	if(!vertex_shader.compile())
	{
		std::string log = vertex_shader.get_info_log();
		throw Exception(string_format("Unable to compile vertex shader object: %1", log));
	}

	ShaderObject fragment_shader(gc, shadertype_fragment, shader_language==shader_glsl ? fragment_glsl : fragment_hlsl);
	if(!fragment_shader.compile())
	{
		std::string log = fragment_shader.get_info_log();
		throw Exception(string_format("Unable to compile fragment shader object: %1", log));
	}

	program_object = ProgramObject(gc);
	program_object.attach(vertex_shader);
	program_object.attach(fragment_shader);
	program_object.bind_attribute_location(0, "InPosition");
	program_object.bind_attribute_location(1, "InNormal");
	program_object.bind_attribute_location(2, "InMaterialAmbient");
	program_object.bind_frag_data_location(0, "cl_FragColor");
	if (!program_object.link())
	{
		throw Exception(string_format("Unable to link program object: %1", program_object.get_info_log()));
	}

	program_object.set_uniform_buffer_index("ProgramUniforms", 0);

	gpu_uniforms = clan::UniformVector<ProgramUniforms>(gc, 1);

	uniforms.LightAmbient = Vec4f(0.2f, 0.2f, 0.2f, 1.0f);
	uniforms.LightVector = Vec3f(0.0f, 0.0f, -1.0f);
	uniforms.LightDiffuse = Vec4f(0.7f, 0.7f, 0.7f, 1.0f);

}
Beispiel #11
0
Shader::Shader(CL_GraphicContext &gc)
{
	CL_ShaderObject vertex_shader(gc, cl_shadertype_vertex, vertex);
	if(!vertex_shader.compile())
	{
		throw CL_Exception(cl_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log()));
	}

	CL_ShaderObject fragment_shader(gc, cl_shadertype_fragment, fragment);
	if(!fragment_shader.compile())
	{
		throw CL_Exception(cl_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log()));
	}

	program_object = CL_ProgramObject(gc);
	program_object.attach(vertex_shader);
	program_object.attach(fragment_shader);

	program_object.bind_attribute_location(cl_attrib_position, "InPosition");
	program_object.bind_attribute_location(cl_attrib_normal, "InNormal");
	program_object.bind_attribute_location(cl_attrib_color, "InMaterialAmbient");
	program_object.bind_attribute_location(cl_attrib_texture_position, "InTextureCoords");
	if (!program_object.link())
	{
		throw CL_Exception(cl_format("Unable to link program object: %1", program_object.get_info_log()));
	}

	material_shininess = 64.0f;
	material_emission = CL_Vec4f(0.0f, 0.0f, 0.0f, 1.0f);
	material_specular = CL_Vec4f(0.0f, 0.0f, 0.0f, 1.0f);

	light_position = CL_Vec3f(0.0f, 0.0f, 1.0f);
	light_specular = CL_Vec4f(0.7f, 0.7f, 0.7f, 1.0f);
	light_diffuse = CL_Vec4f(0.7f, 0.7f, 0.7f, 1.0f);


}
Beispiel #12
0
std::string Video3DUberShader::_blend_pass_fragment_shader() const
{
  std::string fragment_shader(
    Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_video3d_blend_pass_frag)
    );

  std::string apply_video_color = fshader_factory_->get_output_mapping().get_output_string(default_video_material_name(), "gua_video_output_color");
  apply_video_color += " = output_color;\n";

  std::string apply_video_normal = fshader_factory_->get_output_mapping().get_output_string(default_video_material_name(), "gua_normal");
  apply_video_normal += " = output_normal;\n";

  string_utils::replace(fragment_shader, "@apply_video3d_color", apply_video_color);
  string_utils::replace(fragment_shader, "@apply_video3d_normal", apply_video_normal);

  // input from vertex shader
  string_utils::replace(fragment_shader, "@input_definition",
    vshader_factory_->get_output_mapping().get_gbuffer_output_definition(true, true));

  // material specific uniforms
  string_utils::replace(fragment_shader, "@uniform_definition",
    get_uniform_mapping()->get_uniform_definition());

  // outputs
  string_utils::replace(fragment_shader, "@output_definition",
    get_gbuffer_mapping()->get_gbuffer_output_definition(false, false));

  // print material specific methods
  string_utils::replace(fragment_shader, "@material_methods",
    UberShader::print_material_methods(*fshader_factory_));

  // print main switch(es)
  string_utils::replace(fragment_shader, "@material_switch",
    UberShader::print_material_switch(*fshader_factory_));

  return fragment_shader;
}
Beispiel #13
0
// The start of the Application
int App::start(const std::vector<std::string> &args)
{
	clan::DisplayWindowDescription win_desc;
	//win_desc.set_version(3, 2, false);
	win_desc.set_allow_resize(true);
	win_desc.set_title("Point Sprite Example");
	win_desc.set_size(clan::Size( 800, 480 ), false);

	clan::DisplayWindow window(win_desc);
    clan::SlotContainer cc;
	cc.connect(window.sig_window_close(), clan::bind_member(this, &App::on_window_close));
	cc.connect(window.get_ic().get_keyboard().sig_key_up(), clan::bind_member(this, &App::on_input_up));

	std::string theme;
	if (clan::FileHelp::file_exists("../../../Resources/GUIThemeAero/theme.css"))
		theme = "../../../Resources/GUIThemeAero";
	else if (clan::FileHelp::file_exists("../../../Resources/GUIThemeBasic/theme.css"))
		theme = "../../../Resources/GUIThemeBasic";
	else
		throw clan::Exception("No themes found");

	clan::GUIWindowManagerTexture wm(window);
	clan::GUIManager gui(wm, theme);
	
	clan::Canvas canvas(window);

	// Deleted automatically by the GUI
	Options *options = new Options(gui, clan::Rect(0, 0, canvas.get_size()));

	clan::Image image_grid(canvas, "../Blend/Resources/grid.png");
	clan::Texture2D texture_particle(canvas, "Resources/particle.png");
	float grid_width = (float) image_grid.get_width();
	float grid_height = (float) image_grid.get_height();

	grid_space = (float) (image_grid.get_width());

	setup_particles();

	clan::ShaderObject vertex_shader(canvas, clan::shadertype_vertex, text_shader_vertex);
	if(!vertex_shader.compile())
	{
		throw clan::Exception(clan::string_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log()));
	}

	clan::ShaderObject fragment_shader(canvas, clan::shadertype_fragment, text_shader_fragment);
	if(!fragment_shader.compile())
	{
		throw clan::Exception(clan::string_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log()));
	}

	clan::ProgramObject program_object(canvas);
	program_object.attach(vertex_shader);
	program_object.attach(fragment_shader);
	program_object.bind_attribute_location(0, "InPosition");
	program_object.bind_attribute_location(1, "InColor");
	if (!program_object.link())
	{
		throw clan::Exception(clan::string_format("Unable to link program object: %1", program_object.get_info_log()));
	}
	program_object.set_uniform1i("Texture0", 0);

	options->request_repaint();

	clan::BlendStateDescription blend_state_desc;
	blend_state_desc.enable_blending(true);
	blend_state_desc.set_blend_function(clan::blend_src_alpha, clan::blend_one, clan::blend_src_alpha, clan::blend_one);
	clan::BlendState blend_state(canvas, blend_state_desc);

	clan::GameTime game_time;

	while (!quit)
	{
		game_time.update();

		wm.process();
		wm.draw_windows(canvas);

		int num_particles = options->num_particles;
		if (num_particles > max_particles)
			num_particles = max_particles;

		move_particles(game_time.get_time_elapsed(), num_particles);

		const float grid_xpos = 10.0f;
		const float grid_ypos = 10.0f;

		// Draw the grid
		image_grid.draw(canvas, grid_xpos, grid_ypos);

		if (num_particles > 0)
		{
			std::vector<clan::Vec2f> positions;
			std::vector<clan::Colorf> colors;
			positions.resize(num_particles);
			colors.resize(num_particles);

			for (int cnt=0; cnt<num_particles; cnt++)
			{
				positions[cnt] = clan::Vec2f(grid_xpos + particles[cnt].xpos, grid_ypos + particles[cnt].ypos);
				switch (cnt % 3)
				{
					case 0:
						colors[cnt] = clan::Colorf(1.0f, 0.0f, 0.0f, 1.0f);
						break;
					case 1:
						colors[cnt] = clan::Colorf(0.0f, 1.0f, 0.0f, 1.0f);
						break;
					case 2:
						colors[cnt] = clan::Colorf(0.0f, 0.0f, 1.0f, 1.0f);
						break;
				}
			};

			canvas.flush();
			clan::GraphicContext gc = canvas.get_gc();

			canvas.set_blend_state(blend_state);

			clan::RasterizerStateDescription raster_state_desc;
			raster_state_desc.set_point_size(options->point_size);
			raster_state_desc.set_point_sprite_origin(clan::origin_upper_left);
			clan::RasterizerState raster_state(canvas, raster_state_desc);
			canvas.set_rasterizer_state(raster_state);

			clan::PrimitivesArray primarray(gc);

			clan::VertexArrayVector<clan::Vec2f> gpu_positions = clan::VertexArrayVector<clan::Vec2f>(gc, &positions[0], positions.size());
			clan::VertexArrayVector<clan::Colorf> gpu_colors = clan::VertexArrayVector<clan::Colorf>(gc, &colors[0], colors.size());

			primarray.set_attributes(0, gpu_positions);
			primarray.set_attributes(1, gpu_colors);

			ProgramUniforms buffer;
			buffer.cl_ModelViewProjectionMatrix = canvas.get_projection() * canvas.get_modelview();
			clan::UniformVector<ProgramUniforms> uniform_vector(gc, &buffer, 1);
			gc.set_uniform_buffer(0, uniform_vector);

			gc.set_texture(0, texture_particle);
			gc.set_program_object(program_object);
			gc.draw_primitives(clan::type_points, num_particles, primarray);
			gc.reset_program_object();
			gc.reset_texture(0);

			gc.reset_blend_state();
			gc.reset_rasterizer_state();
		}

		window.flip(1);

		clan::KeepAlive::process();
	}
	return 0;
}
Beispiel #14
0
App::App()
{
	clan::OpenGLTarget::enable();

	clan::DisplayWindowDescription win_desc;
	//win_desc.set_version(3, 2, false);
	win_desc.set_allow_resize(true);
	win_desc.set_title("Point Sprite Example");
	win_desc.set_size(clan::Size( 900, 480 ), false);

	window = clan::DisplayWindow(win_desc);
	sc.connect(window.sig_window_close(), clan::bind_member(this, &App::on_window_close));
	sc.connect(window.get_ic().get_keyboard().sig_key_up(), clan::bind_member(this, &App::on_input_up));
	canvas = clan::Canvas(window);

	clan::FileResourceDocument doc(clan::FileSystem("../../ThemeAero"));
	clan::ResourceManager resources = clan::FileResourceManager::create(doc);
	ui_thread = clan::UIThread(resources);

	options = std::make_shared<Options>(canvas);
	options->set_event_window(window);
	options->set_cursor_window(window);

	image_grid = clan::Image(canvas, "../Blend/Resources/grid.png");
	texture_particle = clan::Texture2D(canvas, "Resources/particle.png");
	float grid_width = (float) image_grid.get_width();
	float grid_height = (float) image_grid.get_height();

	grid_space = (float) (image_grid.get_width());

	setup_particles();

	clan::ShaderObject vertex_shader(canvas, clan::shadertype_vertex, text_shader_vertex);
	if(!vertex_shader.compile())
	{
		throw clan::Exception(clan::string_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log()));
	}

	clan::ShaderObject fragment_shader(canvas, clan::shadertype_fragment, text_shader_fragment);
	if(!fragment_shader.compile())
	{
		throw clan::Exception(clan::string_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log()));
	}

	program_object = clan::ProgramObject(canvas);
	program_object.attach(vertex_shader);
	program_object.attach(fragment_shader);
	program_object.bind_attribute_location(0, "InPosition");
	program_object.bind_attribute_location(1, "InColor");
	if (!program_object.link())
	{
		throw clan::Exception(clan::string_format("Unable to link program object: %1", program_object.get_info_log()));
	}
	program_object.set_uniform1i("Texture0", 0);

	clan::BlendStateDescription blend_state_desc;
	blend_state_desc.enable_blending(true);
	blend_state_desc.set_blend_function(clan::blend_src_alpha, clan::blend_one, clan::blend_src_alpha, clan::blend_one);
	blend_state = clan::BlendState(canvas, blend_state_desc);

	game_time.reset();
}
Beispiel #15
0
void ShaderEffect_Impl::create_shaders(GraphicContext &gc, const ShaderEffectDescription_Impl *description)
{
	std::string vertex_shader_code = add_defines(gc, description->vertex_shader_code, description);
	std::string fragment_shader_code = add_defines(gc, description->fragment_shader_code, description);
	std::string compute_shader_code = add_defines(gc, description->compute_shader_code, description);

	if (!vertex_shader_code.empty()) 
	{
		ShaderObject vertex_shader(gc, shadertype_vertex, vertex_shader_code);
		if(!vertex_shader.compile())
			throw Exception(string_format("Unable to compile vertex shader: %1", vertex_shader.get_info_log()));
		program.attach(vertex_shader);
	}

	if (!fragment_shader_code.empty()) 
	{
		ShaderObject fragment_shader(gc, shadertype_fragment, fragment_shader_code);
		if(!fragment_shader.compile())
			throw Exception(string_format("Unable to compile fragment shader: %1", fragment_shader.get_info_log()));
		program.attach(fragment_shader);
	}

	if (!compute_shader_code.empty()) 
	{
		ShaderObject compute_shader(gc, shadertype_compute, compute_shader_code);
		if(!compute_shader.compile())
			throw Exception(string_format("Unable to compile compute shader: %1", compute_shader.get_info_log()));
		program.attach(compute_shader);
	}

	int index = 0;
	for(const auto & elem : description->attributes)
	{
		program.bind_attribute_location(index++, elem.first);
	}

	index = 0;
	for(const auto & elem : description->frag_data)
	{
		program.bind_frag_data_location(index++, elem.first);
	}

	if (!program.link())
		throw Exception(string_format("Link failed: %1", program.get_info_log()));

	index = 0;
	for(auto it = description->uniform_buffers.begin(); it != description->uniform_buffers.end(); ++it, index++)
	{
		program.set_uniform_buffer_index(it->first, index);
		uniform_bindings[index] = it->second;
	}

	index = 0;
	for(auto it = description->textures.begin(); it != description->textures.end(); ++it, index++)
	{
		program.set_uniform1i(it->first, index);
		texture_bindings[index] = it->second;
	}

	index = 0;
	for(auto it = description->images.begin(); it != description->images.end(); ++it, index++)
	{
		program.set_uniform1i(it->first, index);
		image_bindings[index] = it->second;
	}

	index = 0;
	for(auto it = description->storage_buffers.begin(); it != description->storage_buffers.end(); ++it, index++)
	{
		program.set_uniform1i(it->first, index);
		storage_bindings[index] = it->second;
	}

}
Beispiel #16
0
int main(int argc, char **argv)
{
	if (argc < 2) {
		printf("Usage: test_media <video_to_play>\n");
		return EXIT_FAILURE;
	}

	player = android_media_new_player();
	if (player == NULL) {
		printf("Problem creating new media player.\n");
		return EXIT_FAILURE;
	}

	// Set player event cb for when the video size is known:
	android_media_set_video_size_cb(player, set_video_size_cb, NULL);
	android_media_set_media_prepared_cb(player, media_prepared_cb, NULL);

	printf("Setting data source to: %s.\n", argv[1]);

	if (android_media_set_data_source(player, argv[1]) != OK) {
		printf("Failed to set data source: %s\n", argv[1]);
		return EXIT_FAILURE;
	}

	printf("Creating EGL surface.\n");
	struct ClientWithSurface cs = client_with_surface(true /* Associate surface with egl. */);
	if (!cs.surface) {
		printf("Problem acquiring surface for preview");
		return EXIT_FAILURE;
	}

	printf("Creating GL texture.\n");

	GLuint preview_texture_id;
	EGLDisplay disp = sf_client_get_egl_display(cs.client);
	EGLSurface surface = sf_surface_get_egl_surface(cs.surface);

	sf_surface_make_current(cs.surface);

	if (setup_video_texture(&cs, &preview_texture_id) != OK) {
		printf("Problem setting up GL texture for video surface.\n");
		return EXIT_FAILURE;
	}

	printf("Starting video playback.\n");
	android_media_play(player);

	while (android_media_is_playing(player)) {
		const GLfloat textureCoordinates[] = {
			1.0f,  1.0f,
			0.0f,  1.0f,
			0.0f,  0.0f,
			1.0f,  0.0f
		};

		android_media_update_surface_texture(player);

		calculate_position_coordinates();

		gProgram = create_program(vertex_shader(), fragment_shader());
		gaPositionHandle = glGetAttribLocation(gProgram, "a_position");
		gaTexHandle = glGetAttribLocation(gProgram, "a_texCoord");
		gsTextureHandle = glGetUniformLocation(gProgram, "s_texture");
		gmTexMatrix = glGetUniformLocation(gProgram, "m_texMatrix");

		glClear(GL_COLOR_BUFFER_BIT);

		// Use the program object
		glUseProgram(gProgram);
		// Enable attributes
		glEnableVertexAttribArray(gaPositionHandle);
		glEnableVertexAttribArray(gaTexHandle);
		// Load the vertex position
		glVertexAttribPointer(gaPositionHandle,
				2,
				GL_FLOAT,
				GL_FALSE,
				0,
				positionCoordinates);
		// Load the texture coordinate
		glVertexAttribPointer(gaTexHandle,
				2,
				GL_FLOAT,
				GL_FALSE,
				0,
				textureCoordinates);

		GLfloat matrix[16];
		android_media_surface_texture_get_transformation_matrix(player, matrix);

		glUniformMatrix4fv(gmTexMatrix, 1, GL_FALSE, matrix);

		glActiveTexture(GL_TEXTURE0);
		// Set the sampler texture unit to 0
		glUniform1i(gsTextureHandle, 0);
		glUniform1i(gmTexMatrix, 0);
		android_media_update_surface_texture(player);
		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		glDisableVertexAttribArray(gaPositionHandle);
		glDisableVertexAttribArray(gaTexHandle);

		eglSwapBuffers(disp, surface);
	}

	android_media_stop(player);

	return EXIT_SUCCESS;
}
View::View(UAUiWindow* surface) 
        : surface(surface),
          rotation_angle(0.f),
          num_vertex(3)
{
    // assert(eglBindAPI(EGL_OPENGL_ES_API) == EGL_TRUE);
    egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    assert(egl_display != EGL_NO_DISPLAY);
    EGLint major, minor;
    if (EGL_FALSE == eglInitialize(egl_display, &major, &minor))
    {
        printf("egl error: problem initializing.\n");
        exit(1);
    }
        
    EGLint attribs[] = 
            {
                EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
                EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
                EGL_NONE 
            };
    EGLint n;
    if (EGL_FALSE == eglChooseConfig(
        egl_display,
        attribs, 
        &egl_config,
        1, 
        &n))
    {
        printf("egl error: Cannot choose configuration.\n");
    }
    
    EGLint context_attribs[] = 
            { 
                EGL_CONTEXT_CLIENT_VERSION, 2, 
                EGL_NONE
            };

    egl_context = eglCreateContext(
        egl_display, 
        egl_config, 
        EGL_NO_CONTEXT, 
        context_attribs);

    assert(EGL_NO_CONTEXT != egl_context);

    EGLNativeWindowType nativeWindow = ua_ui_window_get_native_type(surface);
    egl_surface = eglCreateWindowSurface(egl_display, egl_config, nativeWindow, NULL);

    eglMakeCurrent(
        egl_display,
        egl_surface,
        egl_surface,
        egl_context);

    vertex_data = triangle();
    color_data = color_triangle();
    
    gProgram = create_program(vertex_shader(), fragment_shader());
    if (!gProgram)
    {
        printf("error making program\n");
        return;
    }
    
    gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
    gvColorHandle = glGetAttribLocation(gProgram, "vColor");
    
    rotation_uniform = glGetUniformLocation(gProgram, "angle");
    
    return;
}
Beispiel #18
0
// The start of the Application
int App::start(const std::vector<CL_String> &args)
{
	CL_OpenGLWindowDescription win_desc;
	//win_desc.set_version(3, 2, false);
	win_desc.set_allow_resize(true);
	win_desc.set_title("Point Sprite Example");
	win_desc.set_size(CL_Size( 800, 480 ), false);

	CL_DisplayWindow window(win_desc);
	CL_Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close);
	CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up);

	CL_String theme;
	if (CL_FileHelp::file_exists("../../../Resources/GUIThemeAero/theme.css"))
		theme = "../../../Resources/GUIThemeAero";
	else if (CL_FileHelp::file_exists("../../../Resources/GUIThemeBasic/theme.css"))
		theme = "../../../Resources/GUIThemeBasic";
	else
		throw CL_Exception("No themes found");

	CL_GUIWindowManagerTexture wm(window);
	CL_GUIManager gui(wm, theme);
	
	CL_GraphicContext gc = window.get_gc();

	// Deleted automatically by the GUI
	Options *options = new Options(gui, CL_Rect(0, 0, gc.get_size()));

	CL_Image image_grid(gc, "../Blend/Resources/grid.png");
	CL_Texture texture_particle(gc, "Resources/particle.png");
	float grid_width = (float) image_grid.get_width();
	float grid_height = (float) image_grid.get_height();

	grid_space = (float) (image_grid.get_width());

	setup_particles();

	CL_ShaderObject vertex_shader(gc, cl_shadertype_vertex, text_shader_vertex);
	if(!vertex_shader.compile())
	{
		throw CL_Exception(cl_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log()));
	}

	CL_ShaderObject fragment_shader(gc, cl_shadertype_fragment, text_shader_fragment);
	if(!fragment_shader.compile())
	{
		throw CL_Exception(cl_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log()));
	}

	CL_ProgramObject program_object(gc);
	program_object.attach(vertex_shader);
	program_object.attach(fragment_shader);
	program_object.bind_attribute_location(0, "InPosition");
	program_object.bind_attribute_location(1, "InColor");
	if (!program_object.link())
	{
		throw CL_Exception(cl_format("Unable to link program object: %1", program_object.get_info_log()));
	}

	options->request_repaint();

	unsigned int time_last = CL_System::get_time();

	while (!quit)
	{
		unsigned int time_now = CL_System::get_time();
		float time_diff = (float) (time_now - time_last);
		time_last = time_now;

		wm.process();
		wm.draw_windows(gc);

		int num_particles = options->num_particles;
		if (num_particles > max_particles)
			num_particles = max_particles;

		move_particles(time_diff, num_particles);

		const float grid_xpos = 10.0f;
		const float grid_ypos = 10.0f;

		// Draw the grid
		image_grid.draw(gc, grid_xpos, grid_ypos);

		if (num_particles > 0)
		{
			std::vector<CL_Vec2f> positions;
			std::vector<CL_Vec4f> colors;
			positions.resize(num_particles);
			colors.resize(num_particles);

			for (int cnt=0; cnt<num_particles; cnt++)
			{
				positions[cnt] = CL_Vec2f(grid_xpos + particles[cnt].xpos, grid_ypos + particles[cnt].ypos);
				switch (cnt % 3)
				{
					case 0:
						colors[cnt] = CL_Vec4f(1.0f, 0.0f, 0.0f, 1.0f);
						break;
					case 1:
						colors[cnt] = CL_Vec4f(0.0f, 1.0f, 0.0f, 1.0f);
						break;
					case 2:
						colors[cnt] = CL_Vec4f(0.0f, 0.0f, 1.0f, 1.0f);
						break;
				}
			};

			CL_BlendMode blend;
			blend.enable_blending(true);
			blend.set_blend_function(cl_blend_src_alpha, cl_blend_one, cl_blend_src_alpha, cl_blend_one);
			gc.set_blend_mode(blend);
			CL_Pen pen;
			pen.enable_point_sprite(true);
			pen.set_point_size(options->point_size);
			pen.set_point_sprite_origin(cl_point_sprite_origin_upper_left);
			gc.set_pen(pen);

			program_object.set_uniform1i("Texture0", 0);

			gc.set_texture(0, texture_particle);
			CL_PrimitivesArray prim_array(gc);
			prim_array.set_attributes(0, &positions[0]);
			prim_array.set_attributes(1, &colors[0]);
			gc.set_program_object(program_object);
			gc.draw_primitives(cl_points, num_particles, prim_array);
			gc.reset_program_object();
			gc.reset_texture(0);
			gc.reset_blend_mode();

			gc.reset_pen();
		}

		window.flip(1);

		CL_KeepAlive::process();
	}
	return 0;
}