UV and ST

This commit is contained in:
John Alanbrook 2022-12-30 19:31:06 +00:00
parent daebe5c665
commit eadf3524ba
6 changed files with 62 additions and 61 deletions

View file

@ -869,12 +869,12 @@ void editor_asset_tex_gui(struct Texture *tex) {
if (old_frames != tex->anim.frames || old_ms != tex->anim.ms) if (old_frames != tex->anim.frames || old_ms != tex->anim.ms)
tex_anim_set(&tex_gui_anim); tex_anim_set(&tex_gui_anim);
nk_layout_row_static(ctx, tex->height*tex_scale*tex_gui_anim.uv.h, tex->width*tex_scale*tex_gui_anim.uv.w, 1); nk_layout_row_static(ctx, tex->height*tex_scale*(tex_gui_anim.st.t1-tex_gui_anim.st.t0), tex->width*tex_scale*(tex_gui_anim.st.s1 - tex_gui_anim.st.s0), 1);
struct nk_rect r; struct nk_rect r;
r.x = tex_gui_anim.uv.x*tex->width; r.x = tex_gui_anim.st.s0*tex->width;
r.y = tex_gui_anim.uv.y*tex->height; r.y = tex_gui_anim.st.t0*tex->height;
r.w = tex_gui_anim.uv.w*tex->width; r.w = st_s_w(tex_gui_anim.st)*tex->width;
r.h = tex_gui_anim.uv.h*tex->height; r.h = st_s_h(tex_gui_anim.st)*tex->height;
nk_image(ctx, nk_subimage_id(tex->id, tex->width, tex->height, r)); nk_image(ctx, nk_subimage_id(tex->id, tex->width, tex->height, r));
} else { } else {

View file

@ -2,17 +2,11 @@
#define FONT_H #define FONT_H
#include "mathc.h" #include "mathc.h"
#include "texture.h"
struct shader; struct shader;
struct window; struct window;
struct glrect {
float s0;
float s1;
float t0;
float t1;
};
/// Holds all state information relevant to a character as loaded using FreeType /// Holds all state information relevant to a character as loaded using FreeType
struct Character { struct Character {
mfloat_t Size[2]; // Size of glyph mfloat_t Size[2]; // Size of glyph
@ -28,7 +22,7 @@ struct sFont {
uint32_t texID; uint32_t texID;
}; };
struct glrect runit = { 0.f, 1.f, 0.f, 1.f };
void font_init(struct shader *s); void font_init(struct shader *s);
void font_frame(struct window *w); void font_frame(struct window *w);

View file

@ -1,6 +1,5 @@
#include "sprite.h" #include "sprite.h"
#include "timer.h" #include "timer.h"
#include "render.h" #include "render.h"
#include "openglrender.h" #include "openglrender.h"
@ -87,16 +86,6 @@ void sprite_loadtex(struct sprite *sprite, const char *path)
sprite->tex = texture_loadfromfile(path); sprite->tex = texture_loadfromfile(path);
} }
void sprite_loadanim(struct sprite *sprite, const char *path, struct Anim2D anim)
{
sprite->tex = texture_loadfromfile(path);
sprite->anim = anim;
sprite->anim.timer = timer_make(sprite->anim.ms, &incrementAnimFrame, sprite);
/*
sprite->tex = texture_loadanimfromfile(sprite->tex, path, sprite->anim.frames, sprite->anim.dimensions);
*/
}
void sprite_settex(struct sprite *sprite, struct Texture *tex) void sprite_settex(struct sprite *sprite, struct Texture *tex)
{ {
sprite->tex = tex; sprite->tex = tex;
@ -122,7 +111,13 @@ void sprite_initialize()
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
} }
struct glrect sprite_get_rect(struct sprite *sprite) {
if (sprite->tex->opts.animation) {
} else {
return tex_get_rect(sprite->tex);
}
}
void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2], struct glrect r) { 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 model[16] = { 0.f };
@ -173,10 +168,19 @@ void sprite_draw(struct sprite *sprite)
cpVect cpos = cpBodyGetPosition(sprite->go->body); cpVect cpos = cpBodyGetPosition(sprite->go->body);
float pos[2] = {cpos.x, cpos.y}; float pos[2] = {cpos.x, cpos.y};
if (sprite->tex->opts.animation) {
float size[2];
struct Anim2D *a = &sprite->anim;
a->frames = sprite->tex->anim.frames;
size[0] = sprite->tex->anim.dimensions[0];
size[1] = sprite->tex->anim.dimensions[1];
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex));
} else {
float size[2] = { sprite->size[0] * sprite->go->scale, sprite->size[1] * sprite->go->scale }; 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_get_rect(sprite->tex)); tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex));
} }
}
} }
void gui_draw_img(const char *img, float x, float y) { void gui_draw_img(const char *img, float x, float y) {

View file

@ -36,7 +36,6 @@ void sprite_delete(struct sprite *sprite);
void sprite_init(struct sprite *sprite, struct gameobject *go); void sprite_init(struct sprite *sprite, struct gameobject *go);
void sprite_io(struct sprite *sprite, FILE *f, int read); void sprite_io(struct sprite *sprite, FILE *f, int read);
void sprite_loadtex(struct sprite *sprite, const char *path); void sprite_loadtex(struct sprite *sprite, const char *path);
void sprite_loadanim(struct sprite *sprite, const char *path, struct Anim2D anim);
void sprite_settex(struct sprite *sprite, struct Texture *tex); void sprite_settex(struct sprite *sprite, struct Texture *tex);
void sprite_initialize(); void sprite_initialize();
void sprite_draw(struct sprite *sprite); void sprite_draw(struct sprite *sprite);

View file

@ -80,8 +80,6 @@ struct Texture *texture_loadfromfile(const char *path)
return new; return new;
} }
void tex_pull(struct Texture *tex) void tex_pull(struct Texture *tex)
{ {
if (tex->data != NULL) if (tex->data != NULL)
@ -184,35 +182,19 @@ void tex_gpu_free(struct Texture *tex)
void tex_anim_calc_uv(struct TexAnimation *anim) void tex_anim_calc_uv(struct TexAnimation *anim)
{ {
struct Rect uv; struct glrect st;
uv.w = 1.f / anim->tex->anim.frames; float w = 1.f / anim->tex->anim.frames;
uv.h = 1.f; st.s0 = w * (anim->frame);
uv.y = 0.f; st.s1 = st.s0 + w;
uv.x = uv.w * (anim->frame); st.t0 = 0.f;
st.t1 = 1.f;
anim->uv = uv; anim->st = st;
} }
struct glrect tex_get_rect(struct Texture *tex) struct glrect tex_get_rect(struct Texture *tex)
{ {
if (tex->opts.animation) { return ST_UNIT;
/*
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) void tex_bind(struct Texture *tex)
@ -271,4 +253,12 @@ void anim_bkwd(struct TexAnimation *anim)
anim_decr(anim); anim_decr(anim);
} }
float st_s_w(struct glrect st)
{
return (st.s1 - st.s0);
}
float st_s_h(struct glrect st)
{
return (st.t1 - st.t0);
}

View file

@ -2,30 +2,44 @@
#define TEXTURE_H #define TEXTURE_H
#include "timer.h" #include "timer.h"
#include "font.h"
#define TEX_SPEC 0 #define TEX_SPEC 0
#define TEX_NORM 1 #define TEX_NORM 1
#define TEX_HEIGHT 2 #define TEX_HEIGHT 2
#define TEX_DIFF 3 #define TEX_DIFF 3
/* Normalized S,T coordinates for rendering */
struct Rect { struct glrect {
float x; float s0;
float y; float s1;
float w; float t0;
float h; float t1;
}; };
float st_s_w(struct glrect st);
float st_s_h(struct glrect st);
#define ST_UNIT (struct glrect) { 0.f, 1.f, 0.f, 1.f }
/* Pixel U,V coordiantes */
struct uvrect {
int u0;
int u1;
int v0;
int v1;
};
/* Tracks a playing animation */
struct TexAnimation { struct TexAnimation {
int frame; int frame;
int playing; int playing;
int pausetime; int pausetime;
struct timer *timer; struct timer *timer;
struct Rect uv; struct glrect st; /* Current ST coordinates for active frame */
struct Texture *tex; struct Texture *tex;
}; };
/* Describes an animation on a particular texture */
struct TexAnim { struct TexAnim {
int frames; int frames;
int dimensions[2]; int dimensions[2];