From: Randy McShandy Date: Wed, 8 May 2024 02:51:46 +0000 (-0500) Subject: Introduce simple shaders for messing with the map and lighting, and some cleanup X-Git-Url: http://git.mcshandy.xyz/gitweb.cgi?a=commitdiff_plain;h=d1824cdfc9e8d5de5079277e72cf43280359974a;p=barrow_crawler Introduce simple shaders for messing with the map and lighting, and some cleanup --- diff --git a/src/stb_image_resize2.h b/src/external/stb_image_resize2.h similarity index 100% rename from src/stb_image_resize2.h rename to src/external/stb_image_resize2.h diff --git a/src/stb_image_write.h b/src/external/stb_image_write.h similarity index 100% rename from src/stb_image_write.h rename to src/external/stb_image_write.h diff --git a/src/main.c b/src/main.c index 5f3a1cb..7f7d7d0 100755 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/render_raylib.c b/src/render_raylib.c index efe8f7b..0f0fab2 100644 --- a/src/render_raylib.c +++ b/src/render_raylib.c @@ -1,16 +1,15 @@ #include #include -#include -#include + #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 index 0000000..1490625 --- /dev/null +++ b/src/shaders/lighting.fs @@ -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 index 0000000..9af145c --- /dev/null +++ b/src/shaders/lighting.vs @@ -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; +} diff --git a/src/utils.c b/src/utils.c index 82dee02..9771659 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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