diff options
author | omnisci3nce <omniscient.oce@gmail.com> | 2024-07-18 23:10:11 +1000 |
---|---|---|
committer | omnisci3nce <omniscient.oce@gmail.com> | 2024-07-18 23:10:11 +1000 |
commit | 01d598220b403f4ac4da3b7aa1f0ce398be3198f (patch) | |
tree | 1408b120506f29d3a2d6b3b29a0c91fc1b1371eb | |
parent | 544aac70015b6b075b172c42dd2d90dee3d029c0 (diff) |
skybox works
-rw-r--r-- | assets/shaders/skybox.frag | 5 | ||||
-rw-r--r-- | assets/shaders/skybox.vert | 3 | ||||
-rw-r--r-- | examples/game_demo/game_demo.c | 8 | ||||
-rw-r--r-- | src/maths/primitives.c | 3 | ||||
-rw-r--r-- | src/new_render/render.c | 12 | ||||
-rw-r--r-- | src/new_render/skybox.c | 49 | ||||
-rw-r--r-- | src/new_render/skybox.h | 2 | ||||
-rw-r--r-- | src/ral/ral_common.h | 2 | ||||
-rw-r--r-- | src/ral/ral_types.h | 5 |
9 files changed, 69 insertions, 20 deletions
diff --git a/assets/shaders/skybox.frag b/assets/shaders/skybox.frag index 242477d..d2f3844 100644 --- a/assets/shaders/skybox.frag +++ b/assets/shaders/skybox.frag @@ -4,8 +4,9 @@ out vec4 FragColor; in vec3 TexCoords; -uniform samplerCube cubemap; +uniform samplerCube cubeMap; void main() { - FragColor = texture(cubemap, TexCoords); + FragColor = texture(cubeMap, TexCoords); + // FragColor = vec4(1.0, 0.0, 0.0, 1.0); } diff --git a/assets/shaders/skybox.vert b/assets/shaders/skybox.vert index 57c6399..8db1f4f 100644 --- a/assets/shaders/skybox.vert +++ b/assets/shaders/skybox.vert @@ -14,5 +14,6 @@ out vec3 TexCoords; void main() { TexCoords = inPosition; - gl_Position = cam.proj * cam.view * vec4(inPosition, 1.0); + vec4 pos = cam.proj * cam.view * vec4(inPosition, 1.0); + gl_Position = pos.xyww; } diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c index 720614f..1f88503 100644 --- a/examples/game_demo/game_demo.c +++ b/examples/game_demo/game_demo.c @@ -24,13 +24,13 @@ int main() { // TODO: Load humanoid model + weapon // TODO: Animate it with WASD keys - // TODO: Skybox + // TODO: Skybox (ALMOST) // TODO: Add a ground terrain // TODO: Move camera with model // --- Render Scene - Vec3 camera_pos = vec3(0.0, 4.0, 8.0); - Camera cam = Camera_Create(camera_pos, vec3_negate(camera_pos), VEC3_Y, 45.0); + Vec3 camera_pos = vec3(0.0, 4.0, -8.0); + Camera cam = Camera_Create(camera_pos, vec3_normalise(vec3_negate(camera_pos)), VEC3_Y, 45.0); SetCamera(cam); // update the camera in RenderScene DirectionalLight sun = { @@ -90,7 +90,7 @@ int main() { // BEGIN Draw calls // draw the player model with shadows - // Render_RenderEntities(entities, entity_count); + Render_RenderEntities(entities, entity_count); // Render_DrawTerrain(); Skybox_Draw(&skybox, cam); diff --git a/src/maths/primitives.c b/src/maths/primitives.c index 04977a6..233a7e0 100644 --- a/src/maths/primitives.c +++ b/src/maths/primitives.c @@ -117,6 +117,9 @@ Geometry Geo_CreateCuboid(f32x3 extents) { for (u32 i = 0; i < vertices->len; i++) { u32_darray_push(indices, i); + vertices->data[i].static_3d.position = + vec3_sub(vertices->data[i].static_3d.position, + vec3(0.5, 0.5, 0.5)); // make center of the cube is the origin of mesh space } Geometry geo = { diff --git a/src/new_render/render.c b/src/new_render/render.c index a757c97..58041dd 100644 --- a/src/new_render/render.c +++ b/src/new_render/render.c @@ -142,7 +142,7 @@ TextureData TextureDataLoad(const char* path, bool invert_y) { stbi_set_flip_vertically_on_load(invert_y); #pragma GCC diagnostic ignored "-Wpointer-sign" - char* data = stbi_load(path, &width, &height, &num_channels, STBI_rgb_alpha); + char* data = stbi_load(path, &width, &height, &num_channels, STBI_rgb); if (data) { DEBUG("loaded texture: %s", path); } else { @@ -182,10 +182,12 @@ Mesh Mesh_Create(Geometry* geometry, bool free_on_upload) { GPU_BufferCreate(vert_bytes, BUFFER_VERTEX, BUFFER_FLAG_GPU, geometry->vertices->data); // Create and upload index buffer - size_t index_bytes = geometry->indices->len * sizeof(u32); - INFO("Creating index buffer with size %d (len: %d)", index_bytes, geometry->indices->len); - m.index_buffer = - GPU_BufferCreate(index_bytes, BUFFER_INDEX, BUFFER_FLAG_GPU, geometry->indices->data); + if (geometry->has_indices) { + size_t index_bytes = geometry->indices->len * sizeof(u32); + INFO("Creating index buffer with size %d (len: %d)", index_bytes, geometry->indices->len); + m.index_buffer = + GPU_BufferCreate(index_bytes, BUFFER_INDEX, BUFFER_FLAG_GPU, geometry->indices->data); + } m.is_uploaded = true; m.geometry = *geometry; // clone geometry data and store on Mesh struct diff --git a/src/new_render/skybox.c b/src/new_render/skybox.c index 267ec6d..8ac8356 100644 --- a/src/new_render/skybox.c +++ b/src/new_render/skybox.c @@ -12,12 +12,42 @@ #include "render_types.h" #include "shader_layouts.h" +float skyboxVertices[] = { + // positions + -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, + + 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, + + -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f +}; + 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)); + Geometry geom = { .format = VERTEX_POS_ONLY, // doesnt matter + .has_indices = false, + .indices = NULL, + .vertices = Vertex_darray_new(36) }; + for (u32 i = 0; i < (36 * 3); i += 3) { + Vertex_darray_push( + geom.vertices, + (Vertex){ .pos_only = { .position = vec3(skyboxVertices[i], skyboxVertices[i + 1], + skyboxVertices[i + 2]) } }); + } Mesh cube = Mesh_Create(&geom, false); // -- cubemap texture @@ -64,10 +94,14 @@ Skybox Skybox_Create(const char** face_paths, int n) { ShaderData camera_data = { .data = NULL, .get_layout = &Binding_Camera_GetLayout }; ShaderData shader_data = { .data = NULL, .get_layout = &Skybox_GetLayout }; + VertexDescription builder = { .debug_label = "pos only" }; + VertexDesc_AddAttr(&builder, "inPosition", ATTR_F32x3); + builder.use_full_vertex_size = true; + GraphicsPipelineDesc pipeline_desc = { .debug_name = "Skybox pipeline", - .vertex_desc = static_3d_vertex_description(), - .data_layouts = { camera_data, shader_data }, + .vertex_desc = builder, + .data_layouts = { shader_data, camera_data }, .data_layouts_count = 2, .vs = { .debug_name = "Skybox Vertex Shader", .filepath = vert_path, @@ -75,7 +109,7 @@ Skybox Skybox_Create(const char** face_paths, int n) { .fs = { .debug_name = "Skybox Fragment Shader", .filepath = frag_path, .code = fragment_shader.contents }, - .wireframe = false, + .wireframe = true, .depth_test = true, }; @@ -86,6 +120,7 @@ Skybox Skybox_Create(const char** face_paths, int n) { void Skybox_Draw(Skybox* skybox, Camera camera) { GPU_CmdEncoder* enc = GPU_GetDefaultEncoder(); + glDepthFunc(GL_LEQUAL); GPU_CmdEncoder_BeginRender(enc, skybox->pipeline->renderpass); GPU_EncodeBindPipeline(enc, skybox->pipeline); GPU_EncodeSetDefaults(enc); @@ -118,9 +153,9 @@ void Skybox_Draw(Skybox* skybox, Camera camera) { GPU_EncodeSetVertexBuffer(enc, skybox->cube.vertex_buffer); GPU_EncodeSetIndexBuffer(enc, skybox->cube.index_buffer); - glDepthFunc(GL_LEQUAL); - GPU_EncodeDrawIndexed(enc, skybox->cube.geometry.indices->len); - glDepthFunc(GL_LESS); + + GPU_EncodeDraw(enc, 36); GPU_CmdEncoder_EndRender(enc); + glDepthFunc(GL_LESS); } diff --git a/src/new_render/skybox.h b/src/new_render/skybox.h index bf70976..71262fd 100644 --- a/src/new_render/skybox.h +++ b/src/new_render/skybox.h @@ -27,7 +27,7 @@ static ShaderDataLayout Skybox_GetLayout(void* data) { bool has_data = data != NULL; ShaderBinding b1 = { - .label = "cubemap", + .label = "cubeMap", .vis = VISIBILITY_FRAGMENT, .kind = BINDING_TEXTURE, }; diff --git a/src/ral/ral_common.h b/src/ral/ral_common.h index 6a373ee..98f54ed 100644 --- a/src/ral/ral_common.h +++ b/src/ral/ral_common.h @@ -39,6 +39,8 @@ struct ResourcePools { typedef struct ResourcePools ResourcePools; void ResourcePools_Init(arena* a, struct ResourcePools* res_pools); +PUB GPU_Renderpass* GPU_GetDefaultRenderpass(); // returns a renderpass that draws directly to default framebuffer with default depth + // --- Vertex formats VertexDescription static_3d_vertex_description(); diff --git a/src/ral/ral_types.h b/src/ral/ral_types.h index 54dba56..bb15d2a 100644 --- a/src/ral/ral_types.h +++ b/src/ral/ral_types.h @@ -76,6 +76,7 @@ typedef enum VertexFormat { VERTEX_SKINNED, VERTEX_COLOURED_STATIC_3D, VERTEX_RAW_POS_COLOUR, + VERTEX_POS_ONLY, VERTEX_COUNT } VertexFormat; @@ -112,6 +113,10 @@ typedef union Vertex { Vec2 position; Vec3 colour; } raw_pos_colour; + + struct { + Vec3 position; + } pos_only; } Vertex; #ifndef TYPED_VERTEX_ARRAY |