add abstractions that allow removing dependencies to glew.h

Wed, 25 Jun 2025 21:58:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 25 Jun 2025 21:58:44 +0200
changeset 168
f70569c49c24
parent 167
8e6a661c87db
child 169
6e6717d9c776

add abstractions that allow removing dependencies to glew.h

src/2d.c file | annotate | diff | comparison | revisions
src/ascension/shader.h file | annotate | diff | comparison | revisions
src/ascension/texture.h file | annotate | diff | comparison | revisions
src/shader.c file | annotate | diff | comparison | revisions
src/sprite.c file | annotate | diff | comparison | revisions
src/texture.c file | annotate | diff | comparison | revisions
--- a/src/2d.c	Tue Jun 24 20:21:38 2025 +0200
+++ b/src/2d.c	Wed Jun 25 21:58:44 2025 +0200
@@ -32,17 +32,15 @@
 #include "ascension/error.h"
 #include "ascension/shader.h"
 
-// TODO: implement abstraction in shader.h to remove glew.h include here
-#include <GL/glew.h>
 #include <assert.h>
 
 typedef struct asc_rectangle_shader_s {
     AscShaderProgram program;
-    GLint color;
-    GLint border_color;
-    GLint size;
-    GLint thickness;
-    GLint radius;
+    asc_uniform_loc color;
+    asc_uniform_loc border_color;
+    asc_uniform_loc size;
+    asc_uniform_loc thickness;
+    asc_uniform_loc radius;
 } AscRectangleShader;
 
 #define ASC_RECTANGLE_SHADER_FLAG_FILL          1
@@ -71,21 +69,21 @@
         asc_shader_free_codes(codes);
         return (AscShaderProgram*) shader;
     }
-    shader->size = glGetUniformLocation(shader->program.gl_id, "size");
+    shader->size = asc_shader_get_uniform_loc(&shader->program, "size");
     if (asc_test_flag(flags, ASC_RECTANGLE_SHADER_FLAG_FILL)) {
-        shader->color = glGetUniformLocation(shader->program.gl_id, "color");
+        shader->color = asc_shader_get_uniform_loc(&shader->program, "color");
     } else {
         shader->color = -1;
     }
     if (asc_test_flag(flags, ASC_RECTANGLE_SHADER_FLAG_BORDER)) {
-        shader->thickness = glGetUniformLocation(shader->program.gl_id, "thickness");
-        shader->border_color = glGetUniformLocation(shader->program.gl_id, "border_color");
+        shader->thickness = asc_shader_get_uniform_loc(&shader->program, "thickness");
+        shader->border_color = asc_shader_get_uniform_loc(&shader->program, "border_color");
     } else {
         shader->thickness = -1;
         shader->border_color = -1;
     }
     if (asc_test_flag(flags, ASC_RECTANGLE_SHADER_FLAG_ROUND)) {
-        shader->radius = glGetUniformLocation(shader->program.gl_id, "radius");
+        shader->radius = asc_shader_get_uniform_loc(&shader->program, "radius");
     } else {
         shader->radius = -1;
     }
--- a/src/ascension/shader.h	Tue Jun 24 20:21:38 2025 +0200
+++ b/src/ascension/shader.h	Wed Jun 25 21:58:44 2025 +0200
@@ -34,6 +34,8 @@
 
 typedef struct asc_scene_node_s AscSceneNode; // avoids full include of scene_node.h
 
+typedef int asc_uniform_loc;
+
 struct asc_shader_code_files_s {
     /**
      * File name of the vertex shader.
@@ -239,6 +241,8 @@
  */
 void asc_shader_clear_registry(void);
 
+asc_uniform_loc asc_shader_get_uniform_loc(const AscShaderProgram *shader, const char *name);
+
 void asc_shader_upload_model_matrix(const AscShaderProgram *shader, const AscSceneNode *node);
 
 void asc_shader_upload_col4f(int uniform_id, asc_col4f color);
--- a/src/ascension/texture.h	Tue Jun 24 20:21:38 2025 +0200
+++ b/src/ascension/texture.h	Wed Jun 25 21:58:44 2025 +0200
@@ -93,6 +93,7 @@
 
 void asc_texture_from_file(AscTexture *tex, const char *name);
 
+bool asc_texture_is_uv_normalized(const AscTexture *tex);
 
 /**
  * Calculates UV scaling factors depending on texture and surface dimensions.
--- a/src/shader.c	Tue Jun 24 20:21:38 2025 +0200
+++ b/src/shader.c	Wed Jun 25 21:58:44 2025 +0200
@@ -264,6 +264,10 @@
     asc_shader_use(NULL, NULL);
 }
 
+asc_uniform_loc asc_shader_get_uniform_loc(const AscShaderProgram *shader, const char *name) {
+    return glGetUniformLocation(shader->gl_id, name);
+}
+
 void asc_shader_upload_model_matrix(const AscShaderProgram *shader, const AscSceneNode *node) {
     glUniformMatrix4fv(shader->model, 1,GL_FALSE, node->world_transform);
 }
--- a/src/sprite.c	Tue Jun 24 20:21:38 2025 +0200
+++ b/src/sprite.c	Wed Jun 25 21:58:44 2025 +0200
@@ -34,12 +34,10 @@
 #include "ascension/shader.h"
 #include "ascension/constants.h"
 
-#include <GL/glew.h>
-
 
 typedef struct asc_sprite_shader_s {
     AscShaderProgram program;
-    GLint tex;
+    asc_uniform_loc tex;
 } AscSpriteShader;
 
 static AscShaderProgram *asc_sprite_shader_create(int rect) {
@@ -58,7 +56,7 @@
         asc_shader_free_codes(codes);
         return (AscShaderProgram*) shader;
     }
-    shader->tex = glGetUniformLocation(shader->program.gl_id, "tex");
+    shader->tex = asc_shader_get_uniform_loc(&shader->program, "tex");
     asc_shader_free_codes(codes);
 
     asc_error_catch_all_gl();
@@ -97,9 +95,9 @@
     // Activate shader
     // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes
     const AscSpriteShader *shader =
-            sprite->texture->target == GL_TEXTURE_RECTANGLE
-                ? asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_create, 1)
-                : asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_create, 0);
+        asc_texture_is_uv_normalized(sprite->texture)
+                ? asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_create, 0)
+                : asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_create, 1);
     // TODO: how to remove the following cast?
     if (asc_shader_invalid((AscShaderProgram*)shader)) return;
     asc_shader_use(&shader->program, camera);
--- a/src/texture.c	Tue Jun 24 20:21:38 2025 +0200
+++ b/src/texture.c	Wed Jun 25 21:58:44 2025 +0200
@@ -162,6 +162,10 @@
     glDeleteTextures(count, textures);
 }
 
+bool asc_texture_is_uv_normalized(const AscTexture *tex) {
+    return tex->target != GL_TEXTURE_RECTANGLE;
+}
+
 asc_vec2f asc_texture_calculate_uv_scale(const AscTexture *tex, asc_vec2u surface_dimension, asc_vec2f factors) {
     if (surface_dimension.width == 0 || surface_dimension.height == 0) {
         asc_wprintf("Tried to calculate UV scale for texture %u with zero dimensions.", tex->tex_id);

mercurial