ProgramObject HSV::create_shader_program(GraphicContext &gc) { ProgramObject program = ProgramObject::load(gc, "Resources/vertex.glsl", "Resources/fragment.glsl"); program.bind_attribute_location(0, "Position"); program.bind_attribute_location(1, "TexCoord0"); program.set_uniform_buffer_index("ProgramUniforms", 0); if (!program.link()) throw Exception("Unable to link program"); return program; }
ProgramObject LightsourceSimplePass::compile_and_link(GraphicContext &gc, const std::string &shader_path, const std::string &type) { ProgramObject program; std::string vertex_filename = PathHelp::combine(shader_path, string_format("LightsourceSimple/vertex_%1.%2", type, gc.get_shader_language() == shader_glsl ? "glsl" : "hlsl")); std::string fragment_filename = PathHelp::combine(shader_path, string_format("LightsourceSimple/fragment_light.%1", gc.get_shader_language() == shader_glsl ? "glsl" : "hlsl")); program = ShaderSetup::compile(gc, "", vertex_filename, fragment_filename, type == "rect" ? "RECT_PASS" : ""); program.bind_frag_data_location(0, "FragColor"); if (!program.link()) throw Exception("Shader linking failed!"); program.bind_attribute_location(0, "AttrPositionInObject"); program.set_uniform_buffer_index("Uniforms", 0); program.set_uniform1i("InstanceTexture", 0); program.set_uniform1i("NormalZTexture", 1); program.set_uniform1i("DiffuseColorTexture", 2); program.set_uniform1i("SpecularColorTexture", 3); program.set_uniform1i("SpecularLevelTexture", 4); program.set_uniform1i("ShadowMapsTexture", 5); program.set_uniform1i("ShadowMapsTextureSampler", 5); program.set_uniform1i("SelfIlluminationTexture", 6); return program; }
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; } }
int App::start(const std::vector<std::string> &args) { OpenGLWindowDescription description; description.set_title("UniformBlock Shader"); //description.set_version(3, 1, false); description.set_size(Size(1024, 768), true); DisplayWindow window(description); InputDevice keyboard = window.get_ic().get_keyboard(); GraphicContext gc = window.get_gc(); Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); Slot slot_window_close = window.sig_window_close().connect(this, &App::window_close); // Load and link shaders ProgramObject shader = ProgramObject::load(gc, "Resources/vertex_shader.glsl", "Resources/fragment_shader.glsl"); shader.bind_attribute_location(0, "Position"); if (!shader.link()) throw Exception("Unable to link shader program: Error:" + shader.get_info_log()); int block_size = shader.get_uniform_block_size("TestBlock"); const int num_blocks = 16; ProgramUniformBlock block(gc, block_size * num_blocks); std::vector<float> data_to_upload; data_to_upload.resize((num_blocks * block_size) / sizeof(float) ); float test_colour = 1.0f; for (int cnt=0; cnt<num_blocks; cnt++) { int offset = cnt * block_size / sizeof(float) ; data_to_upload[offset + 0] = test_colour; data_to_upload[offset + 1] = 0.5f; data_to_upload[offset + 2] = 1.0f; test_colour -= 0.05f; } block.upload_data(0, &data_to_upload[0], block_size * num_blocks); quit = false; Font font(gc, "tahoma", 32); clan::ubyte64 startTime = System::get_time(); //// Test code //GLuint uniform_index = -1; //const char *names[] = {"src_color"}; //glGetUniformIndices(handle, 1, names, &uniform_index); //if (uniform_index >=0) //{ // GLint offset; // GLint singleSize; // GLint uniform_type; // glGetActiveUniformsiv(handle, 1, &uniform_index, GL_UNIFORM_TYPE, &uniform_type); // glGetActiveUniformsiv(handle, 1, &uniform_index, GL_UNIFORM_OFFSET, &offset); // glGetActiveUniformsiv(handle, 1, &uniform_index, GL_UNIFORM_SIZE, &singleSize); // if ((uniform_type != GL_FLOAT_VEC3) || (offset !=0) || (singleSize != 1)) // { // throw Exception("well it seems it does not work"); // } //} while (!quit) { gc.clear(Colorf(0.1f, 0.1f, 0.2f)); OpenGL::check_error(); for (int test_run_y=0; test_run_y < 16; test_run_y++) { shader.set_uniform_block("TestBlock", block, test_run_y*block_size); for (int test_run_x=0; test_run_x < 16; test_run_x++) { Vec2f positions[3]; float size = 32.0f; positions[0].x = test_run_x * size; positions[0].y = test_run_y * size + size; positions[1].x = test_run_x * size + size; positions[1].y = test_run_y * size + size; positions[2].x = test_run_x * size + size; positions[2].y = test_run_y * size; PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); gc.set_program_object(shader, cl_program_matrix_modelview_projection); gc.draw_primitives(cl_triangles, 3, prim_array); gc.reset_program_object(); } } font.draw_text(gc, 32, 200, "Hello World"); window.flip(1); KeepAlive::process(); } return 0; }
// The start of the Application int App::start(const std::vector<std::string> &args) { try { DisplayWindowDescription win_desc; win_desc.set_allow_resize(true); win_desc.set_title("HDR Example"); win_desc.set_size(Size( 600, 630 ), false); DisplayWindow window(win_desc); Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close); Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); GraphicContext gc = window.get_gc(); // Load and link shaders ProgramObject shader = 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 Exception("Unable to link shader program: Error:" + shader.get_info_log()); const float dynamic_range_start = -1.5f; const float dynamic_range_end = 1.5f; const Size image_size(gc.get_width() - 64, 128); Texture image_rgb32f = create_rgb32f(gc, image_size, dynamic_range_start, dynamic_range_end); Font font(gc, "Tahoma", 20); clan::ubyte64 time_last = System::get_time(); bool direction = false; while (!quit) { clan::ubyte64 time_now = System::get_time(); time_delta = ((float) time_now - time_last) / 1000.0f; time_last = time_now; if (direction) { color_offset.r += time_delta * 1.0f; if (color_offset.r > 2.0f) { color_offset.r = 2.0f; direction = false; } } else { color_offset.r -= time_delta * 1.0f; if (color_offset.r < -2.0f) { color_offset.r = -2.0f; direction = true; } } color_offset.g = color_offset.r; color_offset.b = color_offset.r; gc.clear(Colorf(0.2f,0.2f,0.5f)); font.draw_text(gc, 32, 50, "Showing Texture RGB values from " + StringHelp::float_to_text(dynamic_range_start) + " to " + StringHelp::float_to_text(dynamic_range_end)); draw_image(gc, image_rgb32f, 32.0f, 100.0f, shader, Vec4f(0.0f, 0.0f, 0.0f, 0.0f)); font.draw_text(gc, 32, 350, "Showing Texture with an offset to the floating point color component"); draw_image(gc, image_rgb32f, 32.0f, 400.0f, shader, color_offset); KeepAlive::process(0); window.flip(1); } } catch(Exception &exception) { // Create a console window for text-output if not available ConsoleWindow console("Console", 80, 160); Console::write_line("Exception caught: " + exception.get_message_and_stack_trace()); console.display_close_message(); return -1; } return 0; }