Cleaned up sprite render; use UVs

This commit is contained in:
John Alanbrook 2022-12-29 17:27:41 +00:00
parent 0bdfa9e814
commit 804c2de617
6 changed files with 43 additions and 38 deletions

View file

@ -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;

View file

@ -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);

View file

@ -11,12 +11,13 @@
#include <string.h>
#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);
}

View file

@ -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);

View file

@ -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

View file

@ -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);
}