From 804c2de61767c4b7f702862cce7329575feba0d1 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 29 Dec 2022 17:27:41 +0000 Subject: [PATCH] Cleaned up sprite render; use UVs --- source/engine/font.c | 6 ----- source/engine/font.h | 2 +- source/engine/sprite.c | 45 ++++++++++++++-------------------- source/engine/texture.c | 20 +++++++++++++++ source/engine/texture.h | 3 ++- source/shaders/spritevert.glsl | 5 ++-- 6 files changed, 43 insertions(+), 38 deletions(-) diff --git a/source/engine/font.c b/source/engine/font.c index eb28ee6..3f1e1cd 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -30,11 +30,6 @@ void font_init(struct shader *textshader) { shader_use(shader); glGenBuffers(1, &VBO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); - // Default font //font = MakeFont("teenytinypixels.ttf", 30); @@ -157,7 +152,6 @@ void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3 glBindTexture(GL_TEXTURE_2D, font->texID); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, len*16*sizeof(float), NULL, GL_STREAM_DRAW); - glEnableVertexAttribArray(0); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); const unsigned char *line, *wordstart; diff --git a/source/engine/font.h b/source/engine/font.h index bb98aeb..e7d9dd0 100644 --- a/source/engine/font.h +++ b/source/engine/font.h @@ -28,7 +28,7 @@ struct sFont { uint32_t texID; }; - +struct glrect runit = { 0.f, 1.f, 0.f, 1.f }; void font_init(struct shader *s); void font_frame(struct window *w); diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 42937fa..e956efa 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -11,12 +11,13 @@ #include #include "stb_ds.h" #include "log.h" +#include "font.h" struct TextureOptions TEX_SPRITE = { 1, 0, 0 }; struct sprite *sprites; -static uint32_t quadVAO; +static uint32_t VBO; struct sprite *make_sprite(struct gameobject *go) { @@ -111,29 +112,12 @@ unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite) // TODO: This should be done once for all sprites void sprite_initialize() { - uint32_t VBO; - float vertices[] = { - // pos - 0.f, 0.f, - 0.0f, 1.0f, - 1.f, 0.f, - 1.f, 1.f - }; - - glGenVertexArrays(1, &quadVAO); glGenBuffers(1, &VBO); - - glBindVertexArray(quadVAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); - - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); } -void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2]) { + + +void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2], struct glrect r) { mfloat_t model[16] = { 0.f }; mfloat_t r_model[16] = { 0.f }; mfloat_t s_model[16] = { 0.f }; @@ -157,11 +141,20 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo shader_setmat4(spriteShader, "model", model); shader_setvec3(spriteShader, "spriteColor", white); - //tex_bind(sprite->tex); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex->id); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + + float vertices[] = { + 0.f, 0.f, r.s0, r.t1, + 1.f, 0.f, r.s1, r.t1, + 0.f, 1.f, r.s0, r.t0, + 1.f, 1.f, r.s1, r.t0 + }; + + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); - glBindVertexArray(quadVAO); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } @@ -173,7 +166,7 @@ void sprite_draw(struct sprite *sprite) float size[2] = { sprite->size[0] * sprite->go->scale, sprite->size[1] * sprite->go->scale }; - tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos); + tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex)); } } @@ -183,7 +176,7 @@ void gui_draw_img(const char *img, float x, float y) { float pos[2] = {x, y}; float size[2] = {1.f, 1.f}; float offset[2] = { 0.f, 0.f }; - tex_draw(tex, pos, 0.f, size, offset); + tex_draw(tex, pos, 0.f, size, offset, tex_get_rect(tex)); } void spriteanim_draw(struct sprite *sprite) @@ -207,7 +200,6 @@ void spriteanim_draw(struct sprite *sprite) glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_ARRAY, sprite->tex->id); - glBindVertexArray(quadVAO); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } @@ -232,7 +224,6 @@ void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2 glBindTexture(GL_TEXTURE_2D, stream->texture_cr); // TODO: video bind VAO - glBindVertexArray(quadVAO); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } diff --git a/source/engine/texture.c b/source/engine/texture.c index d06729f..98cd53d 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -195,6 +195,26 @@ void tex_anim_calc_uv(struct TexAnimation *anim) +struct glrect tex_get_rect(struct Texture *tex) +{ + if (tex->opts.animation) { + /* + tex_anim_calc_uv(tex->anim); + struct glrect ret; + + ret.s0 = tex->anim.uv.x; + ret.s1 = tex->anim.uv.x + tex->anim.uv.w; + ret.t0 = tex->anim.uv.y; + ret.t1 = tex->anim.uv.y + tex->anim.uv.h; +*/ + return runit; + } else { + return runit; + } + + return runit; +} + void tex_bind(struct Texture *tex) { glActiveTexture(GL_TEXTURE0); diff --git a/source/engine/texture.h b/source/engine/texture.h index 1b316de..8a441db 100644 --- a/source/engine/texture.h +++ b/source/engine/texture.h @@ -2,6 +2,7 @@ #define TEXTURE_H #include "timer.h" +#include "font.h" #define TEX_SPEC 0 #define TEX_NORM 1 @@ -75,7 +76,7 @@ void tex_incr_anim(struct TexAnimation *tex_anim); void tex_anim_calc_uv(struct TexAnimation *anim); void tex_anim_set(struct TexAnimation *anim); - +struct glrect tex_get_rect(struct Texture *tex); #endif diff --git a/source/shaders/spritevert.glsl b/source/shaders/spritevert.glsl index 064e57a..16f7b6d 100644 --- a/source/shaders/spritevert.glsl +++ b/source/shaders/spritevert.glsl @@ -1,5 +1,5 @@ #version 330 core -layout (location = 0) in vec2 vertex; +layout (location = 0) in vec4 vertex; out vec2 texcoords; layout (std140) uniform Projection @@ -11,7 +11,6 @@ uniform mat4 model; void main() { - texcoords = vertex.xy; - texcoords.y *= -1; + texcoords = vertex.zw; gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0); } \ No newline at end of file