]> git.mcshandy.xyz Git - barrow_crawler/commitdiff
Introduce simple shaders for messing with the map and lighting, and some cleanup
authorRandy McShandy <randy@mcshandy.xyz>
Wed, 8 May 2024 02:51:46 +0000 (21:51 -0500)
committerRandy McShandy <randy@mcshandy.xyz>
Wed, 8 May 2024 02:51:46 +0000 (21:51 -0500)
src/external/stb_image_resize2.h [moved from src/stb_image_resize2.h with 100% similarity]
src/external/stb_image_write.h [moved from src/stb_image_write.h with 100% similarity]
src/main.c
src/render_raylib.c
src/shaders/lighting.fs [new file with mode: 0644]
src/shaders/lighting.vs [new file with mode: 0644]
src/utils.c

index 5f3a1cb15d4bb3533c0ae7cf2ac38b2b8f974435..7f7d7d063bddb48b3b924bc0093c3eb151eba9f3 100755 (executable)
@@ -22,7 +22,9 @@ int main(int argc, char** argv)
        }
        barrow.max_iterations *= 1;
 
+#ifdef ENABLE_BARROWGEN
        generate_rd(8, barrow, grid, grid_size);
+#endif
        start_render_loop();
 
        return 0;
index efe8f7b6c666f3d4a44e08cbf870785620ab6f5f..0f0fab28fe8e24786f93c74886b02129af98ef0a 100644 (file)
@@ -1,16 +1,15 @@
 #include <raylib.h>
 #include <raymath.h>
-#include <stdio.h>
-#include <string.h>
+
 #include "structs.h"
+#include "external/rlights.h"
 
+Vector2 fscreen_dims;
 Camera3D cam;
 
 Image barrow_image;
-
 Texture barrow_texture;
 Texture albedo_texture;
-
 Model barrow_model;
 Mesh barrow_meshes[1U];
 Material barrow_material;
@@ -18,6 +17,10 @@ Matrix barrow_transform;
 Vector3 barrow_position;
 Vector3 barrow_scale;
 
+Shader shader;
+int cam_position_shader_loc;
+int screen_dims_shader_loc;
+
 Vector3 player_velocity;
 Vector3 player_rotation;
 
@@ -64,6 +67,15 @@ void process_inputs()
 
 void initialize_renderer()
 {
+       fscreen_dims = (Vector2){.x=screen_dims.x, .y=screen_dims.y};
+
+       shader = LoadShader("./src/shaders/lighting.vs", "./src/shaders/lighting.fs");
+       shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
+       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");
+       while(!IsShaderReady(shader)){}
+
        //barrow_position = (Vector3){-8.0f, 0.0f, -8.0f};
        barrow_position = (Vector3){-8.0f, 0.0f, -8.0f};
        barrow_image = LoadImage("./barrow.png");
@@ -81,12 +93,13 @@ void initialize_renderer()
        barrow_model = LoadModelFromMesh(barrow_meshes[0]);
        barrow_model.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = albedo_texture;
        barrow_model.materials[0].maps[MATERIAL_MAP_ROUGHNESS].texture = albedo_texture;
+       barrow_model.materials[0].shader = shader;
 
        cam = (Camera3D){0};
        cam.position    = barrow_position;
-       cam.position.y += 3.0f;
+       cam.position.y += 2.0f;
        cam.position.x = barrow_scale.x/2 + barrow_position.x;
-       cam.target              = (Vector3){0.0f, cam.position.y * 0.66f, 0.0f};
+       cam.target              = (Vector3){0.0f, cam.position.y * 0.1, 0.0f};
        cam.up                          = (Vector3){0.0f, 1.0f, 0.0f};
        cam.fovy                        = 75.0f;
        cam.projection= CAMERA_PERSPECTIVE;
@@ -96,7 +109,7 @@ void initialize_renderer()
 
 void start_render_loop()
 {
-       InitWindow(screen_dims.x, screen_dims.y, screen_title);
+       InitWindow(fscreen_dims.x, fscreen_dims.y, screen_title);
        initialize_renderer();
 
        Rectangle minimap_dest = {.width = 64.0f*img_export_scale.x, .height = 64.0f*img_export_scale.y, .x = 0.0f, .y = 0.0f};
@@ -106,13 +119,17 @@ 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);
 
                UpdateCameraPro(&cam, player_velocity, player_rotation, 0.0f);
                BeginDrawing();
                        ClearBackground(BLACK);
 
                        BeginMode3D(cam);
+                       BeginShaderMode(shader);
                                DrawModel(barrow_model, barrow_position, 1.0f, DARKGRAY);
+                       EndShaderMode();
                        EndMode3D();
 
                        DrawTexturePro(barrow_texture, minimap_src, minimap_dest, (Vector2){0.0f, 0.0f}, 0.0f, RAYWHITE);
@@ -126,6 +143,7 @@ void start_render_loop()
 
        UnloadImage(barrow_image);
        UnloadTexture(barrow_texture);
+       UnloadShader(shader);
        CloseWindow();
 }
 
