From 4e73ace0dfb415e9172b7568060b2f436bf23cfe Mon Sep 17 00:00:00 2001 From: konstin Date: Fri, 25 Jul 2014 14:06:04 +0200 Subject: [PATCH 01/26] allow the track info dialog to override the default lap number for a track --- src/states_screens/dialogs/track_info_dialog.cpp | 1 + src/tracks/track.hpp | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/states_screens/dialogs/track_info_dialog.cpp b/src/states_screens/dialogs/track_info_dialog.cpp index 5231808e0..be74bb925 100644 --- a/src/states_screens/dialogs/track_info_dialog.cpp +++ b/src/states_screens/dialogs/track_info_dialog.cpp @@ -236,6 +236,7 @@ void TrackInfoDialog::onEnterPressedInternal() const int num_laps = (m_spinner == NULL ? -1 : m_spinner->getValue()); const bool reverse_track = m_checkbox == NULL ? false : m_checkbox->getState(); + track_manager->getTrack(m_track_ident)->setDefaultNumberOfLaps(num_laps); race_manager->setReverseTrack(reverse_track); std::string track_ident = m_track_ident; // Disable accidentally unlocking of a challenge diff --git a/src/tracks/track.hpp b/src/tracks/track.hpp index 92273860a..62f6b9b43 100644 --- a/src/tracks/track.hpp +++ b/src/tracks/track.hpp @@ -616,9 +616,10 @@ public: void addNode(scene::ISceneNode* node) { m_all_nodes.push_back(node); } - float getDisplacementSpeed() const { return m_displacement_speed; } - float getCausticsSpeed() const { return m_caustics_speed; } - const int getDefaultNumberOfLaps() const { return m_default_number_of_laps;} + float getDisplacementSpeed() const { return m_displacement_speed; } + float getCausticsSpeed() const { return m_caustics_speed; } + int getDefaultNumberOfLaps() const { return m_default_number_of_laps;} + void setDefaultNumberOfLaps(unsigned int laps) { m_default_number_of_laps = laps;} bool operator<(const Track &other) const; }; // class Track From 7a2e4e6336fb12ad9332735c6b43711eef512920 Mon Sep 17 00:00:00 2001 From: konstin Date: Sat, 26 Jul 2014 12:00:24 +0200 Subject: [PATCH 02/26] corrections --- src/states_screens/dialogs/track_info_dialog.cpp | 4 ++-- src/tracks/track.cpp | 6 +++++- src/tracks/track.hpp | 15 ++++++++++----- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/states_screens/dialogs/track_info_dialog.cpp b/src/states_screens/dialogs/track_info_dialog.cpp index be74bb925..e7d7025f2 100644 --- a/src/states_screens/dialogs/track_info_dialog.cpp +++ b/src/states_screens/dialogs/track_info_dialog.cpp @@ -99,7 +99,7 @@ TrackInfoDialog::TrackInfoDialog(const std::string& ribbonItem, const std::strin if (UserConfigParams::m_artist_debug_mode) m_spinner->setMin(0); - m_spinner->setValue(track->getDefaultNumberOfLaps()); + m_spinner->setValue(track->getActualNumberOfLap()); race_manager->setNumLaps(m_spinner->getValue()); } else @@ -236,7 +236,7 @@ void TrackInfoDialog::onEnterPressedInternal() const int num_laps = (m_spinner == NULL ? -1 : m_spinner->getValue()); const bool reverse_track = m_checkbox == NULL ? false : m_checkbox->getState(); - track_manager->getTrack(m_track_ident)->setDefaultNumberOfLaps(num_laps); + track_manager->getTrack(m_track_ident)->setActualNumberOfLaps(num_laps); race_manager->setReverseTrack(reverse_track); std::string track_ident = m_track_ident; // Disable accidentally unlocking of a challenge diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index 90e7f74f1..9943638c7 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -485,10 +485,14 @@ void Track::loadTrackInfo() root->get("color-level-in", &m_color_inlevel); root->get("color-level-out", &m_color_outlevel); + if (m_default_number_of_laps <= 0) + m_default_number_of_laps = 3; + m_actual_number_of_laps = m_default_number_of_laps; + // Make the default for auto-rescue in battle mode and soccer mode to be false if(m_is_arena || m_is_soccer) m_enable_auto_rescue = false; - root->get("auto-rescue", & m_enable_auto_rescue); + root->get("auto-rescue", &m_enable_auto_rescue); root->get("smooth-normals", &m_smooth_normals); // Reverse is meaningless in arena if(m_is_arena || m_is_soccer) diff --git a/src/tracks/track.hpp b/src/tracks/track.hpp index 26f3a2288..d2cfca660 100644 --- a/src/tracks/track.hpp +++ b/src/tracks/track.hpp @@ -398,8 +398,11 @@ private: /** List of all bezier curves in the track - for e.g. camera, ... */ std::vector m_all_curves; - /** The number of laps the track will be raced if no other value is given.*/ + /** The number of laps the track will be raced in a random GP. + * m_actual_number_of_laps is initialised with this value.*/ int m_default_number_of_laps; + /** The number of laps that is predefined in a track info dialog. */ + int m_actual_number_of_laps; void loadTrackInfo(); void loadQuadGraph(unsigned int mode_id, const bool reverse); @@ -613,10 +616,12 @@ public: void addNode(scene::ISceneNode* node) { m_all_nodes.push_back(node); } - float getDisplacementSpeed() const { return m_displacement_speed; } - float getCausticsSpeed() const { return m_caustics_speed; } - int getDefaultNumberOfLaps() const { return m_default_number_of_laps;} - void setDefaultNumberOfLaps(unsigned int laps) { m_default_number_of_laps = laps;} + float getDisplacementSpeed() const { return m_displacement_speed; } + float getCausticsSpeed() const { return m_caustics_speed; } + const int getDefaultNumberOfLaps() const { return m_default_number_of_laps;} + const int getActualNumberOfLap() const { return m_actual_number_of_laps; } + void setActualNumberOfLaps(unsigned int laps) + { m_actual_number_of_laps = laps; } bool operator<(const Track &other) const; }; // class Track From de16d1ace3481cd5df3da55cb6e0f91926a1e6e2 Mon Sep 17 00:00:00 2001 From: hiker Date: Wed, 13 Aug 2014 21:46:53 +1000 Subject: [PATCH 03/26] Bugfix: A new local only user was now able to change the online account. If a new account is created with an online account, make the online account the default. --- src/states_screens/register_screen.cpp | 15 +++++++++------ src/states_screens/user_screen.cpp | 4 ++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/states_screens/register_screen.cpp b/src/states_screens/register_screen.cpp index 08d453a7f..067cfcf3b 100644 --- a/src/states_screens/register_screen.cpp +++ b/src/states_screens/register_screen.cpp @@ -244,16 +244,19 @@ void RegisterScreen::doRegister() { m_info_widget->setDefaultColor(); new RegistrationDialog(); + if (local_name.size() > 0) + { + PlayerProfile *player = PlayerManager::get()->getPlayer(local_name); + if (player) + { + player->setLastOnlineName(username); + player->setWasOnlineLastTime(true); + } + } return; } sfx_manager->quickSound( "anvil" ); - if(local_name.size()>0) - { - PlayerProfile *player = PlayerManager::get()->getPlayer(local_name); - if (player) - player->setLastOnlineName(username); - } } // doRegister // ----------------------------------------------------------------------------- diff --git a/src/states_screens/user_screen.cpp b/src/states_screens/user_screen.cpp index 262d44e5c..0520edc19 100644 --- a/src/states_screens/user_screen.cpp +++ b/src/states_screens/user_screen.cpp @@ -274,6 +274,10 @@ void BaseUserScreen::eventCallback(Widget* widget, else if (button == "new_user") { StateManager::get()->pushScreen(RegisterScreen::getInstance()); + // Make sure the new user will have an empty online name field + // that can also be edited. + m_username_tb->setText(""); + m_username_tb->setActivated(); } else if (button == "cancel") { From fda6aac5dded4210744fea2365d4db288d33e129 Mon Sep 17 00:00:00 2001 From: Vlj Date: Wed, 13 Aug 2014 17:31:40 +0200 Subject: [PATCH 04/26] Reenable shadows for instanced mesh --- src/graphics/render_geometry.cpp | 83 ++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/src/graphics/render_geometry.cpp b/src/graphics/render_geometry.cpp index c27f838ac..cf042b2ce 100644 --- a/src/graphics/render_geometry.cpp +++ b/src/graphics/render_geometry.cpp @@ -609,49 +609,53 @@ void renderShadow(const std::vector TextureUnits, const std::vector +struct instanced_shadow_custom_unroll_args; + +template<> +struct instanced_shadow_custom_unroll_args<> { - irr_driver->IncreaseObjectCount(); - GLenum ptype = mesh.PrimitiveType; - GLenum itype = mesh.IndexType; - size_t count = mesh.IndexCount; + template + static void exec(const T *Shader, const STK::Tuple &t, Args... args) + { + const GLMesh *mesh = STK::tuple_get<0>(t); + size_t instance_count = STK::tuple_get<1>(t); + irr_driver->IncreaseObjectCount(); + GLenum ptype = mesh->PrimitiveType; + GLenum itype = mesh->IndexType; + size_t count = mesh->IndexCount; - MeshShader::InstancedShadowShader::getInstance()->setUniforms(); + Shader->setUniforms(args...); + glDrawElementsInstanced(ptype, count, itype, 0, 4 * instance_count); + } +}; - glBindVertexArray(mesh.vao_shadow_pass); - glDrawElementsInstanced(ptype, count, itype, 0, 4 * instance_count); -} - - - -static void drawShadowAlphaRefTexture(GLMesh &mesh, size_t instance_count) +template +struct instanced_shadow_custom_unroll_args { - irr_driver->IncreaseObjectCount(); - GLenum ptype = mesh.PrimitiveType; - GLenum itype = mesh.IndexType; - size_t count = mesh.IndexCount; + template + static void exec(const T *Shader, const STK::Tuple &t, Args... args) + { + instanced_shadow_custom_unroll_args::template exec(Shader, t, STK::tuple_get(t), args...); + } +}; - compressTexture(mesh.textures[0], true); - setTexture(MeshShader::InstancedRefShadowShader::getInstance()->TU_tex, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); - MeshShader::InstancedRefShadowShader::getInstance()->setUniforms(); - - glBindVertexArray(mesh.vao_shadow_pass); - glDrawElementsInstanced(ptype, count, itype, 0, 4 * instance_count); -} - -static void drawShadowGrass(GLMesh &mesh, const core::vector3df &windDir, size_t instance_count) +template +void renderInstancedShadow(const std::vector TextureUnits, const std::vector > *t) { - irr_driver->IncreaseObjectCount(); - GLenum ptype = mesh.PrimitiveType; - GLenum itype = mesh.IndexType; - size_t count = mesh.IndexCount; + glUseProgram(T::getInstance()->Program); + for (unsigned i = 0; i < t->size(); i++) + { + const GLMesh *mesh = STK::tuple_get<0>(t->at(i)); + glBindVertexArray(mesh->vao_shadow_pass); + for (unsigned j = 0; j < TextureUnits.size(); j++) + { + compressTexture(mesh->textures[j], true); + setTexture(TextureUnits[j], getTextureGLuint(mesh->textures[j]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); + } - compressTexture(mesh.textures[0], true); - setTexture(MeshShader::InstancedGrassShadowShader::getInstance()->TU_tex, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); - MeshShader::InstancedGrassShadowShader::getInstance()->setUniforms(windDir); - - glBindVertexArray(mesh.vao_shadow_pass); - glDrawElementsInstanced(ptype, count, itype, 0, 4 * instance_count); + instanced_shadow_custom_unroll_args::template exec(T::getInstance(), t->at(i)); + } } @@ -722,6 +726,9 @@ void IrrDriver::renderShadows() ListMatNormalMap::getInstance()->clear(); ListMatGrass::getInstance()->clear(); ListMatSplatting::getInstance()->clear(); + ListInstancedMatDefault::getInstance()->clear(); + ListInstancedMatAlphaRef::getInstance()->clear(); + ListInstancedMatGrass::getInstance()->clear(); m_scene_manager->drawAll(scene::ESNRP_SOLID); std::vector noTexUnits; @@ -734,6 +741,10 @@ void IrrDriver::renderShadows() renderShadow(std::vector{ MeshShader::RefShadowShader::getInstance()->TU_tex }, ListMatUnlit::getInstance()); renderShadow(std::vector{ MeshShader::GrassShadowShader::getInstance()->TU_tex }, ListMatGrass::getInstance()); + renderInstancedShadow(noTexUnits, ListInstancedMatDefault::getInstance()); + renderInstancedShadow(std::vector{ MeshShader::InstancedRefShadowShader::getInstance()->TU_tex }, ListInstancedMatAlphaRef::getInstance()); + renderInstancedShadow(std::vector{ MeshShader::InstancedGrassShadowShader::getInstance()->TU_tex }, ListInstancedMatGrass::getInstance()); + glDisable(GL_POLYGON_OFFSET_FILL); if (!UserConfigParams::m_gi) From ebacfcfcef622f7ca478d71ca3dd380556cb32f0 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 18:59:30 +0200 Subject: [PATCH 05/26] Put RSM draw in its own method. --- src/graphics/irr_driver.hpp | 1 + src/graphics/render.cpp | 4 ++ src/graphics/render_geometry.cpp | 98 ++++++++++++++++---------------- 3 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/graphics/irr_driver.hpp b/src/graphics/irr_driver.hpp index 689d6b007..110205de0 100644 --- a/src/graphics/irr_driver.hpp +++ b/src/graphics/irr_driver.hpp @@ -367,6 +367,7 @@ private: void renderParticles(); void computeSunVisibility(); void renderShadows(); + void renderRSM(); void renderGlow(std::vector& glows); void renderSSAO(); void renderLights(unsigned pointlightCount); diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index 30ce62227..28544dc33 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -281,7 +281,11 @@ void IrrDriver::renderScene(scene::ICameraSceneNode * const camnode, unsigned po m_scene_manager->setActiveCamera(m_suncam); if (!m_mipviz && !m_wireframe && UserConfigParams::m_dynamic_lights && UserConfigParams::m_shadows && !irr_driver->needUBOWorkaround() && hasShadow) + { renderShadows(); + if (UserConfigParams::m_gi) + renderRSM(); + } m_scene_manager->setActiveCamera(camnode); PROFILER_POP_CPU_MARKER(); } diff --git a/src/graphics/render_geometry.cpp b/src/graphics/render_geometry.cpp index cf042b2ce..baa6d6317 100644 --- a/src/graphics/render_geometry.cpp +++ b/src/graphics/render_geometry.cpp @@ -592,7 +592,7 @@ struct shadow_custom_unroll_args }; template -void renderShadow(const std::vector TextureUnits, const std::vector > *t) +void renderShadow(const std::vector TextureUnits, const std::vector > *t) { glUseProgram(T::getInstance()->Program); glBindVertexArray(getVAO(VertexType)); @@ -658,52 +658,6 @@ void renderInstancedShadow(const std::vector TextureUnits, const std::ve } } - -template -struct rsm_custom_unroll_args; - -template<> -struct rsm_custom_unroll_args<> -{ - template - static void exec(const core::matrix4 &rsm_matrix, const STK::Tuple &t, Args... args) - { - draw(T::getInstance(), STK::tuple_get<0>(t), rsm_matrix, args...); - } -}; - -template -struct rsm_custom_unroll_args -{ - template - static void exec(const core::matrix4 &rsm_matrix, const STK::Tuple &t, Args... args) - { - rsm_custom_unroll_args::template exec(rsm_matrix, t, STK::tuple_get(t), args...); - } -}; - - - - -template -void drawRSM(const core::matrix4 & rsm_matrix, const std::vector TextureUnits, const std::vector > *t) -{ - glUseProgram(T::getInstance()->Program); - glBindVertexArray(getVAO(VertexType)); - for (unsigned i = 0; i < t->size(); i++) - { - GLMesh *mesh = STK::tuple_get<0>(t->at(i)); - for (unsigned j = 0; j < TextureUnits.size(); j++) - { - if (!mesh->textures[j]) - mesh->textures[j] = getUnicolorTexture(video::SColor(255, 255, 255, 255)); - compressTexture(mesh->textures[j], true); - setTexture(TextureUnits[j], getTextureGLuint(mesh->textures[j]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); - } - rsm_custom_unroll_args::template exec(rsm_matrix, t->at(i)); - } -} - void IrrDriver::renderShadows() { glDepthFunc(GL_LEQUAL); @@ -746,16 +700,60 @@ void IrrDriver::renderShadows() renderInstancedShadow(std::vector{ MeshShader::InstancedGrassShadowShader::getInstance()->TU_tex }, ListInstancedMatGrass::getInstance()); glDisable(GL_POLYGON_OFFSET_FILL); +} - if (!UserConfigParams::m_gi) - return; + +template +struct rsm_custom_unroll_args; + +template<> +struct rsm_custom_unroll_args<> +{ + template + static void exec(const core::matrix4 &rsm_matrix, const STK::Tuple &t, Args... args) + { + draw(T::getInstance(), STK::tuple_get<0>(t), rsm_matrix, args...); + } +}; + +template +struct rsm_custom_unroll_args +{ + template + static void exec(const core::matrix4 &rsm_matrix, const STK::Tuple &t, Args... args) + { + rsm_custom_unroll_args::template exec(rsm_matrix, t, STK::tuple_get(t), args...); + } +}; + +template +void drawRSM(const core::matrix4 & rsm_matrix, const std::vector &TextureUnits, std::vector > *t) +{ + glUseProgram(T::getInstance()->Program); + glBindVertexArray(getVAO(VertexType)); + for (unsigned i = 0; i < t->size(); i++) + { + GLMesh *mesh = STK::tuple_get<0>(t->at(i)); + for (unsigned j = 0; j < TextureUnits.size(); j++) + { + if (!mesh->textures[j]) + mesh->textures[j] = getUnicolorTexture(video::SColor(255, 255, 255, 255)); + compressTexture(mesh->textures[j], true); + setTexture(TextureUnits[j], getTextureGLuint(mesh->textures[j]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); + } + rsm_custom_unroll_args::template exec(rsm_matrix, t->at(i)); + } +} + +void IrrDriver::renderRSM() +{ m_rtts->getRSM().Bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawRSM(rsm_matrix, std::vector{ MeshShader::RSMShader::getInstance()->TU_tex }, ListMatDefault::getInstance()); drawRSM(rsm_matrix, std::vector{ MeshShader::RSMShader::getInstance()->TU_tex }, ListMatAlphaRef::getInstance()); -// drawRSM(rsm_matrix, std::vector{ MeshShader::RSMShader::getInstance()->TU_tex }, ListMatSphereMap::getInstance()); + drawRSM(rsm_matrix, std::vector{ MeshShader::RSMShader::getInstance()->TU_tex }, ListMatNormalMap::getInstance()); drawRSM(rsm_matrix, std::vector{ MeshShader::RSMShader::getInstance()->TU_tex }, ListMatUnlit::getInstance()); drawRSM(rsm_matrix, std::vector{ MeshShader::RSMShader::getInstance()->TU_tex }, ListMatDetails::getInstance()); drawRSM(rsm_matrix, From 70b0fce50c1c2e7d5eb2bbeba18dc7228a4da7ea Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 19:20:40 +0200 Subject: [PATCH 06/26] Add support for instanced normal map mesh. --- data/shaders/instanced_object_pass.vert | 6 +++++ data/shaders/normalmap.vert | 29 ------------------------- data/shaders/object_pass.vert | 6 +++++ src/graphics/render_geometry.cpp | 9 ++++++++ src/graphics/shaders.cpp | 21 +++++++++++++++++- src/graphics/shaders.hpp | 8 +++++++ src/graphics/stkinstancedscenenode.cpp | 3 +++ src/graphics/stkinstancedscenenode.hpp | 3 +++ 8 files changed, 55 insertions(+), 30 deletions(-) delete mode 100644 data/shaders/normalmap.vert diff --git a/data/shaders/instanced_object_pass.vert b/data/shaders/instanced_object_pass.vert index 409487993..7d94bd78a 100644 --- a/data/shaders/instanced_object_pass.vert +++ b/data/shaders/instanced_object_pass.vert @@ -3,6 +3,8 @@ layout(location = 0) in vec3 Position; layout(location = 1) in vec3 Normal; layout(location = 2) in vec4 Color; layout(location = 3) in vec2 Texcoord; +layout(location = 5) in vec3 Tangent; +layout(location = 6) in vec3 Bitangent; layout(location = 7) in vec3 Origin; layout(location = 8) in vec3 Orientation; @@ -19,6 +21,8 @@ in vec3 Scale; #endif out vec3 nor; +out vec3 tangent; +out vec3 bitangent; out vec2 uv; out vec4 color; @@ -31,6 +35,8 @@ void main(void) mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin, Orientation, Scale) * InverseViewMatrix); gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vec4(Position, 1.); nor = (TransposeInverseModelView * vec4(Normal, 0.)).xyz; + tangent = (TransposeInverseModelView * vec4(Tangent, 1.)).xyz; + bitangent = (TransposeInverseModelView * vec4(Bitangent, 1.)).xyz; uv = Texcoord; color = Color.zyxw; } diff --git a/data/shaders/normalmap.vert b/data/shaders/normalmap.vert deleted file mode 100644 index 91bd707b7..000000000 --- a/data/shaders/normalmap.vert +++ /dev/null @@ -1,29 +0,0 @@ -uniform mat4 ModelMatrix; -uniform mat4 InverseModelMatrix; - -#if __VERSION__ >= 330 -layout(location = 0) in vec3 Position; -layout(location = 3) in vec2 Texcoord; -layout(location = 5) in vec3 Tangent; -layout(location = 6) in vec3 Bitangent; -#else -in vec3 Position; -in vec2 Texcoord; -in vec3 Tangent; -in vec3 Bitangent; -#endif - -out vec3 tangent; -out vec3 bitangent; -out vec2 uv; - -void main() -{ - mat4 ModelViewProjectionMatrix = ProjectionMatrix * ViewMatrix * ModelMatrix; - mat4 TransposeInverseModelView = transpose(InverseModelMatrix * InverseViewMatrix); - uv = Texcoord; - tangent = (TransposeInverseModelView * vec4(Tangent, 1.)).xyz; - bitangent = (TransposeInverseModelView * vec4(Bitangent, 1.)).xyz; - gl_Position = ModelViewProjectionMatrix * vec4(Position, 1.); - -} diff --git a/data/shaders/object_pass.vert b/data/shaders/object_pass.vert index dfd7d315f..fc4394118 100644 --- a/data/shaders/object_pass.vert +++ b/data/shaders/object_pass.vert @@ -13,6 +13,8 @@ layout(location = 1) in vec3 Normal; layout(location = 2) in vec4 Color; layout(location = 3) in vec2 Texcoord; layout(location = 4) in vec2 SecondTexcoord; +layout(location = 5) in vec3 Tangent; +layout(location = 6) in vec3 Bitangent; #else in vec3 Position; in vec3 Normal; @@ -22,6 +24,8 @@ in vec2 SecondTexcoord; #endif out vec3 nor; +out vec3 tangent; +out vec3 bitangent; out vec2 uv; out vec2 uv_bis; out vec4 color; @@ -34,6 +38,8 @@ void main(void) mat4 TransposeInverseModelView = transpose(InverseModelMatrix * InverseViewMatrix); gl_Position = ModelViewProjectionMatrix * vec4(Position, 1.); nor = (TransposeInverseModelView * vec4(Normal, 0.)).xyz; + tangent = (TransposeInverseModelView * vec4(Tangent, 1.)).xyz; + bitangent = (TransposeInverseModelView * vec4(Bitangent, 1.)).xyz; uv = (TextureMatrix * vec4(Texcoord, 1., 1.)).xy; uv_bis = SecondTexcoord; } diff --git a/src/graphics/render_geometry.cpp b/src/graphics/render_geometry.cpp index baa6d6317..d104ee4b3 100644 --- a/src/graphics/render_geometry.cpp +++ b/src/graphics/render_geometry.cpp @@ -215,6 +215,7 @@ void IrrDriver::renderSolidFirstPass() ListInstancedMatDefault::getInstance()->clear(); ListInstancedMatAlphaRef::getInstance()->clear(); ListInstancedMatGrass::getInstance()->clear(); + ListInstancedMatNormalMap::getInstance()->clear(); m_scene_manager->drawAll(scene::ESNRP_SOLID); if (!UserConfigParams::m_dynamic_lights) @@ -245,6 +246,9 @@ void IrrDriver::renderSolidFirstPass() renderInstancedMeshes1stPass( TexUnits(TexUnit(MeshShader::InstancedGrassPass1Shader::getInstance()->TU_tex, true)), ListInstancedMatGrass::getInstance()); + renderInstancedMeshes1stPass( + TexUnits(TexUnit(MeshShader::InstancedNormalMapShader::getInstance()->TU_glossy, true), TexUnit(MeshShader::InstancedNormalMapShader::getInstance()->TU_normalmap, true)), + ListInstancedMatNormalMap::getInstance()); } } @@ -388,6 +392,9 @@ void IrrDriver::renderSolidSecondPass() renderInstancedMeshes2ndPass( TexUnits(TexUnit(MeshShader::InstancedObjectPass2Shader::getInstance()->TU_Albedo, true)), ListInstancedMatDefault::getInstance()); + renderInstancedMeshes2ndPass( + TexUnits(TexUnit(MeshShader::InstancedObjectPass2Shader::getInstance()->TU_Albedo, true)), + ListInstancedMatNormalMap::getInstance()); renderInstancedMeshes2ndPass( TexUnits(TexUnit(MeshShader::InstancedObjectRefPass2Shader::getInstance()->TU_Albedo, true)), ListInstancedMatAlphaRef::getInstance()); @@ -683,6 +690,7 @@ void IrrDriver::renderShadows() ListInstancedMatDefault::getInstance()->clear(); ListInstancedMatAlphaRef::getInstance()->clear(); ListInstancedMatGrass::getInstance()->clear(); + ListInstancedMatNormalMap::getInstance()->clear(); m_scene_manager->drawAll(scene::ESNRP_SOLID); std::vector noTexUnits; @@ -698,6 +706,7 @@ void IrrDriver::renderShadows() renderInstancedShadow(noTexUnits, ListInstancedMatDefault::getInstance()); renderInstancedShadow(std::vector{ MeshShader::InstancedRefShadowShader::getInstance()->TU_tex }, ListInstancedMatAlphaRef::getInstance()); renderInstancedShadow(std::vector{ MeshShader::InstancedGrassShadowShader::getInstance()->TU_tex }, ListInstancedMatGrass::getInstance()); + renderInstancedShadow(noTexUnits, ListInstancedMatNormalMap::getInstance()); glDisable(GL_POLYGON_OFFSET_FILL); } diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index b0e67d0d5..d6c378ae9 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -628,7 +628,7 @@ namespace MeshShader NormalMapShader::NormalMapShader() { Program = LoadProgram( - GL_VERTEX_SHADER, file_manager->getAsset("shaders/normalmap.vert").c_str(), + GL_VERTEX_SHADER, file_manager->getAsset("shaders/object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/normalmap.frag").c_str()); AssignUniforms("ModelMatrix", "InverseModelMatrix"); @@ -649,6 +649,7 @@ namespace MeshShader GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass1.frag").c_str()); TU_tex = 0; + AssignUniforms(); AssignTextureUnit(Program, TexUnit(TU_tex, "tex")); GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); @@ -663,6 +664,7 @@ namespace MeshShader GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass1.frag").c_str()); TU_tex = 0; + AssignUniforms(); AssignTextureUnit(Program, TexUnit(TU_tex, "tex")); GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); @@ -684,6 +686,23 @@ namespace MeshShader glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0); } + InstancedNormalMapShader::InstancedNormalMapShader() + { + Program = LoadProgram( + GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(), + GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/normalmap.frag").c_str()); + AssignUniforms(); + + GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); + glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0); + + TU_normalmap = 1; + TU_glossy = 0; + AssignTextureUnit(Program, TexUnit(TU_normalmap, "normalMap"), TexUnit(TU_glossy, "DiffuseForAlpha")); + } + // Solid Lit pass shaders ObjectPass2Shader::ObjectPass2Shader() { diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index 43b1d31e8..e39208b1b 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -220,6 +220,14 @@ public: InstancedGrassPass1Shader(); }; +class InstancedNormalMapShader : public ShaderHelperSingleton +{ +public: + GLuint TU_glossy, TU_normalmap; + + InstancedNormalMapShader(); +}; + class ObjectPass2Shader : public ShaderHelperSingleton { public: diff --git a/src/graphics/stkinstancedscenenode.cpp b/src/graphics/stkinstancedscenenode.cpp index 81a1438de..9c3d5c2f9 100644 --- a/src/graphics/stkinstancedscenenode.cpp +++ b/src/graphics/stkinstancedscenenode.cpp @@ -160,4 +160,7 @@ void STKInstancedSceneNode::render() SunLightProvider * const cb = (SunLightProvider *)irr_driver->getCallback(ES_SUNLIGHT); for (auto mesh : MeshSolidMaterial[MAT_GRASS]) ListInstancedMatGrass::getInstance()->push_back(STK::make_tuple(mesh, instance_pos.size() / 9, windDir, cb->getPosition())); + + for (auto mesh : MeshSolidMaterial[MAT_NORMAL_MAP]) + ListInstancedMatNormalMap::getInstance()->push_back(STK::make_tuple(mesh, instance_pos.size() / 9)); } diff --git a/src/graphics/stkinstancedscenenode.hpp b/src/graphics/stkinstancedscenenode.hpp index 0f07db057..d1063ca6d 100644 --- a/src/graphics/stkinstancedscenenode.hpp +++ b/src/graphics/stkinstancedscenenode.hpp @@ -13,6 +13,9 @@ class ListInstancedMatAlphaRef : public MeshList {}; +class ListInstancedMatNormalMap : public MeshList +{}; + class STKInstancedSceneNode : public irr::scene::CMeshSceneNode { protected: From dc9993d7802bc5819bf4495d6426b735c7672b9f Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 19:28:02 +0200 Subject: [PATCH 07/26] Add Debug for instanced vertex type mismatch. --- src/graphics/render_geometry.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/graphics/render_geometry.cpp b/src/graphics/render_geometry.cpp index d104ee4b3..a7ab8c67c 100644 --- a/src/graphics/render_geometry.cpp +++ b/src/graphics/render_geometry.cpp @@ -172,13 +172,17 @@ struct instanced_custom_unroll_args } }; -template +template void renderInstancedMeshes1stPass(const std::vector &TexUnits, std::vector > *meshes) { glUseProgram(Shader::getInstance()->Program); for (unsigned i = 0; i < meshes->size(); i++) { GLMesh &mesh = *(STK::tuple_get<0>(meshes->at(i))); +#ifdef DEBUG + if (mesh.VAOType != VertexType) + Log::error("RenderGeometry", "Wrong instanced vertex format"); +#endif glBindVertexArray(mesh.vao); for (unsigned j = 0; j < TexUnits.size(); j++) { @@ -237,16 +241,16 @@ void IrrDriver::renderSolidFirstPass() TexUnit(MeshShader::NormalMapShader::getInstance()->TU_normalmap, false) ), ListMatNormalMap::getInstance()); - renderInstancedMeshes1stPass( + renderInstancedMeshes1stPass( TexUnits(TexUnit(MeshShader::InstancedObjectPass1Shader::getInstance()->TU_tex, true)), ListInstancedMatDefault::getInstance()); - renderInstancedMeshes1stPass( + renderInstancedMeshes1stPass( TexUnits(TexUnit(MeshShader::InstancedObjectRefPass1Shader::getInstance()->TU_tex, true)), ListInstancedMatAlphaRef::getInstance()); - renderInstancedMeshes1stPass( + renderInstancedMeshes1stPass( TexUnits(TexUnit(MeshShader::InstancedGrassPass1Shader::getInstance()->TU_tex, true)), ListInstancedMatGrass::getInstance()); - renderInstancedMeshes1stPass( + renderInstancedMeshes1stPass( TexUnits(TexUnit(MeshShader::InstancedNormalMapShader::getInstance()->TU_glossy, true), TexUnit(MeshShader::InstancedNormalMapShader::getInstance()->TU_normalmap, true)), ListInstancedMatNormalMap::getInstance()); } From 7a9e77da01235bcd1c09bac43dc1dae2980b927a Mon Sep 17 00:00:00 2001 From: Deve Date: Wed, 13 Aug 2014 19:34:28 +0200 Subject: [PATCH 08/26] Exit cmake with error if xrandr library was not found. --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 322c12f9a..d490b6bb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,6 +136,9 @@ include_directories(${OPENGL_INCLUDE_DIR}) if(UNIX AND NOT APPLE) if(USE_XRANDR) find_package(Xrandr REQUIRED) + if(NOT XRANDR_FOUND) + message(FATAL_ERROR "XRANDR not found.") + endif() else() find_library(IRRLICHT_XF86VM_LIBRARY Xxf86vm) mark_as_advanced(IRRLICHT_XF86VM_LIBRARY) From 22137eee883fc1a985595d47371d967380b381d4 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 20:25:56 +0200 Subject: [PATCH 09/26] Port UI shaders. --- data/shaders/coloredquad.vert | 8 +- data/shaders/colortexturedquad.vert | 26 ++--- data/shaders/texturedquad.vert | 17 ++- src/graphics/glwrap.cpp | 49 ++++---- src/graphics/shaders.cpp | 169 +++++----------------------- src/graphics/shaders.hpp | 44 +++----- 6 files changed, 97 insertions(+), 216 deletions(-) diff --git a/data/shaders/coloredquad.vert b/data/shaders/coloredquad.vert index 2864e469a..4f48e98f2 100644 --- a/data/shaders/coloredquad.vert +++ b/data/shaders/coloredquad.vert @@ -1,14 +1,14 @@ uniform vec2 center; uniform vec2 size; -#if __VERSION__ >= 130 -in vec2 position; +#if __VERSION__ >= 330 +layout(location = 0) in vec2 Position; #else -attribute vec2 position; +in vec2 Position; #endif void main() { - gl_Position = vec4(position * size + center, 0., 1.); + gl_Position = vec4(Position * size + center, 0., 1.); } \ No newline at end of file diff --git a/data/shaders/colortexturedquad.vert b/data/shaders/colortexturedquad.vert index c4e4d41a0..467fdd080 100644 --- a/data/shaders/colortexturedquad.vert +++ b/data/shaders/colortexturedquad.vert @@ -3,24 +3,22 @@ uniform vec2 size; uniform vec2 texcenter; uniform vec2 texsize; -#if __VERSION__ >= 130 -in vec2 position; -in vec2 texcoord; -in uvec4 color; -out vec2 uv; -out vec4 col; +#if __VERSION__ >= 330 +layout(location=0) in vec2 Position; +layout(location=3) in vec2 Texcoord; +layout(location=2) in uvec4 Color; #else -attribute vec2 position; -attribute vec2 texcoord; -attribute uvec4 color; -varying vec2 uv; -varying vec4 col; +in vec2 Position; +in vec2 Texcoord; +in uvec4 Color; #endif +out vec2 uv; +out vec4 col; void main() { - col = vec4(color) / 255.; - uv = texcoord * texsize + texcenter; - gl_Position = vec4(position * size + center, 0., 1.); + col = vec4(Color) / 255.; + uv = Texcoord * texsize + texcenter; + gl_Position = vec4(Position * size + center, 0., 1.); } diff --git a/data/shaders/texturedquad.vert b/data/shaders/texturedquad.vert index bc6f1b03d..3299139c0 100644 --- a/data/shaders/texturedquad.vert +++ b/data/shaders/texturedquad.vert @@ -3,19 +3,18 @@ uniform vec2 size; uniform vec2 texcenter; uniform vec2 texsize; -#if __VERSION__ >= 130 -in vec2 position; -in vec2 texcoord; -out vec2 uv; +#if __VERSION__ >= 330 +layout(location=0) in vec2 Position; +layout(location=3) in vec2 Texcoord; #else -attribute vec2 position; -attribute vec2 texcoord; -varying vec2 uv; +in vec2 Position; +in vec2 Texcoord; #endif +out vec2 uv; void main() { - uv = texcoord * texsize + texcenter; - gl_Position = vec4(position * size + center, 0., 1.); + uv = Texcoord * texsize + texcenter; + gl_Position = vec4(Position * size + center, 0., 1.); } \ No newline at end of file diff --git a/src/graphics/glwrap.cpp b/src/graphics/glwrap.cpp index 8b4ff6e79..44e297a4a 100644 --- a/src/graphics/glwrap.cpp +++ b/src/graphics/glwrap.cpp @@ -908,14 +908,16 @@ static void drawTexColoredQuad(const video::ITexture *texture, const video::SCol col[3].getRed(), col[3].getGreen(), col[3].getBlue(), col[3].getAlpha(), }; - glBindBuffer(GL_ARRAY_BUFFER, UIShader::ColoredTextureRectShader::colorvbo); + glBindBuffer(GL_ARRAY_BUFFER, UIShader::ColoredTextureRectShader::getInstance()->colorvbo); glBufferSubData(GL_ARRAY_BUFFER, 0, 16 * sizeof(unsigned), colors); - glUseProgram(UIShader::ColoredTextureRectShader::Program); - glBindVertexArray(UIShader::ColoredTextureRectShader::vao); + glUseProgram(UIShader::ColoredTextureRectShader::getInstance()->Program); + glBindVertexArray(UIShader::ColoredTextureRectShader::getInstance()->vao); - setTexture(0, static_cast(texture)->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR); - UIShader::TextureRectShader::setUniforms(center_pos_x, center_pos_y, width, height, tex_center_pos_x, tex_center_pos_y, tex_width, tex_height, 0); + setTexture(UIShader::ColoredTextureRectShader::getInstance()->TU_tex, static_cast(texture)->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR); + UIShader::ColoredTextureRectShader::getInstance()->setUniforms( + core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height), + core::vector2df(tex_center_pos_x, tex_center_pos_y), core::vector2df(tex_width, tex_height)); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); @@ -929,11 +931,14 @@ void drawTexQuad(GLuint texture, float width, float height, float center_pos_x, float center_pos_y, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height) { - glUseProgram(UIShader::TextureRectShader::Program); - glBindVertexArray(UIShader::TextureRectShader::vao); + glUseProgram(UIShader::TextureRectShader::getInstance()->Program); + glBindVertexArray(SharedObject::UIVAO); - setTexture(0, texture, GL_LINEAR, GL_LINEAR); - UIShader::TextureRectShader::setUniforms(center_pos_x, center_pos_y, width, height, tex_center_pos_x, tex_center_pos_y, tex_width, tex_height, 0); + setTexture(UIShader::TextureRectShader::getInstance()->TU_tex, texture, GL_LINEAR, GL_LINEAR); + UIShader::TextureRectShader::getInstance()->setUniforms( + core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height), + core::vector2df(tex_center_pos_x, tex_center_pos_y), + core::vector2df(tex_width, tex_height)); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); @@ -1029,11 +1034,12 @@ void draw2DImage(const video::ITexture* texture, const core::rect& destRect clipRect->getWidth(), clipRect->getHeight()); } - glUseProgram(UIShader::UniformColoredTextureRectShader::Program); - glBindVertexArray(UIShader::UniformColoredTextureRectShader::vao); + glUseProgram(UIShader::UniformColoredTextureRectShader::getInstance()->Program); + glBindVertexArray(SharedObject::UIVAO); - setTexture(0, static_cast(texture)->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR); - UIShader::UniformColoredTextureRectShader::setUniforms(center_pos_x, center_pos_y, width, height, tex_center_pos_x, tex_center_pos_y, tex_width, tex_height,colors, 0); + setTexture(UIShader::UniformColoredTextureRectShader::getInstance()->TU_tex, static_cast(texture)->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR); + UIShader::UniformColoredTextureRectShader::getInstance()->setUniforms( + core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height), core::vector2df(tex_center_pos_x, tex_center_pos_y), core::vector2df(tex_width, tex_height), colors); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); @@ -1064,11 +1070,14 @@ void draw2DImageFromRTT(GLuint texture, size_t texture_w, size_t texture_h, destRect, sourceRect, width, height, center_pos_x, center_pos_y, tex_width, tex_height, tex_center_pos_x, tex_center_pos_y); - glUseProgram(UIShader::UniformColoredTextureRectShader::Program); - glBindVertexArray(UIShader::UniformColoredTextureRectShader::vao); + glUseProgram(UIShader::UniformColoredTextureRectShader::getInstance()->Program); + glBindVertexArray(SharedObject::UIVAO); - setTexture(0, texture, GL_LINEAR, GL_LINEAR); - UIShader::UniformColoredTextureRectShader::setUniforms(center_pos_x, center_pos_y, width, height, tex_center_pos_x, tex_center_pos_y, tex_width, tex_height, colors, 0); + setTexture(UIShader::UniformColoredTextureRectShader::getInstance()->TU_tex, texture, GL_LINEAR, GL_LINEAR); + UIShader::UniformColoredTextureRectShader::getInstance()->setUniforms( + core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height), + core::vector2df(tex_center_pos_x, tex_center_pos_y), core::vector2df(tex_width, tex_height), + colors); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); @@ -1172,9 +1181,9 @@ void GL32_draw2DRectangle(video::SColor color, const core::rect& position, clip->getWidth(), clip->getHeight()); } - glUseProgram(UIShader::ColoredRectShader::Program); - glBindVertexArray(UIShader::ColoredRectShader::vao); - UIShader::ColoredRectShader::setUniforms(center_pos_x, center_pos_y, width, height, color); + glUseProgram(UIShader::ColoredRectShader::getInstance()->Program); + glBindVertexArray(SharedObject::UIVAO); + UIShader::ColoredRectShader::getInstance()->setUniforms(core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height), color); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index d6c378ae9..321abf523 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -133,6 +133,7 @@ Shaders::Shaders() GLuint quad_vbo, tri_vbo; GLuint SharedObject::FullScreenQuadVAO = 0; +GLuint SharedObject::UIVAO = 0; static void initQuadVBO() { @@ -179,6 +180,15 @@ static void initQuadBuffer() glGenBuffers(1, &quad_buffer); glBindBuffer(GL_ARRAY_BUFFER, quad_buffer); glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad_vertex, GL_STATIC_DRAW); + + glGenVertexArrays(1, &SharedObject::UIVAO); + glBindVertexArray(SharedObject::UIVAO); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(3); + glBindBuffer(GL_ARRAY_BUFFER, quad_buffer); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); + glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float))); + glBindVertexArray(0); } GLuint SharedObject::billboardvbo = 0; @@ -391,10 +401,6 @@ void Shaders::loadShaders() ParticleShader::HeightmapSimulationShader::init(); ParticleShader::SimpleParticleRender::init(); ParticleShader::SimpleSimulationShader::init(); - UIShader::ColoredRectShader::init(); - UIShader::ColoredTextureRectShader::init(); - UIShader::TextureRectShader::init(); - UIShader::UniformColoredTextureRectShader::init(); UtilShader::ColoredLine::init(); } @@ -1966,127 +1972,44 @@ namespace FullScreenShader namespace UIShader { - GLuint TextureRectShader::Program; - GLuint TextureRectShader::attrib_position; - GLuint TextureRectShader::attrib_texcoord; - GLuint TextureRectShader::uniform_tex; - GLuint TextureRectShader::uniform_center; - GLuint TextureRectShader::uniform_size; - GLuint TextureRectShader::uniform_texcenter; - GLuint TextureRectShader::uniform_texsize; - GLuint TextureRectShader::vao; - - void TextureRectShader::init() + TextureRectShader::TextureRectShader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/texturedquad.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/texturedquad.frag").c_str()); - - attrib_position = glGetAttribLocation(Program, "position"); - attrib_texcoord = glGetAttribLocation(Program, "texcoord"); - uniform_tex = glGetUniformLocation(Program, "tex"); - uniform_center = glGetUniformLocation(Program, "center"); - uniform_size = glGetUniformLocation(Program, "size"); - uniform_texcenter = glGetUniformLocation(Program, "texcenter"); - uniform_texsize = glGetUniformLocation(Program, "texsize"); - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - glEnableVertexAttribArray(attrib_position); - glEnableVertexAttribArray(attrib_texcoord); - glBindBuffer(GL_ARRAY_BUFFER, quad_buffer); - glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); - glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float))); - glBindVertexArray(0); + AssignUniforms("center", "size", "texcenter", "texsize"); + TU_tex = 0; + AssignTextureUnit(Program, TexUnit(TU_tex, "tex")); } - void TextureRectShader::setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, unsigned TU_tex) - { - glUniform1i(uniform_tex, TU_tex); - glUniform2f(uniform_center, center_pos_x, center_pos_y); - glUniform2f(uniform_size, width, height); - glUniform2f(uniform_texcenter, tex_center_pos_x, tex_center_pos_y); - glUniform2f(uniform_texsize, tex_width, tex_height); - } - - GLuint UniformColoredTextureRectShader::Program; - GLuint UniformColoredTextureRectShader::attrib_position; - GLuint UniformColoredTextureRectShader::attrib_texcoord; - GLuint UniformColoredTextureRectShader::uniform_tex; - GLuint UniformColoredTextureRectShader::uniform_color; - GLuint UniformColoredTextureRectShader::uniform_center; - GLuint UniformColoredTextureRectShader::uniform_size; - GLuint UniformColoredTextureRectShader::uniform_texcenter; - GLuint UniformColoredTextureRectShader::uniform_texsize; - GLuint UniformColoredTextureRectShader::vao; - - void UniformColoredTextureRectShader::init() + UniformColoredTextureRectShader::UniformColoredTextureRectShader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/texturedquad.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/uniformcolortexturedquad.frag").c_str()); - attrib_position = glGetAttribLocation(Program, "position"); - attrib_texcoord = glGetAttribLocation(Program, "texcoord"); - uniform_tex = glGetUniformLocation(Program, "tex"); - uniform_color = glGetUniformLocation(Program, "color"); - uniform_center = glGetUniformLocation(Program, "center"); - uniform_size = glGetUniformLocation(Program, "size"); - uniform_texcenter = glGetUniformLocation(Program, "texcenter"); - uniform_texsize = glGetUniformLocation(Program, "texsize"); - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - glEnableVertexAttribArray(attrib_position); - glEnableVertexAttribArray(attrib_texcoord); - glBindBuffer(GL_ARRAY_BUFFER, quad_buffer); - glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); - glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float))); - glBindVertexArray(0); + AssignUniforms("center", "size", "texcenter", "texsize", "color"); + TU_tex = 0; + AssignTextureUnit(Program, TexUnit(TU_tex, "tex")); } - void UniformColoredTextureRectShader::setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, const SColor &color, unsigned TU_tex) - { - glUniform1i(uniform_tex, TU_tex); - glUniform2f(uniform_center, center_pos_x, center_pos_y); - glUniform2f(uniform_size, width, height); - glUniform2f(uniform_texcenter, tex_center_pos_x, tex_center_pos_y); - glUniform2f(uniform_texsize, tex_width, tex_height); - glUniform4i(uniform_color, color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()); - } - - GLuint ColoredTextureRectShader::Program; - GLuint ColoredTextureRectShader::attrib_position; - GLuint ColoredTextureRectShader::attrib_texcoord; - GLuint ColoredTextureRectShader::attrib_color; - GLuint ColoredTextureRectShader::uniform_tex; - GLuint ColoredTextureRectShader::uniform_center; - GLuint ColoredTextureRectShader::uniform_size; - GLuint ColoredTextureRectShader::uniform_texcenter; - GLuint ColoredTextureRectShader::uniform_texsize; - GLuint ColoredTextureRectShader::colorvbo; - GLuint ColoredTextureRectShader::vao; - - void ColoredTextureRectShader::init() + ColoredTextureRectShader::ColoredTextureRectShader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/colortexturedquad.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/colortexturedquad.frag").c_str()); + AssignUniforms("center", "size", "texcenter", "texsize"); + TU_tex = 0; + AssignTextureUnit(Program, TexUnit(TU_tex, "tex")); - attrib_position = glGetAttribLocation(Program, "position"); - attrib_texcoord = glGetAttribLocation(Program, "texcoord"); - attrib_color = glGetAttribLocation(Program, "color"); - uniform_tex = glGetUniformLocation(Program, "tex"); - uniform_center = glGetUniformLocation(Program, "center"); - uniform_size = glGetUniformLocation(Program, "size"); - uniform_texcenter = glGetUniformLocation(Program, "texcenter"); - uniform_texsize = glGetUniformLocation(Program, "texsize"); glGenVertexArrays(1, &vao); glBindVertexArray(vao); - glEnableVertexAttribArray(attrib_position); - glEnableVertexAttribArray(attrib_texcoord); - glEnableVertexAttribArray(attrib_color); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(3); + glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, quad_buffer); - glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); - glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float))); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); + glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float))); const unsigned quad_color[] = { 0, 0, 0, 255, 255, 0, 0, 255, @@ -2096,47 +2019,15 @@ namespace UIShader glGenBuffers(1, &colorvbo); glBindBuffer(GL_ARRAY_BUFFER, colorvbo); glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(unsigned), quad_color, GL_DYNAMIC_DRAW); - glVertexAttribIPointer(attrib_color, 4, GL_UNSIGNED_INT, 4 * sizeof(unsigned), 0); + glVertexAttribIPointer(2, 4, GL_UNSIGNED_INT, 4 * sizeof(unsigned), 0); glBindVertexArray(0); } - void ColoredTextureRectShader::setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, unsigned TU_tex) - { - glUniform1i(uniform_tex, TU_tex); - glUniform2f(uniform_center, center_pos_x, center_pos_y); - glUniform2f(uniform_size, width, height); - glUniform2f(uniform_texcenter, tex_center_pos_x, tex_center_pos_y); - glUniform2f(uniform_texsize, tex_width, tex_height); - } - - GLuint ColoredRectShader::Program; - GLuint ColoredRectShader::attrib_position; - GLuint ColoredRectShader::uniform_center; - GLuint ColoredRectShader::uniform_size; - GLuint ColoredRectShader::uniform_color; - GLuint ColoredRectShader::vao; - - void ColoredRectShader::init() + ColoredRectShader::ColoredRectShader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/coloredquad.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/coloredquad.frag").c_str()); - attrib_position = glGetAttribLocation(Program, "position"); - uniform_color = glGetUniformLocation(Program, "color"); - uniform_center = glGetUniformLocation(Program, "center"); - uniform_size = glGetUniformLocation(Program, "size"); - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - glEnableVertexAttribArray(attrib_position); - glBindBuffer(GL_ARRAY_BUFFER, quad_buffer); - glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); - glBindVertexArray(0); - } - - void ColoredRectShader::setUniforms(float center_pos_x, float center_pos_y, float width, float height, const video::SColor &color) - { - glUniform2f(uniform_center, center_pos_x, center_pos_y); - glUniform2f(uniform_size, width, height); - glUniform4i(uniform_color, color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()); + AssignUniforms("center", "size", "color"); } } diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index e39208b1b..639b13617 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -34,6 +34,7 @@ public: static GLuint cubevbo, cubeindexes, frustrumvbo, frustrumindexes; static GLuint ViewProjectionMatrixesUBO; static GLuint FullScreenQuadVAO; + static GLuint UIVAO; }; namespace UtilShader @@ -790,53 +791,36 @@ public: namespace UIShader { -class TextureRectShader +class TextureRectShader : public ShaderHelperSingleton { public: - static GLuint Program; - static GLuint attrib_position, attrib_texcoord; - static GLuint uniform_tex, uniform_center, uniform_size, uniform_texcenter, uniform_texsize; - static GLuint vao; + GLuint TU_tex; - static void init(); - static void setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, unsigned TU_tex); + TextureRectShader(); }; -class UniformColoredTextureRectShader +class UniformColoredTextureRectShader : public ShaderHelperSingleton { public: - static GLuint Program; - static GLuint attrib_position, attrib_texcoord; - static GLuint uniform_tex, uniform_color, uniform_center, uniform_size, uniform_texcenter, uniform_texsize; - static GLuint vao; + GLuint TU_tex; - static void init(); - static void setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, const video::SColor &color, unsigned TU_tex); + UniformColoredTextureRectShader(); }; -class ColoredTextureRectShader +class ColoredTextureRectShader : public ShaderHelperSingleton { public: - static GLuint Program; - static GLuint attrib_position, attrib_texcoord, attrib_color; - static GLuint uniform_tex, uniform_center, uniform_size, uniform_texcenter, uniform_texsize; - static GLuint colorvbo; - static GLuint vao; + GLuint TU_tex; + GLuint colorvbo; + GLuint vao; - static void init(); - static void setUniforms(float center_pos_x, float center_pos_y, float width, float height, float tex_center_pos_x, float tex_center_pos_y, float tex_width, float tex_height, unsigned TU_tex); + ColoredTextureRectShader(); }; -class ColoredRectShader +class ColoredRectShader : public ShaderHelperSingleton { public: - static GLuint Program; - static GLuint attrib_position; - static GLuint uniform_center, uniform_size, uniform_color; - static GLuint vao; - - static void init(); - static void setUniforms(float center_pos_x, float center_pos_y, float width, float height, const video::SColor &color); + ColoredRectShader(); }; } From dc02eec7b6e83a1eb84020b81856b4d184586683 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 21:04:03 +0200 Subject: [PATCH 10/26] Port particle rendering shaders too. --- data/shaders/flipparticle.vert | 19 +++---- data/shaders/particle.frag | 15 +++--- data/shaders/particle.vert | 21 ++++---- src/graphics/gpuparticles.cpp | 81 ++++++++++++---------------- src/graphics/shaders.cpp | 96 +++++----------------------------- src/graphics/shaders.hpp | 20 +++---- 6 files changed, 79 insertions(+), 173 deletions(-) diff --git a/data/shaders/flipparticle.vert b/data/shaders/flipparticle.vert index 4ef8cc68d..591d121af 100644 --- a/data/shaders/flipparticle.vert +++ b/data/shaders/flipparticle.vert @@ -1,11 +1,12 @@ -in vec2 quadcorner; -in vec2 texcoord; -in vec3 position; -in float lifetime; -in float size; +layout(location=0) in vec3 Position; +layout(location = 1) in float lifetime; +layout(location = 2) in float size; -in vec3 rotationvec; -in float anglespeed; +layout(location = 3) in vec2 Texcoord; +layout(location = 4) in vec2 quadcorner; + +layout(location = 5) in vec3 rotationvec; +layout(location = 6) in float anglespeed; out float lf; out vec2 tc; @@ -13,9 +14,9 @@ out vec3 pc; void main(void) { - tc = texcoord; + tc = Texcoord; lf = lifetime; - vec3 newposition = position; + vec3 newposition = Position; // from http://jeux.developpez.com/faq/math float angle = lf * anglespeed; diff --git a/data/shaders/particle.frag b/data/shaders/particle.frag index 54fca2871..dfe447838 100644 --- a/data/shaders/particle.frag +++ b/data/shaders/particle.frag @@ -7,17 +7,16 @@ in vec2 tc; in vec3 pc; out vec4 FragColor; +vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix); void main(void) { - vec2 xy = gl_FragCoord.xy / screen; - float FragZ = gl_FragCoord.z; - float EnvZ = texture(dtex, xy).x; - vec4 FragmentPos = invproj * (2. * vec4(xy, FragZ, 1.0) - 1.); - FragmentPos /= FragmentPos.w; - vec4 EnvPos = invproj * (2. * vec4(xy, EnvZ, 1.0) - 1.); - EnvPos /= EnvPos.w; - float alpha = clamp((EnvPos.z - FragmentPos.z) * 0.3, 0., 1.); + vec2 xy = gl_FragCoord.xy / screen; + float FragZ = gl_FragCoord.z; + vec4 FragmentPos = getPosFromUVDepth(vec3(xy, FragZ), InverseProjectionMatrix); + float EnvZ = texture(dtex, xy).x; + vec4 EnvPos = getPosFromUVDepth(vec3(xy, EnvZ), InverseProjectionMatrix); + float alpha = clamp((EnvPos.z - FragmentPos.z) * 0.3, 0., 1.); vec4 color = texture(tex, tc) * vec4(pc, 1.0); FragColor = color * alpha * smoothstep(1., 0.8, lf); } diff --git a/data/shaders/particle.vert b/data/shaders/particle.vert index dd8efbac2..99d38137c 100644 --- a/data/shaders/particle.vert +++ b/data/shaders/particle.vert @@ -1,11 +1,12 @@ uniform vec3 color_from; uniform vec3 color_to; -in vec2 quadcorner; -in vec2 texcoord; -in vec3 position; -in float lifetime; -in float size; +layout(location=0) in vec3 Position; +layout(location = 1) in float lifetime; +layout(location = 2) in float size; + +layout(location=3) in vec2 Texcoord; +layout(location = 4) in vec2 quadcorner; out float lf; out vec2 tc; @@ -13,12 +14,12 @@ out vec3 pc; void main(void) { - tc = texcoord; - lf = lifetime; + tc = Texcoord; + lf = lifetime; pc = color_from + (color_to - color_from) * lifetime; - vec3 newposition = position; + vec3 newposition = Position; vec4 viewpos = ViewMatrix * vec4(newposition, 1.0); - viewpos += size * vec4(quadcorner, 0., 0.); - gl_Position = ProjectionMatrix * viewpos; + viewpos += size * vec4(quadcorner, 0., 0.); + gl_Position = ProjectionMatrix * viewpos; } diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index 49a0494f6..c2c85e54b 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -261,44 +261,38 @@ static bool isGPUParticleType(scene::E_PARTICLE_EMITTER_TYPE type) } } -template -void setPositionQuadAttributes(GLuint quad_vbo, GLuint position_vbo) +void ParticleSystemProxy::SimpleParticleVAOBind(GLuint PositionBuffer) { - glBindBuffer(GL_ARRAY_BUFFER, quad_vbo); - glEnableVertexAttribArray(T::attrib_quadcorner); - glVertexAttribPointer(T::attrib_quadcorner, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); - glEnableVertexAttribArray(T::attrib_texcoord); - glVertexAttribPointer(T::attrib_texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float))); + glBindBuffer(GL_ARRAY_BUFFER, quad_vertex_buffer); + glEnableVertexAttribArray(4); + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); + glEnableVertexAttribArray(3); + glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(2 * sizeof(float))); - glBindBuffer(GL_ARRAY_BUFFER, position_vbo); - glEnableVertexAttribArray(T::attrib_pos); - glVertexAttribPointer(T::attrib_pos, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleData), 0); - glVertexAttribDivisor(T::attrib_pos, 1); - glEnableVertexAttribArray(T::attrib_lf); - glVertexAttribPointer(T::attrib_lf, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid *)(3 * sizeof(float))); - glVertexAttribDivisor(T::attrib_lf, 1); - glEnableVertexAttribArray(T::attrib_sz); - glVertexAttribPointer(T::attrib_sz, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid *)(7 * sizeof(float))); - glVertexAttribDivisor(T::attrib_sz, 1); + glBindBuffer(GL_ARRAY_BUFFER, PositionBuffer); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleData), 0); + glVertexAttribDivisor(0, 1); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid *)(3 * sizeof(float))); + glVertexAttribDivisor(1, 1); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid *)(7 * sizeof(float))); + glVertexAttribDivisor(2, 1); } void ParticleSystemProxy::FlipParticleVAOBind(GLuint PositionBuffer, GLuint QuaternionBuffer) { - setPositionQuadAttributes(quad_vertex_buffer, PositionBuffer); - glEnableVertexAttribArray(ParticleShader::FlipParticleRender::attrib_rotationvec); - glEnableVertexAttribArray(ParticleShader::FlipParticleRender::attrib_anglespeed); - + SimpleParticleVAOBind(PositionBuffer); glBindBuffer(GL_ARRAY_BUFFER, QuaternionBuffer); - glVertexAttribPointer(ParticleShader::FlipParticleRender::attrib_rotationvec, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); - glVertexAttribPointer(ParticleShader::FlipParticleRender::attrib_anglespeed, 1, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(3 * sizeof(float))); + glEnableVertexAttribArray(5); - glVertexAttribDivisor(ParticleShader::FlipParticleRender::attrib_rotationvec, 1); - glVertexAttribDivisor(ParticleShader::FlipParticleRender::attrib_anglespeed, 1); -} + glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); + glVertexAttribDivisor(5, 1); -void ParticleSystemProxy::SimpleParticleVAOBind(GLuint PositionBuffer) -{ - setPositionQuadAttributes(quad_vertex_buffer, PositionBuffer); + glEnableVertexAttribArray(6); + glVertexAttribPointer(6, 1, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *)(3 * sizeof(float))); + glVertexAttribDivisor(6, 1); } template @@ -442,17 +436,12 @@ void ParticleSystemProxy::simulate() void ParticleSystemProxy::drawFlip() { glBlendFunc(GL_ONE, GL_ONE); - glUseProgram(ParticleShader::FlipParticleRender::Program); + glUseProgram(ParticleShader::FlipParticleRender::getInstance()->Program); - float screen[2] = { - (float)UserConfigParams::m_width, - (float)UserConfigParams::m_height - }; + setTexture(ParticleShader::FlipParticleRender::getInstance()->TU_tex, texture, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR); + setTexture(ParticleShader::FlipParticleRender::getInstance()->TU_dtex, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST); - setTexture(0, texture, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR); - setTexture(1, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST); - - ParticleShader::FlipParticleRender::setUniforms(irr_driver->getViewMatrix(), irr_driver->getProjMatrix(), irr_driver->getInvProjMatrix(), screen[0], screen[1], 0, 1); + ParticleShader::FlipParticleRender::getInstance()->setUniforms(); glBindVertexArray(current_rendering_vao); glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, count); @@ -464,18 +453,14 @@ void ParticleSystemProxy::drawNotFlip() glBlendFunc(GL_ONE, GL_ONE); else glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - glUseProgram(ParticleShader::SimpleParticleRender::Program); + glUseProgram(ParticleShader::SimpleParticleRender::getInstance()->Program); - float screen[2] = { - (float)UserConfigParams::m_width, - (float)UserConfigParams::m_height - }; + setTexture(ParticleShader::SimpleParticleRender::getInstance()->TU_tex, texture, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR); + setTexture(ParticleShader::SimpleParticleRender::getInstance()->TU_dtex, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST); + video::SColorf ColorFrom = video::SColorf(getColorFrom()[0], getColorFrom()[1], getColorFrom()[2]); + video::SColorf ColorTo = video::SColorf(getColorTo()[0], getColorTo()[1], getColorTo()[2]); - setTexture(0, texture, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR); - setTexture(1, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST); - - ParticleShader::SimpleParticleRender::setUniforms(irr_driver->getViewMatrix(), irr_driver->getProjMatrix(), - irr_driver->getInvProjMatrix(), screen[0], screen[1], 0, 1, this); + ParticleShader::SimpleParticleRender::getInstance()->setUniforms(ColorFrom, ColorTo); glBindVertexArray(current_rendering_vao); glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, count); diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 321abf523..7c477bcf0 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -397,9 +397,7 @@ void Shaders::loadShaders() LightShader::PointLightShader::init(); MeshShader::SkyboxShader::init(); MeshShader::ViewFrustrumShader::init(); - ParticleShader::FlipParticleRender::init(); ParticleShader::HeightmapSimulationShader::init(); - ParticleShader::SimpleParticleRender::init(); ParticleShader::SimpleSimulationShader::init(); UtilShader::ColoredLine::init(); } @@ -1451,100 +1449,30 @@ namespace ParticleShader uniform_track_z_len = glGetUniformLocation(Program, "track_z_len"); } - GLuint SimpleParticleRender::Program; - GLuint SimpleParticleRender::attrib_pos; - GLuint SimpleParticleRender::attrib_lf; - GLuint SimpleParticleRender::attrib_quadcorner; - GLuint SimpleParticleRender::attrib_texcoord; - GLuint SimpleParticleRender::attrib_sz; - GLuint SimpleParticleRender::uniform_matrix; - GLuint SimpleParticleRender::uniform_viewmatrix; - GLuint SimpleParticleRender::uniform_tex; - GLuint SimpleParticleRender::uniform_dtex; - GLuint SimpleParticleRender::uniform_invproj; - GLuint SimpleParticleRender::uniform_color_from; - GLuint SimpleParticleRender::uniform_color_to; - - void SimpleParticleRender::init() + SimpleParticleRender::SimpleParticleRender() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/particle.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getPosFromUVDepth.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/particle.frag").c_str()); - attrib_pos = glGetAttribLocation(Program, "position"); - attrib_sz = glGetAttribLocation(Program, "size"); - attrib_lf = glGetAttribLocation(Program, "lifetime"); - attrib_quadcorner = glGetAttribLocation(Program, "quadcorner"); - attrib_texcoord = glGetAttribLocation(Program, "texcoord"); + AssignUniforms("color_from", "color_to"); - - uniform_matrix = glGetUniformLocation(Program, "ProjectionMatrix"); - uniform_viewmatrix = glGetUniformLocation(Program, "ViewMatrix"); - uniform_tex = glGetUniformLocation(Program, "tex"); - uniform_invproj = glGetUniformLocation(Program, "invproj"); - uniform_dtex = glGetUniformLocation(Program, "dtex"); - uniform_color_from = glGetUniformLocation(Program, "color_from"); - assert(uniform_color_from != -1); - uniform_color_to = glGetUniformLocation(Program, "color_to"); - assert(uniform_color_to != -1); + TU_tex = 0; + TU_dtex = 1; + AssignTextureUnit(Program, TexUnit(TU_tex, "tex"), TexUnit(TU_dtex, "dtex")); } - void SimpleParticleRender::setUniforms(const core::matrix4 &ViewMatrix, const core::matrix4 &ProjMatrix, - const core::matrix4 InvProjMatrix, float width, float height, unsigned TU_tex, unsigned TU_dtex, - const ParticleSystemProxy* particle_system) - { - glUniformMatrix4fv(uniform_invproj, 1, GL_FALSE, InvProjMatrix.pointer()); - 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_dtex, TU_dtex); - - const float* color_from = particle_system->getColorFrom(); - const float* color_to = particle_system->getColorTo(); - glUniform3f(uniform_color_from, color_from[0], color_from[1], color_from[2]); - glUniform3f(uniform_color_to, color_to[0], color_to[1], color_to[2]); - } - - GLuint FlipParticleRender::Program; - GLuint FlipParticleRender::attrib_pos; - GLuint FlipParticleRender::attrib_lf; - GLuint FlipParticleRender::attrib_quadcorner; - GLuint FlipParticleRender::attrib_texcoord; - GLuint FlipParticleRender::attrib_sz; - GLuint FlipParticleRender::attrib_rotationvec; - GLuint FlipParticleRender::attrib_anglespeed; - GLuint FlipParticleRender::uniform_matrix; - GLuint FlipParticleRender::uniform_viewmatrix; - GLuint FlipParticleRender::uniform_tex; - GLuint FlipParticleRender::uniform_dtex; - GLuint FlipParticleRender::uniform_invproj; - - void FlipParticleRender::init() + FlipParticleRender::FlipParticleRender() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/flipparticle.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getPosFromUVDepth.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/particle.frag").c_str()); - attrib_pos = glGetAttribLocation(Program, "position"); - attrib_sz = glGetAttribLocation(Program, "size"); - attrib_lf = glGetAttribLocation(Program, "lifetime"); - attrib_quadcorner = glGetAttribLocation(Program, "quadcorner"); - attrib_texcoord = glGetAttribLocation(Program, "texcoord"); - attrib_anglespeed = glGetAttribLocation(Program, "anglespeed"); - attrib_rotationvec = glGetAttribLocation(Program, "rotationvec"); + AssignUniforms(); - uniform_matrix = glGetUniformLocation(Program, "ProjectionMatrix"); - uniform_viewmatrix = glGetUniformLocation(Program, "ViewMatrix"); - uniform_tex = glGetUniformLocation(Program, "tex"); - uniform_invproj = glGetUniformLocation(Program, "invproj"); - uniform_dtex = glGetUniformLocation(Program, "dtex"); - } - - void FlipParticleRender::setUniforms(const core::matrix4 &ViewMatrix, const core::matrix4 &ProjMatrix, const core::matrix4 InvProjMatrix, float width, float height, unsigned TU_tex, unsigned TU_dtex) - { - glUniformMatrix4fv(uniform_invproj, 1, GL_FALSE, InvProjMatrix.pointer()); - 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_dtex, TU_dtex); + TU_tex = 0; + TU_dtex = 1; + AssignTextureUnit(Program, TexUnit(TU_tex, "tex"), TexUnit(TU_dtex, "dtex")); } } diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index 639b13617..e6dda115b 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -509,28 +509,20 @@ public: static void init(); }; -class SimpleParticleRender +class SimpleParticleRender : public ShaderHelperSingleton { public: - static GLuint Program; - static GLuint attrib_pos, attrib_lf, attrib_quadcorner, attrib_texcoord, attrib_sz; - static GLuint uniform_matrix, uniform_viewmatrix, uniform_tex, uniform_dtex, uniform_invproj, uniform_color_from, uniform_color_to; + GLuint TU_tex, TU_dtex; - 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, const ParticleSystemProxy* particle_system); + SimpleParticleRender(); }; -class FlipParticleRender +class FlipParticleRender : public ShaderHelperSingleton { public: - static GLuint Program; - static GLuint attrib_pos, attrib_lf, attrib_quadcorner, attrib_texcoord, attrib_sz, attrib_rotationvec, attrib_anglespeed; - static GLuint uniform_matrix, uniform_viewmatrix, uniform_tex, uniform_dtex, uniform_invproj; + GLuint TU_tex, TU_dtex; - 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); + FlipParticleRender(); }; } From 013e8234085917cb620266a8e3cdd85adf1c2aab Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 21:13:28 +0200 Subject: [PATCH 11/26] Extract ParticleVBO from gpuparticle.cpp --- src/graphics/gpuparticles.cpp | 17 +---------------- src/graphics/gpuparticles.hpp | 2 -- src/graphics/shaders.cpp | 17 +++++++++++++++++ src/graphics/shaders.hpp | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index c2c85e54b..97f828edc 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -24,8 +24,6 @@ scene::IParticleSystemSceneNode *ParticleSystemProxy::addParticleNode( return node; } -GLuint ParticleSystemProxy::quad_vertex_buffer = 0; - ParticleSystemProxy::ParticleSystemProxy(bool createDefaultEmitter, ISceneNode* parent, scene::ISceneManager* mgr, s32 id, const core::vector3df& position, @@ -54,19 +52,6 @@ ParticleSystemProxy::ParticleSystemProxy(bool createDefaultEmitter, track_x_len = 0; track_z_len = 0; texture = 0; - - if (quad_vertex_buffer) - return; - static const GLfloat quad_vertex[] = { - -.5, -.5, 0., 0., - .5, -.5, 1., 0., - -.5, .5, 0., 1., - .5, .5, 1., 1., - }; - glGenBuffers(1, &quad_vertex_buffer); - glBindBuffer(GL_ARRAY_BUFFER, quad_vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertex), quad_vertex, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); } ParticleSystemProxy::~ParticleSystemProxy() @@ -263,7 +248,7 @@ static bool isGPUParticleType(scene::E_PARTICLE_EMITTER_TYPE type) void ParticleSystemProxy::SimpleParticleVAOBind(GLuint PositionBuffer) { - glBindBuffer(GL_ARRAY_BUFFER, quad_vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, SharedObject::ParticleQuadVBO); glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); glEnableVertexAttribArray(3); diff --git a/src/graphics/gpuparticles.hpp b/src/graphics/gpuparticles.hpp index b1db6b927..0490ddfa1 100644 --- a/src/graphics/gpuparticles.hpp +++ b/src/graphics/gpuparticles.hpp @@ -20,8 +20,6 @@ protected: float m_color_from[3]; float m_color_to[3]; - static GLuint quad_vertex_buffer; - GLuint texture; unsigned count; static void SimpleParticleVAOBind(GLuint PositionBuffer); diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 7c477bcf0..ac66f7288 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -298,6 +298,22 @@ static void initShadowVPMUBO() glBindBuffer(GL_UNIFORM_BUFFER, 0); } +GLuint SharedObject::ParticleQuadVBO = 0; + +static void initParticleQuadVBO() +{ + static const GLfloat quad_vertex[] = { + -.5, -.5, 0., 0., + .5, -.5, 1., 0., + -.5, .5, 0., 1., + .5, .5, 1., 1., + }; + glGenBuffers(1, &SharedObject::ParticleQuadVBO); + glBindBuffer(GL_ARRAY_BUFFER, SharedObject::ParticleQuadVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertex), quad_vertex, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + void Shaders::loadShaders() { const std::string &dir = file_manager->getAsset(FileManager::SHADER, ""); @@ -392,6 +408,7 @@ void Shaders::loadShaders() initCubeVBO(); initFrustrumVBO(); initShadowVPMUBO(); + initParticleQuadVBO(); FullScreenShader::DiffuseEnvMapShader::init(); MeshShader::BubbleShader::init(); LightShader::PointLightShader::init(); diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index e6dda115b..1d7339699 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -31,7 +31,7 @@ class SharedObject { public: static GLuint billboardvbo; - static GLuint cubevbo, cubeindexes, frustrumvbo, frustrumindexes; + static GLuint cubevbo, cubeindexes, frustrumvbo, frustrumindexes, ParticleQuadVBO; static GLuint ViewProjectionMatrixesUBO; static GLuint FullScreenQuadVAO; static GLuint UIVAO; From d1f46fdb7fd67fde654a369ad6d8c200e28bd4c8 Mon Sep 17 00:00:00 2001 From: samuncle Date: Wed, 13 Aug 2014 21:30:27 +0200 Subject: [PATCH 12/26] Bring back the rain --- data/gfx/rain.xml | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/data/gfx/rain.xml b/data/gfx/rain.xml index aa1f775bd..4aeff8e37 100644 --- a/data/gfx/rain.xml +++ b/data/gfx/rain.xml @@ -1,39 +1,33 @@ - - - + + + + - + - + - + - + - - + + - + max="255 255 255" /> + - + + - From b5f89501f6be5bd9592243043c01bb748fff4d55 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 21:38:33 +0200 Subject: [PATCH 13/26] Factorize some GL code in gpuparticle. --- src/graphics/gpuparticles.cpp | 124 ++++++++++++++-------------------- src/graphics/gpuparticles.hpp | 17 +++++ 2 files changed, 68 insertions(+), 73 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index 97f828edc..7b8e9e322 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -36,6 +36,8 @@ ParticleSystemProxy::ParticleSystemProxy(bool createDefaultEmitter, glGenVertexArrays(1, ¤t_rendering_vao); glGenVertexArrays(1, &non_current_rendering_vao); size_increase_factor = 0.; + ParticleParams = nullptr; + InitialValues = nullptr; m_color_from[0] = m_color_from[1] = m_color_from[2] = 1.0; m_color_to[0] = m_color_to[1] = m_color_to[2] = 1.0; @@ -56,6 +58,10 @@ ParticleSystemProxy::ParticleSystemProxy(bool createDefaultEmitter, ParticleSystemProxy::~ParticleSystemProxy() { + if (InitialValues) + delete InitialValues; + if (ParticleParams) + delete ParticleParams; glDeleteBuffers(2, tfb_buffers); glDeleteBuffers(1, &initial_values_buffer); if (quaternionsbuffer) @@ -122,51 +128,32 @@ void generateLifetimeSizeDirection(scene::IParticleEmitter *emitter, float &life dirZ = particledir.Z; } -struct ParticleData -{ - float PositionX; - float PositionY; - float PositionZ; - float Lifetime; - float DirectionX; - float DirectionY; - float DirectionZ; - float Size; -}; - void ParticleSystemProxy::generateParticlesFromPointEmitter(scene::IParticlePointEmitter *emitter) { - ParticleData *particles = new ParticleData[count], *initialvalue = new ParticleData[count]; + ParticleParams = (ParticleData *) realloc(ParticleParams, sizeof(ParticleData) * count); + InitialValues = (ParticleData *)realloc(InitialValues, sizeof(ParticleData)* count); for (unsigned i = 0; i < count; i++) { - particles[i].PositionX = 0; - particles[i].PositionY = 0; - particles[i].PositionZ = 0; + ParticleParams[i].PositionX = 0; + ParticleParams[i].PositionY = 0; + ParticleParams[i].PositionZ = 0; // Initial lifetime is >1 - particles[i].Lifetime = 2.; + InitialValues[i].Lifetime = 2.; - memcpy(&(initialvalue[i].PositionX), &(particles[i].PositionX), 3 * sizeof(float)); + memcpy(&(InitialValues[i].PositionX), &(ParticleParams[i].PositionX), 3 * sizeof(float)); - generateLifetimeSizeDirection(emitter, initialvalue[i].Lifetime, initialvalue[i].Size, - initialvalue[i].DirectionX, initialvalue[i].DirectionY, initialvalue[i].DirectionZ); + generateLifetimeSizeDirection(emitter, ParticleParams[i].Lifetime, ParticleParams[i].Size, + ParticleParams[i].DirectionX, ParticleParams[i].DirectionY, ParticleParams[i].DirectionZ); - memcpy(&(particles[i].DirectionX), &(initialvalue[i].DirectionX), 4 * sizeof(float)); + memcpy(&(InitialValues[i].DirectionX), &(ParticleParams[i].DirectionX), 4 * sizeof(float)); } - - glBindBuffer(GL_ARRAY_BUFFER, initial_values_buffer); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), initialvalue, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[0]); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), particles, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[1]); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), 0, GL_STREAM_DRAW); - delete[] particles; - delete[] initialvalue; } void ParticleSystemProxy::generateParticlesFromBoxEmitter(scene::IParticleBoxEmitter *emitter) { - ParticleData *particles = new ParticleData[count], *initialvalue = new ParticleData[count]; + ParticleParams = (ParticleData *)realloc(ParticleParams, sizeof(ParticleData)* count); + InitialValues = (ParticleData *)realloc(InitialValues, sizeof(ParticleData)* count); const core::vector3df& extent = emitter->getBox().getExtent(); @@ -174,33 +161,26 @@ void ParticleSystemProxy::generateParticlesFromBoxEmitter(scene::IParticleBoxEmi for (unsigned i = 0; i < count; i++) { - particles[i].PositionX = emitter->getBox().MinEdge.X + os::Randomizer::frand() * extent.X; - particles[i].PositionY = emitter->getBox().MinEdge.Y + os::Randomizer::frand() * extent.Y; - particles[i].PositionZ = emitter->getBox().MinEdge.Z + os::Randomizer::frand() * extent.Z; + ParticleParams[i].PositionX = emitter->getBox().MinEdge.X + os::Randomizer::frand() * extent.X; + ParticleParams[i].PositionY = emitter->getBox().MinEdge.Y + os::Randomizer::frand() * extent.Y; + ParticleParams[i].PositionZ = emitter->getBox().MinEdge.Z + os::Randomizer::frand() * extent.Z; // Initial lifetime is random - particles[i].Lifetime = os::Randomizer::frand(); + InitialValues[i].Lifetime = os::Randomizer::frand(); - memcpy(&(initialvalue[i].PositionX), &(particles[i].PositionX), 3 * sizeof(float)); - generateLifetimeSizeDirection(emitter, initialvalue[i].Lifetime, initialvalue[i].Size, - initialvalue[i].DirectionX, initialvalue[i].DirectionY, initialvalue[i].DirectionZ); - memcpy(&(particles[i].DirectionX), &(initialvalue[i].DirectionX), 4 * sizeof(float)); + memcpy(&(InitialValues[i].PositionX), &(ParticleParams[i].PositionX), 3 * sizeof(float)); + generateLifetimeSizeDirection(emitter, ParticleParams[i].Lifetime, ParticleParams[i].Size, + ParticleParams[i].DirectionX, ParticleParams[i].DirectionY, ParticleParams[i].DirectionZ); + memcpy(&(InitialValues[i].DirectionX), &(ParticleParams[i].DirectionX), 4 * sizeof(float)); if (randomize_initial_y) - particles[i].PositionY = os::Randomizer::frand()*50.0f; // -100.0f; + InitialValues[i].PositionY = os::Randomizer::frand()*50.0f; // -100.0f; } - glBindBuffer(GL_ARRAY_BUFFER, initial_values_buffer); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), initialvalue, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[0]); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), particles, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[1]); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), 0, GL_STREAM_DRAW); - delete[] particles; - delete[] initialvalue; } void ParticleSystemProxy::generateParticlesFromSphereEmitter(scene::IParticleSphereEmitter *emitter) { - ParticleData *particles = new ParticleData[count], *initialvalue = new ParticleData[count]; + ParticleParams = (ParticleData *)realloc(ParticleParams, sizeof(ParticleData)* count); + InitialValues = (ParticleData *)realloc(InitialValues, sizeof(ParticleData)* count); for (unsigned i = 0; i < count; i++) { // Random distance from center @@ -212,25 +192,17 @@ void ParticleSystemProxy::generateParticlesFromSphereEmitter(scene::IParticleSph pos.rotateYZBy(os::Randomizer::frand() * 360.f, emitter->getCenter()); pos.rotateXZBy(os::Randomizer::frand() * 360.f, emitter->getCenter()); - particles[i].PositionX = pos.X; - particles[i].PositionY = pos.Y; - particles[i].PositionZ = pos.Z; + ParticleParams[i].PositionX = pos.X; + ParticleParams[i].PositionY = pos.Y; + ParticleParams[i].PositionZ = pos.Z; // Initial lifetime is > 1 - particles[i].Lifetime = 2.; + InitialValues[i].Lifetime = 2.; - memcpy(&(initialvalue[i].PositionX), &(particles[i].PositionX), 3 * sizeof(float)); - generateLifetimeSizeDirection(emitter, initialvalue[i].Lifetime, initialvalue[i].Size, - initialvalue[i].DirectionX, initialvalue[i].DirectionY, initialvalue[i].DirectionZ); - memcpy(&(particles[i].DirectionX), &(initialvalue[i].DirectionX), 4 * sizeof(float)); + memcpy(&(InitialValues[i].PositionX), &(ParticleParams[i].PositionX), 3 * sizeof(float)); + generateLifetimeSizeDirection(emitter, ParticleParams[i].Lifetime, ParticleParams[i].Size, + ParticleParams[i].DirectionX, ParticleParams[i].DirectionY, ParticleParams[i].DirectionZ); + memcpy(&(InitialValues[i].DirectionX), &(ParticleParams[i].DirectionX), 4 * sizeof(float)); } - glBindBuffer(GL_ARRAY_BUFFER, initial_values_buffer); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), initialvalue, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[0]); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), particles, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[1]); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), 0, GL_STREAM_DRAW); - delete[] particles; - delete[] initialvalue; } static bool isGPUParticleType(scene::E_PARTICLE_EMITTER_TYPE type) @@ -285,26 +257,26 @@ void setSimulationBind(GLuint position_vbo, GLuint initialValues_vbo) { glBindBuffer(GL_ARRAY_BUFFER, position_vbo); glEnableVertexAttribArray(T::attrib_position); - glVertexAttribPointer(T::attrib_position, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid*)0); + glVertexAttribPointer(T::attrib_position, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)0); glEnableVertexAttribArray(T::attrib_lifetime); - glVertexAttribPointer(T::attrib_lifetime, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid*)(3 * sizeof(float))); + glVertexAttribPointer(T::attrib_lifetime, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(3 * sizeof(float))); glEnableVertexAttribArray(T::attrib_velocity); - glVertexAttribPointer(T::attrib_velocity, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid*)(4 * sizeof(float))); + glVertexAttribPointer(T::attrib_velocity, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(4 * sizeof(float))); if (T::attrib_size < 30) { glEnableVertexAttribArray(T::attrib_size); - glVertexAttribPointer(T::attrib_size, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid*)(7 * sizeof(float))); + glVertexAttribPointer(T::attrib_size, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(7 * sizeof(float))); } glBindBuffer(GL_ARRAY_BUFFER, initialValues_vbo); glEnableVertexAttribArray(T::attrib_initial_position); - glVertexAttribPointer(T::attrib_initial_position, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid*)0); + glVertexAttribPointer(T::attrib_initial_position, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)0); glEnableVertexAttribArray(T::attrib_initial_lifetime); - glVertexAttribPointer(T::attrib_initial_lifetime, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid*)(3 * sizeof(float))); + glVertexAttribPointer(T::attrib_initial_lifetime, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(3 * sizeof(float))); glEnableVertexAttribArray(T::attrib_initial_velocity); - glVertexAttribPointer(T::attrib_initial_velocity, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid*)(4 * sizeof(float))); + glVertexAttribPointer(T::attrib_initial_velocity, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(4 * sizeof(float))); glEnableVertexAttribArray(T::attrib_initial_size); - glVertexAttribPointer(T::attrib_initial_size, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (GLvoid*)(7 * sizeof(float))); + glVertexAttribPointer(T::attrib_initial_size, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(7 * sizeof(float))); } void ParticleSystemProxy::SimpleSimulationBind(GLuint PositionBuffer, GLuint InitialValuesBuffer) @@ -342,6 +314,12 @@ void ParticleSystemProxy::setEmitter(scene::IParticleEmitter* emitter) assert(0 && "Wrong particle type"); } + glBindBuffer(GL_ARRAY_BUFFER, initial_values_buffer); + glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), ParticleParams, GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[0]); + glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), InitialValues, GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[1]); + glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), 0, GL_STREAM_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); video::ITexture *tex = getMaterial(0).getTexture(0); diff --git a/src/graphics/gpuparticles.hpp b/src/graphics/gpuparticles.hpp index 0490ddfa1..574aa9cb5 100644 --- a/src/graphics/gpuparticles.hpp +++ b/src/graphics/gpuparticles.hpp @@ -35,6 +35,23 @@ protected: void drawNotFlip(); virtual void simulate(); virtual void draw(); + +public: + struct ParticleData + { + float PositionX; + float PositionY; + float PositionZ; + float Lifetime; + float DirectionX; + float DirectionY; + float DirectionZ; + float Size; + }; + +private: + + ParticleData *ParticleParams, *InitialValues; void generateParticlesFromPointEmitter(scene::IParticlePointEmitter *); void generateParticlesFromBoxEmitter(scene::IParticleBoxEmitter *); void generateParticlesFromSphereEmitter(scene::IParticleSphereEmitter *); From f43538aad7d94d195a6b8dc191eacc88a0e9edb0 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 21:59:57 +0200 Subject: [PATCH 14/26] Factorize again code in gpuparticle. --- src/graphics/gpuparticles.cpp | 122 ++++++++++++++++++---------------- src/graphics/gpuparticles.hpp | 6 +- 2 files changed, 69 insertions(+), 59 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index 7b8e9e322..faa36023e 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -28,25 +28,19 @@ ParticleSystemProxy::ParticleSystemProxy(bool createDefaultEmitter, ISceneNode* parent, scene::ISceneManager* mgr, s32 id, const core::vector3df& position, const core::vector3df& rotation, - const core::vector3df& scale) : CParticleSystemSceneNode(createDefaultEmitter, parent, mgr, id, position, rotation, scale), m_alpha_additive(false) + const core::vector3df& scale) : CParticleSystemSceneNode(createDefaultEmitter, parent, mgr, id, position, rotation, scale), m_alpha_additive(false), m_first_execution(true) { - glGenBuffers(1, &initial_values_buffer); - glGenBuffers(2, tfb_buffers); glGenBuffers(1, &quaternionsbuffer); - glGenVertexArrays(1, ¤t_rendering_vao); - glGenVertexArrays(1, &non_current_rendering_vao); size_increase_factor = 0.; ParticleParams = nullptr; InitialValues = nullptr; m_color_from[0] = m_color_from[1] = m_color_from[2] = 1.0; m_color_to[0] = m_color_to[1] = m_color_to[2] = 1.0; - - + // We set these later but avoid coverity report them heighmapbuffer = 0; heightmaptexture = 0; - current_simulation_vao = 0; has_height_map = false; flip = false; track_x = 0; @@ -62,8 +56,8 @@ ParticleSystemProxy::~ParticleSystemProxy() delete InitialValues; if (ParticleParams) delete ParticleParams; - glDeleteBuffers(2, tfb_buffers); - glDeleteBuffers(1, &initial_values_buffer); + if (!m_first_execution) + cleanGL(); if (quaternionsbuffer) glDeleteBuffers(1, &quaternionsbuffer); if (heighmapbuffer) @@ -218,7 +212,49 @@ static bool isGPUParticleType(scene::E_PARTICLE_EMITTER_TYPE type) } } -void ParticleSystemProxy::SimpleParticleVAOBind(GLuint PositionBuffer) +void ParticleSystemProxy::setEmitter(scene::IParticleEmitter* emitter) +{ + CParticleSystemSceneNode::setEmitter(emitter); + if (!emitter || !isGPUParticleType(emitter->getType())) + return; + has_height_map = false; + flip = false; + if (!m_first_execution) + cleanGL(); + m_first_execution = true; + + count = emitter->getMaxParticlesPerSecond() * emitter->getMaxLifeTime() / 1000; + switch (emitter->getType()) + { + case scene::EPET_POINT: + generateParticlesFromPointEmitter(emitter); + break; + case scene::EPET_BOX: + generateParticlesFromBoxEmitter(static_cast(emitter)); + break; + case scene::EPET_SPHERE: + generateParticlesFromSphereEmitter(static_cast(emitter)); + break; + default: + assert(0 && "Wrong particle type"); + } + + video::ITexture *tex = getMaterial(0).getTexture(0); + compressTexture(tex, true, true); + texture = getTextureGLuint(getMaterial(0).getTexture(0)); +} + +void ParticleSystemProxy::cleanGL() +{ + glDeleteBuffers(2, tfb_buffers); + glDeleteBuffers(1, &initial_values_buffer); + glDeleteVertexArrays(1, ¤t_rendering_vao); + glDeleteVertexArrays(1, &non_current_rendering_vao); + glDeleteVertexArrays(1, ¤t_simulation_vao); + glDeleteVertexArrays(1, &non_current_simulation_vao); +} + +void ParticleSystemProxy::CommonRenderingVAO(GLuint PositionBuffer) { glBindBuffer(GL_ARRAY_BUFFER, SharedObject::ParticleQuadVBO); glEnableVertexAttribArray(4); @@ -238,9 +274,8 @@ void ParticleSystemProxy::SimpleParticleVAOBind(GLuint PositionBuffer) glVertexAttribDivisor(2, 1); } -void ParticleSystemProxy::FlipParticleVAOBind(GLuint PositionBuffer, GLuint QuaternionBuffer) +void ParticleSystemProxy::AppendQuaternionRenderingVAO(GLuint QuaternionBuffer) { - SimpleParticleVAOBind(PositionBuffer); glBindBuffer(GL_ARRAY_BUFFER, QuaternionBuffer); glEnableVertexAttribArray(5); @@ -290,43 +325,6 @@ void ParticleSystemProxy::HeightmapSimulationBind(GLuint PositionBuffer, GLuint } -void ParticleSystemProxy::setEmitter(scene::IParticleEmitter* emitter) -{ - CParticleSystemSceneNode::setEmitter(emitter); - if (!emitter || !isGPUParticleType(emitter->getType())) - return; - has_height_map = false; - flip = false; - - count = emitter->getMaxParticlesPerSecond() * emitter->getMaxLifeTime() / 1000; - switch (emitter->getType()) - { - case scene::EPET_POINT: - generateParticlesFromPointEmitter(emitter); - break; - case scene::EPET_BOX: - generateParticlesFromBoxEmitter(static_cast(emitter)); - break; - case scene::EPET_SPHERE: - generateParticlesFromSphereEmitter(static_cast(emitter)); - break; - default: - assert(0 && "Wrong particle type"); - } - - glBindBuffer(GL_ARRAY_BUFFER, initial_values_buffer); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), ParticleParams, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[0]); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), InitialValues, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[1]); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), 0, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - video::ITexture *tex = getMaterial(0).getTexture(0); - compressTexture(tex, true, true); - texture = getTextureGLuint(getMaterial(0).getTexture(0)); -} - void ParticleSystemProxy::simulateHeightmap() { int timediff = int(GUIEngine::getLatestDt() * 1000.f); @@ -439,6 +437,17 @@ void ParticleSystemProxy::draw() void ParticleSystemProxy::generateVAOs() { + glBindVertexArray(0); + glGenBuffers(1, &initial_values_buffer); + glBindBuffer(GL_ARRAY_BUFFER, initial_values_buffer); + glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), ParticleParams, GL_STREAM_DRAW); + glGenBuffers(2, tfb_buffers); + glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[0]); + glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), InitialValues, GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, tfb_buffers[1]); + glBufferData(GL_ARRAY_BUFFER, count * sizeof(ParticleData), 0, GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glGenVertexArrays(1, ¤t_rendering_vao); glGenVertexArrays(1, &non_current_rendering_vao); glGenVertexArrays(1, ¤t_simulation_vao); @@ -474,16 +483,14 @@ void ParticleSystemProxy::generateVAOs() } glBindVertexArray(current_rendering_vao); + CommonRenderingVAO(tfb_buffers[0]); if (flip) - FlipParticleVAOBind(tfb_buffers[0], quaternionsbuffer); - else - SimpleParticleVAOBind(tfb_buffers[0]); + AppendQuaternionRenderingVAO(quaternionsbuffer); glBindVertexArray(non_current_rendering_vao); + CommonRenderingVAO(tfb_buffers[1]); if (flip) - FlipParticleVAOBind(tfb_buffers[1], quaternionsbuffer); - else - SimpleParticleVAOBind(tfb_buffers[1]); + AppendQuaternionRenderingVAO(quaternionsbuffer); glBindVertexArray(0); delete[] quaternions; @@ -495,8 +502,9 @@ void ParticleSystemProxy::render() { CParticleSystemSceneNode::render(); return; } - if (!current_rendering_vao || !non_current_rendering_vao || !current_simulation_vao || !non_current_simulation_vao) + if (m_first_execution) generateVAOs(); + m_first_execution = false; simulate(); draw(); } diff --git a/src/graphics/gpuparticles.hpp b/src/graphics/gpuparticles.hpp index 574aa9cb5..cefde46cb 100644 --- a/src/graphics/gpuparticles.hpp +++ b/src/graphics/gpuparticles.hpp @@ -19,15 +19,17 @@ protected: float size_increase_factor, track_x, track_z, track_x_len, track_z_len; float m_color_from[3]; float m_color_to[3]; + bool m_first_execution; GLuint texture; unsigned count; - static void SimpleParticleVAOBind(GLuint PositionBuffer); - static void FlipParticleVAOBind(GLuint PositionBuffer, GLuint QuaternionBuffer); + static void CommonRenderingVAO(GLuint PositionBuffer); + static void AppendQuaternionRenderingVAO(GLuint QuaternionBuffer); static void SimpleSimulationBind(GLuint PositionBuffer, GLuint InitialValuesBuffer); static void HeightmapSimulationBind(GLuint PositionBuffer, GLuint InitialValuesBuffer); void generateVAOs(); + void cleanGL(); void simulateHeightmap(); void simulateNoHeightmap(); From e6eac5b57f6a3fad858e49825a570eb76d8a438f Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 22:02:18 +0200 Subject: [PATCH 15/26] Clean quaternionbuffer at the right place. --- src/graphics/gpuparticles.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index faa36023e..d044b36c7 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -30,7 +30,6 @@ ParticleSystemProxy::ParticleSystemProxy(bool createDefaultEmitter, const core::vector3df& rotation, const core::vector3df& scale) : CParticleSystemSceneNode(createDefaultEmitter, parent, mgr, id, position, rotation, scale), m_alpha_additive(false), m_first_execution(true) { - glGenBuffers(1, &quaternionsbuffer); size_increase_factor = 0.; ParticleParams = nullptr; InitialValues = nullptr; @@ -58,8 +57,6 @@ ParticleSystemProxy::~ParticleSystemProxy() delete ParticleParams; if (!m_first_execution) cleanGL(); - if (quaternionsbuffer) - glDeleteBuffers(1, &quaternionsbuffer); if (heighmapbuffer) glDeleteBuffers(1, &heighmapbuffer); if (heightmaptexture) @@ -217,10 +214,10 @@ void ParticleSystemProxy::setEmitter(scene::IParticleEmitter* emitter) CParticleSystemSceneNode::setEmitter(emitter); if (!emitter || !isGPUParticleType(emitter->getType())) return; - has_height_map = false; - flip = false; if (!m_first_execution) cleanGL(); + has_height_map = false; + flip = false; m_first_execution = true; count = emitter->getMaxParticlesPerSecond() * emitter->getMaxLifeTime() / 1000; @@ -246,6 +243,8 @@ void ParticleSystemProxy::setEmitter(scene::IParticleEmitter* emitter) void ParticleSystemProxy::cleanGL() { + if (flip) + glDeleteBuffers(1, &quaternionsbuffer); glDeleteBuffers(2, tfb_buffers); glDeleteBuffers(1, &initial_values_buffer); glDeleteVertexArrays(1, ¤t_rendering_vao); From 8117260bf6328688bdbd3c27d23838757a9a440d Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 22:26:52 +0200 Subject: [PATCH 16/26] Factorise attribute location of simulation program. --- data/shaders/particlesimheightmap.vert | 16 +++---- data/shaders/pointemitter.vert | 16 +++---- src/graphics/gpuparticles.cpp | 59 +++++++++----------------- src/graphics/gpuparticles.hpp | 3 +- 4 files changed, 36 insertions(+), 58 deletions(-) diff --git a/data/shaders/particlesimheightmap.vert b/data/shaders/particlesimheightmap.vert index b34fb360e..dc5241719 100644 --- a/data/shaders/particlesimheightmap.vert +++ b/data/shaders/particlesimheightmap.vert @@ -9,15 +9,15 @@ uniform float track_x_len; uniform float track_z_len; uniform samplerBuffer heightmap; -in vec3 particle_position_initial; -in float lifetime_initial; -in vec3 particle_velocity_initial; -in float size_initial; +layout (location = 4) in vec3 particle_position_initial; +layout (location = 5) in float lifetime_initial; +layout (location = 6) in vec3 particle_velocity_initial; +layout (location = 7) in float size_initial; -in vec3 particle_position; -in float lifetime; -in vec3 particle_velocity; -in float size; +layout (location = 0) in vec3 particle_position; +layout (location = 1) in float lifetime; +layout (location = 2) in vec3 particle_velocity; +layout (location = 3) in float size; out vec3 new_particle_position; out float new_lifetime; diff --git a/data/shaders/pointemitter.vert b/data/shaders/pointemitter.vert index 333673f27..003c128d6 100644 --- a/data/shaders/pointemitter.vert +++ b/data/shaders/pointemitter.vert @@ -3,15 +3,15 @@ uniform mat4 sourcematrix; uniform int level; uniform float size_increase_factor; -in vec3 particle_position_initial; -in float lifetime_initial; -in vec3 particle_velocity_initial; -in float size_initial; +layout (location = 4) in vec3 particle_position_initial; +layout (location = 5) in float lifetime_initial; +layout (location = 6) in vec3 particle_velocity_initial; +layout (location = 7) in float size_initial; -in vec3 particle_position; -in float lifetime; -in vec3 particle_velocity; -in float size; +layout (location = 0) in vec3 particle_position; +layout (location = 1) in float lifetime; +layout (location = 2) in vec3 particle_velocity; +layout (location = 3) in float size; out vec3 new_particle_position; out float new_lifetime; diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index d044b36c7..ceea9bbe7 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -286,44 +286,29 @@ void ParticleSystemProxy::AppendQuaternionRenderingVAO(GLuint QuaternionBuffer) glVertexAttribDivisor(6, 1); } -template -void setSimulationBind(GLuint position_vbo, GLuint initialValues_vbo) +void ParticleSystemProxy::CommonSimulationVAO(GLuint position_vbo, GLuint initialValues_vbo) { glBindBuffer(GL_ARRAY_BUFFER, position_vbo); - glEnableVertexAttribArray(T::attrib_position); - glVertexAttribPointer(T::attrib_position, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)0); - glEnableVertexAttribArray(T::attrib_lifetime); - glVertexAttribPointer(T::attrib_lifetime, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(3 * sizeof(float))); - glEnableVertexAttribArray(T::attrib_velocity); - glVertexAttribPointer(T::attrib_velocity, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(4 * sizeof(float))); - if (T::attrib_size < 30) - { - glEnableVertexAttribArray(T::attrib_size); - glVertexAttribPointer(T::attrib_size, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(7 * sizeof(float))); - } + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(3 * sizeof(float))); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(4 * sizeof(float))); + glEnableVertexAttribArray(3); + glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(7 * sizeof(float))); glBindBuffer(GL_ARRAY_BUFFER, initialValues_vbo); - glEnableVertexAttribArray(T::attrib_initial_position); - glVertexAttribPointer(T::attrib_initial_position, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)0); - glEnableVertexAttribArray(T::attrib_initial_lifetime); - glVertexAttribPointer(T::attrib_initial_lifetime, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(3 * sizeof(float))); - glEnableVertexAttribArray(T::attrib_initial_velocity); - glVertexAttribPointer(T::attrib_initial_velocity, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(4 * sizeof(float))); - glEnableVertexAttribArray(T::attrib_initial_size); - glVertexAttribPointer(T::attrib_initial_size, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(7 * sizeof(float))); + glEnableVertexAttribArray(4); + glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)0); + glEnableVertexAttribArray(5); + glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(3 * sizeof(float))); + glEnableVertexAttribArray(6); + glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(4 * sizeof(float))); + glEnableVertexAttribArray(7); + glVertexAttribPointer(7, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(7 * sizeof(float))); } -void ParticleSystemProxy::SimpleSimulationBind(GLuint PositionBuffer, GLuint InitialValuesBuffer) -{ - setSimulationBind(PositionBuffer, InitialValuesBuffer); -} - -void ParticleSystemProxy::HeightmapSimulationBind(GLuint PositionBuffer, GLuint InitialValuesBuffer) -{ - setSimulationBind(PositionBuffer, InitialValuesBuffer); -} - - void ParticleSystemProxy::simulateHeightmap() { int timediff = int(GUIEngine::getLatestDt() * 1000.f); @@ -453,15 +438,9 @@ void ParticleSystemProxy::generateVAOs() glGenVertexArrays(1, &non_current_simulation_vao); glBindVertexArray(current_simulation_vao); - if (has_height_map) - HeightmapSimulationBind(tfb_buffers[0], initial_values_buffer); - else - SimpleSimulationBind(tfb_buffers[0], initial_values_buffer); + CommonSimulationVAO(tfb_buffers[0], initial_values_buffer); glBindVertexArray(non_current_simulation_vao); - if (has_height_map) - HeightmapSimulationBind(tfb_buffers[1], initial_values_buffer); - else - SimpleSimulationBind(tfb_buffers[1], initial_values_buffer); + CommonSimulationVAO(tfb_buffers[1], initial_values_buffer); float *quaternions = new float[4 * count]; glBindVertexArray(0); diff --git a/src/graphics/gpuparticles.hpp b/src/graphics/gpuparticles.hpp index cefde46cb..91527bacc 100644 --- a/src/graphics/gpuparticles.hpp +++ b/src/graphics/gpuparticles.hpp @@ -25,8 +25,7 @@ protected: unsigned count; static void CommonRenderingVAO(GLuint PositionBuffer); static void AppendQuaternionRenderingVAO(GLuint QuaternionBuffer); - static void SimpleSimulationBind(GLuint PositionBuffer, GLuint InitialValuesBuffer); - static void HeightmapSimulationBind(GLuint PositionBuffer, GLuint InitialValuesBuffer); + static void CommonSimulationVAO(GLuint position_vbo, GLuint initialValues_vbo); void generateVAOs(); void cleanGL(); From 47cfe08fe14980b04b5990b9d042af06b84990d3 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 22:32:15 +0200 Subject: [PATCH 17/26] Port simple simulation particle shader. --- src/graphics/gpuparticles.cpp | 7 ++----- src/graphics/shaders.cpp | 32 ++------------------------------ src/graphics/shaders.hpp | 8 ++------ 3 files changed, 6 insertions(+), 41 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index ceea9bbe7..5264c7a30 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -348,13 +348,10 @@ void ParticleSystemProxy::simulateNoHeightmap() int timediff = int(GUIEngine::getLatestDt() * 1000.f); int active_count = getEmitter()->getMaxLifeTime() * getEmitter()->getMaxParticlesPerSecond() / 1000; core::matrix4 matrix = getAbsoluteTransformation(); - glUseProgram(ParticleShader::SimpleSimulationShader::Program); + glUseProgram(ParticleShader::SimpleSimulationShader::getInstance()->Program); glEnable(GL_RASTERIZER_DISCARD); - glUniform1i(ParticleShader::SimpleSimulationShader::uniform_dt, timediff); - glUniform1i(ParticleShader::SimpleSimulationShader::uniform_level, active_count); - glUniformMatrix4fv(ParticleShader::SimpleSimulationShader::uniform_sourcematrix, 1, GL_FALSE, matrix.pointer()); - glUniform1f(ParticleShader::SimpleSimulationShader::uniform_size_increase_factor, size_increase_factor); + ParticleShader::SimpleSimulationShader::getInstance()->setUniforms(matrix, timediff, active_count, size_increase_factor); glBindVertexArray(current_simulation_vao); glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfb_buffers[1]); diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index ac66f7288..b1f53e0d9 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -415,7 +415,6 @@ void Shaders::loadShaders() MeshShader::SkyboxShader::init(); MeshShader::ViewFrustrumShader::init(); ParticleShader::HeightmapSimulationShader::init(); - ParticleShader::SimpleSimulationShader::init(); UtilShader::ColoredLine::init(); } @@ -1377,21 +1376,7 @@ namespace LightShader namespace ParticleShader { - GLuint SimpleSimulationShader::Program; - GLuint SimpleSimulationShader::attrib_position; - GLuint SimpleSimulationShader::attrib_velocity; - GLuint SimpleSimulationShader::attrib_lifetime; - GLuint SimpleSimulationShader::attrib_initial_position; - GLuint SimpleSimulationShader::attrib_initial_velocity; - GLuint SimpleSimulationShader::attrib_initial_lifetime; - GLuint SimpleSimulationShader::attrib_size; - GLuint SimpleSimulationShader::attrib_initial_size; - GLuint SimpleSimulationShader::uniform_sourcematrix; - GLuint SimpleSimulationShader::uniform_dt; - GLuint SimpleSimulationShader::uniform_level; - GLuint SimpleSimulationShader::uniform_size_increase_factor; - - void SimpleSimulationShader::init() + SimpleSimulationShader::SimpleSimulationShader() { const char *varyings[] = { "new_particle_position", @@ -1400,20 +1385,7 @@ namespace ParticleShader "new_size", }; Program = LoadTFBProgram(file_manager->getAsset("shaders/pointemitter.vert").c_str(), varyings, 4); - - uniform_dt = glGetUniformLocation(Program, "dt"); - uniform_sourcematrix = glGetUniformLocation(Program, "sourcematrix"); - uniform_level = glGetUniformLocation(Program, "level"); - uniform_size_increase_factor = glGetUniformLocation(Program, "size_increase_factor"); - - attrib_position = glGetAttribLocation(Program, "particle_position"); - attrib_lifetime = glGetAttribLocation(Program, "lifetime"); - attrib_velocity = glGetAttribLocation(Program, "particle_velocity"); - attrib_size = glGetAttribLocation(Program, "size"); - attrib_initial_position = glGetAttribLocation(Program, "particle_position_initial"); - attrib_initial_lifetime = glGetAttribLocation(Program, "lifetime_initial"); - attrib_initial_velocity = glGetAttribLocation(Program, "particle_velocity_initial"); - attrib_initial_size = glGetAttribLocation(Program, "size_initial"); + AssignUniforms("sourcematrix", "dt", "level", "size_increase_factor"); } GLuint HeightmapSimulationShader::Program; diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index 1d7339699..974e250a6 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -486,14 +486,10 @@ namespace LightShader namespace ParticleShader { -class SimpleSimulationShader +class SimpleSimulationShader : public ShaderHelperSingleton { public: - static GLuint Program; - static GLuint attrib_position, attrib_velocity, attrib_lifetime, attrib_initial_position, attrib_initial_velocity, attrib_initial_lifetime, attrib_size, attrib_initial_size; - static GLuint uniform_sourcematrix, uniform_dt, uniform_level, uniform_size_increase_factor; - - static void init(); + SimpleSimulationShader(); }; From 00e945e75ccc19a1505f6aa9df597dfb012ddf6e Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 22:42:02 +0200 Subject: [PATCH 18/26] Port heightmap simulation shader. --- src/graphics/gpuparticles.cpp | 15 ++++-------- src/graphics/shaders.cpp | 45 ++++------------------------------- src/graphics/shaders.hpp | 9 +++---- 3 files changed, 11 insertions(+), 58 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index 5264c7a30..f0727ce15 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -314,20 +314,13 @@ void ParticleSystemProxy::simulateHeightmap() int timediff = int(GUIEngine::getLatestDt() * 1000.f); int active_count = getEmitter()->getMaxLifeTime() * getEmitter()->getMaxParticlesPerSecond() / 1000; core::matrix4 matrix = getAbsoluteTransformation(); - glUseProgram(ParticleShader::HeightmapSimulationShader::Program); + glUseProgram(ParticleShader::HeightmapSimulationShader::getInstance()->Program); glEnable(GL_RASTERIZER_DISCARD); - glUniform1i(ParticleShader::HeightmapSimulationShader::uniform_dt, timediff); - glUniform1i(ParticleShader::HeightmapSimulationShader::uniform_level, active_count); - glUniformMatrix4fv(ParticleShader::HeightmapSimulationShader::uniform_sourcematrix, 1, GL_FALSE, matrix.pointer()); - glUniform1f(ParticleShader::HeightmapSimulationShader::uniform_size_increase_factor, size_increase_factor); - glActiveTexture(GL_TEXTURE2); + glActiveTexture(GL_TEXTURE0 + ParticleShader::HeightmapSimulationShader::getInstance()->TU_heightmap); glBindTexture(GL_TEXTURE_BUFFER, heightmaptexture); - glUniform1i(ParticleShader::HeightmapSimulationShader::uniform_heightmap, 2); - glUniform1f(ParticleShader::HeightmapSimulationShader::uniform_track_x, track_x); - glUniform1f(ParticleShader::HeightmapSimulationShader::uniform_track_z, track_z); - glUniform1f(ParticleShader::HeightmapSimulationShader::uniform_track_x_len, track_x_len); - glUniform1f(ParticleShader::HeightmapSimulationShader::uniform_track_z_len, track_z_len); + + ParticleShader::HeightmapSimulationShader::getInstance()->setUniforms(matrix, timediff, active_count, size_increase_factor, track_x, track_x_len, track_z, track_z_len); glBindVertexArray(current_simulation_vao); glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfb_buffers[1]); diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index b1f53e0d9..22041c420 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -414,7 +414,6 @@ void Shaders::loadShaders() LightShader::PointLightShader::init(); MeshShader::SkyboxShader::init(); MeshShader::ViewFrustrumShader::init(); - ParticleShader::HeightmapSimulationShader::init(); UtilShader::ColoredLine::init(); } @@ -1388,26 +1387,7 @@ namespace ParticleShader AssignUniforms("sourcematrix", "dt", "level", "size_increase_factor"); } - GLuint HeightmapSimulationShader::Program; - GLuint HeightmapSimulationShader::attrib_position; - GLuint HeightmapSimulationShader::attrib_velocity; - GLuint HeightmapSimulationShader::attrib_lifetime; - GLuint HeightmapSimulationShader::attrib_initial_position; - GLuint HeightmapSimulationShader::attrib_initial_velocity; - GLuint HeightmapSimulationShader::attrib_initial_lifetime; - GLuint HeightmapSimulationShader::attrib_size; - GLuint HeightmapSimulationShader::attrib_initial_size; - GLuint HeightmapSimulationShader::uniform_sourcematrix; - GLuint HeightmapSimulationShader::uniform_dt; - GLuint HeightmapSimulationShader::uniform_level; - GLuint HeightmapSimulationShader::uniform_size_increase_factor; - GLuint HeightmapSimulationShader::uniform_track_x; - GLuint HeightmapSimulationShader::uniform_track_z; - GLuint HeightmapSimulationShader::uniform_track_x_len; - GLuint HeightmapSimulationShader::uniform_track_z_len; - GLuint HeightmapSimulationShader::uniform_heightmap; - - void HeightmapSimulationShader::init() + HeightmapSimulationShader::HeightmapSimulationShader() { const char *varyings[] = { "new_particle_position", @@ -1416,26 +1396,9 @@ namespace ParticleShader "new_size", }; Program = LoadTFBProgram(file_manager->getAsset("shaders/particlesimheightmap.vert").c_str(), varyings, 4); - - uniform_dt = glGetUniformLocation(Program, "dt"); - uniform_sourcematrix = glGetUniformLocation(Program, "sourcematrix"); - uniform_level = glGetUniformLocation(Program, "level"); - uniform_size_increase_factor = glGetUniformLocation(Program, "size_increase_factor"); - - attrib_position = glGetAttribLocation(Program, "particle_position"); - attrib_lifetime = glGetAttribLocation(Program, "lifetime"); - attrib_velocity = glGetAttribLocation(Program, "particle_velocity"); - attrib_size = glGetAttribLocation(Program, "size"); - attrib_initial_position = glGetAttribLocation(Program, "particle_position_initial"); - attrib_initial_lifetime = glGetAttribLocation(Program, "lifetime_initial"); - attrib_initial_velocity = glGetAttribLocation(Program, "particle_velocity_initial"); - attrib_initial_size = glGetAttribLocation(Program, "size_initial"); - - uniform_heightmap = glGetUniformLocation(Program, "heightmap"); - uniform_track_x = glGetUniformLocation(Program, "track_x"); - uniform_track_x_len = glGetUniformLocation(Program, "track_x_len"); - uniform_track_z = glGetUniformLocation(Program, "track_z"); - uniform_track_z_len = glGetUniformLocation(Program, "track_z_len"); + TU_heightmap = 2; + AssignTextureUnit(Program, TexUnit(TU_heightmap, "heightmap")); + AssignUniforms("sourcematrix", "dt", "level", "size_increase_factor", "track_x", "track_x_len", "track_z", "track_z_len"); } SimpleParticleRender::SimpleParticleRender() diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index 974e250a6..eeeffcfec 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -494,15 +494,12 @@ public: -class HeightmapSimulationShader +class HeightmapSimulationShader : public ShaderHelperSingleton { public: - static GLuint Program; - static GLuint attrib_position, attrib_velocity, attrib_lifetime, attrib_initial_position, attrib_initial_velocity, attrib_initial_lifetime, attrib_size, attrib_initial_size; - static GLuint uniform_sourcematrix, uniform_dt, uniform_level, uniform_size_increase_factor; - static GLuint uniform_track_x, uniform_track_z, uniform_track_x_len, uniform_track_z_len, uniform_heightmap; + GLuint TU_heightmap; - static void init(); + HeightmapSimulationShader(); }; class SimpleParticleRender : public ShaderHelperSingleton From b5016ba85e03cca35408b94b14223c2db78cfef9 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Wed, 13 Aug 2014 22:47:03 +0200 Subject: [PATCH 19/26] Factorise simulation code. --- src/graphics/gpuparticles.cpp | 81 ++++++++++++----------------------- src/graphics/gpuparticles.hpp | 2 - 2 files changed, 28 insertions(+), 55 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index f0727ce15..b2ff3d8ba 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -309,63 +309,38 @@ void ParticleSystemProxy::CommonSimulationVAO(GLuint position_vbo, GLuint initia glVertexAttribPointer(7, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleSystemProxy::ParticleData), (GLvoid*)(7 * sizeof(float))); } -void ParticleSystemProxy::simulateHeightmap() -{ - int timediff = int(GUIEngine::getLatestDt() * 1000.f); - int active_count = getEmitter()->getMaxLifeTime() * getEmitter()->getMaxParticlesPerSecond() / 1000; - core::matrix4 matrix = getAbsoluteTransformation(); - glUseProgram(ParticleShader::HeightmapSimulationShader::getInstance()->Program); - glEnable(GL_RASTERIZER_DISCARD); - - glActiveTexture(GL_TEXTURE0 + ParticleShader::HeightmapSimulationShader::getInstance()->TU_heightmap); - glBindTexture(GL_TEXTURE_BUFFER, heightmaptexture); - - ParticleShader::HeightmapSimulationShader::getInstance()->setUniforms(matrix, timediff, active_count, size_increase_factor, track_x, track_x_len, track_z, track_z_len); - - glBindVertexArray(current_simulation_vao); - glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfb_buffers[1]); - - glBeginTransformFeedback(GL_POINTS); - glDrawArrays(GL_POINTS, 0, count); - glEndTransformFeedback(); - glBindVertexArray(0); - - glDisable(GL_RASTERIZER_DISCARD); - std::swap(tfb_buffers[0], tfb_buffers[1]); - std::swap(current_rendering_vao, non_current_rendering_vao); - std::swap(current_simulation_vao, non_current_simulation_vao); -} - -void ParticleSystemProxy::simulateNoHeightmap() -{ - int timediff = int(GUIEngine::getLatestDt() * 1000.f); - int active_count = getEmitter()->getMaxLifeTime() * getEmitter()->getMaxParticlesPerSecond() / 1000; - core::matrix4 matrix = getAbsoluteTransformation(); - glUseProgram(ParticleShader::SimpleSimulationShader::getInstance()->Program); - glEnable(GL_RASTERIZER_DISCARD); - - ParticleShader::SimpleSimulationShader::getInstance()->setUniforms(matrix, timediff, active_count, size_increase_factor); - - glBindVertexArray(current_simulation_vao); - glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfb_buffers[1]); - - glBeginTransformFeedback(GL_POINTS); - glDrawArrays(GL_POINTS, 0, count); - glEndTransformFeedback(); - glBindVertexArray(0); - - glDisable(GL_RASTERIZER_DISCARD); - std::swap(tfb_buffers[0], tfb_buffers[1]); - std::swap(current_rendering_vao, non_current_rendering_vao); - std::swap(current_simulation_vao, non_current_simulation_vao); -} - void ParticleSystemProxy::simulate() { + int timediff = int(GUIEngine::getLatestDt() * 1000.f); + int active_count = getEmitter()->getMaxLifeTime() * getEmitter()->getMaxParticlesPerSecond() / 1000; + core::matrix4 matrix = getAbsoluteTransformation(); + + glEnable(GL_RASTERIZER_DISCARD); if (has_height_map) - simulateHeightmap(); + { + glUseProgram(ParticleShader::HeightmapSimulationShader::getInstance()->Program); + glActiveTexture(GL_TEXTURE0 + ParticleShader::HeightmapSimulationShader::getInstance()->TU_heightmap); + glBindTexture(GL_TEXTURE_BUFFER, heightmaptexture); + ParticleShader::HeightmapSimulationShader::getInstance()->setUniforms(matrix, timediff, active_count, size_increase_factor, track_x, track_x_len, track_z, track_z_len); + } else - simulateNoHeightmap(); + { + glUseProgram(ParticleShader::SimpleSimulationShader::getInstance()->Program); + ParticleShader::SimpleSimulationShader::getInstance()->setUniforms(matrix, timediff, active_count, size_increase_factor); + } + + glBindVertexArray(current_simulation_vao); + glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfb_buffers[1]); + + glBeginTransformFeedback(GL_POINTS); + glDrawArrays(GL_POINTS, 0, count); + glEndTransformFeedback(); + glBindVertexArray(0); + + glDisable(GL_RASTERIZER_DISCARD); + std::swap(tfb_buffers[0], tfb_buffers[1]); + std::swap(current_rendering_vao, non_current_rendering_vao); + std::swap(current_simulation_vao, non_current_simulation_vao); } void ParticleSystemProxy::drawFlip() diff --git a/src/graphics/gpuparticles.hpp b/src/graphics/gpuparticles.hpp index 91527bacc..2f174e1fd 100644 --- a/src/graphics/gpuparticles.hpp +++ b/src/graphics/gpuparticles.hpp @@ -30,8 +30,6 @@ protected: void generateVAOs(); void cleanGL(); - void simulateHeightmap(); - void simulateNoHeightmap(); void drawFlip(); void drawNotFlip(); virtual void simulate(); From c7a4d45d082547511931a2108c58b7a47bb835aa Mon Sep 17 00:00:00 2001 From: Vlj Date: Wed, 13 Aug 2014 23:52:47 +0200 Subject: [PATCH 20/26] Fix malloc/delete mismatch --- src/graphics/gpuparticles.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index b2ff3d8ba..07d0e424a 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -52,9 +52,9 @@ ParticleSystemProxy::ParticleSystemProxy(bool createDefaultEmitter, ParticleSystemProxy::~ParticleSystemProxy() { if (InitialValues) - delete InitialValues; + free(InitialValues); if (ParticleParams) - delete ParticleParams; + free(ParticleParams); if (!m_first_execution) cleanGL(); if (heighmapbuffer) From 3e277f8edf6a97ca863f99c9bc5a5251b0476333 Mon Sep 17 00:00:00 2001 From: Vlj Date: Wed, 13 Aug 2014 23:56:27 +0200 Subject: [PATCH 21/26] Dont alloc quaternion buffer if not flip --- src/graphics/gpuparticles.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/graphics/gpuparticles.cpp b/src/graphics/gpuparticles.cpp index 07d0e424a..169af5a73 100644 --- a/src/graphics/gpuparticles.cpp +++ b/src/graphics/gpuparticles.cpp @@ -407,10 +407,11 @@ void ParticleSystemProxy::generateVAOs() glBindVertexArray(non_current_simulation_vao); CommonSimulationVAO(tfb_buffers[1], initial_values_buffer); - float *quaternions = new float[4 * count]; + glBindVertexArray(0); if (flip) { + float *quaternions = new float[4 * count]; for (unsigned i = 0; i < count; i++) { core::vector3df rotationdir(0., 1., 0.); @@ -423,6 +424,7 @@ void ParticleSystemProxy::generateVAOs() glGenBuffers(1, &quaternionsbuffer); glBindBuffer(GL_ARRAY_BUFFER, quaternionsbuffer); glBufferData(GL_ARRAY_BUFFER, 4 * count * sizeof(float), quaternions, GL_STATIC_DRAW); + delete[] quaternions; } glBindVertexArray(current_rendering_vao); @@ -435,8 +437,6 @@ void ParticleSystemProxy::generateVAOs() if (flip) AppendQuaternionRenderingVAO(quaternionsbuffer); glBindVertexArray(0); - - delete[] quaternions; } void ParticleSystemProxy::render() { From 976ea043bb060a8f5c57adb1c33b8fce96237439 Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Wed, 13 Aug 2014 18:25:21 -0400 Subject: [PATCH 22/26] Add missing bit of code --- src/tracks/model_definition_loader.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/tracks/model_definition_loader.cpp b/src/tracks/model_definition_loader.cpp index c783b3221..dbfaae82c 100644 --- a/src/tracks/model_definition_loader.cpp +++ b/src/tracks/model_definition_loader.cpp @@ -130,6 +130,16 @@ STKInstancedSceneNode* ModelDefinitionLoader::instanciate(const irr::core::vecto } scene::IMesh* mesh = irr_driver->getMesh(m_lod_groups[name][0].m_model_file); + + if (m_lod_groups[name][0].m_tangent && mesh->getMeshBuffer(0)->getVertexType() != video::EVT_TANGENTS) + { + scene::IMeshManipulator* manip = irr_driver->getVideoDriver()->getMeshManipulator(); + scene::IMesh* m2 = manip->createMeshWithTangents(mesh); + // FIXME: do we need to clean up 'a_mesh' ? + mesh = m2; + irr_driver->setAllMaterialFlags(mesh); + } + m_instancing_nodes[name] = new STKInstancedSceneNode(mesh, irr_driver->getSceneManager()->getRootSceneNode(), irr_driver->getSceneManager(), -1); m_track->addNode(m_instancing_nodes[name]); From b95765c17296e6ea5113275eeea29fd831a447f7 Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Wed, 13 Aug 2014 18:35:07 -0400 Subject: [PATCH 23/26] use better tool to calculate tangents --- src/tracks/model_definition_loader.cpp | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/tracks/model_definition_loader.cpp b/src/tracks/model_definition_loader.cpp index dbfaae82c..0e0303ced 100644 --- a/src/tracks/model_definition_loader.cpp +++ b/src/tracks/model_definition_loader.cpp @@ -21,6 +21,7 @@ using namespace irr; #include "graphics/irr_driver.hpp" #include "graphics/lod_node.hpp" +#include "graphics/mesh_tools.hpp" #include "graphics/stkinstancedscenenode.hpp" #include "io/xml_node.hpp" #include "tracks/track.hpp" @@ -82,14 +83,8 @@ LODNode* ModelDefinitionLoader::instanciateAsLOD(const XMLNode* node, scene::ISc continue; } - if (group[m].m_tangent && a_mesh->getMeshBuffer(0)->getVertexType() != video::EVT_TANGENTS) - { - scene::IMeshManipulator* manip = irr_driver->getVideoDriver()->getMeshManipulator(); - scene::IMesh* m2 = manip->createMeshWithTangents(a_mesh); - // FIXME: do we need to clean up 'a_mesh' ? - a_mesh = m2; - irr_driver->setAllMaterialFlags(a_mesh); - } + a_mesh = MeshTools::createMeshWithTangents(a_mesh, &MeshTools::isNormalMap); + irr_driver->setAllMaterialFlags(a_mesh); a_mesh->grab(); //cache.push_back(a_mesh); @@ -130,15 +125,8 @@ STKInstancedSceneNode* ModelDefinitionLoader::instanciate(const irr::core::vecto } scene::IMesh* mesh = irr_driver->getMesh(m_lod_groups[name][0].m_model_file); - - if (m_lod_groups[name][0].m_tangent && mesh->getMeshBuffer(0)->getVertexType() != video::EVT_TANGENTS) - { - scene::IMeshManipulator* manip = irr_driver->getVideoDriver()->getMeshManipulator(); - scene::IMesh* m2 = manip->createMeshWithTangents(mesh); - // FIXME: do we need to clean up 'a_mesh' ? - mesh = m2; - irr_driver->setAllMaterialFlags(mesh); - } + mesh = MeshTools::createMeshWithTangents(mesh, &MeshTools::isNormalMap); + irr_driver->setAllMaterialFlags(mesh); m_instancing_nodes[name] = new STKInstancedSceneNode(mesh, irr_driver->getSceneManager()->getRootSceneNode(), irr_driver->getSceneManager(), -1); From 47e176d4a38c852ac67e926f373d6ae88018d18a Mon Sep 17 00:00:00 2001 From: vlj Date: Thu, 14 Aug 2014 00:55:12 +0200 Subject: [PATCH 24/26] User wrong VS for instanced normal Also fix the srgb normal map. --- src/graphics/render_geometry.cpp | 2 +- src/graphics/shaders.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/graphics/render_geometry.cpp b/src/graphics/render_geometry.cpp index a7ab8c67c..8dc71634e 100644 --- a/src/graphics/render_geometry.cpp +++ b/src/graphics/render_geometry.cpp @@ -251,7 +251,7 @@ void IrrDriver::renderSolidFirstPass() TexUnits(TexUnit(MeshShader::InstancedGrassPass1Shader::getInstance()->TU_tex, true)), ListInstancedMatGrass::getInstance()); renderInstancedMeshes1stPass( - TexUnits(TexUnit(MeshShader::InstancedNormalMapShader::getInstance()->TU_glossy, true), TexUnit(MeshShader::InstancedNormalMapShader::getInstance()->TU_normalmap, true)), + TexUnits(TexUnit(MeshShader::InstancedNormalMapShader::getInstance()->TU_glossy, true), TexUnit(MeshShader::InstancedNormalMapShader::getInstance()->TU_normalmap, false)), ListInstancedMatNormalMap::getInstance()); } } diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 22041c420..b95e8c502 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -709,7 +709,7 @@ namespace MeshShader { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(), - GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(), + GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/normalmap.frag").c_str()); AssignUniforms(); From ba3027b6bbac7ce90644cb0e46cf100d14ebb4be Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Wed, 13 Aug 2014 19:47:59 -0400 Subject: [PATCH 25/26] Little improvement to for_in macro. Still only works on PtrVector though --- src/animations/animation_base.cpp | 9 +++----- src/animations/animation_base.hpp | 3 +-- src/config/player_manager.cpp | 22 +++++++----------- src/guiengine/engine.cpp | 3 +-- .../widgets/dynamic_ribbon_widget.cpp | 23 ++++++------------- src/guiengine/widgets/ribbon_widget.cpp | 3 +-- src/utils/ptr_vector.hpp | 4 +++- 7 files changed, 24 insertions(+), 43 deletions(-) diff --git a/src/animations/animation_base.cpp b/src/animations/animation_base.cpp index 784068288..74f987d39 100644 --- a/src/animations/animation_base.cpp +++ b/src/animations/animation_base.cpp @@ -67,8 +67,7 @@ AnimationBase::AnimationBase(Ipo *ipo) void AnimationBase::setInitialTransform(const Vec3 &xyz, const Vec3 &hpr) { - Ipo* curr; - for_in (curr, m_all_ipos) + for_var_in(Ipo*, curr, m_all_ipos) { curr->setInitialTransform(xyz, hpr); } @@ -80,8 +79,7 @@ void AnimationBase::setInitialTransform(const Vec3 &xyz, void AnimationBase::reset() { m_current_time = 0; - Ipo* curr; - for_in (curr, m_all_ipos) + for_var_in(Ipo*, curr, m_all_ipos) { curr->reset(); } @@ -103,8 +101,7 @@ void AnimationBase::update(float dt, Vec3 *xyz, Vec3 *hpr, Vec3 *scale) assert(!isnan(m_current_time)); - Ipo* curr; - for_in (curr, m_all_ipos) + for_var_in (Ipo*, curr, m_all_ipos) { curr->update(m_current_time, xyz, hpr, scale); } diff --git a/src/animations/animation_base.hpp b/src/animations/animation_base.hpp index 1fc6ef090..57f98dbc5 100644 --- a/src/animations/animation_base.hpp +++ b/src/animations/animation_base.hpp @@ -90,8 +90,7 @@ public: { float duration = -1; - const Ipo* currIpo; - for_in (currIpo, m_all_ipos) + for_var_in (const Ipo*, currIpo, m_all_ipos) { duration = std::max(duration, currIpo->getEndTime()); } diff --git a/src/config/player_manager.cpp b/src/config/player_manager.cpp index af4e0bc63..d138b8b18 100644 --- a/src/config/player_manager.cpp +++ b/src/config/player_manager.cpp @@ -167,8 +167,7 @@ PlayerManager::PlayerManager() PlayerManager::~PlayerManager() { // If the passwords should not be remembered, clear the saved session. - PlayerProfile *player; - for_in(player, m_all_players) + for_var_in(PlayerProfile*, player, m_all_players) { if(!player->rememberPassword()) player->clearSession(); @@ -270,8 +269,7 @@ void PlayerManager::save() } // Save all non-guest players - PlayerProfile *player; - for_in(player, m_all_players) + for_var_in(PlayerProfile*, player, m_all_players) { if(!player->isGuestAccount()) player->save(players_file); @@ -320,8 +318,8 @@ void PlayerManager::deletePlayer(PlayerProfile *player) void PlayerManager::enforceCurrentPlayer() { if (m_current_player) return; - - PlayerProfile *player; + + PlayerProfile* player; for_in(player, m_all_players) { if (!player->isGuestAccount()) @@ -408,8 +406,7 @@ void PlayerManager::createGuestPlayers(int n) unsigned int PlayerManager::getNumNonGuestPlayers() const { unsigned int count=0; - const PlayerProfile *player; - for_in(player, m_all_players) + for_var_in(const PlayerProfile*, player, m_all_players) { if(!player->isGuestAccount()) count ++; } @@ -422,8 +419,7 @@ unsigned int PlayerManager::getNumNonGuestPlayers() const unsigned int PlayerManager::getUniqueId() const { unsigned int max_id=0; - const PlayerProfile *player; - for_in(player, m_all_players) + for_var_in(const PlayerProfile*, player, m_all_players) { if(player->getUniqueID()>max_id) max_id = player->getUniqueID(); @@ -439,8 +435,7 @@ unsigned int PlayerManager::getUniqueId() const */ const PlayerProfile *PlayerManager::getPlayerById(unsigned int id) { - const PlayerProfile *player; - for_in(player, m_all_players) + for_var_in(const PlayerProfile*, player, m_all_players) { if(player->getUniqueID()==id) return player; @@ -455,8 +450,7 @@ const PlayerProfile *PlayerManager::getPlayerById(unsigned int id) */ PlayerProfile *PlayerManager::getPlayer(const irr::core::stringw &name) { - PlayerProfile *player; - for_in(player, m_all_players) + for_var_in(PlayerProfile*, player, m_all_players) { if(player->getName()==name) return player; diff --git a/src/guiengine/engine.cpp b/src/guiengine/engine.cpp index b1903c1d3..bcd202c83 100644 --- a/src/guiengine/engine.cpp +++ b/src/guiengine/engine.cpp @@ -828,8 +828,7 @@ namespace GUIEngine { // This code needs to go outside beginScene() / endScene() since // the model view widget will do off-screen rendering there - GUIEngine::Widget* widget; - for_in (widget, GUIEngine::needsUpdate) + for_var_in(GUIEngine::Widget*, widget, GUIEngine::needsUpdate) { widget->update(dt); } diff --git a/src/guiengine/widgets/dynamic_ribbon_widget.cpp b/src/guiengine/widgets/dynamic_ribbon_widget.cpp index 0ef2b97e0..0a3040068 100644 --- a/src/guiengine/widgets/dynamic_ribbon_widget.cpp +++ b/src/guiengine/widgets/dynamic_ribbon_widget.cpp @@ -534,8 +534,7 @@ irr::core::stringw DynamicRibbonWidget::getSelectionText(const int playerID) // ----------------------------------------------------------------------------- RibbonWidget* DynamicRibbonWidget::getRowContaining(Widget* w) { - RibbonWidget* row; - for_in (row, m_rows) + for_var_in (RibbonWidget*, row, m_rows) { if (row != NULL) { @@ -548,8 +547,7 @@ RibbonWidget* DynamicRibbonWidget::getRowContaining(Widget* w) // ----------------------------------------------------------------------------- RibbonWidget* DynamicRibbonWidget::getSelectedRibbon(const int playerID) { - RibbonWidget* row; - for_in (row, m_rows) + for_var_in (RibbonWidget*, row, m_rows) { if (GUIEngine::isFocusedForPlayer(row, playerID)) { @@ -608,8 +606,7 @@ EventPropagation DynamicRibbonWidget::leftPressed(const int playerID) updateLabel(); propagateSelection(); - DynamicRibbonHoverListener* listener; - for_in( listener, m_hover_listeners ) + for_var_in (DynamicRibbonHoverListener*, listener, m_hover_listeners) { listener->onSelectionChanged(this, w->getSelectionIDString(playerID), w->getSelectionText(playerID), playerID); @@ -666,8 +663,7 @@ EventPropagation DynamicRibbonWidget::mouseHovered(Widget* child, const int play if (getSelectedRibbon(playerID) != NULL) { - DynamicRibbonHoverListener* listener; - for_in( listener, m_hover_listeners ) + for_var_in (DynamicRibbonHoverListener*, listener, m_hover_listeners) { listener->onSelectionChanged(this, getSelectedRibbon(playerID)->getSelectionIDString(playerID), getSelectedRibbon(playerID)->getSelectionText(playerID), playerID); @@ -682,15 +678,13 @@ EventPropagation DynamicRibbonWidget::focused(const int playerID) Widget::focused(playerID); updateLabel(); - DynamicRibbonHoverListener* listener; - if (getSelectedRibbon(playerID)->getSelectionIDString(playerID) == "") { //fprintf(stderr, "[DynamicRibbonWidget] WARNING: Can't find selection for player %i, selecting first item\n", playerID); getSelectedRibbon(playerID)->setSelection(0, playerID); } - for_in( listener, m_hover_listeners ) + for_var_in (DynamicRibbonHoverListener*, listener, m_hover_listeners) { listener->onSelectionChanged(this, getSelectedRibbon(playerID)->getSelectionIDString(playerID), getSelectedRibbon(playerID)->getSelectionText(playerID), playerID); @@ -719,15 +713,13 @@ void DynamicRibbonWidget::onRibbonWidgetFocus(RibbonWidget* emitter, const int p updateLabel(emitter); - DynamicRibbonHoverListener* listener; - if (emitter->getSelectionIDString(playerID) == "") { //fprintf(stderr, "[DynamicRibbonWidget] WARNING: Can't find selection for player %i, selecting first item\n", playerID); emitter->setSelection(0, playerID); } - for_in( listener, m_hover_listeners ) + for_var_in(DynamicRibbonHoverListener*, listener, m_hover_listeners) { listener->onSelectionChanged(this, emitter->getSelectionIDString(playerID), emitter->getSelectionText(playerID), playerID); @@ -802,8 +794,7 @@ void DynamicRibbonWidget::propagateSelection() } // set same selection in all ribbons - RibbonWidget* ribbon; - for_in( ribbon, m_rows ) + for_var_in (RibbonWidget*, ribbon, m_rows) { if (ribbon != selected_ribbon) { diff --git a/src/guiengine/widgets/ribbon_widget.cpp b/src/guiengine/widgets/ribbon_widget.cpp index 3f3481288..9432335ed 100644 --- a/src/guiengine/widgets/ribbon_widget.cpp +++ b/src/guiengine/widgets/ribbon_widget.cpp @@ -443,8 +443,7 @@ void RibbonWidget::removeChildNamed(const char* name) // This method should only be called BEFORE a widget is added assert(m_element == NULL); - Widget* child; - for_in (child, m_children) + for_var_in(Widget*, child, m_children) { if (child->m_properties[PROP_ID] == name) { diff --git a/src/utils/ptr_vector.hpp b/src/utils/ptr_vector.hpp index ef9c3d5a0..65dc75956 100644 --- a/src/utils/ptr_vector.hpp +++ b/src/utils/ptr_vector.hpp @@ -303,6 +303,8 @@ public: #define for_in( VAR, VECTOR ) for (unsigned int _foreach_i = 0; \ VAR = (_foreach_i < VECTOR.size() ? VECTOR.get(_foreach_i) : NULL),\ _foreach_i < VECTOR.size(); _foreach_i++) - +#define for_var_in( TYPE, VAR, VECTOR ) TYPE VAR; for (unsigned int _foreach_i = 0; \ + VAR = (_foreach_i < VECTOR.size() ? VECTOR.get(_foreach_i) : NULL), \ + _foreach_i < VECTOR.size(); _foreach_i++) #endif From 9120d792beabd8a41bc0cebc61b5694f40b4aea6 Mon Sep 17 00:00:00 2001 From: vlj Date: Thu, 14 Aug 2014 02:09:12 +0200 Subject: [PATCH 26/26] Fix GPUTimer --- src/graphics/render_lighting.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/graphics/render_lighting.cpp b/src/graphics/render_lighting.cpp index 98efacd02..3deb06b3d 100644 --- a/src/graphics/render_lighting.cpp +++ b/src/graphics/render_lighting.cpp @@ -161,9 +161,10 @@ void IrrDriver::renderLights(unsigned pointlightcount) m_post_processing->renderGI(rh_matrix, rh_extend, m_rtts->getRH().getRTT()[0], m_rtts->getRH().getRTT()[1], m_rtts->getRH().getRTT()[2]); } - - ScopedGPUTimer timer(irr_driver->getGPUTimer(Q_ENVMAP)); - m_post_processing->renderDiffuseEnvMap(blueSHCoeff, greenSHCoeff, redSHCoeff); + { + ScopedGPUTimer timer(irr_driver->getGPUTimer(Q_ENVMAP)); + m_post_processing->renderDiffuseEnvMap(blueSHCoeff, greenSHCoeff, redSHCoeff); + } m_rtts->getFBO(FBO_COMBINED_TMP1_TMP2).Bind();