From f1ddf87a5f5b8dcd5e26aee21bfb6b77dd35d2f5 Mon Sep 17 00:00:00 2001 From: vlj Date: Mon, 14 Jul 2014 01:12:56 +0200 Subject: [PATCH] Plugin displacement material. --- src/graphics/irr_driver.hpp | 2 - src/graphics/render.cpp | 45 +------------------ src/graphics/render_geometry.cpp | 73 +++++++++++++++++++++++++++++++ src/graphics/stkanimatedmesh.cpp | 2 +- src/graphics/stkmesh.cpp | 5 ++- src/graphics/stkmesh.hpp | 9 +++- src/graphics/stkmeshscenenode.cpp | 59 ++----------------------- src/graphics/stkmeshscenenode.hpp | 1 - src/tracks/track.cpp | 1 - 9 files changed, 91 insertions(+), 106 deletions(-) diff --git a/src/graphics/irr_driver.hpp b/src/graphics/irr_driver.hpp index 1cb8e08f8..69909bce2 100644 --- a/src/graphics/irr_driver.hpp +++ b/src/graphics/irr_driver.hpp @@ -70,7 +70,6 @@ enum STKRenderingPass SOLID_LIT_PASS, TRANSPARENT_PASS, GLOW_PASS, - DISPLACEMENT_PASS, SHADOW_PASS, PASS_COUNT, }; @@ -378,7 +377,6 @@ private: void renderGlow(std::vector& glows); void renderSSAO(); void renderLights(unsigned pointlightCount); - void renderDisplacement(); void renderShadowsDebug(); void doScreenShot(); public: diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index a3b7eeaab..5aee14b0d 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -378,13 +378,6 @@ void IrrDriver::renderScene(scene::ICameraSceneNode * const camnode, unsigned po return; } - // Render displacement - { - PROFILER_PUSH_CPU_MARKER("- Displacement", 0x00, 0x00, 0xFF); - ScopedGPUTimer Timer(getGPUTimer(Q_DISPLACEMENT)); - renderDisplacement(); - PROFILER_POP_CPU_MARKER(); - } // Ensure that no object will be drawn after that by using invalid pass irr_driver->setPhase(PASS_COUNT); } @@ -1463,40 +1456,4 @@ void IrrDriver::renderSkybox(const scene::ICameraSceneNode *camera) 0); glDrawElements(GL_TRIANGLES, 6 * 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); -} - -// ---------------------------------------------------------------------------- - -void IrrDriver::renderDisplacement() -{ - irr_driver->getFBO(FBO_TMP1_WITH_DS).Bind(); - glClear(GL_COLOR_BUFFER_BIT); - irr_driver->getFBO(FBO_DISPLACE).Bind(); - glClear(GL_COLOR_BUFFER_BIT); - - DisplaceProvider * const cb = (DisplaceProvider *)irr_driver->getCallback(ES_DISPLACE); - cb->update(); - - const int displacingcount = m_displacing.size(); - irr_driver->setPhase(DISPLACEMENT_PASS); - glEnable(GL_DEPTH_TEST); - glDisable(GL_ALPHA_TEST); - glDepthMask(GL_FALSE); - glDisable(GL_BLEND); - glClear(GL_STENCIL_BUFFER_BIT); - glEnable(GL_STENCIL_TEST); - glStencilFunc(GL_ALWAYS, 1, 0xFF); - glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); - - glBindVertexArray(getVAO(EVT_2TCOORDS)); - for (int i = 0; i < displacingcount; i++) - { - m_scene_manager->setCurrentRendertime(scene::ESNRP_TRANSPARENT); - m_displacing[i]->render(); - } - - irr_driver->getFBO(FBO_COLORS).Bind(); - glStencilFunc(GL_EQUAL, 1, 0xFF); - m_post_processing->renderPassThrough(m_rtts->getRenderTarget(RTT_DISPLACE)); - glDisable(GL_STENCIL_TEST); -} +} \ No newline at end of file diff --git a/src/graphics/render_geometry.cpp b/src/graphics/render_geometry.cpp index 60a4f6796..1b3b6ac38 100644 --- a/src/graphics/render_geometry.cpp +++ b/src/graphics/render_geometry.cpp @@ -204,6 +204,7 @@ void IrrDriver::renderSolidSecondPass() } } +static video::ITexture *displaceTex = 0; void IrrDriver::renderTransparent() { @@ -218,6 +219,7 @@ void IrrDriver::renderTransparent() ListAdditiveTransparent::Arguments.clear(); ListBlendTransparentFog::Arguments.clear(); ListAdditiveTransparentFog::Arguments.clear(); + ListDisplacement::Arguments.clear(); m_scene_manager->drawAll(scene::ESNRP_TRANSPARENT); glBindVertexArray(getVAO(EVT_STANDARD)); @@ -236,6 +238,77 @@ void IrrDriver::renderTransparent() glBlendFunc(GL_ONE, GL_ONE); renderMeshes2ndPass(MeshShader::TransparentShaderInstance, { MeshShader::TransparentShaderInstance->TU_tex }, ListAdditiveTransparent::Arguments); } + + if (!UserConfigParams::m_dynamic_lights) + return; + + // Render displacement nodes + irr_driver->getFBO(FBO_TMP1_WITH_DS).Bind(); + glClear(GL_COLOR_BUFFER_BIT); + irr_driver->getFBO(FBO_DISPLACE).Bind(); + glClear(GL_COLOR_BUFFER_BIT); + + DisplaceProvider * const cb = (DisplaceProvider *)irr_driver->getCallback(ES_DISPLACE); + cb->update(); + + glEnable(GL_DEPTH_TEST); + glDisable(GL_ALPHA_TEST); + glDepthMask(GL_FALSE); + glDisable(GL_BLEND); + glClear(GL_STENCIL_BUFFER_BIT); + glEnable(GL_STENCIL_TEST); + glStencilFunc(GL_ALWAYS, 1, 0xFF); + glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); + + glBindVertexArray(getVAO(EVT_2TCOORDS)); + for (int i = 0; i < ListDisplacement::Arguments.size(); i++) + { + const GLMesh &mesh = *(std::get<0>(ListDisplacement::Arguments[i])); + const core::matrix4 &AbsoluteTransformation = std::get<1>(ListDisplacement::Arguments[i]); + if (mesh.VAOType != video::EVT_2TCOORDS) + { + Log::error("Materials", "Displacement has wrong vertex type"); + continue; + } + glBindVertexArray(getVAO(video::EVT_2TCOORDS)); + DisplaceProvider * const cb = (DisplaceProvider *)irr_driver->getCallback(ES_DISPLACE); + + GLenum ptype = mesh.PrimitiveType; + GLenum itype = mesh.IndexType; + size_t count = mesh.IndexCount; + + // Generate displace mask + // Use RTT_TMP4 as displace mask + irr_driver->getFBO(FBO_TMP1_WITH_DS).Bind(); + + glUseProgram(MeshShader::DisplaceMaskShader::Program); + MeshShader::DisplaceMaskShader::setUniforms(AbsoluteTransformation); + glDrawElementsBaseVertex(ptype, count, itype, (GLvoid *)mesh.vaoOffset, mesh.vaoBaseVertex); + + // Render the effect + if (!displaceTex) + displaceTex = irr_driver->getTexture(FileManager::TEXTURE, "displace.png"); + irr_driver->getFBO(FBO_DISPLACE).Bind(); + setTexture(0, getTextureGLuint(displaceTex), GL_LINEAR, GL_LINEAR, true); + setTexture(1, irr_driver->getRenderTargetTexture(RTT_TMP1), GL_LINEAR, GL_LINEAR, true); + setTexture(2, irr_driver->getRenderTargetTexture(RTT_COLOR), GL_LINEAR, GL_LINEAR, true); + setTexture(3, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR, true); + glUseProgram(MeshShader::DisplaceShader::Program); + MeshShader::DisplaceShader::setUniforms(AbsoluteTransformation, + core::vector2df(cb->getDirX(), cb->getDirY()), + core::vector2df(cb->getDir2X(), cb->getDir2Y()), + core::vector2df(float(UserConfigParams::m_width), + float(UserConfigParams::m_height)), + 0, 1, 2, 3); + + glDrawElementsBaseVertex(ptype, count, itype, (GLvoid *)mesh.vaoOffset, mesh.vaoBaseVertex); + } + + irr_driver->getFBO(FBO_COLORS).Bind(); + glStencilFunc(GL_EQUAL, 1, 0xFF); + m_post_processing->renderPassThrough(m_rtts->getRenderTarget(RTT_DISPLACE)); + glDisable(GL_STENCIL_TEST); + } template diff --git a/src/graphics/stkanimatedmesh.cpp b/src/graphics/stkanimatedmesh.cpp index 69ed87ee4..8d0dc7c5c 100644 --- a/src/graphics/stkanimatedmesh.cpp +++ b/src/graphics/stkanimatedmesh.cpp @@ -93,7 +93,7 @@ void STKAnimatedMesh::render() GLMesh &mesh = GLmeshes[i]; if (rnd->isTransparent()) { - TransparentMaterial TranspMat = MaterialTypeToTransparentMaterial(type, MaterialTypeParam); + TransparentMaterial TranspMat = MaterialTypeToTransparentMaterial(type, MaterialTypeParam, false); TransparentMesh[TranspMat].push_back(&mesh); } else diff --git a/src/graphics/stkmesh.cpp b/src/graphics/stkmesh.cpp index 5b39b2db9..d7b9d8211 100644 --- a/src/graphics/stkmesh.cpp +++ b/src/graphics/stkmesh.cpp @@ -42,8 +42,10 @@ ShadedMaterial MaterialTypeToShadedMaterial(video::E_MATERIAL_TYPE type, video:: return SM_DEFAULT_STANDARD; } -TransparentMaterial MaterialTypeToTransparentMaterial(video::E_MATERIAL_TYPE type, f32 MaterialTypeParam) +TransparentMaterial MaterialTypeToTransparentMaterial(video::E_MATERIAL_TYPE type, f32 MaterialTypeParam, bool isDisplacement) { + if (isDisplacement) + return TM_DISPLACEMENT; if (type == irr_driver->getShader(ES_BUBBLES)) return TM_BUBBLE; video::E_BLEND_FACTOR srcFact, DstFact; @@ -328,6 +330,7 @@ std::vector > ListUnlitSM::Arguments; std::vector > ListDetailSM::Arguments; std::vector > ListBlendTransparent::Arguments; std::vector > ListAdditiveTransparent::Arguments; +std::vector > ListDisplacement::Arguments; std::vector > ListBlendTransparentFog::Arguments; std::vector > ListAdditiveTransparentFog::Arguments; std::vector > ListGrassSM::Arguments; diff --git a/src/graphics/stkmesh.hpp b/src/graphics/stkmesh.hpp index 40c861eff..6179a2c28 100644 --- a/src/graphics/stkmesh.hpp +++ b/src/graphics/stkmesh.hpp @@ -39,6 +39,7 @@ enum TransparentMaterial TM_DEFAULT, TM_ADDITIVE, TM_BUBBLE, + TM_DISPLACEMENT, TM_COUNT }; @@ -198,11 +199,17 @@ public: static std::vector > Arguments; }; +class ListDisplacement +{ +public: + static std::vector > Arguments; +}; + // Forward pass (for transparents meshes) void drawBubble(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix); GeometricMaterial MaterialTypeToGeometricMaterial(video::E_MATERIAL_TYPE, video::E_VERTEX_TYPE); ShadedMaterial MaterialTypeToShadedMaterial(video::E_MATERIAL_TYPE, irr::video::ITexture **textures, video::E_VERTEX_TYPE tp); -TransparentMaterial MaterialTypeToTransparentMaterial(video::E_MATERIAL_TYPE, f32 MaterialTypeParam); +TransparentMaterial MaterialTypeToTransparentMaterial(video::E_MATERIAL_TYPE, f32 MaterialTypeParam, bool isDisplacement); #endif // STKMESH_H diff --git a/src/graphics/stkmeshscenenode.cpp b/src/graphics/stkmeshscenenode.cpp index e843b8d8d..2938a3c6a 100644 --- a/src/graphics/stkmeshscenenode.cpp +++ b/src/graphics/stkmeshscenenode.cpp @@ -63,7 +63,7 @@ void STKMeshSceneNode::setFirstTimeMaterial() GLMesh &mesh = GLmeshes[i]; if (rnd->isTransparent()) { - TransparentMaterial TranspMat = MaterialTypeToTransparentMaterial(type, MaterialTypeParam); + TransparentMaterial TranspMat = MaterialTypeToTransparentMaterial(type, MaterialTypeParam, isDisplacement); if (immediate_draw) { fillLocalBuffer(mesh, mb); @@ -145,49 +145,6 @@ void STKMeshSceneNode::drawGlow(const GLMesh &mesh) glDrawElementsBaseVertex(ptype, count, itype, (GLvoid *)mesh.vaoOffset, mesh.vaoBaseVertex); } -static video::ITexture *displaceTex = 0; - -void STKMeshSceneNode::drawDisplace(const GLMesh &mesh) -{ - if (mesh.VAOType != video::EVT_2TCOORDS) - { - Log::error("Materials", "Displacement has wrong vertex type"); - return; - } - glBindVertexArray(getVAO(video::EVT_2TCOORDS)); - DisplaceProvider * const cb = (DisplaceProvider *)irr_driver->getCallback(ES_DISPLACE); - - GLenum ptype = mesh.PrimitiveType; - GLenum itype = mesh.IndexType; - size_t count = mesh.IndexCount; - - // Generate displace mask - // Use RTT_TMP4 as displace mask - irr_driver->getFBO(FBO_TMP1_WITH_DS).Bind(); - - glUseProgram(MeshShader::DisplaceMaskShader::Program); - MeshShader::DisplaceMaskShader::setUniforms(AbsoluteTransformation); - glDrawElementsBaseVertex(ptype, count, itype, (GLvoid *)mesh.vaoOffset, mesh.vaoBaseVertex); - - // Render the effect - if (!displaceTex) - displaceTex = irr_driver->getTexture(FileManager::TEXTURE, "displace.png"); - irr_driver->getFBO(FBO_DISPLACE).Bind(); - setTexture(0, getTextureGLuint(displaceTex), GL_LINEAR, GL_LINEAR, true); - setTexture(1, irr_driver->getRenderTargetTexture(RTT_TMP1), GL_LINEAR, GL_LINEAR, true); - setTexture(2, irr_driver->getRenderTargetTexture(RTT_COLOR), GL_LINEAR, GL_LINEAR, true); - setTexture(3, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR, true); - glUseProgram(MeshShader::DisplaceShader::Program); - MeshShader::DisplaceShader::setUniforms(AbsoluteTransformation, - core::vector2df(cb->getDirX(), cb->getDirY()), - core::vector2df(cb->getDir2X(), cb->getDir2Y()), - core::vector2df(float(UserConfigParams::m_width), - float(UserConfigParams::m_height)), - 0, 1, 2, 3); - - glDrawElementsBaseVertex(ptype, count, itype, (GLvoid *)mesh.vaoOffset, mesh.vaoBaseVertex); -} - void STKMeshSceneNode::updatevbo() { for (unsigned i = 0; i < Mesh->getMeshBufferCount(); ++i) @@ -477,6 +434,9 @@ void STKMeshSceneNode::render() ListAdditiveTransparent::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix)); } + for_in(mesh, TransparentMesh[TM_DISPLACEMENT]) + ListDisplacement::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation)); + if (!TransparentMesh[TM_BUBBLE].empty()) glUseProgram(MeshShader::BubbleShader::Program); glBindVertexArray(getVAO(video::EVT_STANDARD)); @@ -484,15 +444,4 @@ void STKMeshSceneNode::render() drawBubble(*mesh, ModelViewProjectionMatrix); return; } - - if (irr_driver->getPhase() == DISPLACEMENT_PASS) - { - for (u32 i = 0; i < Mesh->getMeshBufferCount(); ++i) - { - scene::IMeshBuffer* mb = Mesh->getMeshBuffer(i); - if (!mb) - continue; - drawDisplace(GLmeshes[i]); - } - } } diff --git a/src/graphics/stkmeshscenenode.hpp b/src/graphics/stkmeshscenenode.hpp index 7ec0c0281..870423ae1 100644 --- a/src/graphics/stkmeshscenenode.hpp +++ b/src/graphics/stkmeshscenenode.hpp @@ -17,7 +17,6 @@ protected: // Misc passes shaders (glow, displace...) void drawGlow(const GLMesh &mesh); - void drawDisplace(const GLMesh &mesh); void createGLMeshes(); void cleanGLMeshes(); void setFirstTimeMaterial(); diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index 0b03e0bed..f64c4b2af 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -377,7 +377,6 @@ void Track::cleanup() irr_driver->clearGlowingNodes(); irr_driver->clearLights(); irr_driver->clearForcedBloom(); - irr_driver->clearDisplacingNodes(); irr_driver->clearBackgroundNodes(); if(UserConfigParams::logMemory())