diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-17 22:08:13 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-17 22:08:13 +1000 |
commit | f1cb5bece520dba63fa77e4721909ab8f0a6c26f (patch) | |
tree | 33e0888475fcb10a4857db6d5570fa7cce459cb7 | |
parent | 9c8ef3cb95cff78bdc9d527b89ba30cf616e18c0 (diff) |
add plane primitive
-rw-r--r-- | assets/shaders/cube.vert | 2 | ||||
-rw-r--r-- | examples/cube/ex_cube.c | 7 | ||||
-rw-r--r-- | examples/primitives/ex_primitives.c | 5 | ||||
-rw-r--r-- | src/maths/primitives.c | 61 | ||||
-rw-r--r-- | src/renderer/render.c | 2 | ||||
-rw-r--r-- | src/renderer/static_pipeline.h | 2 |
6 files changed, 53 insertions, 26 deletions
diff --git a/assets/shaders/cube.vert b/assets/shaders/cube.vert index 7af7413..cd345f6 100644 --- a/assets/shaders/cube.vert +++ b/assets/shaders/cube.vert @@ -1,4 +1,4 @@ -#version 410 +#version 410 core uniform Matrices { mat4 model; diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c index 2cd0a51..3c98087 100644 --- a/examples/cube/ex_cube.c +++ b/examples/cube/ex_cube.c @@ -41,7 +41,7 @@ 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", + shader_binding b1 = { .label = "Matrices", .type = SHADER_BINDING_BYTES, .stores_data = has_data, .data = { .bytes = { .size = sizeof(mvp_uniforms) } } }; @@ -154,7 +154,10 @@ int main() { encode_bind_shader_data(enc, 0, &mvp_uniforms_data); // Record draw calls - draw_mesh(&cube, &model, &cam); + // draw_mesh(&cube, &model, &cam); + encode_set_vertex_buffer(enc, cube.vertex_buffer); + encode_set_index_buffer(enc, cube.index_buffer); + encode_draw_indexed(enc, cube.geometry->indices->len); // End recording gpu_cmd_encoder_end_render(enc); diff --git a/examples/primitives/ex_primitives.c b/examples/primitives/ex_primitives.c index 2109215..d39c0d1 100644 --- a/examples/primitives/ex_primitives.c +++ b/examples/primitives/ex_primitives.c @@ -30,6 +30,9 @@ int main() { geometry_data sphere_data = geo_create_uvsphere(1.0, 8, 8); mesh sphere = mesh_create(&sphere_data, false); + geometry_data plane_data = geo_create_plane(f32x2(6,4)); + mesh plane = mesh_create(&plane_data, false); + // FIXME: // Texture // texture_data tex_data = texture_data_load("assets/textures/texture.jpg", false); // texture_handle texture = texture_data_upload(tex_data, true); @@ -51,8 +54,10 @@ int main() { mat4 sphere_model = transform_to_mat(&transform); mat4 cube_model = mat4_translation(vec3(-2., 0, 0)); + mat4 plane_model = mat4_translation(vec3(0,-2,0)); draw_mesh(&cube, &cube_model, &cam); draw_mesh(&sphere, &sphere_model, &cam); + draw_mesh(&plane, &plane_model, &cam); render_frame_end(&g_core.renderer); } diff --git a/src/maths/primitives.c b/src/maths/primitives.c index 46175fd..2fbe71a 100644 --- a/src/maths/primitives.c +++ b/src/maths/primitives.c @@ -5,6 +5,18 @@ #include "ral_types.h" #include "render_types.h" +#define VERT_3D(arr, pos, norm, uv) \ + { \ + vertex v = { .static_3d = { .position = pos, .normal = norm, .tex_coords = uv } }; \ + vertex_darray_push(arr, v); \ + } + +void push_triangle(u32_darray* arr, u32 i0, u32 i1, u32 i2) { + u32_darray_push(arr, i0); + u32_darray_push(arr, i1); + u32_darray_push(arr, i2); +} + // TODO: move to another file void geo_free_data(geometry_data* geo) { vertex_darray_free(geo->vertices); @@ -16,21 +28,40 @@ void geo_free_data(geometry_data* geo) { } // vertices -f32 plane_vertex_positions[] = { - // triangle 1 - -0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, - // triangle 2 - 0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5 +vec3 plane_vertex_positions[] = { + (vec3){ -0.5, 0, -0.5}, + (vec3){ 0.5, 0, -0.5}, + (vec3){ -0.5, 0, 0.5}, + (vec3){ 0.5, 0, 0.5}, }; geometry_data geo_create_plane(f32x2 extents) { - f32x2 half_extents = vec2_div(extents, 2.0); - vertex_format format = VERTEX_STATIC_3D; vertex_darray* vertices = vertex_darray_new(4); + u32_darray* indices = u32_darray_new(vertices->len); - // vertex_darray_push(vertices, (vertex){ .static_3d = { .position = } }); + vec3 vert_pos[4]; + memcpy(&vert_pos, plane_vertex_positions, sizeof(plane_vertex_positions)); + for (int i = 0; i < 4; i++) { + vert_pos[i].x *= extents.x; + vert_pos[i].z *= extents.y; + } + VERT_3D(vertices, vert_pos[0], VEC3_Y, vec2(0, 0)); + VERT_3D(vertices, vert_pos[1], VEC3_Y, vec2(1, 0)); + VERT_3D(vertices, vert_pos[2], VEC3_Y, vec2(0, 1)); + VERT_3D(vertices, vert_pos[3], VEC3_Y, vec2(1, 1)); + + push_triangle(indices, 2,1,0); + push_triangle(indices, 1,2,3); - // return (geometry_data) { .format = format, .vertices =.has_indices = true, } + geometry_data geo = { + .format = VERTEX_STATIC_3D, + .vertices = vertices, + .has_indices = true, + .indices = indices, + .colour = (rgba){ 0, 0, 0, 1 } + }; + + return geo; } // OLD @@ -44,18 +75,6 @@ static const vec3 FRONT_BOT_RIGHT = (vec3){ 1, 0, 1 }; static const vec3 FRONT_TOP_LEFT = (vec3){ 0, 1, 1 }; static const vec3 FRONT_TOP_RIGHT = (vec3){ 1, 1, 1 }; -#define VERT_3D(arr, pos, norm, uv) \ - { \ - vertex v = { .static_3d = { .position = pos, .normal = norm, .tex_coords = uv } }; \ - vertex_darray_push(arr, v); \ - } - -void push_triangle(u32_darray* arr, u32 i0, u32 i1, u32 i2) { - u32_darray_push(arr, i0); - u32_darray_push(arr, i1); - u32_darray_push(arr, i2); -} - geometry_data geo_create_cuboid(f32x3 extents) { /* static mesh prim_cube_mesh_create() { */ vertex_darray* vertices = vertex_darray_new(36); diff --git a/src/renderer/render.c b/src/renderer/render.c index 1c98b0b..f52e2be 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -62,7 +62,7 @@ bool renderer_init(renderer* ren) { resource_pools_init(&pool_arena, ren->resource_pools); // Create default rendering pipeline - /* default_pipelines_init(ren); */ + default_pipelines_init(ren); return true; } diff --git a/src/renderer/static_pipeline.h b/src/renderer/static_pipeline.h index 15e8842..bf5bc42 100644 --- a/src/renderer/static_pipeline.h +++ b/src/renderer/static_pipeline.h @@ -18,7 +18,7 @@ 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", + shader_binding b1 = { .label = "Matrices", .type = SHADER_BINDING_BYTES, .stores_data = has_data, .data = { .bytes = { .size = sizeof(mvp_uniforms) } } }; |