From 9da56ddefcd8d0db3a258eb2455cf39230a4aa1e Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Tue, 21 Jan 2014 20:58:24 +0100 Subject: [PATCH] STKMesh, GPUParticles: Some factorization. --- src/graphics/glwrap.cpp | 10 ++++++++++ src/graphics/glwrap.hpp | 2 +- src/graphics/gpuparticles.cpp | 18 ++++++------------ src/graphics/shaders.cpp | 20 ++++++++++++++++++++ src/graphics/shaders.hpp | 2 ++ src/graphics/stkmesh.cpp | 11 ----------- 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/graphics/glwrap.cpp b/src/graphics/glwrap.cpp index ec59db960..758ac4b37 100644 --- a/src/graphics/glwrap.cpp +++ b/src/graphics/glwrap.cpp @@ -296,6 +296,16 @@ void bindUniformToTextureUnit(GLuint location, GLuint texid, unsigned textureUni glUniform1i(location, textureUnit); } +void setTexture(unsigned TextureUnit, GLuint TextureId, GLenum MagFilter, GLenum MinFilter) +{ + glActiveTexture(GL_TEXTURE0 + TextureUnit); + glBindTexture(GL_TEXTURE_2D, TextureId); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, MagFilter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinFilter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +} + static GLuint TexturedQuadShader; static GLuint TexturedQuadAttribPosition; static GLuint TexturedQuadAttribTexCoord; diff --git a/src/graphics/glwrap.hpp b/src/graphics/glwrap.hpp index ee431953c..a03a77c2c 100644 --- a/src/graphics/glwrap.hpp +++ b/src/graphics/glwrap.hpp @@ -22,7 +22,7 @@ void initGL(); GLuint LoadProgram(const char * vertex_file_path, const char * fragment_file_path); GLuint LoadTFBProgram(const char * vertex_file_path, const char **varyings, unsigned varyingscount); void bindUniformToTextureUnit(GLuint location, GLuint texid, unsigned textureUnit); - +void setTexture(unsigned TextureUnit, GLuint TextureId, GLenum MagFilter, GLenum MinFilter); // already includes glext.h, which defines useful GL constants. // COpenGLDriver has already loaded the extension GL functions we use (e.g glBeginQuery) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index c86f9a1c9..74ba7cddb 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -522,13 +522,10 @@ void ParticleSystemProxy::drawFlip() (float)UserConfigParams::m_height }; - bindUniformToTextureUnit(ParticleShader::FlipParticleRender::uniform_tex, texture, 0); - bindUniformToTextureUnit(ParticleShader::FlipParticleRender::uniform_normal_and_depths, normal_and_depth, 1); + setTexture(0, texture, GL_LINEAR, GL_LINEAR); + setTexture(1, normal_and_depth, GL_NEAREST, GL_NEAREST); - glUniformMatrix4fv(ParticleShader::FlipParticleRender::uniform_invproj, 1, GL_FALSE, irr_driver->getInvProjMatrix().pointer()); - glUniform2f(ParticleShader::FlipParticleRender::uniform_screen, screen[0], screen[1]); - glUniformMatrix4fv(ParticleShader::FlipParticleRender::uniform_matrix, 1, GL_FALSE, irr_driver->getProjMatrix().pointer()); - glUniformMatrix4fv(ParticleShader::FlipParticleRender::uniform_viewmatrix, 1, GL_FALSE, irr_driver->getViewMatrix().pointer()); + ParticleShader::FlipParticleRender::setUniforms(irr_driver->getViewMatrix(), irr_driver->getProjMatrix(), irr_driver->getInvProjMatrix(), screen[0], screen[1], 0, 1); glBindVertexArray(current_rendering_flip_vao); glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, count); @@ -557,13 +554,10 @@ void ParticleSystemProxy::drawNotFlip() (float)UserConfigParams::m_height }; - bindUniformToTextureUnit(ParticleShader::SimpleParticleRender::uniform_tex, texture, 0); - bindUniformToTextureUnit(ParticleShader::SimpleParticleRender::uniform_normal_and_depths, normal_and_depth, 1); + setTexture(0, texture, GL_LINEAR, GL_LINEAR); + setTexture(1, normal_and_depth, GL_NEAREST, GL_NEAREST); - glUniformMatrix4fv(ParticleShader::SimpleParticleRender::uniform_invproj, 1, GL_FALSE, irr_driver->getInvProjMatrix().pointer()); - glUniform2f(ParticleShader::SimpleParticleRender::uniform_screen, screen[0], screen[1]); - glUniformMatrix4fv(ParticleShader::SimpleParticleRender::uniform_matrix, 1, GL_FALSE, irr_driver->getProjMatrix().pointer()); - glUniformMatrix4fv(ParticleShader::SimpleParticleRender::uniform_viewmatrix, 1, GL_FALSE, irr_driver->getViewMatrix().pointer()); + ParticleShader::SimpleParticleRender::setUniforms(irr_driver->getViewMatrix(), irr_driver->getProjMatrix(), irr_driver->getInvProjMatrix(), screen[0], screen[1], 0, 1); glBindVertexArray(current_rendering_vao); glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, count); diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 5ed054dcf..02693608b 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -803,6 +803,16 @@ namespace ParticleShader uniform_normal_and_depths = glGetUniformLocation(Program, "normals_and_depth"); } + void SimpleParticleRender::setUniforms(const core::matrix4 &ViewMatrix, const core::matrix4 &ProjMatrix, const core::matrix4 InvProjMatrix, float width, float height, unsigned TU_tex, unsigned TU_normal_and_depth) + { + glUniformMatrix4fv(uniform_invproj, 1, GL_FALSE, InvProjMatrix.pointer()); + glUniform2f(uniform_screen, width, height); + glUniformMatrix4fv(uniform_matrix, 1, GL_FALSE, irr_driver->getProjMatrix().pointer()); + glUniformMatrix4fv(uniform_viewmatrix, 1, GL_FALSE, irr_driver->getViewMatrix().pointer()); + glUniform1i(uniform_tex, TU_tex); + glUniform1i(uniform_normal_and_depths, TU_normal_and_depth); + } + GLuint FlipParticleRender::Program; GLuint FlipParticleRender::attrib_pos; GLuint FlipParticleRender::attrib_lf; @@ -836,6 +846,16 @@ namespace ParticleShader uniform_screen = glGetUniformLocation(Program, "screen"); uniform_normal_and_depths = glGetUniformLocation(Program, "normals_and_depth"); } + + void FlipParticleRender::setUniforms(const core::matrix4 &ViewMatrix, const core::matrix4 &ProjMatrix, const core::matrix4 InvProjMatrix, float width, float height, unsigned TU_tex, unsigned TU_normal_and_depth) + { + glUniformMatrix4fv(uniform_invproj, 1, GL_FALSE, InvProjMatrix.pointer()); + glUniform2f(uniform_screen, width, height); + glUniformMatrix4fv(uniform_matrix, 1, GL_FALSE, irr_driver->getProjMatrix().pointer()); + glUniformMatrix4fv(uniform_viewmatrix, 1, GL_FALSE, irr_driver->getViewMatrix().pointer()); + glUniform1i(uniform_tex, TU_tex); + glUniform1i(uniform_normal_and_depths, TU_normal_and_depth); + } } static GLuint createVAO(GLuint Program) diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index ab790fd4f..a326a7565 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -206,6 +206,7 @@ public: static GLuint uniform_matrix, uniform_viewmatrix, uniform_tex, uniform_normal_and_depths, uniform_screen, uniform_invproj; static void init(); + static void setUniforms(const core::matrix4 &ViewMatrix, const core::matrix4 &ProjMatrix, const core::matrix4 InvProjMatrix, float width, float height, unsigned TU_tex, unsigned TU_normal_and_depth); }; class FlipParticleRender @@ -216,6 +217,7 @@ public: static GLuint uniform_matrix, uniform_viewmatrix, uniform_tex, uniform_normal_and_depths, uniform_screen, uniform_invproj; static void init(); + static void setUniforms(const core::matrix4 &ViewMatrix, const core::matrix4 &ProjMatrix, const core::matrix4 InvProjMatrix, float width, float height, unsigned TU_tex, unsigned TU_normal_and_depth); }; } diff --git a/src/graphics/stkmesh.cpp b/src/graphics/stkmesh.cpp index fafebf94a..6f6a91a5f 100644 --- a/src/graphics/stkmesh.cpp +++ b/src/graphics/stkmesh.cpp @@ -204,17 +204,6 @@ void STKMesh::drawObjectPass1(const GLMesh &mesh) glDrawElements(ptype, count, itype, 0); } -static void -setTexture(unsigned TextureUnit, GLuint TextureId, GLenum MagFilter, GLenum MinFilter) -{ - glActiveTexture(GL_TEXTURE0 + TextureUnit); - glBindTexture(GL_TEXTURE_2D, TextureId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, MagFilter); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinFilter); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); -} - void STKMesh::drawObjectRefPass1(const GLMesh &mesh) { GLenum ptype = mesh.PrimitiveType;