From 9d9684d7bf3d50b2a40e8c01d4f4a4bf084b5f00 Mon Sep 17 00:00:00 2001 From: Mathias Paulin Date: Tue, 29 Aug 2023 08:52:24 +0200 Subject: [PATCH] [engine] fix culling when material is singleSided --- src/Engine/Data/GLTFMaterial.hpp | 2 +- src/Engine/Data/Material.hpp | 5 +++++ src/Engine/Rendering/RenderObject.cpp | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Engine/Data/GLTFMaterial.hpp b/src/Engine/Data/GLTFMaterial.hpp index de668d7322e..b59a70d54ac 100644 --- a/src/Engine/Data/GLTFMaterial.hpp +++ b/src/Engine/Data/GLTFMaterial.hpp @@ -302,7 +302,7 @@ class RA_ENGINE_API GLTFMaterial : public Material, public ParameterSetEditingIn float getAlphaCutoff() const { return m_alphaCutoff; } void setAlphaCutoff( float alphaCutoff ) { m_alphaCutoff = alphaCutoff; } - bool isDoubleSided() const { return m_doubleSided; } + bool isDoubleSided() const override { return m_doubleSided; } void setDoubleSided( bool doubleSided ) { m_doubleSided = doubleSided; } float getIndexOfRefraction() const { return m_indexOfRefraction; } diff --git a/src/Engine/Data/Material.hpp b/src/Engine/Data/Material.hpp index bc6b64bc262..ce260c9a622 100644 --- a/src/Engine/Data/Material.hpp +++ b/src/Engine/Data/Material.hpp @@ -83,6 +83,11 @@ class RA_ENGINE_API Material : public Data::ShaderParameterProvider */ virtual bool isTransparent() const; + /** Test if material is transperent. + * @return true if the material is transparent + */ + virtual bool isDoubleSided() const { return true; } + /** * Get the list of properties the material migh use in a shader. * each property will be added to the shader used for rendering this material under the form diff --git a/src/Engine/Rendering/RenderObject.cpp b/src/Engine/Rendering/RenderObject.cpp index 56bd674eae9..3b0daab98e9 100644 --- a/src/Engine/Rendering/RenderObject.cpp +++ b/src/Engine/Rendering/RenderObject.cpp @@ -285,7 +285,11 @@ void RenderObject::render( const Data::RenderParameters& lightParams, // Note that this hack implies the inclusion of OpenGL.h in this file if ( viewParams.viewMatrix.determinant() < 0 ) { glFrontFace( GL_CW ); } else { glFrontFace( GL_CCW ); } + // To enable correct culling when required (e.g. by gltf material) + GLboolean cullEnable = glIsEnabled( GL_CULL_FACE ); + if ( !m_material->isDoubleSided() ) { glEnable( GL_CULL_FACE ); } m_mesh->render( shader ); + if ( !cullEnable ) glDisable( GL_CULL_FACE ); } void RenderObject::render( const Data::RenderParameters& lightParams,