OpenGL: new built-in shaders for drawing points

Both of these draw round points with jaggy edges, but treat color & size differently.
This commit is contained in:
2016-09-30 20:44:22 -04:00
parent d1b21d1278
commit 5753a1462f
7 changed files with 114 additions and 0 deletions

View File

@@ -135,6 +135,11 @@ data_to_c_simple(shaders/gpu_shader_3D_flat_color_vert.glsl SRC)
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_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_text_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_text_frag.glsl SRC)

View File

@@ -104,6 +104,9 @@ typedef enum GPUBuiltinShader {
/* basic 2D texture drawing */
GPU_SHADER_2D_TEXTURE_2D,
GPU_SHADER_2D_TEXTURE_RECT,
/* points */
GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR,
GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR,
} GPUBuiltinShader;
GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader);

View File

@@ -61,6 +61,11 @@ extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
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_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_text_vert_glsl[];
extern char datatoc_gpu_shader_text_frag_glsl[];
@@ -103,6 +108,9 @@ static struct GPUShadersGlobal {
GPUShader *flat_color_3D;
GPUShader *smooth_color_3D;
GPUShader *depth_only_3D;
/* points */
GPUShader *point_fixed_size_varying_color_3D;
GPUShader *point_varying_size_uniform_color_3D;
} shaders;
} GG = {{NULL}};
@@ -709,6 +717,22 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
NULL, NULL, NULL, 0, 0, 0);
retval = GG.shaders.depth_only_3D;
break;
case GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR:
if (!GG.shaders.point_fixed_size_varying_color_3D)
GG.shaders.point_fixed_size_varying_color_3D = GPU_shader_create(
datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl,
datatoc_gpu_shader_point_varying_color_frag_glsl,
NULL, NULL, NULL, 0, 0, 0);
retval = GG.shaders.point_fixed_size_varying_color_3D;
break;
case GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR:
if (!GG.shaders.point_varying_size_uniform_color_3D)
GG.shaders.point_varying_size_uniform_color_3D = GPU_shader_create(
datatoc_gpu_shader_3D_point_varying_size_no_color_vert_glsl,
datatoc_gpu_shader_point_uniform_color_frag_glsl,
NULL, NULL, NULL, 0, 0, 0);
retval = GG.shaders.point_varying_size_uniform_color_3D;
break;
}
if (retval == NULL)
@@ -865,6 +889,16 @@ void GPU_shader_free_builtin_shaders(void)
GG.shaders.smooth_color_3D = NULL;
}
if (GG.shaders.point_fixed_size_varying_color_3D) {
GPU_shader_free(GG.shaders.point_fixed_size_varying_color_3D);
GG.shaders.point_fixed_size_varying_color_3D = NULL;
}
if (GG.shaders.point_varying_size_uniform_color_3D) {
GPU_shader_free(GG.shaders.point_varying_size_uniform_color_3D);
GG.shaders.point_varying_size_uniform_color_3D = NULL;
}
for (i = 0; i < 2 * MAX_FX_SHADERS; ++i) {
if (GG.shaders.fx_shaders[i]) {
GPU_shader_free(GG.shaders.fx_shaders[i]);

View File

@@ -0,0 +1,16 @@
#if __VERSION__ == 120
attribute vec3 pos;
attribute vec4 color;
varying vec4 finalColor;
#else
in vec3 pos;
in vec4 color;
out vec4 finalColor;
#endif
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
finalColor = color;
}

View File

@@ -0,0 +1,14 @@
#if __VERSION__ == 120
attribute vec3 pos;
attribute float size;
#else
in vec3 pos;
in float size;
#endif
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
gl_PointSize = size;
}

View File

@@ -0,0 +1,21 @@
uniform vec4 color;
#if __VERSION__ == 120
#define fragColor gl_FragColor
#else
out vec4 fragColor;
#endif
void main()
{
vec2 centered = gl_PointCoord - vec2(0.5);
float dist_squared = dot(centered, centered);
const float rad_squared = 0.25;
// round point with jaggy edges
if (dist_squared > rad_squared)
discard;
fragColor = color;
}

View File

@@ -0,0 +1,21 @@
#if __VERSION__ == 120
varying vec4 finalColor;
#define fragColor gl_FragColor
#else
in vec4 finalColor;
out vec4 fragColor;
#endif
void main()
{
vec2 centered = gl_PointCoord - vec2(0.5);
float dist_squared = dot(centered, centered);
const float rad_squared = 0.25;
// round point with jaggy edges
if (dist_squared > rad_squared)
discard;
fragColor = finalColor;
}