diff --git a/src/shaders/lighting.fs b/src/shaders/lighting.fs
new file mode 100644 (file)
index 0000000..1490625
--- /dev/null
@@ -0,0 +1,104 @@
+#version 330
+
+// Input vertex attributes (from vertex shader)
+in vec2 fragTexCoord;
+in vec4 fragColor;
+in vec4 vertexCoord;
+
+// Input uniform values
+uniform sampler2D texture0;
+uniform vec4 colDiffuse;
+uniform vec3 cam_position;
+uniform vec2 screen_dims;
+
+// Output fragment color
+out vec4 finalColor;
+
+const vec3 v3_unit = vec3(1.0f, 1.0f, 1.0f);
+float lumWeight = 0.0f;
+
+// A single iteration of Bob Jenkins' One-At-A-Time hashing algorithm.
+uint hash( uint x ) {
+    x += ( x << 10u );
+    x ^= ( x >>  6u );
+    x += ( x <<  3u );
+    x ^= ( x >> 11u );
+    x += ( x << 15u );
+    return x;
+}
+
+// Compound versions of the hashing algorithm I whipped together.
+uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y)                         ); }
+uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z)             ); }
+uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }
+
+// Construct a float with half-open range [0:1] using low 23 bits.
+// All zeroes yields 0.0, all ones yields the next smallest representable value below 1.0.
+float floatConstruct( uint m ) {
+    const uint ieeeMantissa = 0x007FFFFFu; // binary32 mantissa bitmask
+    const uint ieeeOne      = 0x3F800000u; // 1.0 in IEEE binary32
+
+    m &= ieeeMantissa;                     // Keep only mantissa bits (fractional part)
+    m |= ieeeOne;                          // Add fractional part to 1.0
+
+    float  f = uintBitsToFloat( m );       // Range [1:2]
+    return f - 1.0;                        // Range [0:1]
+}
+
+// Pseudo-random value in half-open range [0:1].
+float random( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }
+float random( vec2  v ) { return floatConstruct(hash(floatBitsToUint(v))); }
+float random( vec3  v ) { return floatConstruct(hash(floatBitsToUint(v))); }
+float random( vec4  v ) { return floatConstruct(hash(floatBitsToUint(v))); }
+
+float easeInOutQuart(float x)
+{
+       return x < 0.5 ? 8 * x * x * x * x : 1 - pow(-2 * x + 2, 4) / 2;
+}
+float easeOutQuint(float x)
+{
+       return 1.0f - pow(1.0 - x, 5);
+}
+
+float easeInExpo(float x)
+{
+       return (x == 0.0) ? 0.0 : pow(2, 10 * x - 10);
+}
+
+float easeInCirc(float x)
+{
+       return 1 - sqrt(1 - pow(x, 2));
+}
+
+float easeInQuart(float x)
+{
+       return x * x * x * x;
+}
+
+void main()
+{
+    // Texel color fetching from texture sampler
+    vec4 texelColor = texture(texture0, fragTexCoord);
+
+    // NOTE: Implement here your fragment shader code
+
+               const float max_dist = 6.0f;
+               const float max_lum = 0.7f;
+               float cam_dist_z = gl_FragCoord.z/gl_FragCoord.w;
+               float norm_dist_z = cam_dist_z/max_dist;
+
+    finalColor = texelColor*colDiffuse;
+
+               if(cam_dist_z < max_dist)
+               {
+                       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));
+                       }
+
+               vec3 lumWeights = v3_unit * lumWeight;
+               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;
+}
diff --git a/src/shaders/lighting.vs b/src/shaders/lighting.vs
new file mode 100644 (file)
index 0000000..9af145c
--- /dev/null
@@ -0,0 +1,61 @@
+#version 330
+
+in vec3 vertexPosition;
+in vec2 vertexTexCoord;
+in vec3 vertexNormal;
+in vec4 vertexColor;
+
+// Input uniform values
+uniform vec3 cam_position;
+uniform mat4 mvp;
+uniform vec3 viewPos;
+uniform vec2 screen_dims;
+
+// Output vertex attributes (to fragment shader)
+out vec2 fragTexCoord;
+out vec4 fragColor;
+out vec4 vertexCoord;
+
+// A single iteration of Bob Jenkins' One-At-A-Time hashing algorithm.
+uint hash( uint x ) {
+    x += ( x << 10u );
+    x ^= ( x >>  6u );
+    x += ( x <<  3u );
+    x ^= ( x >> 11u );
+    x += ( x << 15u );
+    return x;
+}
+
+// Compound versions of the hashing algorithm I whipped together.
+uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y)                         ); }
+uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z)             ); }
+uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }
+
+// Construct a float with half-open range [0:1] using low 23 bits.
+// All zeroes yields 0.0, all ones yields the next smallest representable value below 1.0.
+float floatConstruct( uint m ) {
+    const uint ieeeMantissa = 0x007FFFFFu; // binary32 mantissa bitmask
+    const uint ieeeOne      = 0x3F800000u; // 1.0 in IEEE binary32
+
+    m &= ieeeMantissa;                     // Keep only mantissa bits (fractional part)
+    m |= ieeeOne;                          // Add fractional part to 1.0
+
+    float  f = uintBitsToFloat( m );       // Range [1:2]
+    return f - 1.0;                        // Range [0:1]
+}
+
+// Pseudo-random value in half-open range [0:1].
+float random( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }
+float random( vec2  v ) { return floatConstruct(hash(floatBitsToUint(v))); }
+float random( vec3  v ) { return floatConstruct(hash(floatBitsToUint(v))); }
+float random( vec4  v ) { return floatConstruct(hash(floatBitsToUint(v))); }
+
+void main()
+{
+       fragTexCoord = vertexTexCoord;
+       fragColor = vertexColor;
+
+       gl_Position = mvp * vec4(vertexPosition, 1.0);
+       gl_Position.y = gl_Position.y + (random(vertexPosition)/(4.0));
+       vertexCoord = gl_Position;
+}
index 82dee02af927e5a9ba0058fce4b79bc0bf70ca2a..9771659e34d252d0279a570bb5d7319b1bb7c6e2 100644 (file)
@@ -10,9 +10,9 @@
 #include "platform.h"
 
 #define STB_IMAGE_WRITE_IMPLEMENTATION
-#include "stb_image_write.h"
+#include "external/stb_image_write.h"
 #define STB_IMAGE_RESIZE_IMPLEMENTATION
-#include "stb_image_resize2.h"
+#include "external/stb_image_resize2.h"
 
 #define SHADOW 1
 #if SHADOW