]> git.mcshandy.xyz Git - barrow_crawler/commitdiff
Overhauled lighting again lmao, also trying to render another model but apparently...
authorRandy McShandy <randy@mcshandy.xyz>
Thu, 16 May 2024 02:24:04 +0000 (21:24 -0500)
committerRandy McShandy <randy@mcshandy.xyz>
Thu, 16 May 2024 02:24:04 +0000 (21:24 -0500)
assets/coffin_1.blend [new file with mode: 0644]
assets/coffin_1.glb [new file with mode: 0644]
src/platforms/platform_posix.c
src/render_raylib.c
src/shaders/lighting.fs
src/shaders/lighting.vs

diff --git a/assets/coffin_1.blend b/assets/coffin_1.blend
new file mode 100644 (file)
index 0000000..3c4ff95
Binary files /dev/null and b/assets/coffin_1.blend differ
diff --git a/assets/coffin_1.glb b/assets/coffin_1.glb
new file mode 100644 (file)
index 0000000..f87fa46
Binary files /dev/null and b/assets/coffin_1.glb differ
index 799e8371320161d337abb45d1769db7bc0137141..3550db51c78236d188ac5b7e66784f407987f085 100644 (file)
@@ -103,3 +103,4 @@ int cleanup()
 
        return 0;
 }
+
index 835ed067d940fbfc6438128e4e468e12b9b36418..cde9047bf6ab8e02d5ee36c52ff286cd6e9c983c 100644 (file)
@@ -26,19 +26,29 @@ Vector3 floor_scale;
 Vector3 floor_rotation_axis;
 float floor_rotation;
 
+Model          coffin_model;
+Vector3 coffin_position;
+Vector3 coffin_scale;
+Vector3 coffin_rotation_axis;
+float  coffin_rotation;
+int coffin_material_count;
+
 Shader shader;
 int cam_position_shader_loc;
 int screen_dims_shader_loc;
+int map_center_shader_loc;
 
 Vector3 player_velocity;
 Vector3 player_rotation;
+Vector3 map_center;
 
 float forward_speed = 0.015f;
 float forward_speed_decay = 0.05f;
 float rotate_speed = 0.8f;
 float rotate_speed_decay = 0.10f;
 
-Vector3 unit_vec3 = {1.0f, 1.0f, 1.0f};
+Vector3 vec3_unit = {1.0f, 1.0f, 1.0f};
+Vector3 vec3_010       = {0.0f, 1.0f, 0.0f};
 
 void process_inputs()
 {
@@ -76,6 +86,15 @@ void process_inputs()
                        cam.target.y -= DEG2RAD * 20.0;
                }
 
