#include #include #include #include #include #include #define STB_IMAGE_IMPLEMENTATION #include #include #include static struct { sg_pipeline pip; sl_vec(sg_bindings*) bindings; sg_pass_action pass_action; } state; typedef struct { float x, y, z; uint32_t color; int16_t u, v; } vertex_t; static void init(void) { sg_setup(&(sg_desc){ .environment = sglue_environment(), .logger.func = slog_func, }); vertex_t vertices[] = { { 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 { -1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 0.0f * 32767, 1.0f * 32767 } // top left }; uint16_t indices[] = { // note that we start from 0! 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" }); 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); bind->fs.images[SLOT_tex] = sg_make_image(&(sg_image_desc){ .width = width, .height = height, .data.subimage[0][0] = {.ptr = pixels, .size = width * height * desired_channels}, .label = "triangle-texture", .pixel_format = SG_PIXELFORMAT_RGBA8, }); stbi_image_free(pixels); bind->fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){ .label = "triangle-sampler", }); sg_shader shd = sg_make_shader(transform_shader_desc(sg_query_backend())); state.pip = sg_make_pipeline(&(sg_pipeline_desc){ .shader = shd, .layout = { .attrs = { [ATTR_vs_pos].format = SG_VERTEXFORMAT_FLOAT3, [ATTR_vs_color0].format = SG_VERTEXFORMAT_UBYTE4N, [ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_SHORT2N, } }, .index_type = SG_INDEXTYPE_UINT16, .cull_mode = SG_CULLMODE_BACK, .depth = { .compare = SG_COMPAREFUNC_LESS_EQUAL, .write_enabled = true, }, .label = "triangle-pipeline", }); state.pass_action = (sg_pass_action) { .colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.2f, 0.4f, 0.3f, 1.0f } } }; } float t = 0.0f; void frame(void) { t += sapp_frame_duration(); printf("%f\n", 1.0f/sapp_frame_duration()); sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() }); sg_apply_pipeline(state.pip); 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 = { .mvp = mvp, .texture_scale = HMM_V2(1.0f, 1.0f), }; sg_apply_bindings(state.bindings.data[i]); sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, SG_RANGE_REF(params)); sg_draw(0, 6, 1); } 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, .window_title = "Gear", .icon.sokol_default = true, .logger.func = slog_func, .swap_interval = 0 }; }