diff options
author | omnisci3nce <omniscient.oce@gmail.com> | 2024-07-18 13:38:27 +1000 |
---|---|---|
committer | omnisci3nce <omniscient.oce@gmail.com> | 2024-07-18 13:38:27 +1000 |
commit | c43bee3ec89e0863b4195ca9298a007d3526a6d9 (patch) | |
tree | 15998b606cc1b86d975f200ce09069bafd10d868 | |
parent | b872bd904f31dd90757a02e1bdda5598193c7bcc (diff) |
getting skybox ready
-rw-r--r-- | assets/shaders/shadows.vert | 13 | ||||
-rw-r--r-- | examples/game_demo/game_demo.c | 3 | ||||
-rw-r--r-- | src/core/camera.h | 2 | ||||
-rw-r--r-- | src/new_render/pbr.c | 7 | ||||
-rw-r--r-- | src/new_render/skybox.c | 59 | ||||
-rw-r--r-- | src/new_render/skybox.h | 11 |
6 files changed, 69 insertions, 26 deletions
diff --git a/assets/shaders/shadows.vert b/assets/shaders/shadows.vert index 1d1c59a..65ff169 100644 --- a/assets/shaders/shadows.vert +++ b/assets/shaders/shadows.vert @@ -6,14 +6,11 @@ layout(location = 1) in vec3 inNormal; layout(location = 2) in vec2 inTexCoords; // Uniforms -uniform Model { - mat4 mat; -} model; - -uniform LightSpace { - mat4 mat; -} lightSpace; +uniform ShadowUniforms { + mat4 model; + mat4 lightSpace; +} uniforms; void main() { - gl_Position = lightSpace.mat * model.mat * vec4(inPosition, 1.0); + gl_Position = uniforms.lightSpace * uniforms.model * vec4(inPosition, 1.0); } diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c index 0287f93..6854449 100644 --- a/examples/game_demo/game_demo.c +++ b/examples/game_demo/game_demo.c @@ -50,7 +50,8 @@ int main() { // ModelHandle player_model = ModelLoad_gltf("Player Model", "assets/demo/player.gltf"); // ModelHandle sword_model = ModelLoad("Sword Model", "assets/demo/sword.gltf"); - // create a wooden crate - loads mesh and material directly rather than via loading a model from a gltf file + // create a wooden crate - loads mesh and material directly rather than via loading a model from a + // gltf file Geometry cube_geo = Geo_CreateCuboid(f32x3(2.0, 2.0, 2.0)); Mesh crate_mesh = Mesh_Create(&cube_geo, false); // dont free as we may use later TextureHandle albedo_map = TextureLoadFromFile("assets/demo/crate/Wood_Crate_001_basecolor.jpg"); diff --git a/src/core/camera.h b/src/core/camera.h index bacbca9..9709677 100644 --- a/src/core/camera.h +++ b/src/core/camera.h @@ -29,7 +29,7 @@ PUB Mat4 Camera_ViewProj(Camera* c, f32 lens_height, f32 lens_width, Mat4* out_v PUB Mat4 Camera_View2D(Camera* c); // TODO: 2D cameras -// TODO: Basic reusable camera controls +// TODO: (HIGH) Basic reusable camera controls /* Right click + move = pan Left click = orbit camera diff --git a/src/new_render/pbr.c b/src/new_render/pbr.c index 6ac5613..f793002 100644 --- a/src/new_render/pbr.c +++ b/src/new_render/pbr.c @@ -59,11 +59,9 @@ GPU_Pipeline* PBR_PipelineCreate(GPU_Renderpass* rpass) { void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_tex, RenderEnt* entities, size_t entity_count) { // 1. set up our pipeline - // 2. upload constant data (camera, lights) - // 3. draw each entity - // - upload material data + // - upload material data -> in the future we will sort & batch by material // - upload model transform // - emit draw call @@ -84,7 +82,8 @@ void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_te Vec3 light_color = vec3(300.0, 300.0, 300.0); Binding_Lights lights_data = { .pointLights = { - // FIXME: fill out soem default lights to use + // FIXME: add lights to our RenderScene structure. for now these are + // hardcoded (pbr_point_light){ .pos = vec3(10, 10, 10), .color = light_color }, (pbr_point_light){ .pos = vec3(-10, 10, 10), .color = light_color }, (pbr_point_light){ .pos = vec3(10, -10, 10), .color = light_color }, diff --git a/src/new_render/skybox.c b/src/new_render/skybox.c index 6afefc3..a0e151a 100644 --- a/src/new_render/skybox.c +++ b/src/new_render/skybox.c @@ -1,6 +1,9 @@ #include "skybox.h" #include <assert.h> +#include "file.h" #include "glad/glad.h" +#include "log.h" +#include "primitives.h" #include "ral_common.h" #include "ral_impl.h" #include "ral_types.h" @@ -8,8 +11,13 @@ #include "render_types.h" Skybox Skybox_Create(const char** face_paths, int n) { + INFO("Creating a skybox"); assert(n == 6); // ! we're only supporting a full cubemap for now + // -- cube verts + Geometry geom = Geo_CreateCuboid(f32x3(1.0, 1.0, 1.0)); + Mesh cube = Mesh_Create(&geom, true); + // -- cubemap texture TextureHandle handle; GPU_Texture* tex = GPU_TextureAlloc(&handle); @@ -33,29 +41,66 @@ Skybox Skybox_Create(const char** face_paths, int n) { // shader pipeline + + + ShaderData shader_data = { .data = NULL, .get_layout = &Skybox_GetLayout }; + + GPU_RenderpassDesc rpass_desc = { + .default_framebuffer = true, + }; + GPU_Renderpass* pass = GPU_Renderpass_Create(rpass_desc); + + arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); + + Str8 vert_path = str8("assets/shaders/skybox.vert"); + Str8 frag_path = str8("assets/shaders/pbr_textured.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") + } + VertexDescription pos_only = { .debug_label = "Position only verts" }; VertexDesc_AddAttr(&pos_only, "inPos", ATTR_F32x3); pos_only.use_full_vertex_size = true; - ShaderData shader_data = { .data = NULL, .get_layout = &Skybox_GetLayout }; - GraphicsPipelineDesc pipeline_desc = { .debug_name = "Skybox pipeline", .vertex_desc = pos_only, .data_layouts = { shader_data }, .data_layouts_count = 1, .vs = { - + .debug_name = "Skybox Vertex Shader", + .filepath = vert_path, + .code = vertex_shader.contents }, .fs = { - + .debug_name = "Skybox Fragment Shader", + .filepath = frag_path, + .code = fragment_shader.contents }, .wireframe = false, .depth_test = true, }; - return (Skybox){ - .texture = handle, + GPU_Pipeline* pipeline = GPU_GraphicsPipeline_Create(pipeline_desc, pass); - }; + return (Skybox){ .cube = cube, .texture = handle, .pipeline = pipeline }; +} + +void Skybox_Draw(Skybox* skybox, Camera camera) { + GPU_CmdEncoder* enc = GPU_GetDefaultEncoder(); + GPU_CmdEncoder_BeginRender(enc, skybox->pipeline->renderpass); + GPU_EncodeBindPipeline(enc, skybox->pipeline); + GPU_EncodeSetDefaults(enc); + + // Shader data + SkyboxUniforms uniforms = { .in_position = camera.position, .cubemap = skybox->texture }; + ShaderData skybox_data = { .data = &uniforms, .get_layout = Skybox_GetLayout }; + + GPU_EncodeSetVertexBuffer(enc, skybox->cube.vertex_buffer); + GPU_EncodeSetVertexBuffer(enc, skybox->cube.index_buffer); + GPU_EncodeDrawIndexed(enc, skybox->cube.geometry->indices->len); + + GPU_CmdEncoder_EndRender(enc); }
\ No newline at end of file diff --git a/src/new_render/skybox.h b/src/new_render/skybox.h index ec06658..5540381 100644 --- a/src/new_render/skybox.h +++ b/src/new_render/skybox.h @@ -3,22 +3,23 @@ */ #pragma once -#include "backend_opengl.h" +#include "camera.h" #include "defines.h" #include "ral_types.h" +#include "render_types.h" typedef struct Skybox { - BufferHandle vertex_buffer; + Mesh cube; TextureHandle texture; GPU_Pipeline* pipeline; // "shader" } Skybox; PUB Skybox Skybox_Create(const char** face_paths, int n); // should always pass n = 6 for now -PUB void Skybox_Draw(Skybox* skybox); +PUB void Skybox_Draw(Skybox* skybox, Camera camera); typedef struct SkyboxUniforms { - Vec3 in_pos; + Vec3 in_position; TextureHandle cubemap; } SkyboxUniforms; @@ -40,7 +41,7 @@ static ShaderDataLayout Skybox_GetLayout(void* data) { }; if (has_data) { - b1.data.bytes.data = &d->in_pos; + b1.data.bytes.data = &d->in_position; b2.data.texture.handle = d->cubemap; } return (ShaderDataLayout) { |