OpenGL: shaders for smooth round points
Solid color with an optional outline. size (diameter) and outlineWidth are in pixels.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
Reference in New Issue
Block a user