diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-08-13 13:18:24 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-08-13 13:18:24 +1000 |
commit | 0c2f902238010903388b7d9a3af00979340eff2c (patch) | |
tree | b718d72d8761769ec4aefbfd74cbb2d695f89741 | |
parent | 43d915b2ceb3f5e439c09c772e9ebc7542c1a2bb (diff) |
add debug draw bbox and add topology to draw calls
-rw-r--r-- | examples/game_demo/game_demo.c | 12 | ||||
-rw-r--r-- | src/maths/primitives.c | 14 | ||||
-rw-r--r-- | src/maths/primitives.h | 15 | ||||
-rw-r--r-- | src/ral/backends/opengl/backend_opengl.c | 14 | ||||
-rw-r--r-- | src/ral/backends/opengl/opengl_helpers.h | 13 | ||||
-rw-r--r-- | src/ral/ral_impl.h | 7 | ||||
-rw-r--r-- | src/ral/ral_types.h | 11 | ||||
-rw-r--r-- | src/render/immdraw.c | 58 | ||||
-rw-r--r-- | src/render/immdraw.h | 6 | ||||
-rw-r--r-- | src/render/pbr.c | 2 | ||||
-rw-r--r-- | src/render/shadows.c | 4 | ||||
-rw-r--r-- | src/render/skybox.c | 2 | ||||
-rw-r--r-- | src/systems/grid.c | 2 | ||||
-rw-r--r-- | src/systems/terrain.c | 2 |
14 files changed, 122 insertions, 40 deletions
diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c index b5db6cd..65b31ba 100644 --- a/examples/game_demo/game_demo.c +++ b/examples/game_demo/game_demo.c @@ -107,14 +107,16 @@ int main() { // Shadow_Run(entities, entity_count); // Quat rot = quat_from_axis_angle(VEC3_X, HALF_PI, true); - Immdraw_Sphere(transform_create(VEC3_ZERO, quat_ident(), vec3(1.0, 3.0, 1.0)), - vec4(1.0, 0.0, 0.0, 1.0), true); - Immdraw_Cuboid(transform_create(vec3(4.0, 0.0, 0.0), quat_ident(), vec3(3.0, 0.5, 3.0)), - vec4(1.0, 0.0, 0.0, 1.0), true); + // Immdraw_Sphere(transform_create(VEC3_ZERO, quat_ident(), vec3(1.0, 3.0, 1.0)), + // vec4(1.0, 0.0, 0.0, 1.0), true); + // Immdraw_Cuboid(transform_create(vec3(4.0, 0.0, 0.0), quat_ident(), vec3(3.0, 0.5, 3.0)), + // vec4(1.0, 0.0, 0.0, 1.0), true); + Immdraw_Bbox(transform_create(vec3(0.0, 0.0, 0.0), quat_ident(), vec3(2.0, 2.0, 2.0)), + vec4(0.0, 1.0, 0.0, 1.0), true); if (draw_debug) { // draw the player model with shadows - Render_RenderEntities(render_entities->data, render_entities->len); + // Render_RenderEntities(render_entities->data, render_entities->len); // Render_DrawTerrain(); // Skybox_Draw(&skybox, cam); } else { diff --git a/src/maths/primitives.c b/src/maths/primitives.c index bb8a348..1e1e673 100644 --- a/src/maths/primitives.c +++ b/src/maths/primitives.c @@ -6,11 +6,6 @@ #include "render_types.h" // --- Helpers -#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); @@ -58,15 +53,6 @@ Geometry Geo_CreatePlane(f32x2 extents) { return geo; } -static const Vec3 BACK_BOT_LEFT = (Vec3){ 0, 0, 0 }; -static const Vec3 BACK_BOT_RIGHT = (Vec3){ 1, 0, 0 }; -static const Vec3 BACK_TOP_LEFT = (Vec3){ 0, 1, 0 }; -static const Vec3 BACK_TOP_RIGHT = (Vec3){ 1, 1, 0 }; -static const Vec3 FRONT_BOT_LEFT = (Vec3){ 0, 0, 1 }; -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 }; - Geometry Geo_CreateCuboid(f32x3 extents) { Vertex_darray* vertices = Vertex_darray_new(36); diff --git a/src/maths/primitives.h b/src/maths/primitives.h index 38ae1de..f7b390d 100644 --- a/src/maths/primitives.h +++ b/src/maths/primitives.h @@ -11,3 +11,18 @@ Geometry Geo_CreateCuboid(f32x3 extents); Geometry Geo_CreateCylinder(f32 radius, f32 height, u32 resolution); Geometry Geo_CreateUVsphere(f32 radius, u32 north_south_lines, u32 east_west_lines); Geometry Geo_CreateIcosphere(f32 radius, f32 n_subdivisions); + +static const Vec3 BACK_BOT_LEFT = (Vec3){ 0, 0, 0 }; +static const Vec3 BACK_BOT_RIGHT = (Vec3){ 1, 0, 0 }; +static const Vec3 BACK_TOP_LEFT = (Vec3){ 0, 1, 0 }; +static const Vec3 BACK_TOP_RIGHT = (Vec3){ 1, 1, 0 }; +static const Vec3 FRONT_BOT_LEFT = (Vec3){ 0, 0, 1 }; +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); \ + }
\ No newline at end of file diff --git a/src/ral/backends/opengl/backend_opengl.c b/src/ral/backends/opengl/backend_opengl.c index 3822220..613d7e1 100644 --- a/src/ral/backends/opengl/backend_opengl.c +++ b/src/ral/backends/opengl/backend_opengl.c @@ -411,11 +411,21 @@ void GPU_EncodeSetIndexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf) { GPU_Buffer* buffer = BUFFER_GET(buf); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->id.ibo); } -void GPU_EncodeDraw(GPU_CmdEncoder* encoder, u64 count) { glDrawArrays(GL_TRIANGLES, 0, count); } -void GPU_EncodeDrawIndexed(GPU_CmdEncoder* encoder, u64 index_count) { +void GPU_EncodeDrawTris(GPU_CmdEncoder* encoder, u64 count) { + glDrawArrays(GL_TRIANGLES, 0, count); +} +void GPU_EncodeDrawIndexedTris(GPU_CmdEncoder* encoder, u64 index_count) { glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, 0); } +PUB void GPU_EncodeDraw(GPU_CmdEncoder* encoder, PrimitiveTopology topology, u64 count) { + glDrawArrays(opengl_prim_topology(topology), 0, count); +} +PUB void GPU_EncodeDrawIndexed(GPU_CmdEncoder* encoder, PrimitiveTopology topology, + u64 index_count) { + glDrawElements(opengl_prim_topology(topology), index_count, GL_UNSIGNED_INT, 0); +} + PUB void GPU_WriteTextureRegion(GPU_CmdEncoder* encoder, TextureHandle dst, u32 x_offset, u32 y_offset, u32 width, u32 height, const void* data) { CASSERT_MSG(data, "const void* data must not be NULL"); diff --git a/src/ral/backends/opengl/opengl_helpers.h b/src/ral/backends/opengl/opengl_helpers.h index 8a78de5..b73eaea 100644 --- a/src/ral/backends/opengl/opengl_helpers.h +++ b/src/ral/backends/opengl/opengl_helpers.h @@ -138,4 +138,17 @@ static GLenum opengl_tex_type(GPU_TextureType tex_type) { } } +static GLenum opengl_prim_topology(PrimitiveTopology t) { + switch (t) { + case CEL_POINT: return GL_POINT; + case CEL_LINE: return GL_LINES; + case CEL_LINE_STRIP: return GL_LINE_STRIP; + case CEL_TRI: return GL_TRIANGLES; + case CEL_TRI_STRIP: return GL_TRIANGLE_STRIP; + case PRIMITIVE_TOPOLOGY_COUNT: + WARN("Invalid PrimitiveTopology value"); + break; + } +} + #endif diff --git a/src/ral/ral_impl.h b/src/ral/ral_impl.h index 2ca75d6..3374d91 100644 --- a/src/ral/ral_impl.h +++ b/src/ral/ral_impl.h @@ -80,8 +80,11 @@ void GPU_EncodeSetDefaults(GPU_CmdEncoder* encoder); PUB void GPU_EncodeSetVertexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf); PUB void GPU_EncodeSetIndexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf); -PUB void GPU_EncodeDraw(GPU_CmdEncoder* encoder, u64 count); -PUB void GPU_EncodeDrawIndexed(GPU_CmdEncoder* encoder, u64 index_count); +PUB void GPU_EncodeDraw(GPU_CmdEncoder* encoder, PrimitiveTopology topology, u64 count); +PUB void GPU_EncodeDrawIndexed(GPU_CmdEncoder* encoder, PrimitiveTopology topology, u64 index_count); +// convenience versions of the above +PUB void GPU_EncodeDrawTris(GPU_CmdEncoder* encoder, u64 count); +PUB void GPU_EncodeDrawIndexedTris(GPU_CmdEncoder* encoder, u64 index_count); PUB void GPU_EncodeDrawInstanced(GPU_CmdEncoder* encoder, u64 index_count, u64 instance_count); // TODO: implement instanced rendering diff --git a/src/ral/ral_types.h b/src/ral/ral_types.h index e15e0f4..7c83af6 100644 --- a/src/ral/ral_types.h +++ b/src/ral/ral_types.h @@ -221,13 +221,24 @@ typedef void (*FN_BindShaderData)(ShaderDataLayout* layout, const void* data); // --- Miscellaneous +#define TOPOLOGY_SHORT_NAMES + typedef enum PrimitiveTopology { +#ifdef TOPOLOGY_SHORT_NAMES + CEL_POINT, + CEL_LINE, + CEL_LINE_STRIP, + CEL_TRI, + CEL_TRI_STRIP, + PRIMITIVE_TOPOLOGY_COUNT +#else PRIMITIVE_TOPOLOGY_POINT, PRIMITIVE_TOPOLOGY_LINE, PRIMITIVE_TOPOLOGY_LINE_STRIP, PRIMITIVE_TOPOLOGY_TRIANGLE, PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, PRIMITIVE_TOPOLOGY_COUNT +#endif } PrimitiveTopology; typedef enum CullMode { CULL_BACK_FACE, CULL_FRONT_FACE, CULL_COUNT } CullMode; diff --git a/src/render/immdraw.c b/src/render/immdraw.c index 142087a..a097f90 100644 --- a/src/render/immdraw.c +++ b/src/render/immdraw.c @@ -3,28 +3,29 @@ #include "file.h" #include "log.h" #include "maths.h" +#include "maths_types.h" #include "primitives.h" #include "ral_common.h" #include "ral_impl.h" #include "ral_types.h" #include "render.h" +#include "render_types.h" #include "shader_layouts.h" -// Forward declares -void Immdraw_Primitive(Transform tf, f32 size, Vec4 colour, bool wireframe, Mesh mesh); - void Immdraw_Init(Immdraw_Storage* storage) { INFO("Immediate drawing initialisation"); // Meshes Geometry sphere_geo = Geo_CreateUVsphere(1.0, 16, 16); - storage->sphere = Mesh_Create(&sphere_geo, false); + storage->sphere = Mesh_Create(&sphere_geo, true); Geometry cube_geo = Geo_CreateCuboid(f32x3(1.0, 1.0, 1.0)); - storage->cube = Mesh_Create(&cube_geo, false); + storage->cube = Mesh_Create(&cube_geo, true); Geometry plane_geo = Geo_CreatePlane(f32x2(1.0, 1.0)); - storage->plane = Mesh_Create(&plane_geo, false); + storage->plane = Mesh_Create(&plane_geo, true); + + storage->bbox = GenBboxMesh(); // Pipeline / material VertexDescription vertex_desc = { @@ -64,20 +65,27 @@ void Immdraw_Shutdown(Immdraw_Storage* storage) { void Immdraw_Sphere(Transform tf, Vec4 colour, bool wireframe) { TRACE("Draw sphere"); Immdraw_Storage* imm = Render_GetImmdrawStorage(); - Immdraw_Primitive(tf, 1.0, colour, wireframe, imm->sphere); + Immdraw_Primitive(tf, CEL_TRI, 1.0, colour, wireframe, imm->sphere); } void Immdraw_Cuboid(Transform tf, Vec4 colour, bool wireframe) { TRACE("Draw cube"); Immdraw_Storage* imm = Render_GetImmdrawStorage(); - Immdraw_Primitive(tf, 1.0, colour, wireframe, imm->cube); + Immdraw_Primitive(tf, CEL_TRI, 1.0, colour, wireframe, imm->cube); } void Immdraw_Plane(Transform tf, Vec4 colour, bool wireframe) { TRACE("Draw plane"); Immdraw_Storage* imm = Render_GetImmdrawStorage(); - Immdraw_Primitive(tf, 1.0, colour, wireframe, imm->plane); + Immdraw_Primitive(tf, CEL_TRI, 1.0, colour, wireframe, imm->plane); +} + +PUB void Immdraw_Bbox(Transform tf, Vec4 colour, bool wireframe) { + TRACE("Draw bbox"); + Immdraw_Storage* imm = Render_GetImmdrawStorage(); + Immdraw_Primitive(tf, CEL_LINE, 1.0, colour, wireframe, imm->bbox); } -void Immdraw_Primitive(Transform tf, f32 size, Vec4 colour, bool wireframe, Mesh mesh) { +void Immdraw_Primitive(Transform tf, PrimitiveTopology topology, f32 size, Vec4 colour, + bool wireframe, Mesh mesh) { Immdraw_Storage* imm = Render_GetImmdrawStorage(); GPU_CmdEncoder* enc = GPU_GetDefaultEncoder(); @@ -115,8 +123,36 @@ void Immdraw_Primitive(Transform tf, f32 size, Vec4 colour, bool wireframe, Mesh // draw call GPU_EncodeSetVertexBuffer(enc, mesh.vertex_buffer); GPU_EncodeSetIndexBuffer(enc, mesh.index_buffer); - GPU_EncodeDrawIndexed(enc, mesh.geometry.index_count); + GPU_EncodeDrawIndexed(enc, topology, mesh.geometry.index_count); // end renderpass GPU_CmdEncoder_EndRender(enc); } + +Mesh GenBboxMesh() { + Vertex_darray* vertices = Vertex_darray_new(8); + u32_darray* indices = u32_darray_new(24); + + // normals & uvs dont matter + VERT_3D(vertices, FRONT_BOT_LEFT, VEC3_NEG_Z, vec2(0, 0)); + VERT_3D(vertices, FRONT_BOT_RIGHT, VEC3_NEG_Z, vec2(0, 0)); + VERT_3D(vertices, BACK_BOT_LEFT, VEC3_NEG_Z, vec2(0, 0)); + VERT_3D(vertices, BACK_BOT_RIGHT, VEC3_NEG_Z, vec2(0, 0)); + VERT_3D(vertices, FRONT_TOP_LEFT, VEC3_NEG_Z, vec2(0, 0)); + VERT_3D(vertices, FRONT_TOP_RIGHT, VEC3_NEG_Z, vec2(0, 0)); + VERT_3D(vertices, BACK_TOP_LEFT, VEC3_NEG_Z, vec2(0, 0)); + VERT_3D(vertices, BACK_TOP_RIGHT, VEC3_NEG_Z, vec2(0, 0)); + + u32 line_indices[24] = { 0, 1, 2, 3, 0, 2, 1, 3, 4, 5, 6, 7, 4, 6, 5, 7, 0, 4, 1, 5, 2, 6, 3, 7 }; + for (u32 i = 0; i < 24; i++) { + u32_darray_push(indices, line_indices[i]); + } + + Geometry geo = { .format = VERTEX_STATIC_3D, + .has_indices = true, + .index_count = indices->len, + .vertices = vertices, + .indices = indices }; + + return Mesh_Create(&geo, true); +}
\ No newline at end of file diff --git a/src/render/immdraw.h b/src/render/immdraw.h index 6b456d5..aa1847b 100644 --- a/src/render/immdraw.h +++ b/src/render/immdraw.h @@ -13,6 +13,7 @@ typedef struct Immdraw_Storage { Mesh plane; Mesh cube; Mesh sphere; + Mesh bbox; GPU_Pipeline* colour_pipeline; /** @brief Pipeline for drawing geometry that has vertex colours */ } Immdraw_Storage; @@ -30,11 +31,16 @@ PUB void Immdraw_Shutdown(Immdraw_Storage* storage); PUB void Immdraw_Plane(Transform tf, Vec4 colour, bool wireframe); PUB void Immdraw_Cuboid(Transform tf, Vec4 colour, bool wireframe); PUB void Immdraw_Sphere(Transform tf, Vec4 colour, bool wireframe); +PUB void Immdraw_Bbox(Transform tf, Vec4 colour, bool wireframe); PUB void Immdraw_TransformGizmo(Transform tf, f32 size); // --- Internal +void Immdraw_Primitive(Transform tf, PrimitiveTopology topology, f32 size, Vec4 colour, bool wireframe, Mesh mesh); + +Mesh GenBboxMesh(); + static ShaderDataLayout ImmediateUniforms_GetLayout(void* data) { ImmediateUniforms* d = (ImmediateUniforms*)data; bool has_data = data != NULL; diff --git a/src/render/pbr.c b/src/render/pbr.c index 1a7b4de..fbdacac 100644 --- a/src/render/pbr.c +++ b/src/render/pbr.c @@ -120,7 +120,7 @@ void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_te GPU_EncodeSetVertexBuffer(enc, mesh->vertex_buffer); GPU_EncodeSetIndexBuffer(enc, mesh->index_buffer); // draw - GPU_EncodeDrawIndexed(enc, mesh->geometry.index_count); + GPU_EncodeDrawIndexedTris(enc, mesh->geometry.index_count); } GPU_CmdEncoder_EndRender(enc); diff --git a/src/render/shadows.c b/src/render/shadows.c index 18a5f7a..499ec02 100644 --- a/src/render/shadows.c +++ b/src/render/shadows.c @@ -167,7 +167,7 @@ void Shadow_DrawDebugQuad() { GPU_EncodeBindShaderData(enc, 0, quad_data); GPU_EncodeSetVertexBuffer(enc, shadow_storage->quad.vertex_buffer); GPU_EncodeSetIndexBuffer(enc, shadow_storage->quad.index_buffer); - GPU_EncodeDrawIndexed(enc, shadow_storage->quad.geometry.indices->len); + GPU_EncodeDrawIndexedTris(enc, shadow_storage->quad.geometry.indices->len); GPU_CmdEncoder_EndRender(enc); } @@ -201,7 +201,7 @@ void Shadow_ShadowmapExecute(Shadow_Storage* storage, Mat4 light_space_transform GPU_EncodeBindShaderData(&shadow_encoder, 0, shader_data); GPU_EncodeSetVertexBuffer(&shadow_encoder, mesh->vertex_buffer); GPU_EncodeSetIndexBuffer(&shadow_encoder, mesh->index_buffer); - GPU_EncodeDrawIndexed(&shadow_encoder, mesh->geometry.indices->len); + GPU_EncodeDrawIndexedTris(&shadow_encoder, mesh->geometry.indices->len); } } diff --git a/src/render/skybox.c b/src/render/skybox.c index b228c82..b4e1e42 100644 --- a/src/render/skybox.c +++ b/src/render/skybox.c @@ -154,7 +154,7 @@ void Skybox_Draw(Skybox* skybox, Camera camera) { GPU_EncodeSetVertexBuffer(enc, skybox->cube.vertex_buffer); GPU_EncodeSetIndexBuffer(enc, skybox->cube.index_buffer); - GPU_EncodeDraw(enc, 36); + GPU_EncodeDrawTris(enc, 36); GPU_CmdEncoder_EndRender(enc); glDepthFunc(GL_LESS); diff --git a/src/systems/grid.c b/src/systems/grid.c index 8a6f4f6..6e62a21 100644 --- a/src/systems/grid.c +++ b/src/systems/grid.c @@ -79,6 +79,6 @@ void Grid_Execute(Grid_Storage* storage) { GPU_EncodeBindShaderData(enc, 0, Binding_Camera_GetLayout(&camera_data)); GPU_EncodeSetVertexBuffer(enc, storage->plane_vertices); GPU_EncodeSetIndexBuffer(enc, storage->plane_indices); - GPU_EncodeDrawIndexed(enc, 6); + GPU_EncodeDrawIndexedTris(enc, 6); GPU_CmdEncoder_EndRender(enc); } diff --git a/src/systems/terrain.c b/src/systems/terrain.c index ead7700..069000e 100644 --- a/src/systems/terrain.c +++ b/src/systems/terrain.c @@ -174,7 +174,7 @@ void Terrain_Draw(Terrain_Storage* storage) { GPU_EncodeSetVertexBuffer(enc, storage->vertex_buffer); GPU_EncodeSetIndexBuffer(enc, storage->index_buffer); - GPU_EncodeDrawIndexed(enc, storage->indices_count); + GPU_EncodeDrawIndexedTris(enc, storage->indices_count); // glDrawArrays(GL_POINTS, 0, storage->num_vertices); } |