gear/src/main.c

152 lines
4.5 KiB
C
Raw Normal View History

2024-08-17 19:12:22 +12:00
#include <sokol/sokol_app.h>
#include <sokol/sokol_gfx.h>
#include <sokol/sokol_log.h>
#include <sokol/sokol_glue.h>
2024-08-18 22:17:22 +12:00
#include <slibs/slibs.h>
2024-08-19 11:54:40 +12:00
#include <HandmadeMath/HandmadeMath.h>
2024-08-19 18:03:22 +12:00
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
#include <stdio.h>
2024-08-19 11:54:40 +12:00
#include <shaders/transform.glsl.h>
2024-08-17 19:12:22 +12:00
static struct {
sg_pipeline pip;
2024-08-18 22:17:22 +12:00
sl_vec(sg_bindings*) bindings;
2024-08-17 19:12:22 +12:00
sg_pass_action pass_action;
} state;
2024-08-18 15:21:25 +12:00
typedef struct {
float x, y, z;
2024-08-19 11:54:40 +12:00
uint32_t color;
2024-08-18 15:21:25 +12:00
int16_t u, v;
} vertex_t;
2024-08-17 19:12:22 +12:00
static void init(void) {
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
});
2024-08-18 22:17:22 +12:00
vertex_t vertices[] = {
2024-08-19 18:03:22 +12:00
{ 1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 1.0f * 32767, 1.0f * 32767 }, // top right
{ 1.0f, -1.0f, 0.0f, 0xFFFFFFFF, 1.0f * 32767, 0.0f * 32767 }, // bottom right
{ -1.0f, -1.0f, 0.0f, 0xFFFFFFFF, 0.0f * 32767, 0.0f * 32767 }, // bottom left
2024-08-19 11:54:40 +12:00
{ -1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 0.0f * 32767, 1.0f * 32767 } // top left
2024-08-18 22:17:22 +12:00
};
2024-08-19 11:54:40 +12:00
uint16_t indices[] = { // note that we start from 0!
2024-08-18 22:17:22 +12:00
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
sg_bindings* bind = calloc(1, sizeof(sg_bindings));
sl_vec_push(state.bindings, bind);
bind->vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.data = SG_RANGE(vertices),
.label = "triangle-vertices"
});
bind->index_buffer = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_INDEXBUFFER,
.data = SG_RANGE(indices),
.label = "triangle-indices"
});
2024-08-19 18:03:22 +12:00
int desired_channels = 4;
int width, height, channels;
stbi_set_flip_vertically_on_load(true);
uint8_t* pixels = stbi_load("happi.jpg", &width, &height, &channels, desired_channels);
2024-08-18 15:21:25 +12:00
2024-08-18 22:17:22 +12:00
bind->fs.images[SLOT_tex] = sg_make_image(&(sg_image_desc){
2024-08-19 18:03:22 +12:00
.width = width,
.height = height,
.data.subimage[0][0] = {.ptr = pixels, .size = width * height * desired_channels},
2024-08-18 15:21:25 +12:00
.label = "triangle-texture",
2024-08-19 16:20:49 +12:00
.pixel_format = SG_PIXELFORMAT_RGBA8,
2024-08-18 15:21:25 +12:00
});
2024-08-19 18:03:22 +12:00
stbi_image_free(pixels);
2024-08-18 22:17:22 +12:00
bind->fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){
2024-08-19 16:20:49 +12:00
.label = "triangle-sampler",
2024-08-18 15:21:25 +12:00
});
2024-08-19 11:54:40 +12:00
sg_shader shd = sg_make_shader(transform_shader_desc(sg_query_backend()));
2024-08-17 19:12:22 +12:00
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.shader = shd,
.layout = {
.attrs = {
2024-08-19 11:54:40 +12:00
[ATTR_vs_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_color0].format = SG_VERTEXFORMAT_UBYTE4N,
2024-08-18 15:21:25 +12:00
[ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_SHORT2N,
2024-08-17 19:12:22 +12:00
}
},
2024-08-19 11:54:40 +12:00
.index_type = SG_INDEXTYPE_UINT16,
2024-08-17 19:12:22 +12:00
.cull_mode = SG_CULLMODE_BACK,
2024-08-19 11:54:40 +12:00
.depth = {
.compare = SG_COMPAREFUNC_LESS_EQUAL,
.write_enabled = true,
},
.label = "triangle-pipeline",
2024-08-17 19:12:22 +12:00
});
state.pass_action = (sg_pass_action) {
2024-08-18 15:21:25 +12:00
.colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.2f, 0.4f, 0.3f, 1.0f } }
2024-08-17 19:12:22 +12:00
};
}
2024-08-19 11:54:40 +12:00
float t = 0.0f;
2024-08-17 19:12:22 +12:00
void frame(void) {
2024-08-19 11:54:40 +12:00
t += sapp_frame_duration();
2024-08-19 18:03:22 +12:00
printf("%f\n", 1.0f/sapp_frame_duration());
2024-08-17 19:12:22 +12:00
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
2024-08-19 11:54:40 +12:00
HMM_Mat4 view = HMM_Translate(HMM_V3(0.0f, 0.0f, -3.0f));
HMM_Mat4 projection = HMM_Perspective_RH_NO(HMM_AngleDeg(45.0f), sapp_widthf() / sapp_heightf(), 0.1f, 100.0f);
HMM_Mat4 view_proj = HMM_MulM4(projection, view);
for(size_t i = 0; i < state.bindings.size; i++) {
HMM_Mat4 model = HMM_Rotate_RH(HMM_AngleDeg(-55.0f), HMM_V3(1.0f, 0.0f, 0.0f));
model = HMM_MulM4(model, HMM_Rotate_RH(HMM_AngleDeg(sin(t * 2) * 10), HMM_V3(0.0f, 0.2f, 0.0f)));
HMM_Mat4 mvp = HMM_MulM4(view_proj, model);
vs_params_t params = {
2024-08-19 16:20:49 +12:00
.mvp = mvp,
.texture_scale = HMM_V2(1.0f, 1.0f),
2024-08-19 11:54:40 +12:00
};
sg_apply_bindings(state.bindings.data[i]);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, SG_RANGE_REF(params));
2024-08-18 22:17:22 +12:00
sg_draw(0, 6, 1);
}
2024-08-17 19:12:22 +12:00
sg_end_pass();
sg_commit();
}
void cleanup(void) {
sg_shutdown();
}
sapp_desc sokol_main(int argc, char* argv[]) {
(void)argc; (void)argv;
return (sapp_desc){
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.width = 640,
.height = 480,
2024-08-18 13:07:10 +12:00
.window_title = "Gear",
2024-08-17 19:12:22 +12:00
.icon.sokol_default = true,
.logger.func = slog_func,
2024-08-19 18:03:22 +12:00
.swap_interval = 0
2024-08-17 19:12:22 +12:00
};
2024-08-19 11:54:40 +12:00
}