+               if (IsKeyDown(KEY_R))
+               {
+                       player_velocity.z += 0.005f;
+               }
+               else if (IsKeyDown(KEY_F))
+               {
+                       player_velocity.z -= 0.005f;
+               }
+
                if (IsKeyDown(KEY_SPACE) || IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT))
                {
                        //player_velocity.x *= 2.0f;
@@ -103,6 +122,7 @@ void initialize_renderer()
        shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
        cam_position_shader_loc = GetShaderLocation(shader, "cam_position");
        screen_dims_shader_loc = GetShaderLocation(shader, "screen_dims");
+       map_center_shader_loc = GetShaderLocation(shader, "map_center");
        while(!IsShaderReady(shader)){}
 
        barrow_position = (Vector3){0.0f, 0.0f, 0.0f};
@@ -112,6 +132,11 @@ void initialize_renderer()
        barrow_rotation_axis = (Vector3){1.0f, 0.0f, 0.0f};
        barrow_rotation = 180.0f;
 
+       map_center = barrow_scale;
+       map_center.x /= 2.0f;
+       map_center.z /= -2.0f;
+       map_center.y = -2.0f;
+
        while(!IsImageReady(barrow_image)){}
        barrow_texture = LoadTextureFromImage(barrow_image);
 
@@ -129,18 +154,28 @@ void initialize_renderer()
        while(!IsTextureReady(floor_albedo)){}
 
        /* Might want to replace barrow_scale with just img dimensions. */
-       barrow_meshes[0U] = GenMeshHeightmap(barrow_image, unit_vec3);
+       barrow_meshes[0U] = GenMeshHeightmap(barrow_image, vec3_unit);
        barrow_model = LoadModelFromMesh(barrow_meshes[0]);
        barrow_model.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = barrow_albedo;
        barrow_model.materials[0].maps[MATERIAL_MAP_ROUGHNESS].texture = barrow_albedo;
        barrow_model.materials[0].shader = shader;
 
-       floor_meshes[0U] = GenMeshHeightmap(floor_image, unit_vec3);
+       floor_meshes[0U] = GenMeshHeightmap(floor_image, vec3_unit);
        floor_model = LoadModelFromMesh(floor_meshes[0]);
        floor_model.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = floor_albedo;
        floor_model.materials[0].maps[MATERIAL_MAP_ROUGHNESS].texture = floor_albedo;
        floor_model.materials[0].shader = shader;
 
+       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_rotation_axis = (Vector3){0.0f, 0.0f, 0.0f};
+       coffin_rotation = 0.0f;
+       coffin_model = LoadModel("./assets/coffin_1.glb");
+       while(!IsModelReady(coffin_model)){}
+       //coffin_model.materials[0] = *LoadMaterials("./assets/coffin_1.mtl", &coffin_material_count);
+       //while(!IsMaterialReady(coffin_model.materials[0])){}
+       coffin_model.materials[0].shader = shader;
+
        cam                                             = (Camera3D){0};
        cam.position            = barrow_position;
        cam.position.y -= 0.5f;
@@ -168,19 +203,21 @@ void start_render_loop()
        while (!WindowShouldClose())
        {
                process_inputs();
+
                SetShaderValue(shader, cam_position_shader_loc, &cam.position, SHADER_UNIFORM_VEC3);
                SetShaderValue(shader, screen_dims_shader_loc, &fscreen_dims, SHADER_UNIFORM_VEC2);
+               SetShaderValue(shader, map_center_shader_loc, &map_center, SHADER_UNIFORM_VEC3);
 
                UpdateCameraPro(&cam, player_velocity, player_rotation, 0.0f);
-
                BeginDrawing();
                        ClearBackground(BLACK);
 
                        BeginMode3D(cam);
                        BeginShaderMode(shader);
-                               DrawGrid(barrow_scale.x, 1.0f);
+                               DrawModelEx(coffin_model, coffin_position, coffin_rotation_axis, coffin_rotation, coffin_scale, DARKGRAY);
                                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);
+                               //DrawSphere(map_center, 1.0f, RED);
                        EndShaderMode();
                        EndMode3D();
 
index 03eb20be5dbd816a0168559a747911bf7c647915..c744bc65ce78bcab61944142554edba58b8829e5 100644 (file)
@@ -1,22 +1,26 @@
 #version 330
-asdfasdf
 
 // Input vertex attributes (from vertex shader)
 in vec2 fragTexCoord;
 in vec4 fragColor;
-in vec4 vertexCoord;
+in vec3 fragPosition;
 
 // Input uniform values
 uniform sampler2D texture0;
 uniform vec4 colDiffuse;
 uniform vec3 cam_position;
 uniform vec2 screen_dims;
+uniform vec3 map_center;
 
-adasd
 // Output fragment color
 out vec4 finalColor;
 
 const vec3 v3_unit = vec3(1.0f, 1.0f, 1.0f);
+const float max_lum = 0.8f;
+const float max_lit_distance = 6.5f;
+const float max_center_distance = 8.0f;
+//const vec4 candle_color = vec4(255.0f/255.0f, 0.001f/255.0f, 0.001f/255.0f, 255.0f/255.0f);
+const vec4 candle_color = vec4(228.0f/255.0f, 103.0f/255.0f, 1.0f/255.0f, 255.0f/255.0f);
 float lumWeight = 0.0f;
 
 const vec2 candle_pos = vec2(0.5, 0.5);
@@ -69,6 +73,11 @@ float easeInExpo(float x)
        return (x == 0.0) ? 0.0 : pow(2, 10 * x - 10);
 }
 
+float easeOutExpo(float x)
+{
+       return 1.0f - easeInExpo(x);
+}
+
 float easeInCirc(float x)
 {
        return 1 - sqrt(1 - pow(x, 2));
@@ -79,43 +88,50 @@ float easeInQuart(float x)
        return x * x * x * x;
 }
 
