citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 0934a017038ba9f34ed34112daa78a7067ddbf53
parent 3255da95486c2a975ad616b805f791f05219439c
Author: Kyle Milz <milz@imac.0x30.net>
Date:   Thu,  4 Mar 2021 01:12:32 -0800

gl: get compiling again

Diffstat:
Mgl/demo-common.h | 4+++-
Mgl/gl.cc | 6+++++-
Agl/gl_atlas_glsl.h | 18++++++++++++++++++
Agl/gl_fshader_glsl.h | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mgl/gl_shader.cc | 1-
Mgl/gl_view.cc | 1-
Agl/gl_vshader_glsl.h | 24++++++++++++++++++++++++
7 files changed, 137 insertions(+), 4 deletions(-)

diff --git a/gl/demo-common.h b/gl/demo-common.h @@ -24,11 +24,13 @@ #include <stdlib.h> #include <stdio.h> +#include <GL/glew.h> + #if defined(__APPLE__) # include <OpenGL/gl.h> # include <OpenGL/glu.h> #else -# include <GLES2/gl2.h> +//# include <GLES2/gl2.h> #endif diff --git a/gl/gl.cc b/gl/gl.cc @@ -1,7 +1,7 @@ #include <GL/glew.h> // glewInit, glewIsSupported #include <GLFW/glfw3.h> #define GLAPI extern -#include <GL/osmesa.h> // OSMesa{Context,CreateContext,MakeCurrent} +//#include <GL/osmesa.h> // OSMesa{Context,CreateContext,MakeCurrent} #include <err.h> #include <stdio.h> // fclose, fopen, fputc @@ -65,6 +65,7 @@ write_targa(const char *filename, const GLubyte *buffer, int width, int height) } } +#if 0 int altmain(int argc, char *argv[]) { @@ -124,12 +125,15 @@ altmain(int argc, char *argv[]) return 0; } +#endif int main(int argc, char *argv[]) { + /* if (argc > 1) return altmain(argc, argv); + */ GLFWwindow *window; diff --git a/gl/gl_atlas_glsl.h b/gl/gl_atlas_glsl.h @@ -0,0 +1,18 @@ +static const char *gl_atlas_glsl = R"( +uniform sampler2D u_atlas_tex; +uniform ivec4 u_atlas_info; + +#define GLYPHY_TEXTURE1D_EXTRA_DECLS , sampler2D _tex, ivec4 _atlas_info, ivec2 _atlas_pos +#define GLYPHY_TEXTURE1D_EXTRA_ARGS , _tex, _atlas_info, _atlas_pos +#define GLYPHY_DEMO_EXTRA_ARGS , u_atlas_tex, u_atlas_info, gi.atlas_pos + +vec4 +glyphy_texture1D_func (int offset GLYPHY_TEXTURE1D_EXTRA_DECLS) +{ + ivec2 item_geom = _atlas_info.zw; + vec2 pos = (vec2 (_atlas_pos.xy * item_geom + + ivec2 (mod (float (offset), float (item_geom.x)), offset / item_geom.x)) + + + vec2 (.5, .5)) / vec2(_atlas_info.xy); + return texture2D (_tex, pos); +} +)"; diff --git a/gl/gl_fshader_glsl.h b/gl/gl_fshader_glsl.h @@ -0,0 +1,87 @@ +static const char *gl_fshader_glsl = R"( +uniform float u_contrast; +uniform float u_gamma_adjust; +uniform float u_outline_thickness; +uniform bool u_outline; +uniform float u_boldness; +uniform bool u_debug; + +varying vec4 v_glyph; + + +#define SQRT2_2 0.70710678118654757 /* 1 / sqrt(2.) */ +#define SQRT2 1.4142135623730951 + +struct glyph_info_t { + ivec2 nominal_size; + ivec2 atlas_pos; +}; + +glyph_info_t +glyph_info_decode (vec4 v) +{ + glyph_info_t gi; + gi.nominal_size = (ivec2 (mod (v.zw, 256.)) + 2) / 4; + gi.atlas_pos = ivec2 (v_glyph.zw) / 256; + return gi; +} + + +float +antialias (float d) +{ + return smoothstep (-.75, +.75, d); +} + +void +main() +{ + vec2 p = v_glyph.xy; + glyph_info_t gi = glyph_info_decode (v_glyph); + + /* isotropic antialiasing */ + vec2 dpdx = dFdx (p); + vec2 dpdy = dFdy (p); + float m = length (vec2 (length (dpdx), length (dpdy))) * SQRT2_2; + + vec4 color = vec4 (0,0,0,1); + + float gsdist = glyphy_sdf (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS); + float sdist = gsdist / m * u_contrast; + + if (!u_debug) { + sdist -= u_boldness * 10.; + if (u_outline) + sdist = abs (sdist) - u_outline_thickness * .5; + if (sdist > 1.) + discard; + float alpha = antialias (-sdist); + if (u_gamma_adjust != 1.) + alpha = pow (alpha, 1./u_gamma_adjust); + color = vec4 (color.rgb,color.a * alpha); + } else { + color = vec4 (0,0,0,0); + + // Color the inside of the glyph a light red + color += vec4 (.5,0,0,.5) * smoothstep (1., -1., sdist); + + float udist = abs (sdist); + float gudist = abs (gsdist); + // Color the outline red + color += vec4 (1,0,0,1) * smoothstep (2., 1., udist); + // Color the distance field in green + if (!glyphy_isinf (udist)) + color += vec4(0,.4,0,.4 - (abs(gsdist) / max(float(gi.nominal_size.x), float(gi.nominal_size.y))) * 4.); + + float pdist = glyphy_point_dist (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS); + // Color points green + color = mix (vec4 (0,1,0,.5), color, smoothstep (.05, .06, pdist)); + + glyphy_arc_list_t arc_list = glyphy_arc_list (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS); + // Color the number of endpoints per cell blue + color += vec4 (0,0,1,.1) * float(arc_list.num_endpoints) * 32./255.; + } + + gl_FragColor = color; +} +)"; diff --git a/gl/gl_shader.cc b/gl/gl_shader.cc @@ -16,7 +16,6 @@ * Google Author(s): Behdad Esfahbod */ #include <assert.h> -#include <GL/glew.h> #include "gl_shader.h" #include "gl_atlas_glsl.h" diff --git a/gl/gl_view.cc b/gl/gl_view.cc @@ -16,7 +16,6 @@ * Google Author(s): Behdad Esfahbod, Maysum Panju, Wojciech Baranowski */ #include <assert.h> -#include <GL/glew.h> #include "gl_view.h" diff --git a/gl/gl_vshader_glsl.h b/gl/gl_vshader_glsl.h @@ -0,0 +1,24 @@ +static const char *gl_vshader_glsl = R"( +uniform mat4 u_matViewProjection; + +attribute vec4 a_glyph_vertex; + +varying vec4 v_glyph; + +vec4 +glyph_vertex_transcode (vec2 v) +{ + ivec2 g = ivec2 (v); + ivec2 corner = ivec2 (mod (v, 2.)); + g /= 2; + ivec2 nominal_size = ivec2 (mod (vec2(g), 64.)); + return vec4 (corner * nominal_size, g * 4); +} + +void +main() +{ + gl_Position = u_matViewProjection * vec4 (a_glyph_vertex.xy, 0, 1); + v_glyph = glyph_vertex_transcode (a_glyph_vertex.zw); +} +)";