summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-08-11 23:12:59 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-08-11 23:12:59 +1000
commit3a151b1d1f99b59dfd6d98c1e7520b48b57d6f5e (patch)
treeaecbe2462529eae01dc16b54d529548e8ae870e9
parentb03bde3d412148cd573f5f14012cdd270f309947 (diff)
plane,cube,sphere
-rw-r--r--examples/game_demo/game_demo.c6
-rw-r--r--src/maths/primitives.c4
-rw-r--r--src/render/immdraw.c54
-rw-r--r--src/render/immdraw.h2
-rw-r--r--src/render/render.c4
5 files changed, 42 insertions, 28 deletions
diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c
index 22af64e..1b86028 100644
--- a/examples/game_demo/game_demo.c
+++ b/examples/game_demo/game_demo.c
@@ -106,8 +106,10 @@ int main() {
ModelExtractRenderEnts(render_entities, cube_handle, affine, REND_ENT_CASTS_SHADOWS);
// Shadow_Run(entities, entity_count);
- Quat rot = quat_from_axis_angle(VEC3_X, HALF_PI, true);
- Immdraw_Sphere(transform_create(VEC3_ZERO, rot, 1.0), 1.0, vec4(1.0, 0.0, 0.0, 1.0), false);
+ // Quat rot = quat_from_axis_angle(VEC3_X, HALF_PI, true);
+ Immdraw_Sphere(transform_create(VEC3_ZERO, quat_ident(), 0.5), vec4(1.0, 0.0, 0.0, 1.0), false);
+ Immdraw_Cuboid(transform_create(vec3(2.0, 0.0, 0.0), quat_ident(), 1.0),
+ vec4(1.0, 0.0, 0.0, 1.0), false);
// if (draw_debug) {
// // draw the player model with shadows
diff --git a/src/maths/primitives.c b/src/maths/primitives.c
index d353d33..bb8a348 100644
--- a/src/maths/primitives.c
+++ b/src/maths/primitives.c
@@ -199,7 +199,7 @@ Geometry Geo_CreateUVsphere(f32 radius, u32 north_south_lines, u32 east_west_lin
for (u32 i = 0; i < north_south_lines; i++) {
u32 i1 = i + 1;
u32 i2 = (i + 1) % north_south_lines + 1;
- push_triangle(indices, 0, i1, i2);
+ push_triangle(indices, 0, i2, i1);
/* TRACE("Push triangle (%.2f %.2f %.2f)->(%.2f %.2f %.2f)->(%.2f %.2f %.2f)\n", */
/* vertices->data[0].static_3d.position.x, vertices->data[0].static_3d.position.y, */
/* vertices->data[0].static_3d.position.z, vertices->data[i1].static_3d.position.x, */
@@ -209,7 +209,7 @@ Geometry Geo_CreateUVsphere(f32 radius, u32 north_south_lines, u32 east_west_lin
u32 bot = vertices->len - 1;
u32 i3 = i + north_south_lines * (east_west_lines - 2) + 1;
u32 i4 = (i + 1) % north_south_lines + north_south_lines * (east_west_lines - 2) + 1;
- push_triangle(indices, i3, bot, i4);
+ push_triangle(indices, bot, i3, i4);
}
// quads
diff --git a/src/render/immdraw.c b/src/render/immdraw.c
index dfe189a..cb823d3 100644
--- a/src/render/immdraw.c
+++ b/src/render/immdraw.c
@@ -10,17 +10,20 @@
#include "render.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, 8, 8);
+ Geometry sphere_geo = Geo_CreateUVsphere(1.0, 16, 16);
storage->sphere = Mesh_Create(&sphere_geo, false);
- Geometry cube_geo = Geo_CreateCuboid(f32x3(2.0, 2.0, 2.0));
+ Geometry cube_geo = Geo_CreateCuboid(f32x3(1.0, 1.0, 1.0));
storage->cube = Mesh_Create(&cube_geo, false);
- Geometry plane_geo = Geo_CreatePlane(f32x2(2.0, 2.0));
+ Geometry plane_geo = Geo_CreatePlane(f32x2(1.0, 1.0));
storage->plane = Mesh_Create(&plane_geo, false);
// Pipeline / material
@@ -31,14 +34,10 @@ void Immdraw_Init(Immdraw_Storage* storage) {
VertexDesc_AddAttr(&vertex_desc, "position", ATTR_F32x3);
VertexDesc_AddAttr(&vertex_desc, "normal", ATTR_F32x3);
- arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024);
- Str8 vert_path = str8("assets/shaders/immdraw.vert");
- Str8 frag_path = str8("assets/shaders/immdraw.frag");
- str8_opt vertex_shader = str8_from_file(&scratch, vert_path);
- str8_opt fragment_shader = str8_from_file(&scratch, frag_path);
- if (!vertex_shader.has_value || !fragment_shader.has_value) {
- ERROR_EXIT("Failed to load shaders from disk");
- }
+ const char* vert_path = "assets/shaders/immdraw.vert";
+ const char* frag_path = "assets/shaders/immdraw.frag";
+ const char* vert_shader = string_from_file(vert_path);
+ const char* frag_shader = string_from_file(frag_path);
ShaderDataLayout camera_data = Binding_Camera_GetLayout(NULL);
ShaderDataLayout imm_uniform_data = ImmediateUniforms_GetLayout(NULL);
@@ -48,12 +47,8 @@ void Immdraw_Init(Immdraw_Storage* storage) {
.vertex_desc = static_3d_vertex_description(),
.data_layouts = { camera_data, imm_uniform_data },
.data_layouts_count = 2,
- .vs = { .debug_name = "Immdraw Vertex Shader",
- .filepath = vert_path,
- .code = vertex_shader.contents },
- .fs = { .debug_name = "Immdraw Fragment Shader",
- .filepath = frag_path,
- .code = fragment_shader.contents },
+ .vs = { .debug_name = "Immdraw Vertex Shader", .filepath = vert_path, .code = vert_shader },
+ .fs = { .debug_name = "Immdraw Fragment Shader", .filepath = frag_path, .code = frag_shader },
.depth_test = true,
.wireframe = false,
};
@@ -66,8 +61,23 @@ void Immdraw_Shutdown(Immdraw_Storage* storage) {
GraphicsPipeline_Destroy(storage->colour_pipeline);
}
-void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe) {
- INFO("Draw sphere");
+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);
+}
+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);
+}
+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);
+}
+
+void Immdraw_Primitive(Transform tf, f32 size, Vec4 colour, bool wireframe, Mesh mesh) {
Immdraw_Storage* imm = Render_GetImmdrawStorage();
GPU_CmdEncoder* enc = GPU_GetDefaultEncoder();
@@ -93,9 +103,9 @@ void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe) {
GPU_EncodeBindShaderData(enc, 1, ImmediateUniforms_GetLayout(&uniforms));
// draw call
- GPU_EncodeSetVertexBuffer(enc, imm->plane.vertex_buffer);
- GPU_EncodeSetIndexBuffer(enc, imm->plane.index_buffer);
- GPU_EncodeDrawIndexed(enc, imm->plane.geometry.index_count);
+ GPU_EncodeSetVertexBuffer(enc, mesh.vertex_buffer);
+ GPU_EncodeSetIndexBuffer(enc, mesh.index_buffer);
+ GPU_EncodeDrawIndexed(enc, mesh.geometry.index_count);
// end renderpass
GPU_CmdEncoder_EndRender(enc);
diff --git a/src/render/immdraw.h b/src/render/immdraw.h
index e635531..2983356 100644
--- a/src/render/immdraw.h
+++ b/src/render/immdraw.h
@@ -29,7 +29,7 @@ PUB void Immdraw_Shutdown(Immdraw_Storage* storage);
// These functions cause a pipeline switch and so aren't optimised for performance
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, f32 size, Vec4 colour, bool wireframe);
+PUB void Immdraw_Sphere(Transform tf, Vec4 colour, bool wireframe);
PUB void Immdraw_TransformGizmo(Transform tf, f32 size);
diff --git a/src/render/render.c b/src/render/render.c
index bf8ac36..26007b7 100644
--- a/src/render/render.c
+++ b/src/render/render.c
@@ -53,6 +53,7 @@ struct Renderer {
Material_pool material_pool;
arena frame_arena;
TextureHandle white_1x1;
+ TextureHandle black_1x1;
};
Renderer* get_renderer() { return g_core.renderer; }
@@ -133,6 +134,7 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window
ren->terrain = calloc(1, sizeof(Terrain_Storage));
Terrain_Init(ren->terrain);
+ // FIXME
// ren->grid = calloc(1, sizeof(Grid_Storage));
// Grid_Init(ren->grid);
@@ -141,7 +143,7 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window
// load default textures
ren->white_1x1 = TextureLoadFromFile("assets/textures/white1x1.png");
- // TODO: black_1x1
+ ren->black_1x1 = TextureLoadFromFile("assets/textures/black1x1.png");
return true;
}