From 4774e65ed913729605edf4306ee20d81de6afb35 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 4 Jan 2025 18:10:29 +0100 Subject: [PATCH] Implement polygon offset in GL3 driver --- irr/include/SMaterial.h | 6 +----- irr/src/OpenGL/Driver.cpp | 12 +++++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/irr/include/SMaterial.h b/irr/include/SMaterial.h index 867557154..2329e2761 100644 --- a/irr/include/SMaterial.h +++ b/irr/include/SMaterial.h @@ -304,11 +304,7 @@ public: //! A constant z-buffer offset for a polygon/line/point /** The range of the value is driver specific. - On OpenGL you get units which are multiplied by the smallest value that is guaranteed to produce a resolvable offset. - On D3D9 you can pass a range between -1 and 1. But you should likely divide it by the range of the depthbuffer. - Like dividing by 65535.0 for a 16 bit depthbuffer. Thought it still might produce too large of a bias. - Some article (https://aras-p.info/blog/2008/06/12/depth-bias-and-the-power-of-deceiving-yourself/) - recommends multiplying by 2.0*4.8e-7 (and strangely on both 16 bit and 24 bit). */ + On OpenGL you get units which are multiplied by the smallest value that is guaranteed to produce a resolvable offset. */ f32 PolygonOffsetDepthBias; //! Variable Z-Buffer offset based on the slope of the polygon. diff --git a/irr/src/OpenGL/Driver.cpp b/irr/src/OpenGL/Driver.cpp index 31fb7a177..dec24c824 100644 --- a/irr/src/OpenGL/Driver.cpp +++ b/irr/src/OpenGL/Driver.cpp @@ -1313,7 +1313,17 @@ void COpenGL3DriverBase::setBasicRenderStates(const SMaterial &material, const S getGLBlend(srcAlphaFact), getGLBlend(dstAlphaFact)); } - // TODO: Polygon Offset. Not sure if it was left out deliberately or if it won't work with this driver. + // Polygon Offset + if (resetAllRenderStates || + lastmaterial.PolygonOffsetDepthBias != material.PolygonOffsetDepthBias || + lastmaterial.PolygonOffsetSlopeScale != material.PolygonOffsetSlopeScale) { + if (material.PolygonOffsetDepthBias || material.PolygonOffsetSlopeScale) { + GL.Enable(GL.POLYGON_OFFSET_FILL); + GL.PolygonOffset(material.PolygonOffsetSlopeScale, material.PolygonOffsetDepthBias); + } else { + GL.Disable(GL.POLYGON_OFFSET_FILL); + } + } if (resetAllRenderStates || lastmaterial.Thickness != material.Thickness) GL.LineWidth(core::clamp(static_cast(material.Thickness), DimAliasedLine[0], DimAliasedLine[1]));