From 621b8bdf86412786e561a5e25fb7583ed61e2b0d Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Wed, 26 Apr 2017 12:04:35 +0200 Subject: Tweak 2D line dashed shader to take a scale parameter. Even though in some cases this does not seems useful, in others (like zommed 2D views) we have to correct MVP matrix scaling to get fixed dashes size. Note that we could do that differently (commented about it in shader), would also have been cleaner to extract that MVP scale from within the shader, but there does not seem to be a way to initialize uniform values from within a shader, and would rather avoid recomputing the scale for every run (especially since 3DViewport does not need it e.g.). Part of D2647. --- source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl | 10 +++++++++- source/blender/windowmanager/intern/wm_gesture.c | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl index 7b4207142e6..94e8de4537b 100644 --- a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl @@ -3,6 +3,7 @@ // Based on a (3D) version by Mike Erwin. uniform mat4 ModelViewProjectionMatrix; +uniform float view_scale; #if __VERSION__ == 120 attribute vec2 pos; @@ -18,5 +19,12 @@ void main() { gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0); - distance_along_line = distance(line_origin, pos); + distance_along_line = distance(line_origin, pos) * view_scale; + + /* Another solution would be to compute line_origin in fragment coordinate, and then compute distance + * in fragment shader, but we'd need opengl window size for that... + * vec4 point = ModelViewProjectionMatrix * vec4(line_origin, 0.0, 1.0); + * ref_point = (point.xy / point.w) * 0.5 + 0.5; // <- device coordinates in [0..1] range. + * ref_point = ref_point * window_size; + */ } diff --git a/source/blender/windowmanager/intern/wm_gesture.c b/source/blender/windowmanager/intern/wm_gesture.c index 84c98f4071d..38b843772ab 100644 --- a/source/blender/windowmanager/intern/wm_gesture.c +++ b/source/blender/windowmanager/intern/wm_gesture.c @@ -178,6 +178,7 @@ static void wm_gesture_draw_line(wmGesture *gt) immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR); + immUniform1f("view_scale", 1.0f); immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f); immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f); immUniform1f("dash_width", 8.0f); @@ -240,6 +241,7 @@ static void wm_gesture_draw_rect(wmGesture *gt) immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR); + immUniform1f("view_scale", 1.0f); immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f); immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f); immUniform1f("dash_width", 8.0f); @@ -301,6 +303,7 @@ static void wm_gesture_draw_circle(wmGesture *gt) immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR); + immUniform1f("view_scale", 1.0f); immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f); immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f); immUniform1f("dash_width", 4.0f); @@ -401,7 +404,7 @@ static void wm_gesture_draw_lasso(wmWindow *win, wmGesture *gt, bool filled) numverts++; } - /* Nothing to drawe, do early output. */ + /* Nothing to draw, do early output. */ if (numverts < 2) { return; } @@ -412,6 +415,7 @@ static void wm_gesture_draw_lasso(wmWindow *win, wmGesture *gt, bool filled) immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR); + immUniform1f("view_scale", 1.0f); immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f); immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f); immUniform1f("dash_width", 2.0f); @@ -456,6 +460,7 @@ static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt) immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR); + immUniform1f("view_scale", 1.0f); immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f); immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f); immUniform1f("dash_width", 8.0f); -- cgit v1.2.3