void FillUniform(const SpotLight & light, ShaderProgramPtr program, const std::string & varName) { program->SetUniform(varName + ".Position_Intensity", light.Position_Intensity); program->SetUniform(varName + ".Color_AmbientIntensity", light.Color_AmbientIntensity); program->SetUniform(varName + ".Attenuation_InnerAngle", light.Attenuation_InnerAngle); program->SetUniform(varName + ".Direction_OuterAngle", light.Direction_OuterAngle); }
void ProgramManager::onReload( const ResourceEvent& event ) { Resource* resource = event.handle.Resolve(); if( resource->getResourceGroup() != ResourceGroup::Shaders ) return; ShaderMaterial* oldShader = (ShaderMaterial*) event.oldResource; #pragma TODO("Handle reloading of unregistered resources") ShaderProgramsMap::iterator it = programs.find(oldShader); assert( it != programs.end() ); ShaderProgramPtr program = it->second; // We need to hold the ref. programs.erase(it); ShaderMaterial* shader = (ShaderMaterial*) event.resource; programs[shader] = program; LogDebug( "Reloading shader '%s'", shader->getPath().c_str() ); program->getVertexShader()->setText( shader->getVertexSource() ); program->getFragmentShader()->setText( shader->getFragmentSource() ); // Force the recompilation of all shader programs. program->forceRecompile(); }
bool Material::sendToGPU(const ShaderProgramPtr& program, const MaterialPtr &material) { bool success = true; int location = -1; if(program==nullptr || material==nullptr) { return false; } location = program->getUniformLocation("material.ambient"); if(location!=ShaderProgram::null_location) { glcheck(glUniform3fv(location, 1, glm::value_ptr(material->ambient()))); } else { success = false; } location = program->getUniformLocation("material.diffuse"); if(location!=ShaderProgram::null_location) { glcheck(glUniform3fv(location, 1, glm::value_ptr(material->diffuse()))); } else { success = false; } location = program->getUniformLocation("material.specular"); if(location!=ShaderProgram::null_location) { glcheck(glUniform3fv(location, 1, glm::value_ptr(material->specular()))); } else { success = false; } location = program->getUniformLocation("material.shininess"); if(location!=ShaderProgram::null_location) { glcheck(glUniform1f(location, material->shininess())); } else { success = false; } return success; }
std::shared_ptr<ShaderProgram> ContentManager::LoadNew(const std::string &name, bool async) { FilePtr fp = FileManager::Instance()->OpenFile(name); if (!fp) { LOG("Failed loading '%s': file not found", name.c_str()); return nullptr; } int size = fp->GetSize(); char *data = new char[size + 1]; fp->Read(data, size); fp->Close(); data[size] = 0; ShaderProgramPtr program = std::make_shared<ShaderProgram>(); ShaderPtr vs = std::make_shared<Shader>(GL_VERTEX_SHADER); if (!vs->Load(data)) { LOG("Error loading '%s' vertex shader: %s", name.c_str(), vs->ErrorMessage().c_str()); return nullptr; } program->AddShader(vs); ShaderPtr ps = std::make_shared<Shader>(GL_FRAGMENT_SHADER); if (!ps->Load(data)) { LOG("Error loading '%s' fragment shader: %s", name.c_str(), vs->ErrorMessage().c_str()); return nullptr; } program->AddShader(ps); if (!program->Link()) { LOG("Error loading '%s': link failed", name.c_str()); return nullptr; } ContentManager::Instance()->CacheObject(name, program); LOG("Loaded shader '%s'", name.c_str()); return program; }
void render(unsigned int mask) { #if 0 Display::push_framebuffer(framebuffer); glClear(GL_COLOR_BUFFER_BIT); draw_particles(); Display::pop_framebuffer(); if (1) { OpenGLState state; glUseProgram(shader_program.get_handle()); shader_program.set_uniform1i("screen", 0); shader_program.set_uniform1i("particles", 1); state.bind_texture(tmp_texture, 0); state.bind_texture(framebuffer.get_texture(), 1); state.color(Color(1.0f, 1.0f, 1.0f, 1.0f)); state.activate(); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(0,0); glTexCoord2f(800,0); glVertex2f(800,0); glTexCoord2f(800,600); glVertex2f(800, 600); glTexCoord2f(0, 600); glVertex2f(0,600); glEnd(); glUseProgram(0); } #endif }
void RenderContext::setUniforms(ShaderProgramPtr shader, ShaderUniforms uniforms) { if(shader == nullptr) return; for(FloatUniformsMap::value_type uniform : uniforms.FloatUniforms) { shader->setUniformValue(uniform.first, uniform.second); } for(Vec2UniformsMap::value_type uniform : uniforms.Vec2Uniforms) { shader->setUniformValue(uniform.first, uniform.second); } for(Vec3UniformsMap::value_type uniform : uniforms.Vec3Uniforms) { shader->setUniformValue(uniform.first, uniform.second); } for(Vec4UniformsMap::value_type uniform : uniforms.Vec4Uniforms) { shader->setUniformValue(uniform.first, uniform.second); } for(Matrix4UniformsMap::value_type uniform : uniforms.Matrix4Uniforms) { shader->setUniformValue(uniform.first, uniform.second); } for(Sampler2DUniformsMap::value_type uniform : uniforms.Sampler2DUniforms) { shader->setUniformValue(uniform.first, uniform.second); } for(FloatArrayUniformsMap::value_type uniform : uniforms.FloatArrayUniforms) { shader->setUniformValueArray(uniform.first, uniform.second); } for(Matrix4ArrayUniformsMap::value_type uniform : uniforms.Matrix4ArrayUniforms) { shader->setUniformValueArray(uniform.first, uniform.second); } for(Sampler2DArrayUniformsMap::value_type uniform : uniforms.Sampler2DArrayUniforms) { shader->setUniformValueArray(uniform.first, uniform.second); } }
MaterialPassPtr PassParser::CreateObject(Sys_GraphicsPtr pGraphics) { ShaderProgramPtr pProgram = pGraphics->CreateShaderProgram(); if(_VertexShader != "" && pProgram->AttachShaderFromFile(ST_VERTEX_SHADER, _VertexShader.string().c_str(), _VertexShaderEntry) == false) { Error(0, "failed to create vertex shader: '" + _VertexShader.string() + "'"); pProgram->Release(); return nullptr; } if(_PixelShader != "" && pProgram->AttachShaderFromFile(ST_PIXEL_SHADER, _PixelShader.string().c_str(), _PixelShaderEntry) == false) { Error(0, "Failed to create pixel shader: '" + _PixelShader.string() + "'"); pProgram->Release(); return nullptr; } if(_GeometryShader != "" && pProgram->AttachShaderFromFile(ST_GEOMETRY_SHADER, _GeometryShader.string().c_str(), _GeometryShaderEntry) == false) { Error(0, "Failed to create geometry shader: '" + _GeometryShader.string() + "'"); pProgram->Release(); return nullptr; } if(false == pProgram->Link()) { pProgram->Release(); return nullptr; } MaterialPassPtr pPass = std::make_shared<MaterialPass>(m_name, pGraphics); pPass->AttachProgram(pProgram); for(auto v : _SamplerLink) { SamplerStateParserPtr pParser = std::dynamic_pointer_cast<SamplerStateParser>(FindSymbol(v.second, false)); if(pParser == nullptr) { assert(0); continue; } if(pProgram->FindParameterByName(v.first.c_str()) == -1) { Error(0, "sampler name not found: '" + v.first + "'"); continue; } pPass->AddSamplerLink(v.first, pParser->CreateObject(pGraphics)); } RenderStateParserPtr pRSParser = std::dynamic_pointer_cast<RenderStateParser>(FindSymbol(_RenderState, false)); if(pRSParser) { pPass->AttachRenderState(pRSParser->CreateObject(pGraphics)); } else { pPass->AttachRenderState(pGraphics->CreateRenderState()); } return pPass; }
void FillUniform(const DirectionalLight & light, ShaderProgramPtr program, const std::string & varName) { program->SetUniform(varName + ".Direction_Intensity", light.Direction_Intensity); program->SetUniform(varName + ".Color_AmbientIntensity", light.Color_AmbientIntensity); }
void FillUniform(const PointLight & light, ShaderProgramPtr program, const std::string & varName) { program->SetUniform(varName + ".Position_Intensity", light.Position_Intensity); program->SetUniform(varName + ".Color_AmbientIntensity", light.Color_AmbientIntensity); program->SetUniform(varName + ".Attenuation_Pad", light.Attenuation_Pad); }