100 lines
3.6 KiB
C
100 lines
3.6 KiB
C
#include "allocator.h"
|
|
#include "kitty.h"
|
|
#include "log.h"
|
|
#include "matrix.h"
|
|
#include "object.h"
|
|
#include "types.h"
|
|
#include "util.h"
|
|
|
|
#define INSTANCE_COUNT 1000
|
|
|
|
struct Trig {
|
|
struct Kitty *kitty;
|
|
struct mat3x3 model[INSTANCE_COUNT];
|
|
struct Vec2 vel[INSTANCE_COUNT];
|
|
};
|
|
|
|
#define TRIG_VERTEX_COUNT 6
|
|
struct Vertex trig_vertices[TRIG_VERTEX_COUNT] = {
|
|
(struct Vertex){(struct Vec2){.x = 0.0f, .y = 0.0f},
|
|
(struct Vec3){1.0f, 0.0f, 1.0f}},
|
|
(struct Vertex){(struct Vec2){.x = 1.0f, .y = 0.0f},
|
|
(struct Vec3){1.0f, 0.0f, 0.0f}},
|
|
(struct Vertex){(struct Vec2){.x = 1.0f, .y = 1.0f},
|
|
(struct Vec3){1.0f, 0.0f, 1.0f}},
|
|
(struct Vertex){(struct Vec2){.x = 0.0f, .y = 0.0f},
|
|
(struct Vec3){1.0f, 0.0f, 1.0f}},
|
|
(struct Vertex){(struct Vec2){.x = 1.0f, .y = 1.0f},
|
|
(struct Vec3){1.0f, 0.0f, 1.0f}},
|
|
(struct Vertex){(struct Vec2){.x = 0.0f, .y = 1.0f},
|
|
(struct Vec3){0.0f, 0.0f, 1.0f}},
|
|
};
|
|
struct TrigUBO {
|
|
struct mat3x3 view;
|
|
struct mat3x3 proj;
|
|
};
|
|
struct Trig *trig_make_args(struct Scene *scene) {
|
|
/* struct Trig *trig = mem_malloc(scene->mem, sizeof(struct Trig)); */
|
|
struct Trig *trig = malloc(sizeof(struct Trig));
|
|
|
|
trig->kitty = kitty_make();
|
|
kitty_set_vertex_shader(trig->kitty, "./Shaders/vert.spv");
|
|
kitty_set_fragment_shader(trig->kitty, "./Shaders/frag.spv");
|
|
kitty_set_vertex_buffer(trig->kitty, trig_vertices, TRIG_VERTEX_COUNT,
|
|
sizeof(struct Vertex));
|
|
kitty_add_vertex_buffer_format(trig->kitty, VK_FORMAT_R32G32_SFLOAT);
|
|
kitty_add_vertex_buffer_format(trig->kitty, VK_FORMAT_R32G32B32_SFLOAT);
|
|
|
|
kitty_attatch_ubo(trig->kitty, sizeof(struct TrigUBO));
|
|
kitty_attatch_ubo(trig->kitty, sizeof(struct mat3x3) * INSTANCE_COUNT);
|
|
kitty_attatch_image(trig->kitty, "./neocat.png");
|
|
|
|
uint32_t instance_buffer[INSTANCE_COUNT] = {0};
|
|
for (int i = 0; i < INSTANCE_COUNT; i++) {
|
|
instance_buffer[i] = i;
|
|
trig->vel[i] = vec2_normalize((struct Vec2){randf() - 0.5, randf() - 0.5});
|
|
trig->model[i] =
|
|
multiply3x3(multiply3x3(translate3x3((struct Vec2){randf(), randf()}),
|
|
scale3x3((struct Vec2){0.1f, 0.1f})),
|
|
rotate3x3(randf() * 2 * PI));
|
|
}
|
|
kitty_add_instance_buffer(trig->kitty, instance_buffer, INSTANCE_COUNT,
|
|
sizeof(uint32_t));
|
|
kitty_add_instance_buffer_format(trig->kitty, VK_FORMAT_R32_UINT);
|
|
|
|
kitty_finalise(scene->vk, trig->kitty);
|
|
|
|
return trig;
|
|
}
|
|
|
|
struct Trig *make_trig(struct Scene *scene, char *_data, int _len) {
|
|
return trig_make_args(scene);
|
|
}
|
|
|
|
void free_trig(struct Scene *scene, struct Trig *trig) {
|
|
free_kitty(scene->vk, trig->kitty);
|
|
/* mem_free(scene->mem, trig); */
|
|
free(trig);
|
|
}
|
|
|
|
void trig_tick(struct Scene *scene, struct Trig *trig) {
|
|
struct TrigUBO ubo = {0};
|
|
ubo.view = multiply3x3(translate3x3((struct Vec2){-1.5f, -0.5f}),
|
|
scale3x3((struct Vec2){1.0f, 1.0f}));
|
|
ubo.proj = scale3x3(
|
|
(struct Vec2){1.f, (float)scene->vk->width / (float)scene->vk->heigh});
|
|
kitty_set_next_ubo(scene->vk, trig->kitty, 0, &ubo);
|
|
|
|
for (int i = 0; i < INSTANCE_COUNT; i++) {
|
|
trig->model[i] = multiply3x3(
|
|
trig->model[i],
|
|
translate3x3(vec2_mul(trig->vel[i], scene->msecs * 0.000000000002f)));
|
|
if (trig->model[i].m13 < 0 || trig->model[i].m13 > 2.5) {
|
|
trig->vel[i].x = -trig->vel[i].x;
|
|
}
|
|
if (trig->model[i].m23 < -0.5 || trig->model[i].m23 > 2) {
|
|
trig->vel[i].y = -trig->vel[i].y;
|
|
}
|
|
}
|
|
kitty_set_next_ubo(scene->vk, trig->kitty, 1, &trig->model);
|
|
}
|