Răsfoiți Sursa

Add scale to the camera

rexim 4 ani în urmă
părinte
comite
5261991f8e

+ 10 - 0
shaders/camera.vert

@@ -0,0 +1,10 @@
+uniform vec2 resolution;
+uniform vec2 camera;
+uniform float time;
+// uniform float camera_scale;
+
+vec2 camera_project(vec2 point)
+{
+    float camera_scale = 1.0 + (sin(time) + 1.0) / 2.0;
+    return 2.0 * (point - camera) * camera_scale / resolution;
+}

+ 4 - 6
shaders/cursor.vert

@@ -9,15 +9,13 @@ uniform float height;
 
 out vec2 uv;
 
-vec2 project_point(vec2 point)
-{
-    return 2.0 * (point - camera) / resolution;
-}
+vec2 camera_project(vec2 point);
 
 void main() {
-    uv = vec2(float(gl_VertexID & 1), float((gl_VertexID >> 1) & 1));
+    uv = vec2(float(gl_VertexID & 1),
+              float((gl_VertexID >> 1) & 1));
     gl_Position = vec4(
-        project_point(uv * vec2(WIDTH, height) + pos),
+        camera_project(uv * vec2(WIDTH, height) + pos),
         0.0,
         1.0);
 }

+ 3 - 4
shaders/free_glyph.vert

@@ -2,6 +2,8 @@
 
 uniform vec2 resolution;
 uniform vec2 camera;
+uniform float time;
+// uniform float camera_scale;
 
 layout(location = 0) in vec2 pos;
 layout(location = 1) in vec2 size;
@@ -16,10 +18,7 @@ out vec2 glyph_uv_size;
 out vec4 glyph_fg_color;
 out vec4 glyph_bg_color;
 
