Plugin displacement material.

This commit is contained in:
vlj 2014-07-14 01:12:56 +02:00
parent f00c55850a
commit f1ddf87a5f
9 changed files with 91 additions and 106 deletions

View File

@ -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<GlowData>& glows);
void renderSSAO();
void renderLights(unsigned pointlightCount);
void renderDisplacement();
void renderShadowsDebug();
void doScreenShot();
public:

View File

@ -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);
}
}

View File

@ -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::TransparentShader, video::EVT_STANDARD>(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<typename T, enum E_VERTEX_TYPE VertexType, typename... Args>

View File

@ -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

View File

@ -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<std::tuple<GLMesh *, core::matrix4> > ListUnlitSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, video::SColorf> > ListDetailSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListBlendTransparent::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListAdditiveTransparent::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4> > ListDisplacement::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, float, float, float, float, float, video::SColorf> > ListBlendTransparentFog::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, float, float, float, float, float, video::SColorf> > ListAdditiveTransparentFog::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::vector3df, video::SColorf> > ListGrassSM::Arguments;

View File

@ -39,6 +39,7 @@ enum TransparentMaterial
TM_DEFAULT,
TM_ADDITIVE,
TM_BUBBLE,
TM_DISPLACEMENT,
TM_COUNT
};
@ -198,11 +199,17 @@ public:
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, float, float, float, float, float, video::SColorf> > Arguments;
};
class ListDisplacement
{
public:
static std::vector<std::tuple<GLMesh *, core::matrix4> > 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

View File

@ -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]);
}
}
}

View File

@ -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();

View File

@ -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())