diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index f74bb6a49..1adebc5cf 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -612,10 +612,10 @@ void IrrDriver::renderSolidFirstPass() } } -template -void renderMeshes2ndPass(const std::vector &TexUnits, std::vector > &meshes) +template +void renderMeshes2ndPass(const T *Shader, const std::vector &TexUnits, std::vector > &meshes) { - glUseProgram(Shader::Program); + glUseProgram(Shader->Program); glBindVertexArray(getVAO(VertexType)); for (unsigned i = 0; i < meshes.size(); i++) { @@ -645,7 +645,7 @@ void renderMeshes2ndPass(const std::vector &TexUnits, std::vector(meshes[i]); + apply_instance(Shader, meshes[i]); } } @@ -687,13 +687,13 @@ void IrrDriver::renderSolidSecondPass() m_scene_manager->drawAll(scene::ESNRP_SOLID); - renderMeshes2ndPass({ MeshShader::ObjectPass2Shader::TU_Albedo }, ListDefaultStandardSM::Arguments); - renderMeshes2ndPass({ MeshShader::ObjectPass2Shader::TU_Albedo }, ListDefaultTangentSM::Arguments); - renderMeshes2ndPass({ MeshShader::ObjectRefPass2Shader::TU_Albedo }, ListAlphaRefSM::Arguments); - renderMeshes2ndPass({ MeshShader::SphereMapShader::TU_tex }, ListSphereMapSM::Arguments); - renderMeshes2ndPass({ MeshShader::ObjectUnlitShader::TU_tex }, ListUnlitSM::Arguments); - renderMeshes2ndPass({ MeshShader::DetailledObjectPass2Shader::TU_Albedo, MeshShader::DetailledObjectPass2Shader::TU_detail }, ListDetailSM::Arguments); - renderMeshes2ndPass({ 8, MeshShader::SplattingShader::TU_tex_layout, MeshShader::SplattingShader::TU_tex_detail0, MeshShader::SplattingShader::TU_tex_detail1, MeshShader::SplattingShader::TU_tex_detail2, MeshShader::SplattingShader::TU_tex_detail3 }, ListSplattingSM::Arguments); + renderMeshes2ndPass(MeshShader::ObjectPass2ShaderInstance, { MeshShader::ObjectPass2ShaderInstance->TU_Albedo }, ListDefaultStandardSM::Arguments); + renderMeshes2ndPass(MeshShader::ObjectPass2ShaderInstance, { MeshShader::ObjectPass2ShaderInstance->TU_Albedo }, ListDefaultTangentSM::Arguments); + renderMeshes2ndPass(MeshShader::ObjectRefPass2ShaderInstance, { MeshShader::ObjectRefPass2ShaderInstance->TU_Albedo }, ListAlphaRefSM::Arguments); +// renderMeshes2ndPass({ MeshShader::SphereMapShader::TU_tex }, ListSphereMapSM::Arguments); +// renderMeshes2ndPass({ MeshShader::ObjectUnlitShader::TU_tex }, ListUnlitSM::Arguments); +// renderMeshes2ndPass({ MeshShader::DetailledObjectPass2Shader::TU_Albedo, MeshShader::DetailledObjectPass2Shader::TU_detail }, ListDetailSM::Arguments); +// renderMeshes2ndPass({ 8, MeshShader::SplattingShader::TU_tex_layout, MeshShader::SplattingShader::TU_tex_detail0, MeshShader::SplattingShader::TU_tex_detail1, MeshShader::SplattingShader::TU_tex_detail2, MeshShader::SplattingShader::TU_tex_detail3 }, ListSplattingSM::Arguments); } } @@ -717,16 +717,16 @@ void IrrDriver::renderTransparent() if (World::getWorld() && World::getWorld()->isFogEnabled()) { glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - renderMeshes2ndPass({ MeshShader::TransparentFogShader::TU_tex }, ListBlendTransparentFog::Arguments); +// renderMeshes2ndPass({ MeshShader::TransparentFogShader::TU_tex }, ListBlendTransparentFog::Arguments); glBlendFunc(GL_ONE, GL_ONE); - renderMeshes2ndPass({ MeshShader::TransparentFogShader::TU_tex }, ListAdditiveTransparentFog::Arguments); +// renderMeshes2ndPass({ MeshShader::TransparentFogShader::TU_tex }, ListAdditiveTransparentFog::Arguments); } else { glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - renderMeshes2ndPass({ MeshShader::TransparentShader::TU_tex }, ListBlendTransparent::Arguments); +// renderMeshes2ndPass({ MeshShader::TransparentShader::TU_tex }, ListBlendTransparent::Arguments); glBlendFunc(GL_ONE, GL_ONE); - renderMeshes2ndPass({ MeshShader::TransparentShader::TU_tex }, ListAdditiveTransparent::Arguments); +// renderMeshes2ndPass({ MeshShader::TransparentShader::TU_tex }, ListAdditiveTransparent::Arguments); } } diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 57e184b8d..b031d42f0 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -331,12 +331,12 @@ void Shaders::loadShaders() MeshShader::InstancedObjectPass1Shader::init(); MeshShader::InstancedObjectRefPass1Shader::init(); MeshShader::InstancedGrassPass1Shader::init(); - MeshShader::ObjectPass2Shader::init(); + MeshShader::ObjectPass2ShaderInstance = new MeshShader::ObjectPass2Shader(); MeshShader::InstancedObjectPass2Shader::init(); MeshShader::InstancedObjectRefPass2Shader::init(); MeshShader::InstancedGrassPass2Shader::init(); MeshShader::DetailledObjectPass2Shader::init(); - MeshShader::ObjectRefPass2Shader::init(); + MeshShader::ObjectRefPass2ShaderInstance = new MeshShader::ObjectRefPass2Shader(); MeshShader::ObjectUnlitShader::init(); MeshShader::SphereMapShader::init(); MeshShader::SplattingShader::init(); @@ -630,22 +630,13 @@ namespace MeshShader } // Solid Lit pass shaders - - GLuint ObjectPass2Shader::Program; - GLuint ObjectPass2Shader::uniform_MM; - GLuint ObjectPass2Shader::uniform_TM; - GLuint ObjectPass2Shader::uniform_ambient; - GLuint ObjectPass2Shader::TU_Albedo; - - void ObjectPass2Shader::init() + ObjectPass2Shader::ObjectPass2Shader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass2.frag").c_str()); - uniform_MM = glGetUniformLocation(Program, "ModelMatrix"); - uniform_TM = glGetUniformLocation(Program, "TextureMatrix"); - uniform_ambient = glGetUniformLocation(Program, "ambient"); + AssignUniforms(Program, uniforms, { "ModelMatrix", "TextureMatrix", "ambient" }); if (!UserConfigParams::m_ubo_disabled) { GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); @@ -656,15 +647,7 @@ namespace MeshShader AssignTextureUnit(Program, { { 0, "DiffuseMap" }, { 1, "SpecularMap" }, { 2, "SSAO" }, { TU_Albedo, "Albedo" } }); } - void ObjectPass2Shader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix) - { - if (UserConfigParams::m_ubo_disabled) - bypassUBO(Program); - glUniformMatrix4fv(uniform_MM, 1, GL_FALSE, ModelMatrix.pointer()); - glUniformMatrix4fv(uniform_TM, 1, GL_FALSE, TextureMatrix.pointer()); - const video::SColorf s = irr_driver->getSceneManager()->getAmbientLight(); - glUniform3f(uniform_ambient, s.r, s.g, s.b); - } + ObjectPass2Shader *ObjectPass2ShaderInstance; GLuint InstancedObjectPass2Shader::Program; GLuint InstancedObjectPass2Shader::uniform_VP; @@ -798,22 +781,13 @@ namespace MeshShader glUniformMatrix4fv(uniform_MM, 1, GL_FALSE, ModelMatrix.pointer()); } - GLuint ObjectRefPass2Shader::Program; - GLuint ObjectRefPass2Shader::uniform_MM; - GLuint ObjectRefPass2Shader::uniform_TM; - GLuint ObjectRefPass2Shader::uniform_ambient; - GLuint ObjectRefPass2Shader::TU_Albedo; - - void ObjectRefPass2Shader::init() + ObjectRefPass2Shader::ObjectRefPass2Shader() { - initGL(); Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass2.frag").c_str()); - uniform_MM = glGetUniformLocation(Program, "ModelMatrix"); - uniform_TM = glGetUniformLocation(Program, "TextureMatrix"); - uniform_ambient = glGetUniformLocation(Program, "ambient"); + AssignUniforms(Program, uniforms, { "ModelMatrix", "TextureMatrix", "ambient" }); if (!UserConfigParams::m_ubo_disabled) { GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); @@ -824,16 +798,7 @@ namespace MeshShader AssignTextureUnit(Program, { { 0, "DiffuseMap" }, { 1, "SpecularMap" }, { 2, "SSAO" }, { TU_Albedo, "Albedo" } }); } - void ObjectRefPass2Shader::setUniforms(const core::matrix4 &ModelMatrix, - const core::matrix4 &TextureMatrix) - { - if (UserConfigParams::m_ubo_disabled) - bypassUBO(Program); - glUniformMatrix4fv(uniform_MM, 1, GL_FALSE, ModelMatrix.pointer()); - glUniformMatrix4fv(uniform_TM, 1, GL_FALSE, TextureMatrix.pointer()); - const video::SColorf s = irr_driver->getSceneManager()->getAmbientLight(); - glUniform3f(uniform_ambient, s.r, s.g, s.b); - } + ObjectRefPass2Shader *ObjectRefPass2ShaderInstance; GLuint GrassPass2Shader::Program; GLuint GrassPass2Shader::uniform_MVP; diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index 4fa278c35..d81a49d33 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -65,6 +65,15 @@ void setUniformsHelper(const std::vector &uniforms, const core::matrix4 setUniformsHelper(uniforms, arg...); } +void glUniform3fWraper(GLuint, size_t, unsigned, const float *mat); + +template +void setUniformsHelper(const std::vector &uniforms, const video::SColorf &col, Args... arg) +{ + glUniform3f(uniforms[N], col.r, col.g, col.b); + setUniformsHelper(uniforms, arg...); +} + void bypassUBO(GLuint Program); template @@ -153,17 +162,16 @@ public: static void setUniforms(const core::vector3df &windDir, unsigned TU_tex); }; -class ObjectPass2Shader +class ObjectPass2Shader : public ShaderHelper { public: - static GLuint Program; - static GLuint uniform_MM, uniform_TM, uniform_ambient; - static GLuint TU_Albedo; + GLuint TU_Albedo; - static void init(); - static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix); + ObjectPass2Shader(); }; +extern ObjectPass2Shader *ObjectPass2ShaderInstance; + class InstancedObjectPass2Shader { public: @@ -208,17 +216,16 @@ public: static void setUniforms(const core::matrix4 &ModelMatrix); }; -class ObjectRefPass2Shader +class ObjectRefPass2Shader : public ShaderHelper { public: - static GLuint Program; - static GLuint uniform_MM, uniform_TM, uniform_ambient; - static GLuint TU_Albedo; + GLuint TU_Albedo; - static void init(); - static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix); + ObjectRefPass2Shader(); }; +extern ObjectRefPass2Shader *ObjectRefPass2ShaderInstance; + class GrassPass2Shader { public: diff --git a/src/graphics/stkanimatedmesh.cpp b/src/graphics/stkanimatedmesh.cpp index e957df395..6439b507f 100644 --- a/src/graphics/stkanimatedmesh.cpp +++ b/src/graphics/stkanimatedmesh.cpp @@ -164,13 +164,13 @@ void STKAnimatedMesh::render() GLMesh* mesh; for_in(mesh, ShadedMesh[SM_DEFAULT_STANDARD]) - ListDefaultStandardSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix)); + ListDefaultStandardSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight())); for_in(mesh, ShadedMesh[SM_DEFAULT_TANGENT]) - ListDefaultTangentSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix)); + ListDefaultTangentSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight())); for_in(mesh, ShadedMesh[SM_ALPHA_REF_TEXTURE]) - ListAlphaRefSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix)); + ListAlphaRefSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight())); for_in (mesh, ShadedMesh[SM_UNLIT]) ListUnlitSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation)); diff --git a/src/graphics/stkmesh.cpp b/src/graphics/stkmesh.cpp index 5c9eb1e79..d4c15137b 100644 --- a/src/graphics/stkmesh.cpp +++ b/src/graphics/stkmesh.cpp @@ -369,9 +369,9 @@ std::vector > ListDefault2TCo std::vector > ListAlphaRefG::Arguments; std::vector > ListNormalG::Arguments; -std::vector > ListDefaultStandardSM::Arguments; -std::vector > ListDefaultTangentSM::Arguments; -std::vector > ListAlphaRefSM::Arguments; +std::vector > ListDefaultStandardSM::Arguments; +std::vector > ListDefaultTangentSM::Arguments; +std::vector > ListAlphaRefSM::Arguments; std::vector > ListSplattingSM::Arguments; std::vector > ListSphereMapSM::Arguments; std::vector > ListUnlitSM::Arguments; diff --git a/src/graphics/stkmesh.hpp b/src/graphics/stkmesh.hpp index 5e28e521c..dd5824fb6 100644 --- a/src/graphics/stkmesh.hpp +++ b/src/graphics/stkmesh.hpp @@ -149,19 +149,19 @@ void drawGrassPass1(const GLMesh &mesh, const core::matrix4 & ModelViewProjectio class ListDefaultStandardSM { public: - static std::vector > Arguments; + static std::vector > Arguments; }; class ListDefaultTangentSM { public: - static std::vector > Arguments; + static std::vector > Arguments; }; class ListAlphaRefSM { public: - static std::vector > Arguments; + static std::vector > Arguments; }; class ListSphereMapSM diff --git a/src/graphics/stkmeshscenenode.cpp b/src/graphics/stkmeshscenenode.cpp index dcdf36f4b..94c59bede 100644 --- a/src/graphics/stkmeshscenenode.cpp +++ b/src/graphics/stkmeshscenenode.cpp @@ -314,7 +314,7 @@ void STKMeshSceneNode::render() glDisable(GL_CULL_FACE); if (!spareWhiteTex) spareWhiteTex = getUnicolorTexture(video::SColor(255, 255, 255, 255)); - glUseProgram(MeshShader::ObjectPass2Shader::Program); + glUseProgram(MeshShader::ObjectPass2ShaderInstance->Program); // Only untextured for (unsigned i = 0; i < GLmeshes.size(); i++) { @@ -324,8 +324,8 @@ void STKMeshSceneNode::render() GLenum itype = mesh.IndexType; size_t count = mesh.IndexCount; - setTexture(MeshShader::ObjectPass2Shader::TU_Albedo, getTextureGLuint(spareWhiteTex), GL_NEAREST, GL_NEAREST, false); - MeshShader::ObjectPass2Shader::setUniforms(AbsoluteTransformation, mesh.TextureMatrix); + setTexture(MeshShader::ObjectPass2ShaderInstance->TU_Albedo, getTextureGLuint(spareWhiteTex), GL_NEAREST, GL_NEAREST, false); + MeshShader::ObjectPass2ShaderInstance->setUniforms(AbsoluteTransformation, mesh.TextureMatrix, irr_driver->getSceneManager()->getAmbientLight()); assert(mesh.vao); glBindVertexArray(mesh.vao); glDrawElements(ptype, count, itype, 0); @@ -337,13 +337,13 @@ void STKMeshSceneNode::render() GLMesh* mesh; for_in(mesh, ShadedMesh[SM_DEFAULT_STANDARD]) - ListDefaultStandardSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix)); + ListDefaultStandardSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight())); for_in(mesh, ShadedMesh[SM_DEFAULT_TANGENT]) - ListDefaultTangentSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix)); + ListDefaultTangentSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight())); for_in(mesh, ShadedMesh[SM_ALPHA_REF_TEXTURE]) - ListAlphaRefSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix)); + ListAlphaRefSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight())); for_in(mesh, ShadedMesh[SM_SPHEREMAP]) ListSphereMapSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel, irr_driver->getSceneManager()->getAmbientLight()));