diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2017-03-25 21:02:04 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2017-03-25 21:11:01 +0300 |
commit | d2c94c78736e84c1318859c8e902b3ee622fd6f0 (patch) | |
tree | 7cfa4c26c527328a8ab1eb6e99f59ffe666636a8 /source/blender/draw/modes | |
parent | 8f4d58de4a4e5d596c309bff1475b5cac498a33f (diff) |
New Grid: Fix depth fighting and remove some unused variables.
Diffstat (limited to 'source/blender/draw/modes')
-rw-r--r-- | source/blender/draw/modes/object_mode.c | 25 | ||||
-rw-r--r-- | source/blender/draw/modes/shaders/object_grid_frag.glsl | 1 | ||||
-rw-r--r-- | source/blender/draw/modes/shaders/object_grid_vert.glsl | 7 |
3 files changed, 24 insertions, 9 deletions
diff --git a/source/blender/draw/modes/object_mode.c b/source/blender/draw/modes/object_mode.c index 40fabe04049..bf431a61728 100644 --- a/source/blender/draw/modes/object_mode.c +++ b/source/blender/draw/modes/object_mode.c @@ -163,6 +163,7 @@ static struct { struct GPUShader *grid_sh; float camera_pos[3]; float grid_settings[4]; + float grid_mat[4][4]; } e_data = {NULL}; /* Engine data */ /* *********** FUNCTIONS *********** */ @@ -205,10 +206,13 @@ static void OBJECT_engine_init(void) } { + float viewinvmat[4][4], winmat[4][4], viewmat[4][4]; + DRW_viewport_matrix_get(winmat, DRW_MAT_WIN); + DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEW); + DRW_viewport_matrix_get(viewinvmat, DRW_MAT_VIEWINV); + /* Setup camera pos */ - float viewmat[4][4]; - DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEWINV); - copy_v3_v3(e_data.camera_pos, viewmat[3]); + copy_v3_v3(e_data.camera_pos, viewinvmat[3]); /* grid settings */ const bContext *C = DRW_get_context(); @@ -219,6 +223,20 @@ static void OBJECT_engine_init(void) e_data.grid_settings[1] = 2.0f; /* gridResolution */ e_data.grid_settings[2] = ED_view3d_grid_scale(scene, v3d, NULL); /* gridScale */ e_data.grid_settings[3] = v3d->gridsubdiv; /* gridSubdiv */ + + /* Grid matrix polygon offset (fix depth fighting) */ + /* see ED_view3d_polygon_offset */ + float offs; + if (winmat[3][3] > 0.5f) { + float viewdist = 1.0f / max_ff(fabsf(winmat[0][0]), fabsf(winmat[1][1])); + offs = 0.00001f * viewdist; + } + else { + offs = winmat[3][2] * -0.0025f; + } + winmat[3][2] -= offs; + + mul_m4_m4m4(e_data.grid_mat, winmat, viewmat); } } @@ -365,6 +383,7 @@ static void OBJECT_cache_init(void) struct Batch *quad = DRW_cache_fullscreen_quad_get(); DRWShadingGroup *grp = DRW_shgroup_create(e_data.grid_sh, psl->grid); + DRW_shgroup_uniform_mat4(grp, "ViewProjectionOffsetMatrix", (float *)e_data.grid_mat); DRW_shgroup_uniform_vec3(grp, "cameraPos", e_data.camera_pos, 1); DRW_shgroup_uniform_vec4(grp, "gridSettings", e_data.grid_settings, 1); DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo, 0); diff --git a/source/blender/draw/modes/shaders/object_grid_frag.glsl b/source/blender/draw/modes/shaders/object_grid_frag.glsl index e4fba09c80e..0f7be9fea3e 100644 --- a/source/blender/draw/modes/shaders/object_grid_frag.glsl +++ b/source/blender/draw/modes/shaders/object_grid_frag.glsl @@ -3,7 +3,6 @@ * Clément Foucault */ in vec3 wPos; -in float viewDist; out vec4 FragColor; diff --git a/source/blender/draw/modes/shaders/object_grid_vert.glsl b/source/blender/draw/modes/shaders/object_grid_vert.glsl index ef457c01284..3c6f0a3bd25 100644 --- a/source/blender/draw/modes/shaders/object_grid_vert.glsl +++ b/source/blender/draw/modes/shaders/object_grid_vert.glsl @@ -2,18 +2,15 @@ /* Infinite grid * Clément Foucault */ -uniform mat4 ViewProjectionMatrix; -uniform mat4 ViewMatrix; +uniform mat4 ViewProjectionOffsetMatrix; in vec3 pos; out vec3 wPos; -out float viewDist; void main() { vec3 realPos = pos * 1e3; - gl_Position = ViewProjectionMatrix * vec4(realPos, 1.0); - viewDist = -(ViewMatrix * vec4(realPos, 1.0)).z; + gl_Position = ViewProjectionOffsetMatrix * vec4(realPos, 1.0); wPos = realPos; } |