This commit is contained in:
sam 2024-08-25 12:52:42 +12:00
parent 6e9ca865f9
commit 83314c848f
11 changed files with 114 additions and 41 deletions

3
.clangd Normal file
View file

@ -0,0 +1,3 @@
CompileFlags:
Add: -ferror-limit=0

View file

@ -0,0 +1,18 @@
#include <gear/graphics/renderer.h>
g_renderer g_renderer_create(g_color clear_color) {
g_renderer renderer;
memset(&renderer, 0, sizeof(g_renderer));
sg_color_attachment_action color_action = {
.load_action = SG_LOADACTION_CLEAR,
.clear_value = G_COLOR_TO_FLOAT_ARR(clear_color),
};
renderer.pass_action = (sg_pass_action){
.colors = { color_action, color_action, color_action, color_action },
.depth = { .load_action = SG_LOADACTION_CLEAR, .clear_value = 1.0f },
};
return renderer;
}

27
src/images/create.c Normal file
View file

@ -0,0 +1,27 @@
#include <gear/images.h>
#include <string.h>
g_image* g_image_create(u8* pixels, i32 width, i32 height) {
g_image* p_image = calloc(1, sizeof(g_image));
if (p_image != NULL) {
p_image->width = width;
p_image->height = height;
p_image->channels = G_IMAGE_CHANNELS;
p_image->pixels = pixels;
}
return p_image;
}
g_image* g_image_default() {
g_image* p_image = calloc(1, sizeof(g_image));
if(p_image != NULL) {
p_image->width = G_IMAGE_MISSING_DIMENSIONS,
p_image->height = G_IMAGE_MISSING_DIMENSIONS,
p_image->channels = G_IMAGE_CHANNELS,
p_image->pixels = calloc(1, G_IMAGE_MISSING_BYTESIZE);
memcpy(p_image->pixels, G_IMAGE_MISSING_PIXELS, G_IMAGE_MISSING_BYTESIZE);
}
return p_image;
}

7
src/images/destroy.c Normal file
View file

@ -0,0 +1,7 @@
#include <gear/images.h>
#include <stb/stb_image.h>
void g_image_free(g_image* image) {
stbi_image_free(image->pixels);
free(image);
}

16
src/images/load.c Normal file
View file

@ -0,0 +1,16 @@
#include <gear/images.h>
#include <stb/stb_image.h>
g_image* g_image_load(const char* filename) {
const i32 desired_channels = 4;
i32 width, height, channels;
stbi_set_flip_vertically_on_load(true);
u8* pixels = stbi_load(filename, &width, &height, &channels, desired_channels);
if(pixels == NULL) {
return g_image_default();
}
return g_image_create(pixels, width, height);
}

View file

@ -1,3 +1,6 @@
#include "gear/images.h"
#include <gear/textures.h>
#include <sokol/sokol_app.h>
#include <sokol/sokol_gfx.h>
#include <sokol/sokol_log.h>
@ -5,18 +8,9 @@
#include <slibs/slibs.h>
#include <HandmadeMath/HandmadeMath.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
#include <stdio.h>
#include <shaders/transform.glsl.h>
static struct {
sg_pipeline pip;
sl_vec(sg_bindings*) bindings;
sg_pass_action pass_action;
} state;
typedef struct {
float x, y, z;
@ -46,33 +40,16 @@ static void init(void) {
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",
});
g_texture* texture = g_texture_create(g_image_load("happi.jpg"));
bind->fs.images[SLOT_tex] = texture->image;
bind->fs.samplers[SLOT_smp] = texture->sampler;
sg_shader shd = sg_make_shader(transform_shader_desc(sg_query_backend()));
@ -83,7 +60,7 @@ static void init(void) {
[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,
@ -91,12 +68,7 @@ static void init(void) {
.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;
@ -104,19 +76,17 @@ 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);
HMM_Mat4 view_proj = HMM_Mul(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);
model = HMM_Mul(model, HMM_Rotate_RH(HMM_AngleDeg(sin(t * 2) * 10), HMM_V3(0.0f, 0.2f, 0.0f)));
HMM_Mat4 mvp = HMM_Mul(view_proj, model);
vs_params_t params = {
.mvp = mvp,

1
src/renderer.c Normal file
View file

@ -0,0 +1 @@
#include <gear/graphics/renderer.h>

29
src/textures/create.c Normal file
View file

@ -0,0 +1,29 @@
#include <gear/textures.h>
#include <gear/images.h>
g_texture* g_texture_create(g_image* image) {
sg_image sg_img = sg_make_image(&(sg_image_desc){
.width = image->width,
.height = image->height,
.data.subimage[0][0] = {
.ptr = image->pixels,
.size = image->width * image->height * image->channels
},
.pixel_format = SG_PIXELFORMAT_RGBA8 // has to be used for opengl support
});
g_image_free(image);
sg_sampler sg_samp = sg_make_sampler(&(sg_sampler_desc){
.min_filter = SG_FILTER_LINEAR,
.mag_filter = SG_FILTER_LINEAR
});
g_texture* texture = calloc(1, sizeof(g_texture));
if(texture != NULL) {
texture->image = sg_img;
texture->sampler = sg_samp;
}
return texture;
}

2
src/thirdparty/stb/stb_image.c vendored Normal file
View file

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>