This commit is contained in:
John Alanbrook 2023-05-31 19:52:30 +00:00
parent aa08815e7c
commit 5d7b7880ef
5 changed files with 70 additions and 44 deletions

View file

@ -960,6 +960,17 @@ static inline HMM_Mat2 HMM_TransposeM2(HMM_Mat2 Matrix) {
return Result; return Result;
} }
static inline HMM_Mat2 HMM_RotateM2(float angle)
{
HMM_Mat2 result;
result.Elements[0][0] = cos(angle);
result.Elements[0][1] = sin(angle);
result.Elements[1][0] = -result.Elements[0][1];
result.Elements[1][1] = result.Elements[0][0];
return result;
}
static inline HMM_Mat2 HMM_AddM2(HMM_Mat2 Left, HMM_Mat2 Right) { static inline HMM_Mat2 HMM_AddM2(HMM_Mat2 Left, HMM_Mat2 Right) {
HMM_Mat2 Result; HMM_Mat2 Result;

View file

@ -269,7 +269,7 @@ JSValue duk_cursor_text(JSContext *js, JSValueConst this, int argc, JSValueConst
JSValue duk_gui_img(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_gui_img(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
const char *img = JS_ToCString(js, argv[0]); const char *img = JS_ToCString(js, argv[0]);
cpVect pos = js2vec2(argv[1]); cpVect pos = js2vec2(argv[1]);
gui_draw_img(img, pos.x, pos.y); gui_draw_img(img, js2hmmv2(argv[1]), js2number(argv[2]), js2number(argv[3]));
JS_FreeCString(js, img); JS_FreeCString(js, img);
return JS_NULL; return JS_NULL;
} }

View file

@ -11,6 +11,8 @@
#include "texture.h" #include "texture.h"
#include "timer.h" #include "timer.h"
#include <string.h> #include <string.h>
#include <ctype.h>
#include <limits.h>
struct TextureOptions TEX_SPRITE = {1, 0, 0}; struct TextureOptions TEX_SPRITE = {1, 0, 0};
@ -20,6 +22,12 @@ static int first = -1;
static sg_pipeline pip_sprite; static sg_pipeline pip_sprite;
static sg_bindings bind_sprite; static sg_bindings bind_sprite;
struct sprite_vert {
HMM_Vec2 pos;
struct uv_n uv;
struct rgba color;
};
int make_sprite(int go) { int make_sprite(int go) {
struct sprite sprite = { struct sprite sprite = {
.color = color_white, .color = color_white,
@ -85,6 +93,7 @@ void sprite_io(struct sprite *sprite, FILE *f, int read) {
void sprite_draw_all() { void sprite_draw_all() {
sg_apply_pipeline(pip_sprite); sg_apply_pipeline(pip_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection));
static struct sprite **layers[5]; static struct sprite **layers[5];
@ -116,7 +125,9 @@ void sprite_initialize() {
.vs.uniform_blocks[0] = { .vs.uniform_blocks[0] = {
.size = 64, .size = 64,
.layout = SG_UNIFORMLAYOUT_STD140, .layout = SG_UNIFORMLAYOUT_STD140,
.uniforms = {[0] = {.name = "mpv", .type = SG_UNIFORMTYPE_MAT4}}}, .uniforms = {
[0] = {.name = "proj", .type = SG_UNIFORMTYPE_MAT4},
}},
.fs.images[0] = { .fs.images[0] = {
.name = "image", .name = "image",
@ -130,7 +141,9 @@ void sprite_initialize() {
.shader = shader_sprite, .shader = shader_sprite,
.layout = { .layout = {
.attrs = { .attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT4}}, [0].format = SG_VERTEXFORMAT_FLOAT2,
[1].format = SG_VERTEXFORMAT_USHORT2N,
[2].format = SG_VERTEXFORMAT_UBYTE4N}},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.label = "sprite pipeline", .label = "sprite pipeline",
/* .depth = { /* .depth = {
@ -141,53 +154,52 @@ void sprite_initialize() {
}); });
bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(float) * 16 * 500, .size = sizeof(struct sprite_vert) * 500,
.type = SG_BUFFERTYPE_VERTEXBUFFER, .type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM, .usage = SG_USAGE_STREAM,
.label = "sprite vertex buffer", .label = "sprite vertex buffer",
}); });
} }
/* offset given in texture offset, so -0.5,-0.5 results in it being centered */
void tex_draw(struct Texture *tex, HMM_Vec2 pos, float angle, HMM_Vec2 size, HMM_Vec2 offset, struct glrect r, struct rgba color) { void tex_draw(struct Texture *tex, HMM_Vec2 pos, float angle, HMM_Vec2 size, HMM_Vec2 offset, struct glrect r, struct rgba color) {
HMM_Mat4 model = HMM_M4D(1.0); struct sprite_vert verts[4];
HMM_Mat4 r_model = HMM_M4D(1.0);
HMM_Vec3 t_scale = { HMM_Vec2 sposes[4] = {
tex->width * st_s_w(r) * size.X, {0.0,0.0},
tex->height * st_s_h(r) * size.Y, {1.0,0.0},
t_scale.Z = 1.0}; {0.0,1.0},
{1.0,1.0},
HMM_Vec3 t_offset = {
offset.X * t_scale.X,
offset.Y * t_scale.Y,
0.0};
HMM_Translate_p(&model, t_offset);
HMM_Scale_p(&model, t_scale);
r_model = HMM_Rotate_RH(angle, vZ);
model = HMM_MulM4(r_model, model);
HMM_Translate_p(&model, (HMM_Vec3){pos.X, pos.Y, 0.0});
model = HMM_MulM4(projection, model);
float vertices[] = {
0.f, 0.f, r.s0, r.t1,
1, 0.f, r.s1, r.t1,
0.f, 1, r.s0, r.t0,
1.f, 1.f, r.s1, r.t0};
bind_sprite.fs_images[0] = tex->id;
sg_append_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(vertices));
sg_apply_bindings(&bind_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(model.Elements));
float cl[3] = {
color.r / 255.0,
color.g / 255.0,
color.b / 255.0
}; };
sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(cl)); HMM_Mat2 rot = HMM_RotateM2(angle);
HMM_Vec2 t_scale = {
tex->width * st_s_w(r) * size.X,
tex->height * st_s_h(r) * size.Y};
for (int i = 0; i < 4; i++) {
sposes[i] = HMM_AddV2(sposes[i], offset);
sposes[i] = HMM_MulV2(sposes[i], t_scale);
sposes[i] = HMM_MulM2V2(rot, sposes[i]);
sposes[i] = HMM_AddV2(sposes[i], pos);
verts[i].pos = sposes[0];
verts[i].color = color;
}
verts[0].uv.u = r.s0 * USHRT_MAX;
verts[0].uv.v = r.t1 * USHRT_MAX;
verts[1].uv.u = r.s1 * USHRT_MAX;
verts[1].uv.v = r.t1 * USHRT_MAX;
verts[2].uv.u = r.s0 * USHRT_MAX;
verts[2].uv.v = r.t0 * USHRT_MAX;
verts[3].uv.u = r.s1 * USHRT_MAX;
verts[3].uv.v = r.t0 * USHRT_MAX;
bind_sprite.fs_images[0] = tex->id;
sg_append_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(verts));
sg_apply_bindings(&bind_sprite);
sg_draw(sprite_count * 4, 4, 1); sg_draw(sprite_count * 4, 4, 1);
sprite_count++; sprite_count++;
@ -212,6 +224,7 @@ void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame) {
void gui_draw_img(const char *img, HMM_Vec2 pos, float scale, float angle) { void gui_draw_img(const char *img, HMM_Vec2 pos, float scale, float angle) {
sg_apply_pipeline(pip_sprite); sg_apply_pipeline(pip_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj));
struct Texture *tex = texture_loadfromfile(img); struct Texture *tex = texture_loadfromfile(img);
HMM_Vec2 size = {scale, scale}; HMM_Vec2 size = {scale, scale};
HMM_Vec2 offset = {0.f, 0.f}; HMM_Vec2 offset = {0.f, 0.f};

View file

@ -41,7 +41,7 @@ void sprite_draw_all();
unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite); unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite);
void sprite_flush(); void sprite_flush();
void gui_draw_img(const char *img, HMM_Vec2 pos); void gui_draw_img(const char *img, HMM_Vec2 pos, float scale, float angle);
#endif #endif

View file

@ -1,5 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec4 vertex; layout (location = 0) in vec2 vertex;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec4 vColor;
out vec2 texcoords; out vec2 texcoords;
uniform mat4 proj; uniform mat4 proj;
@ -7,6 +9,6 @@ uniform mat4 mpv;
void main() void main()
{ {
texcoords = vertex.zw; texcoords = uv;
gl_Position = mpv * vec4(vertex.xy, 0.0, 1.0); gl_Position = mpv * proj * vec4(vertex, 0.0, 1.0);
} }