From 1144d868359977d034633cf716ae8ceffd5d18b1 Mon Sep 17 00:00:00 2001 From: Randy McShandy Date: Tue, 15 Oct 2024 22:01:28 -0500 Subject: [PATCH] Overhaul lots of positioning math to hopefully simplify future placements, and then spend a week unbreaking everything --- src/main.c | 2 +- src/render_raylib.c | 184 +++++++++++++++++++++++++++++++--------- src/shaders/lighting.fs | 2 +- src/structs.c | 3 + src/structs.h | 3 + src/utils.c | 1 + 6 files changed, 154 insertions(+), 41 deletions(-) diff --git a/src/main.c b/src/main.c index bf43b64..187ec7a 100755 --- a/src/main.c +++ b/src/main.c @@ -18,7 +18,7 @@ int main(int argc, char** argv) room_positions = (Vector3*)malloc(sizeof(Vector3)*(room_count + 1)); /* TODO: Clean this up and integrate into platform system. */ -#define ENABLE_BARROWGEN 0 +#define ENABLE_BARROWGEN 1 #if ENABLE_BARROWGEN barrow.max_iterations *= 1; IVec2 grid_size = {.x = GRID_X*2.0, .y = GRID_Y * 2.0}; diff --git a/src/render_raylib.c b/src/render_raylib.c index 51210ce..b5b19c4 100644 --- a/src/render_raylib.c +++ b/src/render_raylib.c @@ -10,7 +10,28 @@ #include "structs.h" -#define VEC3_UNROLL(v3) v3.x, v3.y, v3.z +#define str(x) #x +#define xstr(x) str(x) +#define V3_UNROLL(v3) v3.x, v3.y, v3.z +#define V3_FORMAT(v3) #v3 ":{%.4f %.4f %.4f}" + +#define MAT4_UNROLL(m) \ + m.m0 , \ + m.m1 , \ + m.m2 , \ + m.m3 , \ + m.m4 , \ + m.m5 , \ + m.m6 , \ + m.m7 , \ + m.m8 , \ + m.m9 , \ + m.m10, \ + m.m11, \ + m.m12, \ + m.m13, \ + m.m14, \ + m.m15 Vector2 fscreen_dims; Camera3D cam; @@ -19,8 +40,19 @@ Texture barrow_texture; Texture barrow_albedo; Model barrow_model; Mesh barrow_meshes[1U]; + +// Absolute position of the center of the barrow model in the world Vector3 barrow_position; -Vector3 barrow_scale; + +// Dead center of the barrow, used for camera spawn etc +Vector3 barrow_center; + +// Scale factor from image -> model +Vector3 barrow_scale_factor; + +// Scaled dimensions of the barrow, barrow_scale_factor applied +Vector3 barrow_size; + Vector3 barrow_rotation_axis; float barrow_rotation; Matrix barrow_mat; @@ -31,7 +63,7 @@ Texture floor_albedo; Model floor_model; Mesh floor_meshes[1U]; Vector3 floor_position; -Vector3 floor_scale; +Vector3 floor_size; Vector3 floor_rotation_axis; float floor_rotation; @@ -70,9 +102,11 @@ float rotate_speed = 0.8f; float rotate_speed_decay = 0.10f; Vector3 vec3_unit = {1.0f, 1.0f, 1.0f}; +Vector3 vec3_twos = {2.0f, 2.0f, 2.0f}; Vector3 vec3_010 = {0.0f, 1.0f, 0.0f}; Vector3 vec3_up = {0.0f, 1.0f, 0.0f}; Vector3 vec3_down = {0.0f, -1.0f, 0.0f}; +Vector3 vec3_zero = {0.0f, 0.0f, 0.0f}; Color orb_color = BLUE; Color orb_target_color = BLUE; @@ -81,6 +115,7 @@ float orb_intensity = 1.0f; /* TODO: normalized distance from center, but lighti Vector3 orb_directionality = {1.0f, 1.0f, 1.0f}; int map_visible = 0; +int draw_barrow = 1; typedef void(*renderfunc)(); typedef void(*controlfunc)(); @@ -106,8 +141,8 @@ NOTE: Only call inside a Raylib BeginDrawing() block! void drawing_game_mode() { Vector2 from_center_coords = (Vector2){cam.position.x, cam.position.z}; - from_center_coords.x -= barrow_scale.x/2; - from_center_coords.y += barrow_scale.z/2; + from_center_coords.x -= barrow_scale_factor.x/2; + from_center_coords.y += barrow_scale_factor.z/2; /* -PI, PI */ player_angles.y = Vector2Angle(Vector2Normalize(from_center_coords), (Vector2){1.0f, 0.0f}); @@ -116,10 +151,24 @@ void drawing_game_mode() BeginMode3D(cam); BeginShaderMode(shader); - DrawModelEx(barrow_model, barrow_position, barrow_rotation_axis, barrow_rotation, barrow_scale, DARKGRAY); - DrawModelEx(floor_model, floor_position, floor_rotation_axis, floor_rotation, floor_scale, DARKGRAY); + if (draw_barrow) + { + DrawModelEx(floor_model, floor_position, floor_rotation_axis, floor_rotation, floor_size, DARKGRAY); + DrawModelEx(barrow_model, barrow_position, barrow_rotation_axis, barrow_rotation, barrow_size, DARKGRAY); + } + + for (int n = 0; n < room_count; n++) + { + Vector3 each = room_positions[n]; + DrawSphere(each, 0.25, RED); + } - DrawSphere(room_positions[0], 0.25, RED); + for (int n = 0; n < spawn_spoke_count; n++) + { + Ray* each = &spawn_spoke_rays[n]; + //DrawRay(*each, RED); + } + //DrawRay(player_collide_ray, RED); EndShaderMode(); EndMode3D(); @@ -135,13 +184,17 @@ void drawing_game_mode() if(map_visible) { + const int line_height = 32; DrawFPS(fscreen_dims.x - 80, 10); DrawTexturePro(floor_texture, minimap_src, minimap_dest, (Vector2){0.0f, 0.0f}, 0.0f, RAYWHITE); - DrawText(TextFormat("cam x %f\ncam y %f\ncam z %f", cam.position.x, cam.position.y, cam.position.z), 0, 128, 16, GREEN); - DrawText(TextFormat("tgt x %f\ntgt y %f\ntgt z %f", cam.target.x, cam.target.y, cam.target.z), 0, 128+64, 16, GREEN); - DrawText(TextFormat("rot x %f\nrot y %f\nrot z %f", player_angles.x, player_angles.y, player_angles.z), 0, 128+(64*2), 16, GREEN); - DrawText(TextFormat("ray hit %d\nlength %f", player_collision.hit, player_collision.distance), 0, 128+(64*3), 16, GREEN); - DrawText(TextFormat("c x %f\nc y %f\nc z %f", room_positions[0].x, room_positions[0].y, room_positions[0].z), 0, 128+(64*4), 16, GREEN); + DrawText(TextFormat(V3_FORMAT(cam.position), V3_UNROLL(cam.position)), 0, 128, 16, GREEN); + DrawText(TextFormat(V3_FORMAT(cam.target), V3_UNROLL(cam.target)), 0, 128+line_height, 16, GREEN); + DrawText(TextFormat(V3_FORMAT(player_angles), V3_UNROLL(player_angles)), 0, 128+(line_height*2), 16, GREEN); + DrawText(TextFormat("ray hit:%d length %.2f" V3_FORMAT(player_collision.point), player_collision.hit, player_collision.distance, player_collision.point), 0, 128+(line_height*3), 16, GREEN); + DrawText(TextFormat(V3_FORMAT(player_collide_ray.position), V3_UNROLL(player_collide_ray.position), V3_UNROLL(player_collide_ray.direction)), 0, 128+(line_height*4), 16, GREEN); + DrawText(TextFormat(V3_FORMAT(room_positions[0]), V3_UNROLL(room_positions[0])), 0, 128+(line_height*5), 16, GREEN); + DrawText(TextFormat(V3_FORMAT(barrow_position), V3_UNROLL(barrow_position)), 0, 128+(line_height*6), 16, GREEN); + DrawText(TextFormat(V3_FORMAT(barrow_size), V3_UNROLL(barrow_size)), 0, 128+(line_height*7), 16, GREEN); } } @@ -150,7 +203,7 @@ void drawing_resource_ready_mode() static int status_phase = 0; const float font_size = 32.0f; const char format_string[] = "%s\n\n%3.2f%%"; - const char tribute_string[] = "This game is dedicated to the North American Muskrat, Ondatra Zibethicus"; + const char tribute_string[] = "This game is dedicated to the North American Muskrat, Ondatra zibethicus"; ClearBackground(LIGHTGRAY); DrawText(tribute_string, @@ -164,7 +217,7 @@ void drawing_resource_wait_mode() { static int status_phase = 0; const char format_string[] = "%s\n\n%3.2f%%"; - const char tribute_string[] = "This game is dedicated to the North American Muskrat, Ondatra Zibethicus"; + const char tribute_string[] = "This game is dedicated to the North American Muskrat, Ondatra zibethicus"; const char wait_string[] = "Generating resources..."; const int sector_speed = 2U; const int sector_segments = 16; @@ -230,10 +283,19 @@ void control_game_mode() cam.target.y -= DEG2RAD * 20.0; } - map_visible = 0; - if (IsKeyDown(KEY_M)) + if (IsKeyReleased(KEY_M)) + { + map_visible = !map_visible; + } + + if (IsKeyReleased(KEY_N)) { - map_visible = 1; + draw_barrow = !draw_barrow; + } + + if (IsKeyReleased(KEY_Z)) + { + cam.position = barrow_center; } if (IsKeyReleased(KEY_ONE)) @@ -251,13 +313,13 @@ void control_game_mode() player_collision = GetRayCollisionMesh(player_collide_ray, barrow_model.meshes[0U], barrow_mat); player_rotation = new_player_rotation; - if (player_collision.distance >= 0.3f) + if (player_collision.hit && player_collision.distance < 0.89f) { - player_velocity = new_player_velocity; + player_velocity.x *= -1.05; } else { - player_velocity.x *= -1.05; + player_velocity = new_player_velocity; } switch(key) @@ -292,12 +354,18 @@ void wait_initialize_resources() { /* Barrow resource setup. */ { - barrow_position = (Vector3){0.0f, 0.0f, 0.0f}; barrow_image = LoadImage("./barrow.png"); barrow_albedo = LoadTexture("./barrow_albedo.png"); - barrow_scale = (Vector3){barrow_image.width/6.0f, 8.0f, barrow_image.height/6.0f}; + barrow_position = vec3_zero; + barrow_position.y += 3.0f; + barrow_scale_factor = (Vector3){1.0f/8.0f, 8.0f, 1.0f/8.0f}; + barrow_size = Vector3Multiply( + (Vector3){barrow_albedo.width, 1.0f, barrow_albedo.height}, + barrow_scale_factor); barrow_rotation_axis = (Vector3){1.0f, 0.0f, 0.0f}; barrow_rotation = 180.0f; + printf("barrow_position: {%f %f %f}\n", V3_UNROLL(barrow_position)); + printf("barrow_size: {%f %f %f}\n", V3_UNROLL(barrow_size)); while(!IsImageReady(barrow_image)){} while(!IsTextureReady(barrow_albedo)){} @@ -310,8 +378,14 @@ void wait_initialize_resources() barrow_model.materials[0].maps[MATERIAL_MAP_ROUGHNESS].texture = barrow_albedo; barrow_model.materials[0].shader = shader; - room_positions[0] = (Vector3){barrow_scale.x/2.0f, floor_position.y - 1.0f, -barrow_scale.z/2.0f}; - printf("sphere %f %f %f\n", VEC3_UNROLL(room_positions[0])); + room_positions[0] = barrow_position; + printf("sphere %f %f %f\n", V3_UNROLL(room_positions[0])); + for (int n = 1; n < room_count; n++) + { + // Scale room positions down from gentime positions + room_positions[n] = Vector3Multiply(room_positions[n], barrow_scale_factor); + printf("sphere %f %f %f\n", V3_UNROLL(room_positions[n])); + } while(!IsModelReady(barrow_model)){} } @@ -321,10 +395,18 @@ void wait_initialize_resources() floor_image = LoadImage("./floor.png"); floor_albedo = LoadTexture("./floor_albedo.png"); while(!IsImageReady(floor_image)){} - floor_scale = (Vector3){floor_image.width/6.0f, 3.0f, floor_image.height/6.0f}; - floor_position = (Vector3){0.0f, -3.0f, -floor_scale.z}; + + floor_size = barrow_size; + floor_size.y = 3.0f; + + floor_position = barrow_position; + barrow_position.z += barrow_size.z; + barrow_position.y -= 0.5f; + floor_position.y -= barrow_position.y; + floor_rotation_axis = (Vector3){0.0f, 0.0f, floor_image.width}; floor_rotation = 0.0f; + while(!IsTextureReady(floor_albedo)){} floor_texture = LoadTextureFromImage(floor_image); while(!IsTextureReady(floor_texture)){} @@ -340,7 +422,7 @@ void wait_initialize_resources() /* Other resource setup. */ { coffin_scale = (Vector3){1.5f, 1.5f, 1.5f}; - coffin_position = (Vector3){barrow_scale.x/2.0f, floor_position.y, -barrow_scale.z/2.0f}; + coffin_position = (Vector3){barrow_scale_factor.x/2.0f, floor_position.y, -barrow_scale_factor.z/2.0f}; coffin_rotation_axis = (Vector3){0.0f, 0.0f, 0.0f}; coffin_rotation = 0.0f; coffin_model = LoadModel("./assets/coffin_1.glb"); @@ -355,17 +437,45 @@ void wait_initialize_resources() hand_position = (Vector2){0, fscreen_dims.y - (hand_01_image.height * hand_scale)}; hand_rotation = 0.0f; } + + /* Setup Rays for visualizing spawn contact points */ + spawn_spoke_count = room_count; + spawn_spoke_rays = malloc(sizeof(Ray) * spawn_spoke_count); + for (int n = 0; n < spawn_spoke_count; n++) + { + Ray* each = &spawn_spoke_rays[n]; + each->position = room_positions[n+1]; + each->position = Vector3Add(each->position, barrow_position); + each->direction = Vector3Subtract(each->position, cam.position); + printf("Initialized a ray at {%f %f %f} to {%f %f %f}\n", + V3_UNROLL(each->position), V3_UNROLL(each->direction)); + printf("\tfrom room position {%f %f %f}\n", V3_UNROLL(room_positions[n+1])); + } + + /* Grab the full transformation matrix of the upper barrow for use in collision */ + /* Apparently matrix work needs to be normalized. */ + { + Vector3 bt = (Vector3){0.0, -0.3, -1.0f}; + Vector3 bs = barrow_size; + Matrix mat_trans = MatrixTranslate(V3_UNROLL(bt)); + Matrix mat_rot = MatrixRotate(barrow_rotation_axis, DEG2RAD * barrow_rotation); + Matrix mat_scale = MatrixScale(V3_UNROLL(bs)); + + barrow_mat = MatrixMultiply(MatrixMultiply(mat_trans, mat_scale), mat_rot); + } } /* Values and things to do that have to have to be done after Raylib is initialized. */ void initialize_renderer() { + barrow_center = barrow_size; + barrow_center.x /= 2.0f; + barrow_center.z /= 2.0f; + barrow_center.y -= 6.5f; cam = (Camera3D){0}; - cam.position = barrow_position; - cam.position.y -= 0.5f; - cam.position.x = barrow_scale.x/2.0f; - cam.position.z = (-barrow_scale.z/2.0f); - cam.target = (Vector3){barrow_scale.x/2.0f, cam.position.y - 7.0f, cam.position.z/2.0f}; + cam.position = barrow_center; + cam.target = cam.position; + cam.target.x += 3.0f; cam.up = (Vector3){0.0f, 1.0f, 0.0f}; cam.fovy = 90.0f; cam.projection = CAMERA_PERSPECTIVE; @@ -397,7 +507,7 @@ void initialize_prerenderer() { fscreen_dims = (Vector2){.x=screen_dims.x, .y=screen_dims.y}; - map_center = barrow_scale; + map_center = barrow_scale_factor; map_center.x /= 2.0f; map_center.z /= -2.0f; map_center.y = -3.0f; @@ -426,13 +536,9 @@ void start_render_loop() int func_idx = resource_state; player_collide_point = cam.position; - player_collide_point.y -= 1.0f; + //player_collide_point.y -= 1.0f; controlfuncs[func_idx](); - Matrix mat_trans = MatrixTranslate(barrow_position.x, barrow_position.y, barrow_position.z); - Matrix mat_rot = MatrixRotate(barrow_rotation_axis, DEG2RAD * barrow_rotation); - Matrix mat_scale = MatrixScale(barrow_scale.x, barrow_scale.y, barrow_scale.z); - barrow_mat = MatrixMultiply(MatrixMultiply(mat_trans, mat_scale), mat_rot); SetShaderValue(shader, cam_position_shader_loc, &cam.position, SHADER_UNIFORM_VEC3); SetShaderValue(shader, screen_dims_shader_loc, &fscreen_dims, SHADER_UNIFORM_VEC2); diff --git a/src/shaders/lighting.fs b/src/shaders/lighting.fs index fb34caf..f098e28 100644 --- a/src/shaders/lighting.fs +++ b/src/shaders/lighting.fs @@ -132,7 +132,7 @@ void main() { lumWeight = 3.0f; finalColor *= (orb_color * lumWeight); - finalColor.a = 1.5 - easeOutExpo(normal_distance_from_orb); + finalColor.a = (1.5 - easeOutExpo(normal_distance_from_orb)); } lumWeight = min(lumWeight, max_lum); diff --git a/src/structs.c b/src/structs.c index 91b6bda..ea7a137 100644 --- a/src/structs.c +++ b/src/structs.c @@ -84,3 +84,6 @@ int resource_state = 0; const int room_count = 7; Vector3* room_positions = NULL; +int spawn_spoke_count; +Ray* spawn_spoke_rays; + diff --git a/src/structs.h b/src/structs.h index 6e87c1d..0bc2604 100644 --- a/src/structs.h +++ b/src/structs.h @@ -97,6 +97,9 @@ extern int resource_state; extern const int room_count; extern Vector3* room_positions; +extern int spawn_spoke_count; +extern Ray* spawn_spoke_rays; + //int generate_rd(int worker_count, RD_Opts active_opt, FVec2 **grid_buffer, IVec2 pgrid_size); void* generate_rd(void* args); #endif //__RD_STRUCTS__ diff --git a/src/utils.c b/src/utils.c index 2f65636..1bc525e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -150,6 +150,7 @@ int initialize(int worker_count, RD_Opts active_opt) // TODO: One of these will probably have to be mirrored. room_positions[n+1] = (Vector3){.x = seed_x, .y = 0.0f, .z = seed_y}; + printf("Set room %d to {%f %f %f}\n", n+1, VEC3_UNROLL(room_positions[n+1])); grid[seed_x][seed_y].b = 1.0f; grid[seed_x][seed_y].c = 0.0f; -- 2.49.0