diff options
author | Mike Erwin <significant.bit@gmail.com> | 2016-10-08 10:10:59 +0300 |
---|---|---|
committer | Mike Erwin <significant.bit@gmail.com> | 2016-10-08 10:11:53 +0300 |
commit | 25e4dc45e55c3b47c953f33574d268d4ba978f9c (patch) | |
tree | 1eea19701f3dae280cee29560fdc83a98627f6de /source/blender | |
parent | b071ac315cb7f068372073d6f5bae6e771a5d860 (diff) |
OpenGL: shaders for smooth round points
Solid color with an optional outline.
size (diameter) and outlineWidth are in pixels.
Diffstat (limited to 'source/blender')
7 files changed, 149 insertions, 0 deletions
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 9d747bac980..6b23bfc4642 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -136,11 +136,15 @@ data_to_c_simple(shaders/gpu_shader_3D_smooth_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_smooth_color_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_point_uniform_color_frag.glsl SRC) +data_to_c_simple(shaders/gpu_shader_point_uniform_color_smooth_frag.glsl SRC) +data_to_c_simple(shaders/gpu_shader_point_uniform_color_outline_smooth_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_point_varying_color_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_point_fixed_size_varying_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_point_varying_size_no_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_point_varying_size_varying_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_2D_point_varying_size_varying_color_vert.glsl SRC) +data_to_c_simple(shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl SRC) +data_to_c_simple(shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_text_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_text_frag.glsl SRC) diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h index 9fc1f29d98f..8ccaa9d470e 100644 --- a/source/blender/gpu/GPU_shader.h +++ b/source/blender/gpu/GPU_shader.h @@ -106,6 +106,8 @@ typedef enum GPUBuiltinShader { GPU_SHADER_2D_TEXTURE_RECT, /* points */ GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR, + GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_SMOOTH, + GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_SMOOTH, GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR, GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR, GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR, diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c index 1b9747b78db..2ad3927d8c6 100644 --- a/source/blender/gpu/intern/gpu_shader.c +++ b/source/blender/gpu/intern/gpu_shader.c @@ -62,11 +62,15 @@ extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[]; extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[]; extern char datatoc_gpu_shader_point_uniform_color_frag_glsl[]; +extern char datatoc_gpu_shader_point_uniform_color_smooth_frag_glsl[]; +extern char datatoc_gpu_shader_point_uniform_color_outline_smooth_frag_glsl[]; extern char datatoc_gpu_shader_point_varying_color_frag_glsl[]; extern char datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl[]; extern char datatoc_gpu_shader_3D_point_varying_size_no_color_vert_glsl[]; extern char datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl[]; extern char datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl[]; +extern char datatoc_gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert_glsl[]; +extern char datatoc_gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert_glsl[]; extern char datatoc_gpu_shader_text_vert_glsl[]; extern char datatoc_gpu_shader_text_frag_glsl[]; @@ -113,6 +117,8 @@ static struct GPUShadersGlobal { /* points */ GPUShader *point_fixed_size_uniform_color_2D; GPUShader *point_varying_size_varying_color_2D; + GPUShader *point_uniform_size_uniform_color_smooth_2D; + GPUShader *point_uniform_size_uniform_color_outline_smooth_2D; GPUShader *point_fixed_size_uniform_color_3D; GPUShader *point_fixed_size_varying_color_3D; GPUShader *point_varying_size_uniform_color_3D; @@ -739,6 +745,22 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader) NULL, NULL, NULL, 0, 0, 0); retval = GG.shaders.point_varying_size_varying_color_2D; break; + case GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_SMOOTH: + if (!GG.shaders.point_uniform_size_uniform_color_smooth_2D) + GG.shaders.point_uniform_size_uniform_color_smooth_2D = GPU_shader_create( + datatoc_gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert_glsl, + datatoc_gpu_shader_point_uniform_color_smooth_frag_glsl, + NULL, NULL, NULL, 0, 0, 0); + retval = GG.shaders.point_uniform_size_uniform_color_smooth_2D; + break; + case GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_SMOOTH: + if (!GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D) + GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D = GPU_shader_create( + datatoc_gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert_glsl, + datatoc_gpu_shader_point_uniform_color_outline_smooth_frag_glsl, + NULL, NULL, NULL, 0, 0, 0); + retval = GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D; + break; case GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR: if (!GG.shaders.point_fixed_size_uniform_color_3D) GG.shaders.point_fixed_size_uniform_color_3D = GPU_shader_create( @@ -935,6 +957,16 @@ void GPU_shader_free_builtin_shaders(void) GG.shaders.point_varying_size_varying_color_2D = NULL; } + if (GG.shaders.point_uniform_size_uniform_color_smooth_2D) { + GPU_shader_free(GG.shaders.point_uniform_size_uniform_color_smooth_2D); + GG.shaders.point_uniform_size_uniform_color_smooth_2D = NULL; + } + + if (GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D) { + GPU_shader_free(GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D); + GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D = NULL; + } + if (GG.shaders.point_fixed_size_uniform_color_3D) { GPU_shader_free(GG.shaders.point_fixed_size_uniform_color_3D); GG.shaders.point_fixed_size_uniform_color_3D = NULL; diff --git a/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl new file mode 100644 index 00000000000..cff493bcfc7 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl @@ -0,0 +1,28 @@ + +uniform float size; +uniform float outlineWidth; + +#if __VERSION__ == 120 + attribute vec2 pos; + varying vec4 radii; +#else + in vec2 pos; + out vec4 radii; +#endif + +void main() { + gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0); + gl_PointSize = size; + + // calculate concentric radii in pixels + float radius = 0.5 * size; + + // start at the outside and progress toward the center + radii[0] = radius; + radii[1] = radius - 1.0; + radii[2] = radius - outlineWidth; + radii[3] = radius - outlineWidth - 1.0; + + // convert to PointCoord units + radii /= size; +} diff --git a/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl new file mode 100644 index 00000000000..f72c4c5a9e7 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl @@ -0,0 +1,25 @@ + +uniform float size; + +#if __VERSION__ == 120 + attribute vec2 pos; + varying vec2 radii; +#else + in vec2 pos; + out vec2 radii; +#endif + +void main() { + gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0); + gl_PointSize = size; + + // calculate concentric radii in pixels + float radius = 0.5 * size; + + // start at the outside and progress toward the center + radii[0] = radius; + radii[1] = radius - 1.0; + + // convert to PointCoord units + radii /= size; +} diff --git a/source/blender/gpu/shaders/gpu_shader_point_uniform_color_outline_smooth_frag.glsl b/source/blender/gpu/shaders/gpu_shader_point_uniform_color_outline_smooth_frag.glsl new file mode 100644 index 00000000000..605235b9335 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_point_uniform_color_outline_smooth_frag.glsl @@ -0,0 +1,34 @@ + +uniform vec4 color; +uniform vec4 outlineColor; + +#if __VERSION__ == 120 + varying vec4 radii; + #define fragColor gl_FragColor +#else + in vec4 radii; + out vec4 fragColor; +#endif + +void main() { + float dist = length(gl_PointCoord - vec2(0.5)); + +// transparent outside of point +// --- 0 --- +// smooth transition +// --- 1 --- +// pure outline color +// --- 2 --- +// smooth transition +// --- 3 --- +// pure point color +// ... +// dist = 0 at center of point + + float midStroke = 0.5 * (radii[1] + radii[2]); + + if (dist > midStroke) + fragColor = mix(outlineColor, vec4(0.0), smoothstep(radii[1], radii[0], dist)); + else + fragColor = mix(color, outlineColor, smoothstep(radii[3], radii[2], dist)); +} diff --git a/source/blender/gpu/shaders/gpu_shader_point_uniform_color_smooth_frag.glsl b/source/blender/gpu/shaders/gpu_shader_point_uniform_color_smooth_frag.glsl new file mode 100644 index 00000000000..dd8ecd6daa6 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_point_uniform_color_smooth_frag.glsl @@ -0,0 +1,24 @@ + +uniform vec4 color; + +#if __VERSION__ == 120 + varying vec2 radii; + #define fragColor gl_FragColor +#else + in vec2 radii; + out vec4 fragColor; +#endif + +void main() { + float dist = length(gl_PointCoord - vec2(0.5)); + +// transparent outside of point +// --- 0 --- +// smooth transition +// --- 1 --- +// pure point color +// ... +// dist = 0 at center of point + + fragColor = mix(color, vec4(0.0), smoothstep(radii[1], radii[0], dist)); +} |