+float easeOutQuad(float x)
+{
+       return 1.0f - (1.0f - x) * (1.0f - x);
+}
+
+float easeInQuad(float x)
+{
+       return (1.0f - x) * (1.0f - x);
+}
+
 void main()
 {
     // Texel color fetching from texture sampler
     vec4 texelColor = texture(texture0, fragTexCoord);
-
-               const float max_dist = 7.5f;
-               const float lit_dist = 2.5f;
-               const float max_lum = 1.0f;
-               float cam_dist_z = gl_FragCoord.z/gl_FragCoord.w;
-               float norm_dist_z = (cam_dist_z/max_dist);
-
-               vec2 uv = gl_FragCoord.xy/screen_dims;
+               vec4 lighting_color = texelColor*colDiffuse;
+               float camera_distance = distance(fragPosition, cam_position);
+               float normalized_lit_distance = camera_distance/max_lit_distance;
+               float center_distance = distance(fragPosition, map_center);
+               float normalized_center_distance = center_distance/max_center_distance;
+               float candle_weight = 0.0f;
+               vec4 lumWeights = vec4(1.0f);
 
     finalColor = texelColor*colDiffuse;
 
-               if(cam_dist_z < max_dist)
+               if(normalized_center_distance <= 1.0f)
                {
-                       float norm_x_dist = abs(gl_FragCoord.x - (screen_dims.x/2))/(screen_dims.x/2);
-                       lumWeight = min(max_lum, 1.0f - easeInQuart(norm_dist_z));
+                       lumWeight = easeInQuad(normalized_center_distance) + 0.9f;
                }
-               vec3 lumWeights = v3_unit * lumWeight;
-
-               // All single color channel variations with this looks cool as FUCK
-               // turns this into three levels going b-r-g
-               float norm_y_dist = uv.y;
-               finalColor.b = 0.0f;
 
-               if(cam_dist_z < lit_dist)
+               if(normalized_lit_distance <= 1.0f)
                {
-                       float norm_x_dist = abs(gl_FragCoord.x - (screen_dims.x/2))/(screen_dims.x/2);
-                       lumWeights.r = 1.8f * (1.0f - norm_dist_z);
+                       lumWeight = max(lumWeight, min(0.5f, easeOutExpo(normalized_lit_distance)));
+                       candle_weight = 1.0f - normalized_lit_distance;
                }
 
-               float lum = dot(finalColor.rgb, lumWeights);
-               float rand = random(fragTexCoord);
-               lumWeights *= lum;
-               finalColor.rgb = mix(vec3(227/255, 60/255, 0/255), finalColor.rgb, lumWeights);
-               finalColor.a = 1.0f;
+               lumWeight = min(lumWeight, max_lum);
+               lumWeights = vec4(1.0f) * lumWeight;
+               lumWeights *= dot(finalColor, lumWeights);
+
+               lighting_color = vec4(mix(candle_color.rgb * candle_weight, lumWeights.rgb, 0.2f), 1.0f);
+               finalColor = finalColor * lighting_color;
+
+               // All single color channel variations with this looks cool as FUCK
+               // turns this into three levels going b-r-g
+               finalColor.b = 0.0f;
 
 }
index 9af145c6d9f02ad2d8383817e1e81cd4f16f812a..bc29dc5838fc69e4b35b30a060b2c3f5f27d5d09 100644 (file)
@@ -10,11 +10,13 @@ uniform vec3 cam_position;
 uniform mat4 mvp;
 uniform vec3 viewPos;
 uniform vec2 screen_dims;
+uniform vec3 map_center;
+uniform mat4 matModel;
 
 // Output vertex attributes (to fragment shader)
 out vec2 fragTexCoord;
 out vec4 fragColor;
-out vec4 vertexCoord;
+out vec3 fragPosition;
 
 // A single iteration of Bob Jenkins' One-At-A-Time hashing algorithm.
 uint hash( uint x ) {
@@ -57,5 +59,5 @@ void main()
 
        gl_Position = mvp * vec4(vertexPosition, 1.0);
        gl_Position.y = gl_Position.y + (random(vertexPosition)/(4.0));
-       vertexCoord = gl_Position;
+       fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
 }