summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorOmniscient <omniscient.oce@gmail.com>2024-06-15 16:38:05 +1000
committerOmniscient <omniscient.oce@gmail.com>2024-06-15 16:38:05 +1000
commitc5808488875484aca814bfc8e526f37f3f447166 (patch)
treee560bc86302fad0c8520140511e1994c2b4800d7 /examples
parent59f67f04d7238453da766218a37b3c78607fb122 (diff)
shinchoku
Diffstat (limited to 'examples')
-rw-r--r--examples/cube/ex_cube.c9
-rw-r--r--examples/pbr_params/ex_pbr_params.c117
-rw-r--r--examples/primitives/ex_primitives.c2
3 files changed, 127 insertions, 1 deletions
diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c
index 5f45b7f..26e6b71 100644
--- a/examples/cube/ex_cube.c
+++ b/examples/cube/ex_cube.c
@@ -16,6 +16,15 @@
extern core g_core;
+// Scene / light setup
+const vec3 pointlight_positions[4] = {
+ { -10.0, 10.0, 10.0},
+ { 10.0, 10.0, 10.0},
+ { -10.0, -10.0, 10.0},
+ { 10.0, -10.0, 10.0},
+};
+point_light point_lights[4];
+
// Define the shader data
typedef struct mvp_uniforms {
mat4 model;
diff --git a/examples/pbr_params/ex_pbr_params.c b/examples/pbr_params/ex_pbr_params.c
new file mode 100644
index 0000000..583fd12
--- /dev/null
+++ b/examples/pbr_params/ex_pbr_params.c
@@ -0,0 +1,117 @@
+#include <glfw3.h>
+
+#include "builtin_materials.h"
+#include "camera.h"
+#include "colours.h"
+#include "core.h"
+#include "log.h"
+#include "maths.h"
+#include "maths_types.h"
+#include "primitives.h"
+#include "ral.h"
+#include "render.h"
+#include "render_types.h"
+#include "str.h"
+
+extern core g_core;
+
+// Lets define some constant PBR parameters here to test on one sphere with first
+const rgba ALBEDO = RED_700;
+const f32 metallic = 0.0;
+const f32 roughness = 0.8;
+const f32 ao = 0.2;
+
+const pbr_params_material_uniforms pbr_params = {.albedo = (vec3){0.5,0.5,0.5}, .metallic = metallic, .roughness = roughness, .ao = ao};
+
+int main() {
+ core_bringup();
+
+ arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024);
+
+ vec3 camera_pos = vec3(3.0, 3., 3.);
+ vec3 camera_front = vec3_normalise(vec3_negate(camera_pos));
+ camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0));
+
+ // PBR Material data
+ shader_data pbr_uniforms = { .data = NULL, .shader_data_get_layout = &pbr_params_shader_layout };
+
+ // Make the pipeline
+ gpu_renderpass_desc pass_description = {};
+ gpu_renderpass* renderpass = gpu_renderpass_create(&pass_description);
+ // FIXME: Make this work on other API backends
+ str8 vert_path = str8lit("assets/shaders/pbr_params.vert");
+ str8 frag_path = str8lit("assets/shaders/pbr_params.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")
+ }
+
+ struct graphics_pipeline_desc pipeline_description = {
+ .debug_name = "Basic Pipeline",
+ .vertex_desc = static_3d_vertex_description(),
+ .data_layouts = { pbr_uniforms },
+ .data_layouts_count = 1,
+ .vs = { .debug_name = "PBR (params) Vertex Shader",
+ .filepath = vert_path,
+ .code = vertex_shader.contents,
+ .is_spirv = false },
+ .fs = { .debug_name = "PBR (params) Fragment Shader",
+ .filepath = frag_path,
+ .code = fragment_shader.contents,
+ .is_spirv = false },
+ .renderpass = renderpass,
+ .wireframe = false,
+ .depth_test = false
+ };
+ gpu_pipeline* pbr_pipeline = gpu_graphics_pipeline_create(pipeline_description);
+
+ // Geometry
+ geometry_data sphere_data = geo_create_uvsphere(1.0, 12, 12);
+ mesh sphere = mesh_create(&sphere_data, false);
+
+ pbr_params_bindgroup pbr_bind_data;
+
+ while (!should_exit()) {
+ input_update(&g_core.input);
+
+ if (!gpu_backend_begin_frame()) {
+ continue;
+ }
+ gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder();
+ // Begin recording
+ gpu_cmd_encoder_begin(*enc);
+ gpu_cmd_encoder_begin_render(enc, renderpass);
+ encode_bind_pipeline(enc, PIPELINE_GRAPHICS, pbr_pipeline);
+ encode_set_default_settings(enc);
+
+ mat4 model_affine = mat4_ident();
+ mat4 view, proj;
+ camera_view_projection(&cam, 1000, 1000, &view, &proj);
+
+ // Feed shader data
+ pbr_bind_data.mvp_matrices = (mvp_matrix_uniforms){.model = model_affine, .view = view, .projection = proj};
+ pbr_bind_data.material = pbr_params;
+ pbr_bind_data.lights = (pbr_params_light_uniforms){.viewPos = cam.position /* TODO: point lights*/};
+ pbr_uniforms.data = &pbr_bind_data;
+ encode_bind_shader_data(enc, 0, &pbr_uniforms);
+
+ // Record draw call
+ /* draw_mesh(&sphere, &model_affine, &cam); */
+ encode_set_vertex_buffer(enc, sphere.vertex_buffer);
+ encode_set_index_buffer(enc, sphere.index_buffer);
+ encode_draw_indexed(enc, sphere.geometry->indices->len);
+
+ // End recording
+ gpu_cmd_encoder_end_render(enc);
+
+ gpu_cmd_buffer buf = gpu_cmd_encoder_finish(enc);
+ gpu_queue_submit(&buf);
+ // Submit
+ gpu_backend_end_frame();
+ }
+
+ renderer_shutdown(&g_core.renderer);
+
+ return 0;
+}
diff --git a/examples/primitives/ex_primitives.c b/examples/primitives/ex_primitives.c
index 8ce2bf2..2109215 100644
--- a/examples/primitives/ex_primitives.c
+++ b/examples/primitives/ex_primitives.c
@@ -50,7 +50,7 @@ int main() {
.scale = 1.0 };
mat4 sphere_model = transform_to_mat(&transform);
- mat4 cube_model = mat4_translation(vec3(-2.,0,0));
+ mat4 cube_model = mat4_translation(vec3(-2., 0, 0));
draw_mesh(&cube, &cube_model, &cam);
draw_mesh(&sphere, &sphere_model, &cam);