BasicShader::BasicShader() : Shader() { AddVertexShader(ResourceLoader::LoadShader("resources/shaders/basicVertex.vs")); AddFragmentShader(ResourceLoader::LoadShader("resources/shaders/basicFragment.fs")); CompileShader(); AddUniform("transform"); AddUniform("color"); }
void ShaderData::AddUniform(const std::string& uniformName, const std::string& uniformType, const std::vector<UniformStruct>& structs) { bool addThis = true; for(unsigned int i = 0; i < structs.size(); i++) { if(structs[i].GetName().compare(uniformType) == 0) { addThis = false; for(unsigned int j = 0; j < structs[i].GetMemberNames().size(); j++) { AddUniform(uniformName + "." + structs[i].GetMemberNames()[j].GetName(), structs[i].GetMemberNames()[j].GetType(), structs); } } } if(!addThis) return; unsigned int location = glGetUniformLocation(m_program, uniformName.c_str()); assert(location != INVALID_VALUE); m_uniformMap.insert(std::pair<std::string, unsigned int>(uniformName, location)); }
void AddUniform(const std::string& uniformName, const std::string& uniformType, unsigned int shaderProgram, const std::vector<UniformStruct>& structs, std::vector<UniformData>& result) { bool addThis = true; for(unsigned int i = 0; i < structs.size(); i++) { if(structs[i].name.compare(uniformType) == 0) { addThis = false; for(unsigned int j = 0; j < structs[i].memberNames.size(); j++) { AddUniform(uniformName + "." + structs[i].memberNames[j].name, structs[i].memberNames[j].type, shaderProgram, structs, result); } } } if(!addThis) return; unsigned int location = glGetUniformLocation(shaderProgram, uniformName.c_str()); if(location == 0xFFFFFFFF) { Engine::GetDisplay()->Error("Could not find uniform: " + uniformName + " " + uniformType); } assert(location != 0xFFFFFFFF); result.push_back(UniformData(location, uniformType, uniformName)); }
void RenderableComponent::Update(uint64_t) { /** Position - Could be moved to Matrix target **/ /** Position **/ { auto & events = mOwner.GetEvents(e::kEventTargetPosition); if(events.size() > 0 ) { mCurrentBounds = HEvent<glm::mat4>::GetData(events[0]); } } /** Matrices **/ { auto & events = mOwner.GetOwner()->GetEvents(e::kEventTargetMatrix); mCurrentMVMat = glm::mat4(0.f); mCurrentPMat = glm::mat4(0.f); for (auto it = events.begin() ; it != events.end() ; ++ it) { if((*it)->GetName() == e::kEventMatrixModelView) { mCurrentMVMat= HEvent<glm::mat4>::GetData((*it)); } else if((*it)->GetName() == e::kEventMatrixProjection) { mCurrentPMat = HEvent<glm::mat4>::GetData((*it)); } } } /** Add uniforms pushed by other components. **/ { auto & events = mOwner.GetEvents(e::kEventTargetUniformData); for ( auto it = events.begin() ; it != events.end() ; ++it) { if((*it)->GetName() == e::kEventUniformData) { auto data = HEvent<UniformData_ptr>::GetData((*it)); AddUniform((*data).name,data); } } } /** Texture Mat **/ { auto & events = mOwner.GetEvents(e::kEventTargetTexture); for ( auto it = events.begin() ; it != events.end () ; ++it ) { mCurrentTMat = HEvent<glm::mat3>::GetData((*it)); } } }
void IEffect::UseCustomTexture(Texture* texture) { Uniform CustomTexture; std::ostringstream toSTR; toSTR << "uTex" << TextureUnits; CustomTexture.Name = toSTR.str(); CustomTexture.Type = DataType::Int; CustomTexture.Usage = PostEffects::Other; CustomTexture.SetValue(&TextureUnits); AddUniform(CustomTexture); // Set RTT Order RTTOrder.push_back(RTT::Info(texture, RTT::CustomTexture, TextureUnits)); TextureUnits++; }
void IEffect::UseLastRTT() { Uniform RTT; std::ostringstream toSTR; toSTR << "uTex" << TextureUnits; RTT.Name = toSTR.str(); RTT.Type = DataType::Int; RTT.Usage = PostEffects::Other; RTT.SetValue(&TextureUnits); AddUniform(RTT); // Set RTT Order RTTOrder.push_back(RTT::Info(RTT::LastRTT, TextureUnits)); TextureUnits++; }
void IEffect::UseDepth() { Uniform Depth; std::ostringstream toSTR; toSTR << "uTex" << TextureUnits; Depth.Name = toSTR.str(); Depth.Type = DataType::Int; Depth.Usage = PostEffects::Other; Depth.SetValue(&TextureUnits); AddUniform(Depth); // Set RTT Order RTTOrder.push_back(RTT::Info(RTT::Depth, TextureUnits)); TextureUnits++; }
void IEffect::UseColor() { Uniform Color; std::ostringstream toSTR; toSTR << "uTex" << TextureUnits; Color.Name = toSTR.str(); Color.Type = DataType::Int; Color.Usage = PostEffects::Other; Color.SetValue(&TextureUnits); AddUniform(Color); // Set RTT Order RTTOrder.push_back(RTT::Info(RTT::Color, TextureUnits)); TextureUnits++; }
void ShaderData::AddUniform(const string& _uniformName, const string& _uniformType, const vector<UniformData>& _structs) { bool addThis = true; for (unsigned int i = 0; i < _structs.size(); i++) { if (_structs[i].name.compare(_uniformType) == 0) { addThis = false; for (unsigned int j = 0; j < _structs[i].memberNames.size(); j++) { AddUniform(_uniformName + "." + _structs[i].memberNames[j].name, _structs[i].memberNames[j].type, _structs); } } } if (!addThis) { return; } unsigned int location = glGetUniformLocation(program, _uniformName.c_str()); uniformMap.insert(pair<string, unsigned int>(_uniformName, location)); }
std::vector<UniformData> Renderer::CreateShaderUniforms(const std::string& shaderText, unsigned int shaderProgram) { static const std::string UNIFORM_KEY = "uniform"; std::vector<UniformStruct> structs = FindUniformStructs(shaderText); std::vector<UniformData> result; size_t uniformLocation = shaderText.find(UNIFORM_KEY); while(uniformLocation != std::string::npos) { bool isCommented = false; size_t lastLineEnd = shaderText.rfind(";", uniformLocation); if(lastLineEnd != std::string::npos) { std::string potentialCommentSection = shaderText.substr(lastLineEnd,uniformLocation - lastLineEnd); isCommented = potentialCommentSection.find("//") != std::string::npos; } if(!isCommented) { size_t begin = uniformLocation + UNIFORM_KEY.length(); size_t end = shaderText.find(";", begin); std::string uniformLine = shaderText.substr(begin + 1, end-begin - 1); begin = uniformLine.find(" "); std::string uniformName = uniformLine.substr(begin + 1); std::string uniformType = uniformLine.substr(0, begin); AddUniform(uniformName, uniformType, shaderProgram, structs, result); } uniformLocation = shaderText.find(UNIFORM_KEY, uniformLocation + UNIFORM_KEY.length()); } return result; }
void sig::Shader::SIG_ExtractUniforms(std::string src) { std::vector<std::string> spl = split(src, ";"); for (auto it = spl.begin(); it != spl.end(); ++it) { std::string ln = (*it); ln.erase(0, ln.find_first_not_of(" \n\r\t")); ln.erase(ln.find_last_not_of(" \n\r\t")+1); std::string uni = ""; if (strncmp(ln.c_str(), "uniform", 7) == 0) { for (int i = ln.size()-1; i >= 0; i--) { if (ln[i] != ';' && ln[i] != ' ') { uni += ln[i]; } else { break; } } std::reverse(uni.begin(), uni.end()); AddUniform(uni); } } }
void ShaderData::AddShaderUniforms(const string& _shaderText) { static const string UNIFORM_KEY = "uniform"; vector<UniformData> structs = FindUniformStructs(_shaderText); size_t uniformLocation = _shaderText.find(UNIFORM_KEY); while (uniformLocation != string::npos) { bool isCommented = false; size_t lastLineEnd = _shaderText.rfind("\n", uniformLocation); if (lastLineEnd != string::npos) { string potentialCommentSection = _shaderText.substr(lastLineEnd, uniformLocation - lastLineEnd); isCommented = potentialCommentSection.find("//") != string::npos; } if (!isCommented) { size_t begin = uniformLocation + UNIFORM_KEY.length(); size_t end = _shaderText.find(";", begin); string uniformLine = _shaderText.substr(begin + 1, end - begin - 1); begin = uniformLine.find(" "); string uniformName = uniformLine.substr(begin + 1); string uniformType = uniformLine.substr(0, begin); size_t subscriptLocation = uniformName.find("["); if (subscriptLocation != string::npos) { uniformName = uniformName.substr(0, subscriptLocation); } uniformNames.push_back(uniformName); uniformTypes.push_back(uniformType); AddUniform(uniformName, uniformType, structs); } uniformLocation = _shaderText.find(UNIFORM_KEY, uniformLocation + UNIFORM_KEY.length()); } }
bool OGLSpotLight::Init(int windowWidth, int windowHeight) { bool res = gs::Stage::Init(windowWidth, windowHeight); if (!res) { return false; } cubePositions.resize(10); res &= InitGUI(); // Init Camera camera.SetPosition(glm::vec3(0, 0, 50)); camera.SetSpeed(15.0f); camera.SetupProjection(45.0f, windowWidth / (float)windowHeight); // Init light InitLight(); // Init materials InitCubePosition(); // Init program res &= AddProgram("mesh.vert", "spotLight.frag"); auto program = programs[0]; // Get uniform locations res &= AddLightUniform(program.get()); res &= AddMatricesUniform(program.get()); res &= program->AddUniform("material.shininess"); res &= program->AddUniform("samplerDiffuse1"); program->AddUniform("samplerDiffuse2"); program->AddUniform("samplerDiffuse3"); program->AddUniform("samplerSpecular1"); program->AddUniform("samplerSpecular2"); glUniform1i(program->GetUniform("samplerDiffuse1"), 0); glUniform1i(program->GetUniform("samplerDiffuse2"), 1); glUniform1i(program->GetUniform("samplerDiffuse3"), 2); glUniform1i(program->GetUniform("samplerSpecular1"), 3); glUniform1i(program->GetUniform("samplerSpecular2"), 4); // Init geometry auto vao = std::make_unique<gs::VertexArray>(); vao->BindVAO(); auto vbo = std::make_unique<gs::VertexBuffer>(GL_ARRAY_BUFFER); vbo->BindVBO(); OGLCube cube; auto& vertices = cube.GetVertices(); cube.InitVertices(glm::vec3(0)); glBufferData(vbo->GetTarget(), sizeof(gs::Vertex) * vertices.size(), vertices.data(), GL_STATIC_DRAW); vbos.push_back(std::move(vbo)); vao->AddAttribute(0, 3, GL_FLOAT, GL_FALSE, sizeof(gs::Vertex), 0); vao->AddAttribute(1, 2, GL_FLOAT, GL_FALSE, sizeof(gs::Vertex), (void*)offsetof(gs::Vertex, texCoords)); vao->AddAttribute(2, 3, GL_FLOAT, GL_FALSE, sizeof(gs::Vertex), (void*)offsetof(gs::Vertex, normal)); vaos.push_back(std::move(vao)); auto diffuse = std::make_unique<gs::Texture>(IMAGE_TYPE::GLI); diffuse->SetContribution(LIGHT_CONTRIBUTION::DIFFUSE); res &= diffuse->LoadTexture("containerDiffuse.dds"); diffuse->BindTexture(GL_TEXTURE0); textures.push_back(std::move(diffuse)); auto specular = std::make_unique<gs::Texture>(IMAGE_TYPE::GLI); specular->SetContribution(LIGHT_CONTRIBUTION::DIFFUSE); res &= specular->LoadTexture("containerSpecular.dds"); specular->BindTexture(GL_TEXTURE3); textures.push_back(std::move(specular)); glEnable(GL_DEPTH_TEST); res = true; return res; }
//------------------------------------------------------------------------------------------------- WorldShader::WorldShader( const char *name ) { AddShader( "glsl\\worldshader.v.glsl", GL_VERTEX_SHADER ); AddShader( "glsl\\worldshader.f.glsl", GL_FRAGMENT_SHADER ); Link(); AddUniform( u_pattern_sampler, "sampler_patterns" ); AddUniform( u_noise_sampler, "sampler_noise" ); AddUniform( u_global_translation, "global_translation" ); AddUniform( u_camera, "camera" ); AddUniform( u_skylight_color, "skylight_color" ); AddUniform( u_skylight_intensity, "skylight_intensity" ); AddUniform( u_fog_distance, "fog_distance" ); AddUniform( u_fog_length, "fog_length" ); AddAttribute( a_instance_translation, "instance_translation", 1 ); AddAttribute( a_instance_form, "instance_form", 1 ); AddAttribute( a_instance_texture, "instance_texture", 1 ); AddAttribute( a_instance_color, "instance_color", 1, 4 ); //AddAttribute( attribInstanceColor2, "instance_color_2", 1 ); //AddAttribute( attribInstanceColor3, "instance_color_3", 1 ); //AddAttribute( attribInstanceColor4, "instance_color_4", 1 ); AddUniform( u_texture_sampler, "sampler_texture" ); AddUniform( u_texture_translation, "texture_translation" ); AddUniform( u_opacity, "opacity" ); AddUniform( u_fog_color, "fog_color" ); AddUniform( u_light_brightness, "light_brightness" ); AddUniform( u_light_colors, "light_colors" ); AddUniform( u_light_positions, "light_positions" ); Register( name ); }
bool OGLFrustumCulling::Init(int windowWidth, int windowHeight) { bool res = gs::Stage::Init(windowWidth, windowHeight); if (res) { res &= InitGUI(); auto program = std::make_shared<gs::Program>(); program->CreateShader(GL_VERTEX_SHADER, "sinDisplacement.vert"); program->CreateShader(GL_GEOMETRY_SHADER, "frustumCulling.geom"); program->CreateShader(GL_FRAGMENT_SHADER, "roundedPoints.frag"); res &= program->Link(); program->Use(); programs.push_back(program); program->AddUniform("MVP"); program->AddUniform("time"); program->AddUniform("frustumPlanes"); program->AddUniform("amplitude"); auto programFrustum = std::make_shared<gs::Program>(); programFrustum->CreateShader(GL_VERTEX_SHADER, "oglLight.vert"); programFrustum->CreateShader(GL_FRAGMENT_SHADER, "firstPoint.frag"); res &= programFrustum->Link(); programFrustum->Use(); programs.push_back(programFrustum); programFrustum->AddUniform("MVP"); auto vao = std::make_unique<gs::VertexArray>(); vao->BindVAO(); auto vbo = std::make_unique<gs::VertexBuffer>(GL_ARRAY_BUFFER); std::vector<glm::vec3> positions; for (size_t z = 0; z <= NUM_VERTICESZ; z++) { for (size_t x = 0; x <= NUM_VERTICESX; x++) { auto xCoord = ((((float)x / ((NUM_VERTICESX - 1)) * 2) - 1)) * HALF_SIZEX; auto zCoord = ((((float)z / ((NUM_VERTICESZ - 1)) * 2) - 1)) * HALF_SIZEZ; positions.push_back(glm::vec3(xCoord, 0.0f, zCoord)); } } vbo->BindVBO(); glBufferData(vbo->GetTarget(), sizeof(glm::vec3) * positions.size(), positions.data(), GL_STATIC_DRAW); vbos.push_back(std::move(vbo)); vao->AddAttribute(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); vaos.push_back(std::move(vao)); totalVertices = (int)positions.size(); ground.SetSize(1000); ground.SetProgram(programs[1]); ground.Load(""); camera.SetSpeed(10); camera.SetPosition(glm::vec3(0,0,20)); camera.SetupProjection(45.0f, windowWidth/(float)windowHeight, 0.01f); cameraExternal.SetSpeed(0); cameraExternal.SetPosition(glm::vec3(0,20,30)); cameraExternal.SetTarget(camera.GetPosition()); cameraExternal.SetupProjection(45.0f, windowWidth/(float)windowHeight); cameraExternal.Update(); glPointSize(30.0f); //glCullFace(GL_FRONT_FACE); } return res; }