diff options
author | Omniscient <omniscient.oce@gmail.com> | 2024-06-10 15:12:28 +1000 |
---|---|---|
committer | Omniscient <omniscient.oce@gmail.com> | 2024-06-10 15:12:28 +1000 |
commit | ae214fc4c9a1cac1969f64b2316d8ec431b33693 (patch) | |
tree | 35968674ad0f7dd9b0f655ccab44a96a62343d6a | |
parent | 99f2476d7bb8479d543f080c209324c77c775737 (diff) |
default renderer pipeline
-rw-r--r-- | assets/shaders/cube.frag | 2 | ||||
-rw-r--r-- | assets/shaders/cube.vert | 1 | ||||
-rw-r--r-- | examples/cube/ex_cube.c | 2 | ||||
-rw-r--r-- | examples/gltf_loading/ex_gltf_loading.c | 16 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.c | 10 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.h | 1 | ||||
-rw-r--r-- | src/renderer/render.c | 56 | ||||
-rw-r--r-- | src/renderer/render.h | 2 | ||||
-rw-r--r-- | src/renderer/static_pipeline.h | 30 | ||||
-rw-r--r-- | src/resources/gltf.c | 384 | ||||
-rw-r--r-- | src/resources/obj.c | 16 | ||||
-rw-r--r-- | src/scene.h | 1 | ||||
-rw-r--r-- | src/std/mem.c | 2 |
13 files changed, 288 insertions, 235 deletions
diff --git a/assets/shaders/cube.frag b/assets/shaders/cube.frag index 21d7b55..5f17e7c 100644 --- a/assets/shaders/cube.frag +++ b/assets/shaders/cube.frag @@ -10,5 +10,5 @@ layout(location = 0) out vec4 outColor; void main() { // outColor = texture(texSampler, fragTexCoord); // vec4(fragTexCoord, 0.0); - outColor = vec4(fragColor, 1.0); + outColor = vec4(1.0); } diff --git a/assets/shaders/cube.vert b/assets/shaders/cube.vert index 2ccab09..7af7413 100644 --- a/assets/shaders/cube.vert +++ b/assets/shaders/cube.vert @@ -15,6 +15,7 @@ layout(location = 1) out vec2 fragTexCoord; void main() { gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); + // gl_Position = vec4(inPosition, 1.0); fragColor = abs(inNormal); fragTexCoord = inTexCoords; } diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c index 8789b00..5f45b7f 100644 --- a/examples/cube/ex_cube.c +++ b/examples/cube/ex_cube.c @@ -145,7 +145,7 @@ int main() { encode_bind_shader_data(enc, 0, &mvp_uniforms_data); // Record draw calls - draw_mesh(&cube, &model); + draw_mesh(&cube, &model, &cam); // End recording gpu_cmd_encoder_end_render(enc); diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c index e7f84ff..85bb389 100644 --- a/examples/gltf_loading/ex_gltf_loading.c +++ b/examples/gltf_loading/ex_gltf_loading.c @@ -31,8 +31,8 @@ int main() { model_handle helmet_handle = model_load_gltf("assets/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf", false); - INFO("GLTF loaded successfully!"); - model* helmet = MODEL_GET(helmet_handle); + INFO("GLTF loaded successfully!"); + model* helmet = MODEL_GET(helmet_handle); vec3 camera_pos = vec3(5., 0., 0.); vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); @@ -55,8 +55,11 @@ int main() { point_lights[i].quadratic = 0.032; } - // scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 }; - // memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); + scene our_scene; + scene_init(&our_scene); + memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); + our_scene.point_lights_count = 4; + our_scene.dir_light = dir_light; while (!should_exit(&g_core)) { input_update(&g_core.input); @@ -68,14 +71,13 @@ int main() { render_frame_begin(&g_core.renderer); // Draw the model - static f32 angle = 0.0; - static f32 rot_speed = 0.5; + static f32 angle = 0.0, rot_speed = 0.5; quat rot = quat_from_axis_angle(VEC3_Z, fmod(angle, TAU), true); angle += (rot_speed * deltaTime); transform model_tf = transform_create(vec3(0.0, 0.1, -0.1), rot, 1.8); mat4 model = transform_to_mat(&model_tf); - draw_mesh(&helmet->meshes->data[0], &model); + draw_mesh(&helmet->meshes->data[0], &model, &cam); render_frame_end(&g_core.renderer); } diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c index de6b71a..5a11f39 100644 --- a/src/renderer/backends/opengl/backend_opengl.c +++ b/src/renderer/backends/opengl/backend_opengl.c @@ -103,7 +103,6 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo); glBindBufferBase(GL_UNIFORM_BUFFER, binding_j, ubo_buf->id.ubo); if (blockIndex != GL_INVALID_INDEX) { - printf("Here\n"); glUniformBlockBinding(pipeline->shader_id, blockIndex, 0); } @@ -112,6 +111,8 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip } } + pipeline->wireframe = description.wireframe; + return pipeline; } void gpu_pipeline_destroy(gpu_pipeline* pipeline) {} @@ -164,6 +165,13 @@ void copy_buffer_to_image_oneshot(buffer_handle src, texture_handle dst) {} // --- Render commands void encode_bind_pipeline(gpu_cmd_encoder* encoder, pipeline_kind kind, gpu_pipeline* pipeline) { encoder->pipeline = pipeline; + + if (pipeline->wireframe) { + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + } else { + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + } + // In OpenGL binding a pipeline is more or less equivalent to just setting the shader glUseProgram(pipeline->shader_id); } diff --git a/src/renderer/backends/opengl/backend_opengl.h b/src/renderer/backends/opengl/backend_opengl.h index f3b4eb7..ccdb446 100644 --- a/src/renderer/backends/opengl/backend_opengl.h +++ b/src/renderer/backends/opengl/backend_opengl.h @@ -20,6 +20,7 @@ typedef struct gpu_pipeline { u32 shader_id; vertex_description vertex_desc; buffer_handle uniform_bindings[MAX_PIPELINE_UNIFORM_BUFFERS]; + bool wireframe; } gpu_pipeline; typedef struct gpu_renderpass { void *pad diff --git a/src/renderer/render.c b/src/renderer/render.c index f9e8ccf..3a373dc 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -11,6 +11,10 @@ #include "ral_types.h" #include "render.h" +//---NEW +#include "static_pipeline.h" +//---END + /** @brief Creates the pipelines built into Celeritas such as rendering static opaque geometry, debug visualisations, immediate mode UI, etc */ void default_pipelines_init(renderer* ren); @@ -56,15 +60,6 @@ bool renderer_init(renderer* ren) { ren->resource_pools = arena_alloc(&pool_arena, sizeof(struct resource_pools)); resource_pools_init(&pool_arena, ren->resource_pools); - // ren->blinn_phong = - // shader_create_separate("assets/shaders/blinn_phong.vert", - // "assets/shaders/blinn_phong.frag"); - - // ren->skinned = - // shader_create_separate("assets/shaders/skinned.vert", "assets/shaders/blinn_phong.frag"); - - // default_material_init(); - // Create default rendering pipeline default_pipelines_init(ren); @@ -103,18 +98,22 @@ void default_pipelines_init(renderer* ren) { ERROR_EXIT("Failed to load shaders from disk") } - vertex_description vertex_input = {0}; + // Vertex attributes + vertex_description vertex_input = { 0 }; vertex_input.debug_label = "Standard Static 3D Vertex Format"; vertex_desc_add(&vertex_input, "inPosition", ATTR_F32x3); vertex_desc_add(&vertex_input, "inNormal", ATTR_F32x3); vertex_desc_add(&vertex_input, "inTexCoords", ATTR_F32x2); vertex_input.use_full_vertex_size = true; + // Shader data bindings + shader_data mvp_uniforms_data = { .data = NULL, .shader_data_get_layout = &mvp_uniforms_layout }; + struct graphics_pipeline_desc pipeline_description = { .debug_name = "Basic Pipeline", .vertex_desc = vertex_input, - // .data_layouts - // .data_layouts_count + .data_layouts = { mvp_uniforms_data }, + .data_layouts_count = 1, .vs = { .debug_name = "Basic Vertex Shader", .filepath = vert_path, .code = vertex_shader.contents, @@ -135,6 +134,7 @@ void render_frame_begin(renderer* ren) { ren->frame_aborted = false; if (!gpu_backend_begin_frame()) { ren->frame_aborted = true; + WARN("Frame aborted"); return; } gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); @@ -148,7 +148,6 @@ void render_frame_end(renderer* ren) { if (ren->frame_aborted) { return; } - // gpu_temp_draw(3); gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); gpu_cmd_encoder_end_render(enc); gpu_cmd_buffer buf = gpu_cmd_encoder_finish(enc); @@ -157,18 +156,33 @@ void render_frame_end(renderer* ren) { } void render_frame_draw(renderer* ren) {} -bool mesh_has_indices(mesh* m) { - return m->geometry->has_indices; -} +bool mesh_has_indices(mesh* m) { return m->geometry->has_indices; } -void draw_mesh(mesh* mesh, mat4* model) { // , mat4* view, mat4* proj) { +/** + * + * @param Camera used for getting the view projection matric to draw the mesh with. + * If NULL use the last used camera */ +void draw_mesh(mesh* mesh, mat4* model, camera* cam) { // , mat4* view, mat4* proj) { gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); + encode_set_vertex_buffer(enc, mesh->vertex_buffer); if (mesh_has_indices(mesh)) { encode_set_index_buffer(enc, mesh->index_buffer); } - // Assume this has already been done - /* encode_bind_shader_data(enc, 0, &mvp_uniforms_data); */ + + mat4 view, proj; + if (cam) { + camera_view_projection(cam, // FIXME: proper swapchain dimensions + 1000, 1000, &view, &proj); + + } else { + WARN("No camera set"); + } + mvp_uniforms mvp_data = { .model = *model, .view = view, .projection = proj }; + my_shader_bind_group shader_bind_data = { .mvp = mvp_data }; + shader_data mvp_uniforms_data = { .data = &shader_bind_data, + .shader_data_get_layout = &mvp_uniforms_layout }; + encode_bind_shader_data(enc, 0, &mvp_uniforms_data); encode_draw_indexed(enc, mesh->geometry->indices->len); } @@ -192,8 +206,8 @@ mesh mesh_create(geometry_data* geometry, bool free_on_upload) { // Create and upload index buffer size_t index_bytes = geometry->indices->len * sizeof(u32); INFO("Creating index buffer with size %d (len: %d)", index_bytes, geometry->indices->len); - m.index_buffer = - gpu_buffer_create(index_bytes, CEL_BUFFER_INDEX, CEL_BUFFER_FLAG_GPU, geometry->indices->data); + m.index_buffer = gpu_buffer_create(index_bytes, CEL_BUFFER_INDEX, CEL_BUFFER_FLAG_GPU, + geometry->indices->data); m.is_uploaded = true; // m.has_indices = geometry->has_indices; diff --git a/src/renderer/render.h b/src/renderer/render.h index b745e7a..12c0ce4 100644 --- a/src/renderer/render.h +++ b/src/renderer/render.h @@ -82,7 +82,7 @@ void shader_hot_reload(const char* filepath); */ mesh mesh_create(geometry_data* geometry, bool free_on_upload); -void draw_mesh(mesh* mesh, mat4* model); //, mat4* view, mat4* proj); // TODO: material +void draw_mesh(mesh* mesh, mat4* model, camera* cam); model_handle model_load(const char* debug_name, const char* filepath); diff --git a/src/renderer/static_pipeline.h b/src/renderer/static_pipeline.h new file mode 100644 index 0000000..78f09f2 --- /dev/null +++ b/src/renderer/static_pipeline.h @@ -0,0 +1,30 @@ +#pragma once +#include "maths_types.h" +#include "defines.h" +#include "ral.h" +#include "ral_types.h" +#include "render_types.h" + +typedef struct mvp_uniforms { + mat4 model; + mat4 view; + mat4 projection; +} mvp_uniforms; +typedef struct my_shader_bind_group { + mvp_uniforms mvp; +} my_shader_bind_group; + +static shader_data_layout mvp_uniforms_layout(void* data) { + my_shader_bind_group* d = (my_shader_bind_group*)data; + bool has_data = data != NULL; + + shader_binding b1 = { .label = "mvp_uniforms", + .type = SHADER_BINDING_BYTES, + .stores_data = has_data, + .data = { .bytes = { .size = sizeof(mvp_uniforms) } } }; + + if (has_data) { + b1.data.bytes.data = &d->mvp; + } + return (shader_data_layout){ .name = "global_ubo", .bindings = { b1}, .bindings_count = 1 }; +} diff --git a/src/resources/gltf.c b/src/resources/gltf.c index 67b4a86..373951b 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -50,7 +50,7 @@ model_handle model_load_gltf(const char *path, bool invert_texture_y) { const char *file_string = string_from_file(path); model_handle handle; - model* model = model_pool_alloc(&g_core.models, &handle); + model *model = model_pool_alloc(&g_core.models, &handle); model->name = str8_cstr_view(path); model->meshes = mesh_darray_new(1); @@ -88,27 +88,27 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel model *out_model, bool invert_textures_y) { TRACE("Load GLTF from string"); - // Setup temps - vec3_darray *tmp_positions = vec3_darray_new(1000); - vec3_darray *tmp_normals = vec3_darray_new(1000); - vec2_darray *tmp_uvs = vec2_darray_new(1000); - vec4u_darray *tmp_joint_indices = vec4u_darray_new(1000); - vec4_darray *tmp_weights = vec4_darray_new(1000); - // FIXME - // joint_darray *tmp_joints = joint_darray_new(256); - // vertex_bone_data_darray *tmp_vertex_bone_data = vertex_bone_data_darray_new(1000); - - cgltf_options options = { 0 }; - cgltf_data *data = NULL; - cgltf_result result = cgltf_parse_file(&options, filepath, &data); - if (result != cgltf_result_success) { - WARN("gltf load failed"); - // TODO: cleanup arrays(allocate all from arena ?) - return false; - } + // Setup temps + vec3_darray *tmp_positions = vec3_darray_new(1000); + vec3_darray *tmp_normals = vec3_darray_new(1000); + vec2_darray *tmp_uvs = vec2_darray_new(1000); + vec4u_darray *tmp_joint_indices = vec4u_darray_new(1000); + vec4_darray *tmp_weights = vec4_darray_new(1000); + // FIXME + // joint_darray *tmp_joints = joint_darray_new(256); + // vertex_bone_data_darray *tmp_vertex_bone_data = vertex_bone_data_darray_new(1000); + + cgltf_options options = { 0 }; + cgltf_data *data = NULL; + cgltf_result result = cgltf_parse_file(&options, filepath, &data); + if (result != cgltf_result_success) { + WARN("gltf load failed"); + // TODO: cleanup arrays(allocate all from arena ?) + return false; + } - cgltf_load_buffers(&options, data, filepath); - DEBUG("loaded buffers"); + cgltf_load_buffers(&options, data, filepath); + DEBUG("loaded buffers"); // // --- Skin // size_t num_skins = data->skins_count; @@ -196,129 +196,129 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel // // material_darray_push(out_model->materials, our_material); // } - // --- Meshes - size_t num_meshes = data->meshes_count; - TRACE("Num meshes %d", num_meshes); - for (size_t m = 0; m < num_meshes; m++) { - cgltf_primitive primitive = data->meshes[m].primitives[0]; - DEBUG("Found %d attributes", primitive.attributes_count); - // DEBUG("Number of this primitive %d", primitive.) - - for (cgltf_size a = 0; a < data->meshes[m].primitives[0].attributes_count; a++) { - cgltf_attribute attribute = data->meshes[m].primitives[0].attributes[a]; - if (attribute.type == cgltf_attribute_type_position) { - TRACE("Load positions from accessor"); - - cgltf_accessor *accessor = attribute.data; - assert(accessor->component_type == cgltf_component_type_r_32f); - // CASSERT_MSG(accessor->type == cgltf_type_vec3, "Vertex positions should be a vec3"); - - TRACE("Loading %d vec3 components", accessor->count); - - for (cgltf_size v = 0; v < accessor->count; ++v) { - vec3 pos; - cgltf_accessor_read_float(accessor, v, &pos.x, 3); - vec3_darray_push(tmp_positions, pos); - } - - } else if (attribute.type == cgltf_attribute_type_normal) { - TRACE("Load normals from accessor"); + // --- Meshes + size_t num_meshes = data->meshes_count; + TRACE("Num meshes %d", num_meshes); + for (size_t m = 0; m < num_meshes; m++) { + cgltf_primitive primitive = data->meshes[m].primitives[0]; + DEBUG("Found %d attributes", primitive.attributes_count); + // DEBUG("Number of this primitive %d", primitive.) + + for (cgltf_size a = 0; a < data->meshes[m].primitives[0].attributes_count; a++) { + cgltf_attribute attribute = data->meshes[m].primitives[0].attributes[a]; + if (attribute.type == cgltf_attribute_type_position) { + TRACE("Load positions from accessor"); + + cgltf_accessor *accessor = attribute.data; + assert(accessor->component_type == cgltf_component_type_r_32f); + // CASSERT_MSG(accessor->type == cgltf_type_vec3, "Vertex positions should be a vec3"); + + TRACE("Loading %d vec3 components", accessor->count); + + for (cgltf_size v = 0; v < accessor->count; ++v) { + vec3 pos; + cgltf_accessor_read_float(accessor, v, &pos.x, 3); + vec3_darray_push(tmp_positions, pos); + } - cgltf_accessor *accessor = attribute.data; - assert(accessor->component_type == cgltf_component_type_r_32f); - // CASSERT_MSG(accessor->type == cgltf_type_vec3, "Normal vectors should be a vec3"); + } else if (attribute.type == cgltf_attribute_type_normal) { + TRACE("Load normals from accessor"); - for (cgltf_size v = 0; v < accessor->count; ++v) { - vec3 pos; - cgltf_accessor_read_float(accessor, v, &pos.x, 3); - vec3_darray_push(tmp_normals, pos); - } + cgltf_accessor *accessor = attribute.data; + assert(accessor->component_type == cgltf_component_type_r_32f); + // CASSERT_MSG(accessor->type == cgltf_type_vec3, "Normal vectors should be a vec3"); - } else if (attribute.type == cgltf_attribute_type_texcoord) { - TRACE("Load texture coordinates from accessor"); - cgltf_accessor *accessor = attribute.data; - assert(accessor->component_type == cgltf_component_type_r_32f); - // CASSERT_MSG(accessor->type == cgltf_type_vec2, "Texture coordinates should be a - // vec2"); + for (cgltf_size v = 0; v < accessor->count; ++v) { + vec3 pos; + cgltf_accessor_read_float(accessor, v, &pos.x, 3); + vec3_darray_push(tmp_normals, pos); + } - for (cgltf_size v = 0; v < accessor->count; ++v) { - vec2 tex; - bool success = cgltf_accessor_read_float(accessor, v, &tex.x, 2); - if (!success) { - ERROR("Error loading tex coord"); - } - vec2_darray_push(tmp_uvs, tex); + } else if (attribute.type == cgltf_attribute_type_texcoord) { + TRACE("Load texture coordinates from accessor"); + cgltf_accessor *accessor = attribute.data; + assert(accessor->component_type == cgltf_component_type_r_32f); + // CASSERT_MSG(accessor->type == cgltf_type_vec2, "Texture coordinates should be a + // vec2"); + + for (cgltf_size v = 0; v < accessor->count; ++v) { + vec2 tex; + bool success = cgltf_accessor_read_float(accessor, v, &tex.x, 2); + if (!success) { + ERROR("Error loading tex coord"); } - } else if (attribute.type == cgltf_attribute_type_joints) { - // FIXME: joints - // TRACE("Load joint indices from accessor"); - // cgltf_accessor *accessor = attribute.data; - // assert(accessor->component_type == cgltf_component_type_r_16u); - // assert(accessor->type == cgltf_type_vec4); - // vec4u joint_indices; - // vec4 joints_as_floats; - // for (cgltf_size v = 0; v < accessor->count; ++v) { - // cgltf_accessor_read_float(accessor, v, &joints_as_floats.x, 4); - // joint_indices.x = (u32)joints_as_floats.x; - // joint_indices.y = (u32)joints_as_floats.y; - // joint_indices.z = (u32)joints_as_floats.z; - // joint_indices.w = (u32)joints_as_floats.w; - // printf("Joints affecting vertex %d : %d %d %d %d\n", v, joint_indices.x, - // joint_indices.y, - // joint_indices.z, joint_indices.w); - // vec4u_darray_push(tmp_joint_indices, joint_indices); - // } - - } else if (attribute.type == cgltf_attribute_type_weights) { - // FIXME: weights - // TRACE("Load joint weights from accessor"); - // cgltf_accessor *accessor = attribute.data; - // assert(accessor->component_type == cgltf_component_type_r_32f); - // assert(accessor->type == cgltf_type_vec4); - - // for (cgltf_size v = 0; v < accessor->count; ++v) { - // vec4 weights; - // cgltf_accessor_read_float(accessor, v, &weights.x, 4); - // printf("Weights affecting vertex %d : %f %f %f %f\n", v, weights.x, weights.y, - // weights.z, - // weights.w); - // vec4_darray_push(tmp_weights, weights); - // } - } else { - WARN("Unhandled cgltf_attribute_type: %s. skipping..", attribute.name); + vec2_darray_push(tmp_uvs, tex); } - } - // mesh.vertex_bone_data = vertex_bone_data_darray_new(1); + } else if (attribute.type == cgltf_attribute_type_joints) { + // FIXME: joints + // TRACE("Load joint indices from accessor"); + // cgltf_accessor *accessor = attribute.data; + // assert(accessor->component_type == cgltf_component_type_r_16u); + // assert(accessor->type == cgltf_type_vec4); + // vec4u joint_indices; + // vec4 joints_as_floats; + // for (cgltf_size v = 0; v < accessor->count; ++v) { + // cgltf_accessor_read_float(accessor, v, &joints_as_floats.x, 4); + // joint_indices.x = (u32)joints_as_floats.x; + // joint_indices.y = (u32)joints_as_floats.y; + // joint_indices.z = (u32)joints_as_floats.z; + // joint_indices.w = (u32)joints_as_floats.w; + // printf("Joints affecting vertex %d : %d %d %d %d\n", v, joint_indices.x, + // joint_indices.y, + // joint_indices.z, joint_indices.w); + // vec4u_darray_push(tmp_joint_indices, joint_indices); + // } - if (primitive.material != NULL) { - // for (int i = 0; i < material_darray_len(out_model->materials); i++) { - // printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name); - // if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) { - // TRACE("Found material"); - // mesh.material_index = i; - // break; - // } + } else if (attribute.type == cgltf_attribute_type_weights) { + // FIXME: weights + // TRACE("Load joint weights from accessor"); + // cgltf_accessor *accessor = attribute.data; + // assert(accessor->component_type == cgltf_component_type_r_32f); + // assert(accessor->type == cgltf_type_vec4); + + // for (cgltf_size v = 0; v < accessor->count; ++v) { + // vec4 weights; + // cgltf_accessor_read_float(accessor, v, &weights.x, 4); + // printf("Weights affecting vertex %d : %f %f %f %f\n", v, weights.x, weights.y, + // weights.z, + // weights.w); + // vec4_darray_push(tmp_weights, weights); // } + } else { + WARN("Unhandled cgltf_attribute_type: %s. skipping..", attribute.name); } + } + // mesh.vertex_bone_data = vertex_bone_data_darray_new(1); + + if (primitive.material != NULL) { + // for (int i = 0; i < material_darray_len(out_model->materials); i++) { + // printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name); + // if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) { + // TRACE("Found material"); + // mesh.material_index = i; + // break; + // } + // } + } - // // FIXME - // // if (is_skinned) { - // // mesh.is_skinned = true; - // // // mesh.vertex_bone_data = vertex_bone_data_darray_new(tmp_joint_indices->len); - // // mesh.bones = joint_darray_new(tmp_joints->len); - // // for (int i = 0; i < tmp_joint_indices->len; i++) { - // // vertex_bone_data data; - // // data.joints = tmp_joint_indices->data[i]; - // // data.weights = tmp_weights->data[i]; - // // vertex_bone_data_darray_push(tmp_vertex_bone_data, - // // data); // Push the temp data that aligns with raw - // vertices - // // } - // // for (int i = 0; i < tmp_joints->len; i++) { - // // joint data = tmp_joints->data[i]; - // // joint_darray_push(mesh.bones, data); - // // } - // // } + // // FIXME + // // if (is_skinned) { + // // mesh.is_skinned = true; + // // // mesh.vertex_bone_data = vertex_bone_data_darray_new(tmp_joint_indices->len); + // // mesh.bones = joint_darray_new(tmp_joints->len); + // // for (int i = 0; i < tmp_joint_indices->len; i++) { + // // vertex_bone_data data; + // // data.joints = tmp_joint_indices->data[i]; + // // data.weights = tmp_weights->data[i]; + // // vertex_bone_data_darray_push(tmp_vertex_bone_data, + // // data); // Push the temp data that aligns with raw + // vertices + // // } + // // for (int i = 0; i < tmp_joints->len; i++) { + // // joint data = tmp_joints->data[i]; + // // joint_darray_push(mesh.bones, data); + // // } + // // } /* typedef struct mesh { @@ -331,69 +331,67 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel bool is_latent; } mesh; */ - bool has_indices = false; - vertex_darray* geo_vertices = vertex_darray_new(3); - u32_darray* geo_indices = u32_darray_new(0); - - // Store vertices - printf("Positions %d Normals %d UVs %d\n", tmp_positions->len, tmp_normals->len, tmp_uvs->len); - assert(tmp_positions->len == tmp_normals->len); - assert(tmp_normals->len == tmp_uvs->len); - for (u32 v_i = 0; v_i < tmp_positions->len; v_i++) { - vertex v = { - .static_3d = { - .position = tmp_positions->data[v_i], - .normal = tmp_normals->data[v_i], - .tex_coords = tmp_uvs->data[v_i], - } - }; - vertex_darray_push(geo_vertices, v); - } - - // Store indices - cgltf_accessor* indices = primitive.indices; - if (primitive.indices > 0) { - WARN("indices! %d", indices->count); - has_indices = true; - - // store indices - for (cgltf_size i = 0; i < indices->count; ++i) { - cgltf_uint ei; - cgltf_accessor_read_uint(indices, i, &ei, 1); - u32_darray_push(geo_indices, ei); - } + bool has_indices = false; + vertex_darray *geo_vertices = vertex_darray_new(3); + u32_darray *geo_indices = u32_darray_new(0); + + // Store vertices + printf("Positions %d Normals %d UVs %d\n", tmp_positions->len, tmp_normals->len, tmp_uvs->len); + assert(tmp_positions->len == tmp_normals->len); + assert(tmp_normals->len == tmp_uvs->len); + for (u32 v_i = 0; v_i < tmp_positions->len; v_i++) { + vertex v = { .static_3d = { + .position = tmp_positions->data[v_i], + .normal = tmp_normals->data[v_i], + .tex_coords = tmp_uvs->data[v_i], + } }; + vertex_darray_push(geo_vertices, v); + } - // fetch and store vertices for each index - // for (cgltf_size i = 0; i < indices->count; ++i) { - // vertex vert; - // cgltf_uint index = mesh.indices[i]; - // vert.position = tmp_positions->data[index]; - // vert.normal = tmp_normals->data[index]; - // vert.uv = tmp_uvs->data[index]; - // vertex_darray_push(mesh.vertices, vert); - - // if (is_skinned) { - // vertex_bone_data vbd = tmp_vertex_bone_data->data[index]; // create a copy - // vertex_bone_data_darray_push(mesh.vertex_bone_data, vbd); - // } - // // for each vertex do the bone data - // } - } else { - has_indices = false; - return false; // TODO: handle this + // Store indices + cgltf_accessor *indices = primitive.indices; + if (primitive.indices > 0) { + WARN("indices! %d", indices->count); + has_indices = true; + + // store indices + for (cgltf_size i = 0; i < indices->count; ++i) { + cgltf_uint ei; + cgltf_accessor_read_uint(indices, i, &ei, 1); + u32_darray_push(geo_indices, ei); } - geometry_data* geometry = malloc(sizeof(geometry_data)); - geometry->format = VERTEX_STATIC_3D; - geometry->colour = vec3(1,1,1); - geometry->vertices = geo_vertices; - geometry->indices = geo_indices; - geometry->has_indices = has_indices; - mesh m = mesh_create(geometry, true); - - mesh_darray_push(out_model->meshes, m); + // fetch and store vertices for each index + // for (cgltf_size i = 0; i < indices->count; ++i) { + // vertex vert; + // cgltf_uint index = mesh.indices[i]; + // vert.position = tmp_positions->data[index]; + // vert.normal = tmp_normals->data[index]; + // vert.uv = tmp_uvs->data[index]; + // vertex_darray_push(mesh.vertices, vert); + + // if (is_skinned) { + // vertex_bone_data vbd = tmp_vertex_bone_data->data[index]; // create a copy + // vertex_bone_data_darray_push(mesh.vertex_bone_data, vbd); + // } + // // for each vertex do the bone data + // } + } else { + has_indices = false; + return false; // TODO: handle this } + geometry_data *geometry = malloc(sizeof(geometry_data)); + geometry->format = VERTEX_STATIC_3D; + geometry->colour = vec3(1, 1, 1); + geometry->vertices = geo_vertices; + geometry->indices = geo_indices; + geometry->has_indices = has_indices; + mesh m = mesh_create(geometry, true); + + mesh_darray_push(out_model->meshes, m); + } + // // clear data for each mesh // vec3_darray_clear(tmp_positions); // vec3_darray_clear(tmp_normals); @@ -528,7 +526,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel // } // } - return true; + return true; } /* diff --git a/src/resources/obj.c b/src/resources/obj.c index f95398a..411ad82 100644 --- a/src/resources/obj.c +++ b/src/resources/obj.c @@ -22,6 +22,8 @@ #include "render_types.h" #include "str.h" +extern core g_core; + struct face { u32 vertex_indices[3]; u32 normal_indices[3]; @@ -52,10 +54,10 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y } const char *file_string = string_from_file(path); - model model = { 0 }; - model.name = str8_cstr_view(path); - /* model.meshes = mallocmesh_darray_new(1); */ - // model.materials = material_darray_new(1); + model_handle handle; + model *model = model_pool_alloc(&g_core.models, &handle); + model->name = str8_cstr_view(path); + model->meshes = mesh_darray_new(1); bool success = model_load_obj_str(file_string, relative_path.path, &model, invert_textures_y); @@ -64,13 +66,9 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y ERROR_EXIT("Load fails are considered crash-worthy right now. This will change later.\n"); } -// FIXME - // u32 index = model_darray_len(core->models); - // model_darray_push(core->models, model); - arena_free_all(&scratch); arena_free_storage(&scratch); - return (model_handle){ .raw = index }; + return handle; } bool model_load_obj_str(const char *file_string, str8 relative_path, model *out_model, diff --git a/src/scene.h b/src/scene.h index 5ac7542..f60bfff 100644 --- a/src/scene.h +++ b/src/scene.h @@ -26,6 +26,7 @@ typedef struct scene { // TODO: tree - transform_hierarchy } scene; + void scene_init(scene* s); void scene_free(scene* s); diff --git a/src/std/mem.c b/src/std/mem.c index ede1db4..5cc4304 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -104,7 +104,7 @@ void* void_pool_alloc(void_pool* pool, u32* out_raw_handle) { TRACE("%ld %ld ", start, cur); assert(cur > start); u32 index = (u32)((cur - start) / pool->entry_size); - printf("Index %d\n", index); + /* printf("Index %d\n", index); */ if (out_raw_handle != NULL) { *out_raw_handle = index; } |