]> git.mcshandy.xyz Git - barrow_crawler/commitdiff
Overhaul lots of positioning math to hopefully simplify future placements, and then...
authorRandy McShandy <randy@mcshandy.xyz>
Wed, 16 Oct 2024 03:01:28 +0000 (22:01 -0500)
committerRandy McShandy <randy@mcshandy.xyz>
Wed, 16 Oct 2024 03:01:28 +0000 (22:01 -0500)
src/main.c
src/render_raylib.c
src/shaders/lighting.fs
src/structs.c
src/structs.h
src/utils.c

index bf43b6455a4b0d372f70a07537dcc4628c819d8c..187ec7ad71e8fab41d52d393d5d20f31a23a7c03 100755 (executable)
@@ -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};
index 51210ce730b3fd6e7b1cab431aa05b3f3128033f..b5b19c42e4034a5a4b0dfc87b19f79b12ec57c77 100644 (file)
 
 #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);
index fb34caf0d01708699e632e1fdfea9c95904f7a05..f098e287fbc8dc1fa1686f49745ba8038fba9433 100644 (file)
@@ -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);
index 91b6bdab07a5ff9e1fcf534e65b79260824efb19..ea7a137d755f153f036df9936d3ecfef81b3fb2d 100644 (file)
@@ -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;
+
index 6e87c1da2452b4bbb0ab4ef549759c2c2cc0ebad..0bc2604d93af25e515167f61cadc4387393a78df 100644 (file)
@@ -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__
index 2f65636bf720d18771c024e9f3f7b310e20c48bc..1bc525e85479bd39ec97adf64f2b6229fa19c3bd 100644 (file)
@@ -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;