forked from tsoding/ded
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(tsoding#27) Add cursor rendering to Free_Glyph
- Loading branch information
Showing
8 changed files
with
223 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#version 330 core | ||
|
||
#define PERIOD 1.0 | ||
#define BLINK_THRESHOLD 0.5 | ||
|
||
uniform float time; | ||
uniform float last_stroke; | ||
|
||
void main() { | ||
float t = time - last_stroke; | ||
float threshold = float(t < BLINK_THRESHOLD); | ||
float blink = mod(floor(t / PERIOD), 2); | ||
gl_FragColor = vec4(1.0) * min(threshold + blink, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#version 330 core | ||
|
||
uniform vec2 resolution; | ||
uniform vec2 camera; | ||
uniform vec2 pos; | ||
uniform float height; | ||
|
||
#define WIDTH 5.0 | ||
|
||
out vec2 uv; | ||
|
||
vec2 project_point(vec2 point) | ||
{ | ||
return 2.0 * (point - camera) / resolution; | ||
} | ||
|
||
void main() { | ||
uv = vec2(float(gl_VertexID & 1), float((gl_VertexID >> 1) & 1)); | ||
gl_Position = vec4( | ||
project_point(uv * vec2(WIDTH, height) + pos), | ||
0.0, | ||
1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <stdlib.h> | ||
#include "./gl_extra.h" | ||
#include "./cursor_renderer.h" | ||
|
||
void cursor_renderer_init(Cursor_Renderer *cr, | ||
const char *vert_file_path, | ||
const char *frag_file_path) | ||
{ | ||
// Init Shaders | ||
{ | ||
GLuint vert_shader = 0; | ||
if (!compile_shader_file(vert_file_path, GL_VERTEX_SHADER, &vert_shader)) { | ||
exit(1); | ||
} | ||
GLuint frag_shader = 0; | ||
if (!compile_shader_file(frag_file_path, GL_FRAGMENT_SHADER, &frag_shader)) { | ||
exit(1); | ||
} | ||
|
||
if (!link_program(vert_shader, frag_shader, &cr->program)) { | ||
exit(1); | ||
} | ||
|
||
glUseProgram(cr->program); | ||
|
||
cr->time_uniform = glGetUniformLocation(cr->program, "time"); | ||
cr->resolution_uniform = glGetUniformLocation(cr->program, "resolution"); | ||
cr->camera_uniform = glGetUniformLocation(cr->program, "camera"); | ||
cr->pos_uniform = glGetUniformLocation(cr->program, "pos"); | ||
cr->height_uniform = glGetUniformLocation(cr->program, "height"); | ||
cr->last_stroke_uniform = glGetUniformLocation(cr->program, "last_stroke"); | ||
} | ||
} | ||
|
||
void cursor_renderer_use(const Cursor_Renderer *cr) | ||
{ | ||
glUseProgram(cr->program); | ||
} | ||
|
||
void cursor_renderer_move_to(const Cursor_Renderer *cr, Vec2f pos) | ||
{ | ||
glUniform2f(cr->pos_uniform, pos.x, pos.y); | ||
} | ||
|
||
void cursor_renderer_draw() | ||
{ | ||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef CURSOR_RENDERER_H_ | ||
#define CURSOR_RENDERER_H_ | ||
|
||
#define GLEW_STATIC | ||
#include <GL/glew.h> | ||
|
||
#define GL_GLEXT_PROTOTYPES | ||
#include <SDL2/SDL_opengl.h> | ||
|
||
#include "./la.h" | ||
|
||
typedef struct { | ||
GLuint program; | ||
|
||
GLint time_uniform; | ||
GLint resolution_uniform; | ||
GLint camera_uniform; | ||
GLint pos_uniform; | ||
GLint height_uniform; | ||
GLint last_stroke_uniform; | ||
} Cursor_Renderer; | ||
|
||
void cursor_renderer_init(Cursor_Renderer *cr, | ||
const char *vert_file_path, | ||
const char *frag_file_path); | ||
|
||
void cursor_renderer_use(const Cursor_Renderer *cr); | ||
void cursor_renderer_move_to(const Cursor_Renderer *cr, Vec2f pos); | ||
void cursor_renderer_draw(void); | ||
|
||
#endif // CURSOR_RENDERER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters