From 58816606a81a3acf8f5e9bfa20079341a1aefcca Mon Sep 17 00:00:00 2001 From: Randy McShandy Date: Sat, 4 May 2024 12:17:57 -0500 Subject: [PATCH] Starting heightmap to rendering pipeline with Raylib --- CMakeLists.txt | 4 +-- src/main.c | 13 +++++++-- src/render.h | 8 ++++++ src/render_raylib.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ src/structs.c | 4 +++ src/structs.h | 4 +++ src/utils.c | 26 +++++++++++++----- 7 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 src/render.h create mode 100644 src/render_raylib.c diff --git a/CMakeLists.txt b/CMakeLists.txt index fdb7373..ff2ecde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,9 +16,9 @@ add_executable(windows_BC ${WINDOWS_SRC}) target_compile_definitions(posix_BC PUBLIC POSIX_BC=1) target_compile_definitions(windows_BC PUBLIC WINDOWS_BC=1) -target_compile_options(posix_BC PUBLIC -Wall) +target_compile_options(posix_BC PUBLIC -Wall -g) -target_link_libraries(posix_BC -lm -lpthread) +target_link_libraries(posix_BC -lm -lpthread -lraylib) target_link_libraries(windows_BC -lm -lpthread) set_target_properties(posix_BC PROPERTIES RUNTIME_OUTPUT_DIRECTORY ./bin/) diff --git a/src/main.c b/src/main.c index 9af3a99..b4a865d 100755 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,19 @@ #include #include + #include "structs.h" +#include "render.h" -int main(int argc, char** argv) +void print_rlog(int logLevel, const char* text, va_list args) { + printf("TRACE %d: ", logLevel); + printf(text, args); + printf("\n"); +} - IVec2 grid_size = {.x = GRID_X * 1.5, .y = GRID_Y * 1.5}; +int main(int argc, char** argv) +{ + IVec2 grid_size = {.x = GRID_X * 1.0, .y = GRID_Y * 1.0}; FVec2 **grid = NULL; grid = (FVec2**)malloc(grid_size.x * sizeof(FVec2*)); for (int n = 0; n < grid_size.x; n++) @@ -14,6 +22,7 @@ int main(int argc, char** argv) } generate_rd(8, barrow, grid, grid_size); + start_render_loop(); return 0; } diff --git a/src/render.h b/src/render.h new file mode 100644 index 0000000..01aab4f --- /dev/null +++ b/src/render.h @@ -0,0 +1,8 @@ +#ifndef RENDER_INTERFACE +#define RENDER_INTERFACE + +void initialize_renderer(); +void start_render_loop(); + +#endif /* RENDER_INTERFACE */ + diff --git a/src/render_raylib.c b/src/render_raylib.c new file mode 100644 index 0000000..d2017e7 --- /dev/null +++ b/src/render_raylib.c @@ -0,0 +1,65 @@ +#include +#include "structs.h" + +Camera3D cam; + +Image barrow_image; +Texture barrow_texture; +Texture diffuse_texture; +Model barrow_model; +Mesh barrow_meshes[1U]; +Material barrow_material; +Matrix barrow_transform; +Vector3 barrow_position; + +void initialize_renderer() +{ + barrow_position = (Vector3){-8.0f, 0.0f, -8.0f}; + barrow_image = LoadImage("./barrow.png"); + diffuse_texture = LoadTexture("./barrow_diffuse.png"); + + while(!IsImageReady(barrow_image)){} + barrow_texture = LoadTextureFromImage(barrow_image); + + while(!IsTextureReady(barrow_texture)){} + while(!IsTextureReady(diffuse_texture)){} + + barrow_meshes[0U] = GenMeshHeightmap(barrow_image, (Vector3){64.0f, 8.0f, 64.0f}); + barrow_model = LoadModelFromMesh(barrow_meshes[0]); + barrow_model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = diffuse_texture; + + cam = (Camera3D){0}; + cam.position = barrow_position; //(Vector3){18.0f, 21.0f, 18.0f}; + cam.position.y += 1.0f; + cam.target = (Vector3){0.0f, 0.0f, 0.0f}; + cam.up = (Vector3){0.0f, 1.0f, 0.0f}; + cam.fovy = 45.0f; + cam.projection= CAMERA_PERSPECTIVE; +} + +void start_render_loop() +{ + InitWindow(screen_dims.x, screen_dims.y, screen_title); + initialize_renderer(); + + SetTargetFPS(target_fps); + while (!WindowShouldClose()) + { + UpdateCamera(&cam, CAMERA_CUSTOM); + BeginDrawing(); + ClearBackground(LIGHTGRAY); + + BeginMode3D(cam); + DrawModel(barrow_model, barrow_position, 1.0f, DARKGRAY); + EndMode3D(); + + DrawTexture(barrow_texture, 64, 64, RAYWHITE); + DrawFPS(10, 10); + EndDrawing(); + } + + UnloadImage(barrow_image); + UnloadTexture(barrow_texture); + CloseWindow(); +} + diff --git a/src/structs.c b/src/structs.c index 0997ca3..530dfc7 100644 --- a/src/structs.c +++ b/src/structs.c @@ -72,3 +72,7 @@ const Mat3 laplacian_kernel = const char chars[6] = {' ', ' ', ' ', '+', '#', '@'}; const int max_chars = sizeof(chars) / sizeof(chars[0]) - 1; +const IVec2 screen_dims = {.x = 1600, .y = 900}; +const char* screen_title = "Barrow Crawler"; +const int target_fps = 60; + diff --git a/src/structs.h b/src/structs.h index 2c60775..dcf1507 100644 --- a/src/structs.h +++ b/src/structs.h @@ -66,6 +66,10 @@ extern RD_Opts worms; extern RD_Opts meiosis; extern const Mat3 laplacian_kernel; +extern const IVec2 screen_dims; +extern const char* screen_title; +extern const int target_fps; + int generate_rd(int worker_count, RD_Opts active_opt, FVec2 **grid_buffer, IVec2 pgrid_size); #endif //__RD_STRUCTS__ diff --git a/src/utils.c b/src/utils.c index b5b8171..d342796 100644 --- a/src/utils.c +++ b/src/utils.c @@ -3,7 +3,7 @@ #include #include -#warning TODO: I think this header is only posix, check for alt platform replacements +#warning "TODO: I think this header is only posix, check for alt platform replacements" #include #include "structs.h" @@ -152,17 +152,31 @@ int generate_rd(int worker_count, RD_Opts active_opt, FVec2 **grid_buffer, IVec2 printf("Opts: {\nIterations: %d\nTime delta: %f\nDiffA: %f\nDiffB: %f\nFeed: %f\nKill: %f\n\n}\n", active_opt.max_iterations, active_opt.delta_t, active_opt.diff_a, active_opt.diff_b, active_opt.feed, active_opt.kill); - char buffer[pgrid_size.x][pgrid_size.y]; + /* png for the heightmap */ + char mesh_buffer[pgrid_size.x][pgrid_size.y]; + /* png for diffuse material */ + char diffuse_buffer[pgrid_size.x][pgrid_size.y]; for (int x = 0; x < pgrid_size.x; x++) { for (int y = 0; y < pgrid_size.y; y++) { - buffer[x][y] = (uint8_t)(255.0f * round(-0.05f + grid_buffer[x][y].a)); + float a = round(-0.05f + grid_buffer[x][y].a); + mesh_buffer[x][y] = (uint8_t)(255.0f * a); + + /* Generate the diffuse texture. + This is a good chance to do cool stuff. + */ + a = grid_buffer[x][y].a; + diffuse_buffer[x][y] = (uint8_t)(255.0f * a); } } - char name[64] = {0}; - sprintf(name, "./%s.png", active_opt.name); - stbi_write_png(name, pgrid_size.x, pgrid_size.y, 1, buffer, pgrid_size.x * sizeof(uint8_t)); + + char mesh_name[64] = {0}; + char diffuse_name[64] = {0}; + sprintf(mesh_name, "./%s.png", active_opt.name); + sprintf(diffuse_name, "./%s_diffuse.png", active_opt.name); + stbi_write_png(mesh_name, pgrid_size.x, pgrid_size.y, 1, mesh_buffer, pgrid_size.x * sizeof(uint8_t)); + stbi_write_png(diffuse_name, pgrid_size.x, pgrid_size.y, 1, diffuse_buffer, pgrid_size.x * sizeof(uint8_t)); cleanup(); return 0; -- 2.49.0