From 0abc4a258a31c62473d2235f2f5c072817a29c75 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sat, 23 Feb 2019 11:33:39 -0800 Subject: [PATCH] Enabled polygon offset (with a smaller offset) for opaque triangles so they don't z fight with lines. --- common/lc_context.cpp | 36 +++++++++++++++++++++++++++++++----- common/lc_context.h | 9 +++++++++ common/lc_scene.cpp | 2 ++ qt/lc_qglwidget.cpp | 3 --- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/common/lc_context.cpp b/common/lc_context.cpp index 889fe059..1a00163a 100644 --- a/common/lc_context.cpp +++ b/common/lc_context.cpp @@ -35,6 +35,7 @@ lcContext::lcContext() mTexture2D = 0; mTexture2DMS = 0; mTextureCubeMap = 0; + mPolygonOffset = LC_POLYGON_OFFSET_NONE; mLineWidth = 1.0f; #ifndef LC_OPENGLES mMatrixMode = GL_MODELVIEW; @@ -241,9 +242,6 @@ void lcContext::SetDefaultState() else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDisable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(0.5f, 0.1f); - if (gSupportsVertexBufferObject) { glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); @@ -294,6 +292,9 @@ void lcContext::SetDefaultState() glBindTexture(GL_TEXTURE_CUBE_MAP, 0); mTextureCubeMap = 0; + glDisable(GL_POLYGON_OFFSET_FILL); + mPolygonOffset = LC_POLYGON_OFFSET_NONE; + glLineWidth(1.0f); mLineWidth = 1.0f; @@ -387,6 +388,31 @@ void lcContext::SetViewport(int x, int y, int Width, int Height) glViewport(x, y, Width, Height); } +void lcContext::SetPolygonOffset(lcPolygonOffset PolygonOffset) +{ + if (mPolygonOffset == PolygonOffset) + return; + + switch (PolygonOffset) + { + case LC_POLYGON_OFFSET_NONE: + glDisable(GL_POLYGON_OFFSET_FILL); + break; + + case LC_POLYGON_OFFSET_OPAQUE: + glPolygonOffset(0.25f, 0.1f); + glEnable(GL_POLYGON_OFFSET_FILL); + break; + + case LC_POLYGON_OFFSET_TRANSLUCENT: + glPolygonOffset(0.5f, 0.1f); + glEnable(GL_POLYGON_OFFSET_FILL); + break; + } + + mPolygonOffset = PolygonOffset; +} + void lcContext::SetLineWidth(float LineWidth) { if (LineWidth == mLineWidth) @@ -408,14 +434,14 @@ void lcContext::BeginTranslucent() { glEnable(GL_BLEND); glDepthMask(GL_FALSE); - glEnable(GL_POLYGON_OFFSET_FILL); + SetPolygonOffset(LC_POLYGON_OFFSET_TRANSLUCENT); } void lcContext::EndTranslucent() { glDepthMask(GL_TRUE); glDisable(GL_BLEND); - glDisable(GL_POLYGON_OFFSET_FILL); + SetPolygonOffset(LC_POLYGON_OFFSET_OPAQUE); } void lcContext::BindTexture2D(GLuint Texture) diff --git a/common/lc_context.h b/common/lc_context.h index 242ddd56..64f5b227 100644 --- a/common/lc_context.h +++ b/common/lc_context.h @@ -100,6 +100,13 @@ public: int mHeight = 0; }; +enum lcPolygonOffset +{ + LC_POLYGON_OFFSET_NONE, + LC_POLYGON_OFFSET_OPAQUE, + LC_POLYGON_OFFSET_TRANSLUCENT +}; + class lcContext { public: @@ -139,6 +146,7 @@ public: void SetMaterial(lcMaterialType MaterialType); void SetViewport(int x, int y, int Width, int Height); + void SetPolygonOffset(lcPolygonOffset PolygonOffset); void SetLineWidth(float LineWidth); void SetSmoothShading(bool Smooth); void BeginTranslucent(); @@ -222,6 +230,7 @@ protected: GLuint mTexture2D; GLuint mTexture2DMS; GLuint mTextureCubeMap; + lcPolygonOffset mPolygonOffset; float mLineWidth; int mMatrixMode; bool mTextureEnabled; diff --git a/common/lc_scene.cpp b/common/lc_scene.cpp index fe9af45c..eff2d121 100644 --- a/common/lc_scene.cpp +++ b/common/lc_scene.cpp @@ -72,6 +72,8 @@ void lcScene::DrawRenderMeshes(lcContext* Context, int PrimitiveTypes, bool Enab if (DrawTranslucent) Context->BeginTranslucent(); + else + Context->SetPolygonOffset(LC_POLYGON_OFFSET_OPAQUE); for (int MeshIndex : Meshes) { diff --git a/qt/lc_qglwidget.cpp b/qt/lc_qglwidget.cpp index 204a89c2..3a208aec 100644 --- a/qt/lc_qglwidget.cpp +++ b/qt/lc_qglwidget.cpp @@ -159,9 +159,6 @@ QSize lcQGLWidget::sizeHint() const void lcQGLWidget::initializeGL() { - glDisable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(0.5f, 0.1f); - glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDepthMask(GL_TRUE);