-vec2 camera_project(vec2 point)
-{
-    return 2.0 * (point - camera) / resolution;
-}
+vec2 camera_project(vec2 point);
 
 void main() {
     uv = vec2(float(gl_VertexID & 1), float((gl_VertexID >> 1) & 1));

+ 2 - 5
shaders/tile_glyph.vert

@@ -22,17 +22,14 @@ flat out int glyph_ch;
 out vec4 glyph_fg_color;
 out vec4 glyph_bg_color;
 
-vec2 project_point(vec2 point)
-{
-    return 2.0 * (point - camera) / resolution;
-}
+vec2 camera_project(vec2 point);
 
 void main() {
     uv = vec2(float(gl_VertexID & 1), float((gl_VertexID >> 1) & 1));
     vec2 char_size = vec2(float(FONT_CHAR_WIDTH), float(FONT_CHAR_HEIGHT));
     vec2 pos = tile * char_size * scale;
 
-    gl_Position = vec4(project_point(uv * char_size * scale + pos), 0.0, 1.0);
+    gl_Position = vec4(camera_project(uv * char_size * scale + pos), 0.0, 1.0);
     glyph_ch = ch;
 
     glyph_fg_color = fg_color;

+ 10 - 5
src/cursor_renderer.c

@@ -8,16 +8,21 @@ void cursor_renderer_init(Cursor_Renderer *cr,
 {
     // Init Shaders
     {
-        GLuint vert_shader = 0;
-        if (!compile_shader_file(vert_file_path, GL_VERTEX_SHADER, &vert_shader)) {
+        GLuint shaders[3] = {0};
+
+        if (!compile_shader_file(vert_file_path, GL_VERTEX_SHADER, &shaders[0])) {
+            exit(1);
+        }
+        if (!compile_shader_file(frag_file_path, GL_FRAGMENT_SHADER, &shaders[1])) {
             exit(1);
         }
-        GLuint frag_shader = 0;
-        if (!compile_shader_file(frag_file_path, GL_FRAGMENT_SHADER, &frag_shader)) {
+        if (!compile_shader_file("./shaders/camera.vert", GL_VERTEX_SHADER, &shaders[2])) {
             exit(1);
         }
 
-        if (!link_program(vert_shader, frag_shader, &cr->program)) {
+        cr->program = glCreateProgram();
+        attach_shaders_to_program(shaders, sizeof(shaders) / sizeof(shaders[0]), cr->program);
+        if (!link_program(cr->program, __FILE__, __LINE__)) {
             exit(1);
         }
 

+ 10 - 5
src/free_glyph.c

@@ -93,16 +93,21 @@ void free_glyph_buffer_init(Free_Glyph_Buffer *fgb,
 
     // Init Shaders
     {
-        GLuint vert_shader = 0;
-        if (!compile_shader_file(vert_file_path, GL_VERTEX_SHADER, &vert_shader)) {
+        GLuint shaders[3] = {0};
+
+        if (!compile_shader_file(vert_file_path, GL_VERTEX_SHADER, &shaders[0])) {
+            exit(1);
+        }
+        if (!compile_shader_file(frag_file_path, GL_FRAGMENT_SHADER, &shaders[1])) {
             exit(1);
         }
-        GLuint frag_shader = 0;
-        if (!compile_shader_file(frag_file_path, GL_FRAGMENT_SHADER, &frag_shader)) {
+        if (!compile_shader_file("./shaders/camera.vert", GL_VERTEX_SHADER, &shaders[2])) {
             exit(1);
         }
 
-        if (!link_program(vert_shader, frag_shader, &fgb->program)) {
+        fgb->program = glCreateProgram();
+        attach_shaders_to_program(shaders, sizeof(shaders) / sizeof(shaders[0]), fgb->program);
+        if (!link_program(fgb->program, __FILE__, __LINE__)) {
             exit(1);
         }
 

+ 12 - 12
src/gl_extra.c

@@ -47,26 +47,26 @@ bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shad
     return ok;
 }
 
-bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program)
+void attach_shaders_to_program(GLuint *shaders, size_t shaders_count, GLuint program)
 {
-    *program = glCreateProgram();
+    for (size_t i = 0; i < shaders_count; ++i) {
+        glAttachShader(program, shaders[i]);
+    }
+}
 
-    glAttachShader(*program, vert_shader);
-    glAttachShader(*program, frag_shader);
-    glLinkProgram(*program);
+bool link_program(GLuint program, const char *file_path, size_t line)
+{
+    glLinkProgram(program);
 
     GLint linked = 0;
-    glGetProgramiv(*program, GL_LINK_STATUS, &linked);
+    glGetProgramiv(program, GL_LINK_STATUS, &linked);
     if (!linked) {
         GLsizei message_size = 0;
         GLchar message[1024];
 
-        glGetProgramInfoLog(*program, sizeof(message), &message_size, message);
-        fprintf(stderr, "Program Linking: %.*s\n", message_size, message);
+        glGetProgramInfoLog(program, sizeof(message), &message_size, message);
+        fprintf(stderr, "%s:%zu: Program Linking: %.*s\n", file_path, line, message_size, message);
     }
 
-    glDeleteShader(vert_shader);
-    glDeleteShader(frag_shader);
-
-    return program;
+    return linked;
 }

+ 2 - 1
src/gl_extra.h

@@ -10,6 +10,7 @@
 
 bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader);
 bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader);
-bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program);
+void attach_shaders_to_program(GLuint *shaders, size_t shaders_count, GLuint program);
+bool link_program(GLuint program, const char *file_path, size_t line);
 
 #endif // GL_EXTRA_H_

+ 7 - 6
src/tile_glyph.c

@@ -114,17 +114,18 @@ void tile_glyph_buffer_init(Tile_Glyph_Buffer *tgb, const char *texture_file_pat
 
     // Init Shaders
     {
-        GLuint vert_shader = 0;
-        if (!compile_shader_file(vert_file_path, GL_VERTEX_SHADER, &vert_shader)) {
+        GLuint shaders[2] = {0};
+
+        if (!compile_shader_file(vert_file_path, GL_VERTEX_SHADER, &shaders[0])) {
             exit(1);
         }
-        GLuint frag_shader = 0;
-        if (!compile_shader_file(frag_file_path, GL_FRAGMENT_SHADER, &frag_shader)) {
+        if (!compile_shader_file(frag_file_path, GL_FRAGMENT_SHADER, &shaders[1])) {
             exit(1);
         }
 
-        GLuint program = 0;
-        if (!link_program(vert_shader, frag_shader, &program)) {
+        GLuint program = glCreateProgram();
+        attach_shaders_to_program(shaders, sizeof(shaders) / sizeof(shaders[0]), program);
+        if (!link_program(program, __FILE__, __LINE__)) {
             exit(1);
         }