void PostProcessor::init() { m_pTextureOriginal = _createTexture(); m_FBO_original = _createFBO(m_pTextureOriginal); m_extractBloomProgram = _createShaderProgram(vertexShader, extractBloomShader); glUseProgram(m_extractBloomProgram); int loc = glGetUniformLocation(m_extractBloomProgram, "Sample0"); assert(loc >= 0); glUniform1i(loc, 0); loc = glGetUniformLocation(m_extractBloomProgram, "ThresholdLevel"); assert(loc >= 0); glUniform1i(loc, config.bloomFilter.thresholdLevel); #ifdef GLES2 m_copyProgram = _createShaderProgram(vertexShader, copyShader); glUseProgram(m_copyProgram); loc = glGetUniformLocation(m_copyProgram, "Sample0"); assert(loc >= 0); glUniform1i(loc, 0); #endif m_seperableBlurProgram = _createShaderProgram(vertexShader, seperableBlurShader); glUseProgram(m_seperableBlurProgram); loc = glGetUniformLocation(m_seperableBlurProgram, "Sample0"); assert(loc >= 0); glUniform1i(loc, 0); loc = glGetUniformLocation(m_seperableBlurProgram, "TexelSize"); assert(loc >= 0); glUniform2f(loc, 1.0f / video().getWidth(), 1.0f / video().getHeight()); loc = glGetUniformLocation(m_seperableBlurProgram, "Orientation"); assert(loc >= 0); glUniform1i(loc, 0); loc = glGetUniformLocation(m_seperableBlurProgram, "BlurAmount"); assert(loc >= 0); glUniform1i(loc, config.bloomFilter.blurAmount); loc = glGetUniformLocation(m_seperableBlurProgram, "BlurScale"); assert(loc >= 0); glUniform1f(loc, 1.0f); loc = glGetUniformLocation(m_seperableBlurProgram, "BlurStrength"); assert(loc >= 0); glUniform1f(loc, config.bloomFilter.blurStrength/100.0f); m_glowProgram = _createShaderProgram(vertexShader, glowShader); glUseProgram(m_glowProgram); loc = glGetUniformLocation(m_glowProgram, "Sample0"); assert(loc >= 0); glUniform1i(loc, 0); loc = glGetUniformLocation(m_glowProgram, "Sample1"); assert(loc >= 0); glUniform1i(loc, 1); loc = glGetUniformLocation(m_glowProgram, "BlendMode"); assert(loc >= 0); glUniform1i(loc, config.bloomFilter.blendMode); m_pTextureGlowMap = _createTexture(); m_pTextureBlur = _createTexture(); m_FBO_glowMap = _createFBO(m_pTextureGlowMap); m_FBO_blur = _createFBO(m_pTextureBlur); glUseProgram(0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); }
void COGL_FragmentProgramCombiner::GenerateCombinerSettingConstants(int index) { OGLShaderCombinerSaveType &prog = m_vCompiledShaders[index]; UseProgram(prog.programID); float *pf; if(prog.EnvColorLocation != -1) { pf = GetEnvColorfv(); if (memcmp(pf, prog.EnvColors, sizeof(prog.EnvColors))) { memcpy(prog.EnvColors, pf, sizeof(prog.EnvColors)); glUniform4fv(prog.EnvColorLocation, 1, pf); OPENGL_CHECK_ERRORS; } } if(prog.PrimColorLocation != -1) { pf = GetPrimitiveColorfv(); if (memcmp(pf, prog.PrimColors, sizeof(prog.PrimColors))) { memcpy(prog.PrimColors, pf, sizeof(prog.PrimColors)); glUniform4fv(prog.PrimColorLocation, 1, pf); OPENGL_CHECK_ERRORS; } } if(prog.EnvFracLocation != -1) { // avoid slow float compare.. if( *(int *)&gRDP.LODFrac != *(int *)&prog.EnvLODFrac ) { prog.EnvLODFrac = gRDP.LODFrac; float frac = gRDP.LODFrac / 255.0f; float tempf[4] = {frac,frac,frac,frac}; glUniform4fv(prog.EnvFracLocation, 1, tempf); OPENGL_CHECK_ERRORS; } } if(prog.PrimFracLocation != -1) { if( *(int *)&gRDP.primLODFrac != *(int *)&prog.PrimLODFrac ) { prog.PrimLODFrac = gRDP.primLODFrac; float frac2 = gRDP.primLODFrac / 255.0f; float tempf2[4] = {frac2,frac2,frac2,frac2}; glUniform4fv(prog.PrimFracLocation, 1, tempf2); OPENGL_CHECK_ERRORS; } } if(prog.FogColorLocation != -1) { pf = &gRDP.fvFogColor[0]; if (memcmp(pf, prog.FogColors, sizeof(prog.FogColors))) { memcpy(prog.FogColors, pf, sizeof(prog.FogColors)); glUniform4fv(prog.FogColorLocation, 1, pf); OPENGL_CHECK_ERRORS; } } if(prog.FogMinMaxLocation != -1) { if( gRSPfFogMin != prog.FogMin || gRSPfFogMax != prog.FogMax ) { prog.FogMin = gRSPfFogMin; prog.FogMax = gRSPfFogMax; glUniform2f(prog.FogMinMaxLocation,gRSPfFogMin,gRSPfFogMax); OPENGL_CHECK_ERRORS; } } if(prog.AlphaRefLocation != -1) { if( m_AlphaRef != prog.AlphaRef ) { prog.AlphaRef = m_AlphaRef; glUniform1f(prog.AlphaRefLocation, m_AlphaRef); OPENGL_CHECK_ERRORS; } } }
void setTextureOffset(vec2 v){ glUniform2f(loc_texOffset,(GLfloat)v.x,(GLfloat)v.y); }
//-------------------------------------------------------------- void ofShader::setUniform2f(const string & name, float v1, float v2) const{ if(bLoaded) { int loc = getUniformLocation(name); if (loc != -1) glUniform2f(loc, v1, v2); } }
void light_system::render_all_lights(augs::renderer& output, const std::array<float, 16> projection_matrix, viewing_step& step, std::function<void()> neon_callback) { const auto& cosmos = step.cosm; const auto dt = step.get_delta(); const float global_time_seconds = static_cast<float>(step.get_interpolated_total_time_passed_in_seconds()); ensure_eq(0, output.get_triangle_count()); output.light_fbo.use(); glClearColor(0.1f, 0.2f, 0.2f, 1.0f); output.clear_current_fbo(); glClearColor(0.f, 0.f, 0.f, 0.f); auto& light_program = *get_resource_manager().find(assets::program_id::LIGHT); auto& default_program = *get_resource_manager().find(assets::program_id::DEFAULT); light_program.use(); const auto light_pos_uniform = glGetUniformLocation(light_program.id, "light_pos"); const auto light_max_distance_uniform = glGetUniformLocation(light_program.id, "max_distance"); const auto light_attenuation_uniform = glGetUniformLocation(light_program.id, "light_attenuation"); const auto light_multiply_color_uniform = glGetUniformLocation(light_program.id, "multiply_color"); const auto projection_matrix_uniform = glGetUniformLocation(light_program.id, "projection_matrix"); const auto& interp = step.session.systems_audiovisual.get<interpolation_system>(); const auto& particles = step.session.systems_audiovisual.get<particles_simulation_system>(); std::vector<messages::visibility_information_request> requests; std::vector<messages::visibility_information_response> responses; glUniformMatrix4fv(projection_matrix_uniform, 1, GL_FALSE, projection_matrix.data()); for (const auto it : cosmos.get(processing_subjects::WITH_LIGHT)) { messages::visibility_information_request request; request.eye_transform = it.viewing_transform(interp); request.filter = filters::line_of_sight_query(); request.square_side = it.get<components::light>().max_distance.base_value; request.subject = it; requests.push_back(request); } { std::vector<messages::line_of_sight_response> dummy; visibility_system().respond_to_visibility_information_requests(cosmos, {}, requests, dummy, responses); } const auto camera_transform = step.camera.transform; const auto camera_size = step.camera.visible_world_area; const auto camera_offset = -camera_transform.pos + camera_size / 2; glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ONE, GL_ONE); glerr; for (size_t i = 0; i < responses.size(); ++i) { const auto& r = responses[i]; const auto& light_entity = cosmos[requests[i].subject]; const auto& light = light_entity.get<components::light>(); auto& cache = per_entity_cache[light_entity.get_id().pool.indirection_index]; const float delta = dt.in_seconds(); light.constant.variation.update_value(rng, cache.all_variation_values[0], delta); light.linear.variation.update_value(rng, cache.all_variation_values[1], delta); light.quadratic.variation.update_value(rng, cache.all_variation_values[2], delta); light.wall_constant.variation.update_value(rng, cache.all_variation_values[3], delta); light.wall_linear.variation.update_value(rng, cache.all_variation_values[4], delta); light.wall_quadratic.variation.update_value(rng, cache.all_variation_values[5], delta); light.position_variations[0].update_value(rng, cache.all_variation_values[6], delta); light.position_variations[1].update_value(rng, cache.all_variation_values[7], delta); for (size_t t = 0; t < r.get_num_triangles(); ++t) { const auto world_light_tri = r.get_world_triangle(t, requests[i].eye_transform.pos); vertex_triangle renderable_light_tri; renderable_light_tri.vertices[0].pos = world_light_tri.points[0] + camera_offset; renderable_light_tri.vertices[1].pos = world_light_tri.points[1] + camera_offset; renderable_light_tri.vertices[2].pos = world_light_tri.points[2] + camera_offset; auto considered_color = light.color; if (considered_color == black) { considered_color.set_hsv({ fmod(global_time_seconds / 16.f, 1.f), 1.0, 1.0 }); } renderable_light_tri.vertices[0].color = considered_color; renderable_light_tri.vertices[1].color = considered_color; renderable_light_tri.vertices[2].color = considered_color; output.push_triangle(renderable_light_tri); } //for (size_t d = 0; d < r.get_num_discontinuities(); ++d) { // const auto world_discontinuity = *r.get_discontinuity(d); // // if (!world_discontinuity.is_boundary) { // vertex_triangle renderable_light_tri; // // const float distance_from_light = (requests[i].eye_transform.pos - world_discontinuity.points.first).length(); // const float angle = 80.f / ((distance_from_light+0.1f)/50.f); // // //(requests[i].eye_transform.pos - world_discontinuity.points.first).length(); // // if (world_discontinuity.winding == world_discontinuity.RIGHT) { // renderable_light_tri.vertices[0].pos = world_discontinuity.points.first + camera_offset; // renderable_light_tri.vertices[1].pos = world_discontinuity.points.second + camera_offset; // renderable_light_tri.vertices[2].pos = vec2(world_discontinuity.points.second).rotate(-angle, world_discontinuity.points.first) + camera_offset; // } // else { // renderable_light_tri.vertices[0].pos = world_discontinuity.points.first + camera_offset; // renderable_light_tri.vertices[1].pos = world_discontinuity.points.second + camera_offset; // renderable_light_tri.vertices[2].pos = vec2(world_discontinuity.points.second).rotate(angle, world_discontinuity.points.first) + camera_offset; // } // // renderable_light_tri.vertices[0].color = light.color; // renderable_light_tri.vertices[1].color = light.color; // renderable_light_tri.vertices[2].color = light.color; // // output.push_triangle(renderable_light_tri); // } //} vec2 light_displacement = vec2(cache.all_variation_values[6], cache.all_variation_values[7]); auto screen_pos = requests[i].eye_transform - camera_transform; screen_pos.pos.x += camera_size.x * 0.5f; screen_pos.pos.y = camera_size.y - (screen_pos.pos.y + camera_size.y * 0.5f); screen_pos += light_displacement; glUniform2f(light_pos_uniform, screen_pos.pos.x, screen_pos.pos.y); glUniform1f(light_max_distance_uniform, light.max_distance.base_value); glUniform3f(light_attenuation_uniform, cache.all_variation_values[0] + light.constant.base_value, cache.all_variation_values[1] + light.linear.base_value, cache.all_variation_values[2] + light.quadratic.base_value ); glUniform3f(light_multiply_color_uniform, 1.f, 1.f, 1.f); output.call_triangles(); output.clear_triangles(); light_program.use(); glUniform1f(light_max_distance_uniform, light.wall_max_distance.base_value); glUniform3f(light_attenuation_uniform, cache.all_variation_values[3] + light.wall_constant.base_value, cache.all_variation_values[4] + light.wall_linear.base_value, cache.all_variation_values[5] + light.wall_quadratic.base_value ); glUniform3f(light_multiply_color_uniform, light.color.r/255.f, light.color.g/255.f, light.color.b/255.f); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::DYNAMIC_BODY], step.camera, renderable_drawing_type::NORMAL); output.call_triangles(); output.clear_triangles(); glUniform3f(light_multiply_color_uniform, 1.f, 1.f, 1.f); } default_program.use(); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::DYNAMIC_BODY], step.camera, renderable_drawing_type::NEON_MAPS); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::SMALL_DYNAMIC_BODY], step.camera, renderable_drawing_type::NEON_MAPS); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::FLYING_BULLETS], step.camera, renderable_drawing_type::NEON_MAPS); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::CAR_INTERIOR], step.camera, renderable_drawing_type::NEON_MAPS); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::CAR_WHEEL], step.camera, renderable_drawing_type::NEON_MAPS); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::EFFECTS], step.camera, renderable_drawing_type::NEON_MAPS); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::ON_GROUND], step.camera, renderable_drawing_type::NEON_MAPS); render_system().draw_entities(interp, global_time_seconds,output.triangles, step.visible_per_layer[render_layer::ON_TILED_FLOOR], step.camera, renderable_drawing_type::NEON_MAPS); { particles_simulation_system::drawing_input in(output.triangles); in.camera = step.camera; in.drawing_type = renderable_drawing_type::NEON_MAPS; particles.draw(render_layer::EFFECTS, in); } neon_callback(); output.call_triangles(); output.clear_triangles(); glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE); glerr; graphics::fbo::use_default(); output.set_active_texture(2); output.bind_texture(output.light_fbo); output.set_active_texture(0); }
void MyGLImageViewer::drawFCVisualization(GLuint *texVBO, GLuint shaderProg, int windowWidth, int windowHeight, VRParams params) { glUseProgram(shaderProg); drawQuads(1.0f/params.scaleWidth, 1.0f/params.scaleHeight, 1.0f/params.scaleDepth); GLuint texLoc = glGetUniformLocation(shaderProg, "position"); glUniform1i(texLoc, 8); glActiveTexture(GL_TEXTURE8); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texVBO[params.positionTextureIndex]); texLoc = glGetUniformLocation(shaderProg, "normal"); glUniform1i(texLoc, 9); glActiveTexture(GL_TEXTURE9); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texVBO[params.normalTextureIndex]); texLoc = glGetUniformLocation(shaderProg, "curvature"); glUniform1i(texLoc, 10); glActiveTexture(GL_TEXTURE10); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texVBO[params.curvatureTextureIndex]); texLoc = glGetUniformLocation(shaderProg, "focusLayer"); glUniform1i(texLoc, 11); glActiveTexture(GL_TEXTURE11); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texVBO[params.focusTextureIndex]); texLoc = glGetUniformLocation(shaderProg, "contextLayer"); glUniform1i(texLoc, 12); glActiveTexture(GL_TEXTURE12); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texVBO[params.contextTextureIndex]); texLoc = glGetUniformLocation(shaderProg, "focusPoint"); glUniform2f(texLoc, params.focusPoint[0], params.focusPoint[1]); texLoc = glGetUniformLocation(shaderProg, "focusRadius"); glUniform1f(texLoc, params.focusRadius); texLoc = glGetUniformLocation(shaderProg, "windowWidth"); glUniform1i(texLoc, windowWidth); texLoc = glGetUniformLocation(shaderProg, "windowHeight"); glUniform1i(texLoc, windowHeight); glUseProgram(0); glActiveTexture(GL_TEXTURE8); glDisable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE9); glDisable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE10); glDisable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE11); glDisable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE12); glDisable(GL_TEXTURE_2D); }
void setUniform (int loc, const Vec2f& v) { if (loc >= 0) glUniform2f(loc, v.x, v.y); }
bool Shader::setShaderUniform(const std::string &name, GLsizei row, GLsizei column, GLsizei count, GLboolean transpose, const GLfloat *value) { GLint loc; if(!this->getUniformLoc(name, loc)) return false; if(count > (row * column)) return false; // TODO: check veracity of this decision tree switch(column) { case 1: { switch(row) { case 1: glUniform1f(loc, value[0]); break; case 2: glUniform2f(loc, value[0], value[1]); break; case 3: glUniform3f(loc, value[0], value[1], value[2]); break; case 4: glUniform4f(loc, value[0], value[1], value[2], value[3]); break; default: return false; } break; } case 2: { switch(column) { case 2: glUniformMatrix2fv(loc, count, transpose, value); break; case 3: glUniformMatrix2x3fv(loc, count, transpose, value); break; case 4: glUniformMatrix2x4fv(loc, count, transpose, value); break; default: return false; } break; } case 3: { switch(column) { case 2: glUniformMatrix3x2fv(loc, count, transpose, value); break; case 3: glUniformMatrix3fv(loc, count, transpose, value); break; case 4: glUniformMatrix3x4fv(loc, count, transpose, value); break; default: return false; } break; } case 4: { switch(column) { case 2: glUniformMatrix4x2fv(loc, count, transpose, value); case 3: glUniformMatrix4x3fv(loc, count, transpose, value); break; case 4: glUniformMatrix4fv(loc, count, transpose, value); break; default: return false; } break; } default: return false; } return true; }
void OpenGLPostProcessing::BlitFromTexture(TargetRectangle src, TargetRectangle dst, int src_texture, int src_width, int src_height, int layer) { ApplyShader(); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glViewport(dst.left, dst.bottom, dst.GetWidth(), dst.GetHeight()); OpenGL_BindAttributelessVAO(); m_shader.Bind(); glUniform4f(m_uniform_resolution, (float)src_width, (float)src_height, 1.0f / (float)src_width, 1.0f / (float)src_height); glUniform4f(m_uniform_src_rect, src.left / (float) src_width, src.bottom / (float) src_height, src.GetWidth() / (float) src_width, src.GetHeight() / (float) src_height); glUniform1ui(m_uniform_time, (GLuint)m_timer.GetTimeElapsed()); glUniform1i(m_uniform_layer, layer); if (m_config.IsDirty()) { for (auto& it : m_config.GetOptions()) { if (it.second.m_dirty) { switch (it.second.m_type) { case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_BOOL: glUniform1i(m_uniform_bindings[it.first], it.second.m_bool_value); break; case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_INTEGER: switch (it.second.m_integer_values.size()) { case 1: glUniform1i(m_uniform_bindings[it.first], it.second.m_integer_values[0]); break; case 2: glUniform2i(m_uniform_bindings[it.first], it.second.m_integer_values[0], it.second.m_integer_values[1]); break; case 3: glUniform3i(m_uniform_bindings[it.first], it.second.m_integer_values[0], it.second.m_integer_values[1], it.second.m_integer_values[2]); break; case 4: glUniform4i(m_uniform_bindings[it.first], it.second.m_integer_values[0], it.second.m_integer_values[1], it.second.m_integer_values[2], it.second.m_integer_values[3]); break; } break; case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_FLOAT: switch (it.second.m_float_values.size()) { case 1: glUniform1f(m_uniform_bindings[it.first], it.second.m_float_values[0]); break; case 2: glUniform2f(m_uniform_bindings[it.first], it.second.m_float_values[0], it.second.m_float_values[1]); break; case 3: glUniform3f(m_uniform_bindings[it.first], it.second.m_float_values[0], it.second.m_float_values[1], it.second.m_float_values[2]); break; case 4: glUniform4f(m_uniform_bindings[it.first], it.second.m_float_values[0], it.second.m_float_values[1], it.second.m_float_values[2], it.second.m_float_values[3]); break; } break; } it.second.m_dirty = false; } } m_config.SetDirty(false); } glActiveTexture(GL_TEXTURE9); glBindTexture(GL_TEXTURE_2D_ARRAY, src_texture); g_sampler_cache->BindLinearSampler(9); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); }
void set(const std::string& name, float value1, float value2) { GL(glUniform2f(get_location(name.c_str()), value1, value2)); }
void set(const std::string& name, double value1, double value2) { GL(glUniform2f(get_location(name.c_str()), static_cast<float>(value1), static_cast<float>(value2))); }
void CustomPostEffectShader::render(Camera* camera, RenderTexture* render_texture, PostEffectData* post_effect_data, std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& tex_coords, std::vector<unsigned short>& triangles) { glUseProgram(program_->id()); GLuint tmpID; if(vaoID_ == 0) { glGenVertexArrays(1, &vaoID_); glBindVertexArray(vaoID_); glGenBuffers(1, &tmpID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tmpID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*triangles.size(), &triangles[0], GL_STATIC_DRAW); if (vertices.size()) { glGenBuffers(1, &tmpID); glBindBuffer(GL_ARRAY_BUFFER, tmpID); glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3)*vertices.size(), &vertices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(a_position_); glVertexAttribPointer(a_position_, 3, GL_FLOAT, 0, 0, 0); } if (tex_coords.size()) { glGenBuffers(1, &tmpID); glBindBuffer(GL_ARRAY_BUFFER, tmpID); glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2)*tex_coords.size(), &tex_coords[0], GL_STATIC_DRAW); glEnableVertexAttribArray(a_tex_coord_); glVertexAttribPointer(a_tex_coord_, 2, GL_FLOAT, 0, 0, 0); } } int texture_index = 0; if (u_texture_ != -1) { glActiveTexture(getGLTexture(texture_index)); glBindTexture(GL_TEXTURE_2D, render_texture->getId()); glUniform1i(u_texture_, texture_index++); } if (u_projection_matrix_ != -1) { glm::mat4 view = camera->getViewMatrix(); glUniformMatrix4fv(u_projection_matrix_, 1, GL_TRUE, glm::value_ptr(view)); } if (u_right_eye_ != -1) { bool right = camera->render_mask() & RenderData::RenderMaskBit::Right; glUniform1i(u_right_eye_, right ? 1 : 0); } for (auto it = texture_keys_.begin(); it != texture_keys_.end(); ++it) { glActiveTexture(getGLTexture(texture_index)); Texture* texture = post_effect_data->getTexture(it->second); glBindTexture(texture->getTarget(), texture->getId()); glUniform1i(it->first, texture_index++); } for (auto it = float_keys_.begin(); it != float_keys_.end(); ++it) { glUniform1f(it->first, post_effect_data->getFloat(it->second)); } for (auto it = vec2_keys_.begin(); it != vec2_keys_.end(); ++it) { glm::vec2 v = post_effect_data->getVec2(it->second); glUniform2f(it->first, v.x, v.y); } for (auto it = vec3_keys_.begin(); it != vec3_keys_.end(); ++it) { glm::vec3 v = post_effect_data->getVec3(it->second); glUniform3f(it->first, v.x, v.y, v.z); } for (auto it = vec4_keys_.begin(); it != vec4_keys_.end(); ++it) { glm::vec4 v = post_effect_data->getVec4(it->second); glUniform4f(it->first, v.x, v.y, v.z, v.w); } for (auto it = mat4_keys_.begin(); it != mat4_keys_.end(); ++it) { glm::mat4 m = post_effect_data->getMat4(it->second); glUniformMatrix4fv(it->first, 1, GL_FALSE, glm::value_ptr(m)); } glBindVertexArray(vaoID_); glDrawElements(GL_TRIANGLES, triangles.size(), GL_UNSIGNED_SHORT, 0); glBindVertexArray(0); }
void TriangularMesh::render(UserInterface *ui, glm::mat4 const& model) { glUseProgram(shader->shaderId); // Our ModelViewProjection : multiplication of our 3 matrices glm::mat4 MVP = ui->projectionMatrix * ui->current_viewpoint.vp_matrix * model; // configure the variable 'theTextureSampler' to use texture unit 0 glUniform1i(shader->shaderPgmTextureIndex, 0); // Send our transformation to the currently bound shader, in the // "MVP" uniform For each model you render, since the MVP will be // different (at least the M part) glUniformMatrix4fv(shader->shaderPgmMVPMatrixIndex, 1, GL_FALSE, &MVP[0][0]); if (shader->waterWiggleIndex) { float dx = 0.3 * cos(ui->frameTime * 1.0e-6) / TEXTURE_GRID_WIDTH; float dy = 0.1 * sin(ui->frameTime * 1.0e-6) / TEXTURE_GRID_WIDTH; glUniform2f(shader->waterWiggleIndex, dx, dy); } if (shader->fogColorIndex) { glUniform4f(shader->fogColorIndex, ui->skyColor.r, ui->skyColor.g, ui->skyColor.b, 1); } if (shader->fogDensityIndex) { glUniform1f(shader->fogDensityIndex, 0.007); } // First attribute buffer -- vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glVertexAttribPointer(0, // attribute 0 3, // size len([x,y,z]) GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0); // array buffer offset; // 2nd attribute -- texture UV coordinates glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, uvBufferId); glVertexAttribPointer(1, // attribute 1 2, // size len([U,V]) GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0); // array buffer offset; // 3rd attribute -- surface ambient lighting value glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, ambientBufferId); glVertexAttribPointer(2, // attribute 2 1, // size len([A]) GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0); // array buffer offset; // Draw the mesh glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId); glDrawElements(GL_TRIANGLES, count*3, GL_UNSIGNED_INT, (void*)0); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glDisableVertexAttribArray(2); }
void reShader::setUniform( int name, reVec2& v, int index ) { glUniform2f(location(name, index), v.x, v.y); }
static void width_uniform(ilG_material *mtl, GLint location, void *user) { (void)mtl; ilG_heightmap *self = user; glUniform2f(location, self->w, self->h); }
void SpotLight::Bind(GLint _shaderMatrixUniform1, GLint _shaderMatrixUniform2) { glUniformMatrix4x3fv(_shaderMatrixUniform1, 1, GL_FALSE, glm::value_ptr(data)); glUniform2f(_shaderMatrixUniform2, cutOffs.x, cutOffs.y); }
void Uniform::set2f(glm::vec2 vec) { glUniform2f(this->glId, vec.x, vec.y); CHECK_GL_ERROR; }
void Uniform2f::apply() { glUniform2f(location(), m_val[0], m_val[1]); }
void Program::SetUniform( const Uniform& uniform, const Vec2& value ) { glUniform2f( uniform, value.X, value.Y ); }
//---------------------------------------------------------------------------------------------- void MaterialEffectUI::Affect() { GLuint program = GetNode()->GetShaderProgram(); float WVMat[16] = {0.f}, WVPMat[16] = {0.f}, UVMat[16] = { m_pDiffuseMap->GetUL(), 0.f, 0.f, 0.f, 0.f, m_pDiffuseMap->GetVL(), 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f}; Matrix4Mul(WVMat, m_pD3DInterface->m_world, m_pD3DInterface->m_view); Matrix4Mul(WVPMat, WVMat, m_pD3DInterface->m_proj); GLint modelViewProjectionMatrixLocation = glGetUniformLocation(program, "mVPMat"); if (modelViewProjectionMatrixLocation != -1){ glUniformMatrix4fv(modelViewProjectionMatrixLocation, 1, GL_TRUE, WVPMat); } GLint modelViewMatrixLocation = glGetUniformLocation(program, "mVMat"); if (modelViewMatrixLocation != -1){ glUniformMatrix4fv(modelViewMatrixLocation, 1, GL_TRUE, WVMat); } GLint modelUVMatrixLocation = glGetUniformLocation(program, "mUVMat"); if (modelUVMatrixLocation != -1){ glUniformMatrix4fv(modelUVMatrixLocation, 1, GL_TRUE, UVMat); } GLint modelUVOffsetLocation = glGetUniformLocation(program, "UVOffset"); if (modelUVOffsetLocation != -1){ glUniform2f(modelUVOffsetLocation, m_pDiffuseMap->GetU(), m_pDiffuseMap->GetV()); } if (m_pRT == nullptr) { SetSampleTextureID(0, m_pDiffuseMap); } else { glActiveTexture(GL_TEXTURE0 + 0); glBindTexture(GL_TEXTURE_2D, m_pRT->GetTexture()); } bool bFog = m_pD3DInterface->m_bFog && fog; GLint InFog = glGetUniformLocation(program, "bFog"); if (InFog != -1){ glUniform1i(InFog, bFog); } GLint InFogColor = glGetUniformLocation(program, "fogColor"); if (InFogColor != -1) { byte a = (m_pD3DInterface->m_FogColor & 0xFF000000) >> 24; byte r = (m_pD3DInterface->m_FogColor & 0x00FF0000) >> 16; byte g = (m_pD3DInterface->m_FogColor & 0x0000FF00) >> 8; byte b = (m_pD3DInterface->m_FogColor & 0x000000FF); glUniform4f(InFogColor, r / 255.f, g / 255.f, b / 255.f, a / 255.f); }
void BSTexturedPolygonProgram2D::set_uniform(std::string _value_shader_name, float _value1, float _value2) { glUniform2f(locations_shader_values[_value_shader_name], _value1, _value2); }
void ShaderProgram::SetCustomUniform(const std::string& name, const Vector2& value) { int loc = glGetUniformLocation(program_, name.c_str()); glUniform2f(loc, value.x, value.y); }
//////////////////////////////////////////////////////////////////////////////// //! Display callback //////////////////////////////////////////////////////////////////////////////// void display() { // run CUDA kernel to generate vertex positions if (animate) { runCuda(); } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // set view matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(translateX, translateY, translateZ); glRotatef(rotateX, 1.0, 0.0, 0.0); glRotatef(rotateY, 0.0, 1.0, 0.0); // render from the vbo glBindBuffer(GL_ARRAY_BUFFER, posVertexBuffer); glVertexPointer(4, GL_FLOAT, 0, 0); glEnableClientState(GL_VERTEX_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, heightVertexBuffer); glClientActiveTexture(GL_TEXTURE0); glTexCoordPointer(1, GL_FLOAT, 0, 0); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, slopeVertexBuffer); glClientActiveTexture(GL_TEXTURE1); glTexCoordPointer(2, GL_FLOAT, 0, 0); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glUseProgram(shaderProg); // Set default uniform variables parameters for the vertex shader GLuint uniHeightScale, uniChopiness, uniSize; uniHeightScale = glGetUniformLocation(shaderProg, "heightScale"); glUniform1f(uniHeightScale, 0.5f); uniChopiness = glGetUniformLocation(shaderProg, "chopiness"); glUniform1f(uniChopiness, 1.0f); uniSize = glGetUniformLocation(shaderProg, "size"); glUniform2f(uniSize, meshW, meshH); // Set default uniform variables parameters for the pixel shader GLuint uniDeepColor, uniShallowColor, uniSkyColor, uniLightDir; uniDeepColor = glGetUniformLocation(shaderProg, "deepColor"); glUniform4f(uniDeepColor, 0.0f, 0.0f, 0.1f, 1.0f); uniShallowColor = glGetUniformLocation(shaderProg, "shallowColor"); glUniform4f(uniShallowColor, 0.1f, 0.4f, 0.3f, 1.0f); uniSkyColor = glGetUniformLocation(shaderProg, "skyColor"); glUniform4f(uniSkyColor, 0.5f, 0.5f, 0.5f, 1.0f); uniLightDir = glGetUniformLocation(shaderProg, "lightDir"); glUniform3f(uniLightDir, 0.0f, 1.0f, 0.0f); // end of uniform settings glColor3f(1.0, 1.0, 1.0); if (drawPoints) { glDrawArrays(GL_POINTS, 0, meshW * meshH); } else { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glPolygonMode(GL_FRONT_AND_BACK, wireFrame ? GL_LINE : GL_FILL); glDrawElements(GL_TRIANGLE_STRIP, ((meshW*2)+2)*(meshH-1), GL_UNSIGNED_INT, 0); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } glDisableClientState(GL_VERTEX_ARRAY); glClientActiveTexture(GL_TEXTURE0); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glClientActiveTexture(GL_TEXTURE1); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glUseProgram(0); if (g_CheckRender && g_CheckRender->IsQAReadback() && g_Verify) { printf("> (Frame %d) Readback BackBuffer\n", frameCount); g_CheckRender->readback( windowW, windowH ); g_CheckRender->savePPM ( sOriginal[0], true, NULL); if (!g_CheckRender->PPMvsPPM( sOriginal[0], sReference[0], MAX_EPSILON, THRESHOLD)) { g_TotalErrors++; } g_Verify = false; } glutSwapBuffers(); computeFPS(); }
void Uniform::Set(const Vector2& v) { glUniform2f(uniform_, v.x, v.y); }
void DeferredShader::setScreenSize(float screenWidth, float screenHeight) { glUniform2f(screenSizeLocation, screenWidth, screenHeight); }
void SSAO::runShader(MeshDocument& md, GLArea* gla){ /***********************************************************/ //NORMAL MAP and DEPTH MAP generation /***********************************************************/ if (gla == NULL) return; this->bind(); glUseProgram(this->_normalMapShaderProgram); vcg::Matrix44f mProj, mInverseProj; glMatrixMode(GL_PROJECTION); glGetFloatv(GL_PROJECTION_MATRIX, mProj.V()); glMatrixMode(GL_MODELVIEW); mProj.transposeInPlace(); mInverseProj = vcg::Inverse(mProj); glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); foreach(MeshModel *m, md.meshList) if(m->visible) { m->render(vcg::GLW::DMFlat, vcg::GLW::CMNone, vcg::GLW::TMNone); } glUseProgram(0); /***********************************************************/ //SSAO PASS /***********************************************************/ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo2); glUseProgram(this->_ssaoShaderProgram); glEnable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, this->_noise); GLuint noiseloc = glGetUniformLocation(this->_ssaoShaderProgram, "rnm"); glUniform1i(noiseloc, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, this->_color1); GLuint loc = glGetUniformLocation(this->_ssaoShaderProgram, "normalMap"); glUniform1i(loc, 1); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, this->_depthMap); loc = glGetUniformLocation(this->_ssaoShaderProgram, "depthMap"); glUniform1i(loc, 2); GLuint radiusLoc = glGetUniformLocation(this->_ssaoShaderProgram, "rad"); glUniform1f(radiusLoc, this->_radius); GLuint matrixLoc = glGetUniformLocation(this->_ssaoShaderProgram, "proj"); glUniformMatrix4fv(matrixLoc, 1, 0, mProj.transpose().V()); GLuint invMatrixLoc = glGetUniformLocation(this->_ssaoShaderProgram, "invProj"); glUniformMatrix4fv(invMatrixLoc, 1, 0, mInverseProj.transpose().V()); glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLE_STRIP); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glEnd(); glUseProgram(0); /***********************************************************/ //BLURRING horizontal /***********************************************************/ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo); glUseProgram(this->_blurShaderProgram); float blur_coef = 0.8; GLfloat scale = 1/(this->_texW * blur_coef); GLuint scaleLoc = glGetUniformLocation(this->_blurShaderProgram, "scale"); glUniform2f(scaleLoc, scale, 0.0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, this->_color2); loc = glGetUniformLocation(this->_blurShaderProgram, "scene"); glUniform1i(loc, 0); glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLE_STRIP); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glEnd(); /***********************************************************/ //BLURRING vertical /***********************************************************/ this->unbind(); glUniform2f(scaleLoc, 0.0, scale); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, this->_color1); loc = glGetUniformLocation(this->_blurShaderProgram, "scene"); glUniform1i(loc, 0); glBegin(GL_TRIANGLE_STRIP); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glEnd(); glUseProgram(0); glEnable(GL_DEPTH_TEST); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); }
int main (int argc, char ** argv) { if (argc < 2) { std::cerr << "Usage: " << argv[0] << " <shader-file-1> [<shader-file-2> ...]" << std::endl; return 1; } SDL_Init(SDL_INIT_VIDEO); SDL_Window * window = SDL_CreateWindow("Shaders", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 600, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); SDL_GLContext gl_context = SDL_GL_CreateContext(window); GLenum shader_program = 0; std::vector<std::string> saved_source; GLint time_location = -1; GLint mouse_location = -1; auto reload = [&]() { char const * common = "varying vec2 position;\nuniform float time;uniform vec2 mouse;\n"; std::vector<std::string> source; for (int i = 1; i < argc; ++i) { std::ifstream ifs(argv[i]); std::ostringstream oss; oss << ifs.rdbuf(); source.push_back(oss.str()); } if (saved_source == source) return; saved_source = source; if (shader_program) glDeleteProgram(shader_program); shader_program = glCreateProgram(); GLenum vertex_shader = glCreateShader(GL_VERTEX_SHADER); { char const * s = "void main ( ) { gl_Position = gl_Vertex; position = (gl_ModelViewMatrix * gl_Vertex).xy; }\n"; char const * source[] = {common, s}; glShaderSource(vertex_shader, 2, source, nullptr); } glCompileShader(vertex_shader); { std::vector<char> log(4096); GLsizei length; glGetShaderInfoLog(vertex_shader, log.size(), &length, log.data()); std::cout << "Vertex shader compilation log:\n" << log.data() << std::flush; } GLenum fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); { char const * s = "void main ( ) { gl_FragColor = color(); }"; std::vector<char const *> src; src.push_back(common); for (auto const & str : source) src.push_back(str.c_str()); src.push_back(s); glShaderSource(fragment_shader, src.size(), src.data(), nullptr); } glCompileShader(fragment_shader); { std::vector<char> log(4096); GLsizei length; glGetShaderInfoLog(fragment_shader, log.size(), &length, log.data()); std::cout << "Fragment shader compilation log:\n" << log.data() << std::flush; } glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); glLinkProgram(shader_program); glUseProgram(shader_program); time_location = glGetUniformLocation(shader_program, "time"); mouse_location = glGetUniformLocation(shader_program, "mouse"); }; bool quit = false; std::size_t frame_number = 0; int width = 600, height = 600; float screen_ratio = 1.0; float mouse_x, mouse_y; bool lbutton = false; float cx = 0.0, cy = 0.0, scale = 1.0, rscale = 1.0; while (!quit) { SDL_Event event; while (SDL_PollEvent(&event)) switch (event.type) { case SDL_QUIT: quit = true; break; case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_RESIZED: width = event.window.data1; height = event.window.data2; glViewport(0, 0, width, height); screen_ratio = static_cast<float>(width) / height; break; } break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_ESCAPE: quit = true; break; case SDLK_l: reload(); break; case SDLK_r: cx = 0.0; cy = 0.0; scale = 1.0; rscale = 1.0; break; } break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == SDL_BUTTON_LEFT) { lbutton = true; } break; case SDL_MOUSEBUTTONUP: if (event.button.button == SDL_BUTTON_LEFT) { lbutton = false; } break; case SDL_MOUSEMOTION: { float old_x = mouse_x; float old_y = mouse_y; mouse_x = (2.0 * event.motion.x - width) / width * screen_ratio; mouse_y = -(2.0 * event.motion.y - height) / height; if (lbutton) { cx -= (mouse_x - old_x) * scale; cy -= (mouse_y - old_y) * scale; } break; } case SDL_MOUSEWHEEL: { scale *= std::pow(0.8, event.wheel.y); break; } } if (frame_number % 20 == 0) reload(); if (time_location != -1) glUniform1f(time_location, frame_number * 0.01f); if (mouse_location != -1) glUniform2f(mouse_location, mouse_x * rscale + cx, mouse_y * rscale + cy); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glTranslated(cx, cy, 0.0); glScaled(rscale, rscale, 1.0); glScaled(screen_ratio, 1.0, 1.0); glBegin(GL_QUADS); glVertex2d(-1.0, -1.0); glVertex2d( 1.0, -1.0); glVertex2d( 1.0, 1.0); glVertex2d(-1.0, 1.0); glEnd(); SDL_GL_SwapWindow(window); ++frame_number; { float old_rscale = rscale; rscale += (scale - rscale) * 0.25; cx += mouse_x * (old_rscale - rscale); cy += mouse_y * (old_rscale - rscale); } } SDL_GL_DeleteContext(gl_context); SDL_DestroyWindow(window); }
void ofxShader::setUniform2f(const char* name, float v1, float v2) { if(bLoaded) glUniform2f(getUniformLocation(name), v1, v2); }
void setTextureScale(vec2 v){ glUniform2f(loc_texScale,(GLfloat)v.x,(GLfloat)v.y); }
void Shader::SetUniform(const char* var, float v0, float v1) { int id = GetUniformId(var); if(id >= 0) glUniform2f(id, v0, v1); }