From 8add817086575d6aa08550e857efb00c233b3fa3 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Mon, 6 Apr 2020 20:50:26 +0100 Subject: [PATCH] Fix depth issues --- GL/draw.c | 10 +++++++--- GL/state.c | 8 ++++---- samples/depth_funcs/main.c | 27 ++++++++++++++++++++++++++- samples/zclip/main.c | 4 ++-- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/GL/draw.c b/GL/draw.c index 34186a1..b31ebec 100644 --- a/GL/draw.c +++ b/GL/draw.c @@ -1172,10 +1172,14 @@ GL_FORCE_INLINE void divide(SubmissionTarget* target) { float f = MATH_Fast_Invert(vertex->w); vertex->xyz[0] *= f; vertex->xyz[1] *= f; - vertex->xyz[2] = vertex->w; - /* FIXME: Consider taking glDepthRange into account. PVR is designed to use invW rather - * than Z which is unlike most GPUs - this apparently provides advantages. + /* We have to use 1/z instead of reusing 1/w from above because + * orthographic projections always product a W == 1 and so would not + * correctly render ordered transparent surfaces */ + vertex->xyz[2] = MATH_Fast_Invert(vertex->xyz[2]); + + /* FIXME: Consider taking glDepthRange into account. PVR is designed to use 1/w + * which is unlike most GPUs - this apparently provides advantages. * * This can be done (if Z is between -1 and 1) with: * diff --git a/GL/state.c b/GL/state.c index d6e18d8..a705526 100644 --- a/GL/state.c +++ b/GL/state.c @@ -63,17 +63,17 @@ static int _calc_pvr_depth_test() { case GL_NEVER: return PVR_DEPTHCMP_NEVER; case GL_LESS: - return PVR_DEPTHCMP_LESS; + return PVR_DEPTHCMP_GREATER; case GL_EQUAL: return PVR_DEPTHCMP_EQUAL; case GL_LEQUAL: - return PVR_DEPTHCMP_LEQUAL; + return PVR_DEPTHCMP_GEQUAL; case GL_GREATER: - return PVR_DEPTHCMP_GREATER; + return PVR_DEPTHCMP_LESS; case GL_NOTEQUAL: return PVR_DEPTHCMP_NOTEQUAL; case GL_GEQUAL: - return PVR_DEPTHCMP_GEQUAL; + return PVR_DEPTHCMP_LEQUAL; break; case GL_ALWAYS: default: diff --git a/samples/depth_funcs/main.c b/samples/depth_funcs/main.c index acf1952..aab0184 100644 --- a/samples/depth_funcs/main.c +++ b/samples/depth_funcs/main.c @@ -7,7 +7,7 @@ void InitGL(int Width, int Height) // We call this right after our OpenG { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black glClearDepth(1.0); // Enables Clearing Of The Depth Buffer - glDepthFunc(GL_LESS); // The Type Of Depth Test To Do + glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do glEnable(GL_DEPTH_TEST); // Enables Depth Testing glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading @@ -60,22 +60,47 @@ void DrawGLScene() glDepthFunc(GL_LEQUAL); DrawSquare(1.0, 1, 0, 0, -5.0f); + glPushMatrix(); + glTranslatef(0, -1.5, 0); + DrawSquare(1.0, 1, 0, 0, -4.9f); + glPopMatrix(); + glTranslatef(1.1, 0, 0); glDepthFunc(GL_EQUAL); DrawSquare(1.0, 1, 0, 0, -5.0f); + glPushMatrix(); + glTranslatef(0, -1.5, 0); + DrawSquare(1.0, 1, 0, 0, -5.0f); + glPopMatrix(); + glTranslatef(1.1, 0, 0); glDepthFunc(GL_GEQUAL); DrawSquare(1.0, 1, 0, 0, -5.0f); + glPushMatrix(); + glTranslatef(0, -1.5, 0); + DrawSquare(1.0, 1, 0, 0, -5.1f); + glPopMatrix(); + glTranslatef(1.1, 0, 0); glDepthFunc(GL_LESS); DrawSquare(1.0, 1, 0, 0, -4.9f); + glPushMatrix(); + glTranslatef(0, -1.5, 0); + DrawSquare(1.0, 1, 0, 0, -4.8f); + glPopMatrix(); + glTranslatef(1.1, 0, 0); glDepthFunc(GL_GREATER); DrawSquare(1.0, 1, 0, 0, -5.1f); + glPushMatrix(); + glTranslatef(0, -1.5, 0); + DrawSquare(1.0, 1, 0, 0, -5.2f); + glPopMatrix(); + // swap buffers to display, since we're double buffered. glKosSwapBuffers(); } diff --git a/samples/zclip/main.c b/samples/zclip/main.c index 8da2498..cbe2148 100644 --- a/samples/zclip/main.c +++ b/samples/zclip/main.c @@ -137,9 +137,9 @@ void InitGL(int Width, int Height) // We call this right after our OpenG { LoadGLTextures(); - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // This Will Clear The Background Color To Black glClearDepth(1.0); // Enables Clearing Of The Depth Buffer - glDepthFunc(GL_LESS); // The Type Of Depth Test To Do + glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do glEnable(GL_DEPTH_TEST); // Enables Depth Testing glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading glEnable(GL_TEXTURE_2D);