summaryrefslogtreecommitdiff
path: root/gl.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-11-03 19:48:26 -0400
committerpommicket <pommicket@gmail.com>2022-11-03 19:48:26 -0400
commit7a2b3ea8c13657ef697d0b4dc73ccada23f81039 (patch)
tree0b3702b9bce4536047525d2f472a937d6e06d039 /gl.c
parent4d995edfdde5953bc5669c54b1194be40a3641f2 (diff)
custom shader texture
Diffstat (limited to 'gl.c')
-rw-r--r--gl.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/gl.c b/gl.c
index c16d252..1c41d54 100644
--- a/gl.c
+++ b/gl.c
@@ -5,6 +5,7 @@
do(DRAWARRAYS, DrawArrays)\
do(GENTEXTURES, GenTextures)\
do(DELETETEXTURES, DeleteTextures)\
+ do(GENERATEMIPMAP, GenerateMipmap)\
do(TEXIMAGE2D, TexImage2D)\
do(BINDTEXTURE, BindTexture)\
do(TEXPARAMETERI, TexParameteri)\
@@ -87,6 +88,29 @@ void gl_rc_sab_decref(GlRcSAB **ps) {
*ps = NULL;
}
+GlRcTexture *gl_rc_texture_new(GLuint texture) {
+ GlRcTexture *t = calloc(1, sizeof *t);
+ t->ref_count = 1;
+ t->texture = texture;
+ return t;
+}
+
+void gl_rc_texture_incref(GlRcTexture *t) {
+ if (!t) return;
+ ++t->ref_count;
+}
+
+void gl_rc_texture_decref(GlRcTexture **pt) {
+ GlRcTexture *t = *pt;
+ if (!t) return;
+ if (--t->ref_count == 0) {
+ debug_println("Delete texture %u", t->texture);
+ glDeleteTextures(1, &t->texture);
+ free(t);
+ }
+ *pt = NULL;
+}
+
// set by main()
static int gl_version_major;
static int gl_version_minor;
@@ -305,3 +329,33 @@ static void gl_geometry_draw(void) {
arr_clear(gl_geometry_triangles);
}
+
+static GLuint gl_load_texture_from_image(const char *path) {
+ GLuint texture = 0;
+ int w=0, h=0, n=0;
+ unsigned char *data = stbi_load(path, &w, &h, &n, 4);
+ if (data && w > 0 && h > 0) {
+ size_t bytes_per_row = (size_t)w * 4;
+ unsigned char *tempbuf = calloc(1, bytes_per_row);
+ // flip image vertically
+ for (int y = 0; y < h/2; ++y) {
+ int y2 = h-1-y;
+ unsigned char *row1 = &data[y*w*4];
+ unsigned char *row2 = &data[y2*w*4];
+ memcpy(tempbuf, row1, bytes_per_row);
+ memcpy(row1, row2, bytes_per_row);
+ memcpy(row2, tempbuf, bytes_per_row);
+ }
+ free(tempbuf);
+
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ free(data);
+ }
+ return texture;
+}