diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-21 20:23:52 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-21 20:23:52 +1000 |
commit | af79d7428895413eb64bcb640e07ebb26b1c19f3 (patch) | |
tree | 2896b9db9be810ca0ee914151d50ca3a3fe325f2 | |
parent | 66b1e02847e08b8e66e9969f0a3d138c15cc70f4 (diff) |
quads kinda work
-rw-r--r-- | assets/shaders/terrain.frag | 4 | ||||
-rw-r--r-- | assets/shaders/terrain.vert | 4 | ||||
-rw-r--r-- | examples/game_demo/game_demo.c | 2 | ||||
-rw-r--r-- | src/new_render/render.c | 10 | ||||
-rw-r--r-- | src/ral/ral_types.h | 1 | ||||
-rw-r--r-- | src/systems/terrain.c | 71 | ||||
-rw-r--r-- | src/systems/terrain.h | 9 |
7 files changed, 81 insertions, 20 deletions
diff --git a/assets/shaders/terrain.frag b/assets/shaders/terrain.frag index 5bce13b..739ff83 100644 --- a/assets/shaders/terrain.frag +++ b/assets/shaders/terrain.frag @@ -2,6 +2,8 @@ out vec4 FragColor; +in vec4 Color; + void main() { - FragColor = vec4(1.0); + FragColor = Color; }
\ No newline at end of file diff --git a/assets/shaders/terrain.vert b/assets/shaders/terrain.vert index 5af1560..32b43b3 100644 --- a/assets/shaders/terrain.vert +++ b/assets/shaders/terrain.vert @@ -8,6 +8,10 @@ uniform Camera { vec4 viewPos; } cam; +out vec4 Color; + void main() { gl_Position = cam.proj * cam.view * vec4(inPosition, 1.0); + + Color = vec4(inPosition.y / 255.0); }
\ No newline at end of file diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c index 9036fbe..7f3496a 100644 --- a/examples/game_demo/game_demo.c +++ b/examples/game_demo/game_demo.c @@ -44,7 +44,7 @@ int main() { // --- Terrain Heightmap hmap = Heightmap_FromImage(str8("assets/test_heightmap.png")); Terrain_Storage* terrain = Render_GetTerrainStorage(); - Terrain_LoadHeightmap(terrain, hmap, false); + Terrain_LoadHeightmap(terrain, hmap, 2.0, false); // assert(Terrain_IsActive()); // --- Skybox diff --git a/src/new_render/render.c b/src/new_render/render.c index 08ef2c0..adf56b1 100644 --- a/src/new_render/render.c +++ b/src/new_render/render.c @@ -3,6 +3,7 @@ */ #include "render.h" +#include <assert.h> #include <glfw3.h> #include "camera.h" #include "core.h" @@ -185,9 +186,12 @@ TextureData TextureDataLoad(const char* path, bool invert_y) { channel_type = GL_RGB; format = TEXTURE_FORMAT_8_8_8_RGB_UNORM; } - TextureDesc desc = { .extents = { width, height }, - .format = format, - .tex_type = TEXTURE_TYPE_2D }; + TextureDesc desc = { + .extents = { width, height }, + .format = format, + .num_channels = num_channels, + .tex_type = TEXTURE_TYPE_2D, + }; return (TextureData){ .description = desc, .image_data = data }; } diff --git a/src/ral/ral_types.h b/src/ral/ral_types.h index d65b35c..56fa141 100644 --- a/src/ral/ral_types.h +++ b/src/ral/ral_types.h @@ -71,6 +71,7 @@ typedef struct TextureDesc { GPU_TextureType tex_type; GPU_TextureFormat format; u32x2 extents; + u32 num_channels; } TextureDesc; // --- Vertices diff --git a/src/systems/terrain.c b/src/systems/terrain.c index f916442..404c7ea 100644 --- a/src/systems/terrain.c +++ b/src/systems/terrain.c @@ -15,8 +15,8 @@ #include "shader_layouts.h" #include "str.h" -#define TERRAIN_GRID_U 64 -#define TERRAIN_GRID_V 64 +#define TERRAIN_GRID_U 505 +#define TERRAIN_GRID_V 505 bool Terrain_Init(Terrain_Storage* storage) { storage->grid_dimensions = u32x2(TERRAIN_GRID_U, TERRAIN_GRID_V); @@ -67,13 +67,18 @@ bool Terrain_Init(Terrain_Storage* storage) { void Terrain_Shutdown(Terrain_Storage* storage) {} -void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, bool free_on_upload) { +void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, f32 grid_scale, + bool free_on_upload) { // If there's a current one we will delete it and reallocate buffers if (storage->hmap_loaded) { GPU_BufferDestroy(storage->vertex_buffer); GPU_BufferDestroy(storage->index_buffer); } + u32 width = hmap.pixel_dimensions.x; + u32 height = hmap.pixel_dimensions.y; + storage->grid_scale = grid_scale; + size_t num_vertices = storage->grid_dimensions.x * storage->grid_dimensions.y; storage->num_vertices = num_vertices; @@ -81,34 +86,66 @@ void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, bool free_o u32 index = 0; for (u32 i = 0; i < storage->grid_dimensions.x; i++) { for (u32 j = 0; j < storage->grid_dimensions.y; j++) { + size_t position = j * storage->grid_dimensions.x + i; + u8* bytes = hmap.image_data; + u8 channel = bytes[position]; + float value = (float)channel / 255.0; + printf("(%d, %d) %d : %f \n", i, j, channel, value); + assert(index < num_vertices); - Vertex v = { .pos_only.position = vec3_create(i, 0.0, j) }; + f32 height = Heightmap_HeightXZ(&hmap, i, j); + Vertex v = { .pos_only.position = vec3_create(i * grid_scale, height, j * grid_scale) }; Vertex_darray_push(vertices, v); index++; } } - BufferHandle vertices_handle = GPU_BufferCreate(num_vertices * sizeof(Vertex), BUFFER_VERTEX, BUFFER_FLAG_GPU, vertices->data); - storage->vertex_buffer = vertices_handle; + + u32 quad_count = (width - 1) * (height - 1); + u32 indices_count = quad_count * 6; + storage->indices_count = indices_count; + u32_darray* indices = u32_darray_new(indices_count); + for (u32 i = 0; i < (width - 1); i++) { // row + for (u32 j = 0; j < (height - 1); j++) { // col + u32 bot_left = i * width + j; + u32 top_left = (i + 1) * width + j; + u32 top_right = (i + 1) * width + (j + 1); + u32 bot_right = i * width + j + 1; + + // top left tri + u32_darray_push(indices, top_right); + u32_darray_push(indices, top_left); + u32_darray_push(indices, bot_left); + + // bottom right tri + u32_darray_push(indices, bot_right); + u32_darray_push(indices, top_right); + u32_darray_push(indices, bot_left); + } + } + + BufferHandle indices_handle = + GPU_BufferCreate(indices_count * sizeof(u32), BUFFER_INDEX, BUFFER_FLAG_GPU, indices->data); + storage->index_buffer = indices_handle; } Heightmap Heightmap_FromImage(Str8 filepath) { size_t max_size = MB(16); arena arena = arena_create(malloc(max_size), max_size); - str8_opt maybe_file = str8_from_file(&arena, filepath); - - assert(maybe_file.has_value); + // str8_opt maybe_file = str8_from_file(&arena, filepath); + // assert(maybe_file.has_value); TextureData hmap_tex = TextureDataLoad(Str8_to_cstr(&arena, filepath), false); - arena_free_storage(&arena); + // arena_free_storage(&arena); return (Heightmap){ .pixel_dimensions = hmap_tex.description.extents, .filepath = filepath, .image_data = hmap_tex.image_data, + .num_channels = hmap_tex.description.num_channels, .is_uploaded = false, }; } @@ -129,7 +166,17 @@ void Terrain_Draw(Terrain_Storage* storage) { enc, 0, (ShaderData){ .data = &camera_data, .get_layout = &Binding_Camera_GetLayout }); GPU_EncodeSetVertexBuffer(enc, storage->vertex_buffer); + GPU_EncodeSetIndexBuffer(enc, storage->index_buffer); - // GPU_EncodeDraw(enc, storage->num_vertices); - glDrawArrays(GL_POINTS, 0, storage->num_vertices); + GPU_EncodeDrawIndexed(enc, storage->indices_count); + // glDrawArrays(GL_POINTS, 0, storage->num_vertices); } + +f32 Heightmap_HeightXZ(const Heightmap* hmap, u32 x, u32 z) { + // its single channel so only one byte per pixel + size_t position = x * hmap->pixel_dimensions.x + z; + u8* bytes = hmap->image_data; + u8 channel = bytes[position]; + float value = (float)channel / 2.0; /// 255.0; + return value; +}
\ No newline at end of file diff --git a/src/systems/terrain.h b/src/systems/terrain.h index a38e45e..2e05a14 100644 --- a/src/systems/terrain.h +++ b/src/systems/terrain.h @@ -23,12 +23,14 @@ typedef struct Heightmap { Str8 filepath; u32x2 pixel_dimensions; void* image_data; + u32 num_channels; bool is_uploaded; } Heightmap; typedef struct Terrain_Storage { // arena terrain_allocator; u32x2 grid_dimensions; + f32 grid_scale; u32 num_vertices; Heightmap heightmap; // NULL = no heightmap GPU_Renderpass* hmap_renderpass; @@ -37,6 +39,7 @@ typedef struct Heightmap { bool hmap_loaded; BufferHandle vertex_buffer; BufferHandle index_buffer; + u32 indices_count; } Terrain_Storage; // --- Public API @@ -46,7 +49,7 @@ PUB void Terrain_Draw( Terrain_Storage* storage); // NOTE: For now it renders directly to main framebuffer /** @brief Sets the active heightmap to be rendered and collided against. */ -PUB void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, bool free_on_upload); +PUB void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, f32 grid_scale, bool free_on_upload); PUB Heightmap Heightmap_FromImage(Str8 filepath); PUB Heightmap Heightmap_FromPerlin(/* TODO: perlin noise generation parameters */); @@ -58,10 +61,10 @@ PUB bool Terrain_IsActive(); // checks whether we have a loaded heightmap and i /** @brief Get the height (the Y component) for a vertex at a particular coordinate in the heightmap */ -f32 Heightmap_HeightXZ(Heightmap* hmap, f32 x, f32 z); +f32 Heightmap_HeightXZ(const Heightmap* hmap, u32 x, u32 z); /** @brief Calculate the normal vector of a vertex at a particular coordinate in the heightmap */ -Vec3 Heightmap_NormalXZ(Heightmap* hmap, f32 x, f32 z); +Vec3 Heightmap_NormalXZ(const Heightmap* hmap, f32 x, f32 z); // /** @brief Generate the `geometry_data` for a heightmap ready to be uploaded to the GPU */ // Geometry geo_heightmap(arena* a, Heightmap heightmap); |