diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2020-07-16 21:24:05 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2020-07-18 04:43:51 +0300 |
commit | 02c09773ea07c8c0a3d7be63abdfd963a9452402 (patch) | |
tree | 5cd052db8fc0dd71072f90dd22175e314a134f5c /source/blender/gpu | |
parent | acad0d75b1448550c863ac8002bba4284d30d10d (diff) |
Cleanup: GPU: rename bglPolygonOffset to GPU_polygon_offset
And move it to GPU module.
Diffstat (limited to 'source/blender/gpu')
-rw-r--r-- | source/blender/gpu/GPU_matrix.h | 4 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_draw.c | 1 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_matrix.c | 66 |
3 files changed, 71 insertions, 0 deletions
diff --git a/source/blender/gpu/GPU_matrix.h b/source/blender/gpu/GPU_matrix.h index 2899fba46e4..56d640ea0e4 100644 --- a/source/blender/gpu/GPU_matrix.h +++ b/source/blender/gpu/GPU_matrix.h @@ -147,6 +147,10 @@ const float (*GPU_matrix_normal_inverse_get(float m[3][3]))[3]; void GPU_matrix_bind(const struct GPUShaderInterface *); bool GPU_matrix_dirty_get(void); /* since last bind */ +/* own working polygon offset */ +float GPU_polygon_offset_calc(const float (*winmat)[4], float viewdist, float dist); +void GPU_polygon_offset(float viewdist, float dist); + /* Python API needs to be able to inspect the stack so errors raise exceptions * instead of crashing. */ #ifdef USE_GPU_PY_MATRIX_API diff --git a/source/blender/gpu/intern/gpu_draw.c b/source/blender/gpu/intern/gpu_draw.c index f07e3ed70d7..7e6d47636f6 100644 --- a/source/blender/gpu/intern/gpu_draw.c +++ b/source/blender/gpu/intern/gpu_draw.c @@ -55,6 +55,7 @@ #include "GPU_draw.h" #include "GPU_extensions.h" #include "GPU_glew.h" +#include "GPU_matrix.h" #include "GPU_platform.h" #include "GPU_texture.h" diff --git a/source/blender/gpu/intern/gpu_matrix.c b/source/blender/gpu/intern/gpu_matrix.c index 2687f56ad27..8df7c7c5d2f 100644 --- a/source/blender/gpu/intern/gpu_matrix.c +++ b/source/blender/gpu/intern/gpu_matrix.c @@ -738,3 +738,69 @@ int GPU_matrix_stack_level_get_projection(void) } /** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Polygon Offset Hack + * + * Workaround the fact that PolygonOffset is implementation dependant. + * We modify the projection matrix (winmat) in order to change the final depth a tiny amount. + * \{ */ + +float GPU_polygon_offset_calc(const float (*winmat)[4], float viewdist, float dist) +{ + /* Seems like we have a factor of 2 more offset than 2.79 for some reason. Correct for this. */ + dist *= 0.5f; + + if (winmat[3][3] > 0.5f) { +#if 1 + return 0.00001f * dist * viewdist; // ortho tweaking +#else + static float depth_fac = 0.0f; + if (depth_fac == 0.0f) { + int depthbits; + glGetIntegerv(GL_DEPTH_BITS, &depthbits); + depth_fac = 1.0f / (float)((1 << depthbits) - 1); + } + offs = (-1.0 / winmat[2][2]) * dist * depth_fac; + + UNUSED_VARS(viewdist); +#endif + } + + /* This adjustment effectively results in reducing the Z value by 0.25%. + * + * winmat[4][3] actually evaluates to `-2 * far * near / (far - near)`, + * is very close to -0.2 with default clip range, + * and is used as the coefficient multiplied by `w / z`, + * thus controlling the z dependent part of the depth value. + */ + return winmat[3][2] * -0.0025f * dist; +} + +/** + * \note \a viewdist is only for ortho at the moment. + */ +void GPU_polygon_offset(float viewdist, float dist) +{ + static float winmat[4][4], offset = 0.0f; + + if (dist != 0.0f) { + /* hack below is to mimic polygon offset */ + GPU_matrix_projection_get(winmat); + + /* dist is from camera to center point */ + + float offs = GPU_polygon_offset_calc(winmat, viewdist, dist); + + winmat[3][2] -= offs; + offset += offs; + } + else { + winmat[3][2] += offset; + offset = 0.0; + } + + GPU_matrix_projection_set(winmat); +} + +/** \} */ |