From 4ea105ee08f45f0be24a0223b33edd38a58d2482 Mon Sep 17 00:00:00 2001 From: 0scar Date: Tue, 27 Feb 2024 11:04:14 +0100 Subject: Add dithering fragment shader --- resources/dither.frag | 61 ++++++++++++++++++++++++++++++ state_mainstate/src/mainstate.c | 83 ++++++++++++++++++++++++----------------- 2 files changed, 110 insertions(+), 34 deletions(-) create mode 100644 resources/dither.frag diff --git a/resources/dither.frag b/resources/dither.frag new file mode 100644 index 0000000..6082ceb --- /dev/null +++ b/resources/dither.frag @@ -0,0 +1,61 @@ +#version 330 core + +const float pR = 0.299; +const float pG = 0.587; +const float pB = 0.114; + +const vec3 p = vec3(pR, pG, pB); + +float dithering_matrix[64]; + +in vec2 UV; +in vec4 gl_FragCoord; +//in vec2 gl_fragCoord; +out vec4 color; + +uniform sampler2D textureSampler; + +void main() { + // Normalized pixel coordinates (from 0 to 1) + vec2 uv = UV; //iTime + (fragCoord.xy / iResolution.xy); + vec2 fragPos = gl_FragCoord.xy; + + vec3 col = texture(textureSampler, UV).rgb; + + vec3 colsqrt = col * col; + float luminance = sqrt(dot(p, colsqrt)); + + dithering_matrix = float[]( + 0.0, 48.0, 12.0, 60.0, 3.0, 51.0, 15.0, 61.0, + 32.0, 16.0, 44.0, 28.0, 35.0, 19.0, 47.0, 31.0, + 8.0, 56.0, 4.0, 52.0, 11.0, 59.0, 7.0, 55.0, + 40.0, 24.0, 36.0, 20.0, 43.0, 27.0, 39.0, 23.0, + 2.0, 50.0, 14.0, 62.0, 1.0, 49.0, 13.0, 61.0, + 34.0, 18.0, 46.0, 30.0, 33.0, 17.0, 45.0, 29.0, + 10.0, 58.0, 6.0, 54.0, 9.0, 57.0, 5.0, 53.0, + 42.0, 26.0, 38.0, 22.0, 41.0, 25.0, 37.0, 21.0 + ); + + /* BEGIN PASTE */ + int x = int(mod(fragPos.x, 8.0)); + int y = int(mod(fragPos.y, 8.0)); + + // index of flat matrix + int idx = (x * 8) + y; + float dither = dithering_matrix[idx]; + // normalize the value + dither *= (1.0 / 64.0); + + /* END PASTE */ + + // Output to screen + if (luminance > dither) { + color = vec4(col, 1.0); + } else { + color = vec4( + float(0x10) / 255.f, float(0x0a) / 255.f, float(0x33) / 255.f, 1.f + ); + + //color = vec4(0.0); + } +} diff --git a/state_mainstate/src/mainstate.c b/state_mainstate/src/mainstate.c index 48b3e4b..70a3418 100644 --- a/state_mainstate/src/mainstate.c +++ b/state_mainstate/src/mainstate.c @@ -25,19 +25,32 @@ enum GameResources { MyDitherShader, MyTexture, MyGrass, + MyStone, }; static const f32 speed = 128.f; static const f32 crate_texture_coords[36*2]; -static vec3 positions[] = { - {0,0,0}, - {2,0,0}, - {4,0,0}, - {2,0,2}, - {6,2,0}, - {4,2,2}, - {6,4,0}, - {4,4,2}, +static vec3 positions[16*16]; +static vec3 thing[] = { + {2,2,4},{2,4,4}, + {4,2,4},{4,4,4}, + {6,2,4},{6,4,4}, + {8,2,4}, + {10,2,4}, + {12,2,4}, + {14,2,4}, + {16,2,4}, + + {2,2,6}, {2,4,6}, + {2,2,8}, {2,4,8}, + {2,2,10},{2,4,10}, + {2,2,12}, + {2,2,14}, + //{24,2,4}, + //{26,2,4}, + //{28,2,4}, + //{30,2,4}, + //{32,2,4}, }; #define ACCELERATE( x, y, z ) glm_vec3_add((vec3){x, y, z}, s->cam_acc, s->cam_acc) @@ -49,15 +62,7 @@ void move_cam_fwd(mainstate_state *s) { ACCELERATE( 0, 0, -spe void move_cam_fwd_stop(mainstate_state *s) { ACCELERATE( 0, 0, +speed ); } void move_cam_bck(mainstate_state *s) { ACCELERATE( 0, 0, speed ); } void move_cam_bck_stop(mainstate_state *s) { ACCELERATE( 0, 0, -speed ); } -// -//void move_cam_left(mainstate_state *s) { ACCELERATE(-speed/2.f, 0, speed/2.f); } -//void move_cam_left_stop(mainstate_state *s) { ACCELERATE(+speed/2.f, 0, -speed/2.f); } -//void move_cam_right(mainstate_state *s) { ACCELERATE( speed/2.f, 0, -speed/2.f); } -//void move_cam_right_stop(mainstate_state *s) { ACCELERATE(-speed/2.f, 0, speed/2.f); } -//void move_cam_fwd(mainstate_state *s) { ACCELERATE(-speed, 0, -speed ); } -//void move_cam_fwd_stop(mainstate_state *s) { ACCELERATE(+speed, 0, +speed ); } -//void move_cam_bck(mainstate_state *s) { ACCELERATE( speed, 0, speed ); } -//void move_cam_bck_stop(mainstate_state *s) { ACCELERATE(-speed, 0, -speed ); } + void move_cam_up(mainstate_state *s) { ACCELERATE( 0, speed, 0); } void move_cam_up_stop(mainstate_state *s) { ACCELERATE( 0, -speed, 0); } void move_cam_dwn(mainstate_state *s) { ACCELERATE( 0, -speed, 0); } @@ -140,6 +145,7 @@ void mainstate_init(mainstate_state *state, void* arg) { dither_shader, sizeof(dither_shader) / sizeof(dither_shader[0])), [MyTexture] = Declare_Texture("resources/texture.png"), [MyGrass] = Declare_Texture("resources/grass.png"), + [MyStone] = Declare_Texture("resources/stone.png"), }; /// Setup the camera @@ -222,7 +228,7 @@ void mainstate_init(mainstate_state *state, void* arg) { // Vertices crate, // Shader - get_asset(&state->resources, MyDitherShader), + get_asset(&state->resources, MyDefaultShader), // Sizeof Vertices sizeof(crate), // UV & UV size @@ -244,6 +250,20 @@ void mainstate_init(mainstate_state *state, void* arg) { ((Texture*)get_asset(&state->resources, MyTexture))->id ); + state->objects[2] = RenderObject_new( + // Vertices + crate, + // Shader + get_asset(&state->resources, MyDefaultShader), + // Sizeof Vertices + sizeof(crate), + // UV & UV size + (f32*)crate_texture_coords, sizeof(crate_texture_coords), + // Texture + ((Texture*)get_asset(&state->resources, MyStone))->id + ); + + // Setup controls state->input_bindings[ 0] = BindState(/*'A'*/ 38, 0, move_cam_left, move_cam_left_stop); state->input_bindings[ 1] = BindState(/*'D'*/ 40, 0, move_cam_right, move_cam_right_stop); @@ -265,6 +285,13 @@ void mainstate_init(mainstate_state *state, void* arg) { WARN("Number of bindings: %lu", state->input_ctx.len); i_ctx_push(&state->input_ctx); + for (usize i = 0; i < sizeof(positions) / sizeof(positions[0]); i++) { + usize x = (i / 16) * 2; + usize y = (i % 16) * 2; + positions[i][0] = (float)y; + positions[i][1] = 0; + positions[i][2] = (float)x; + } //exit(EXIT_SUCCESS); } @@ -278,6 +305,9 @@ StateType mainstate_update(f64 dt, mainstate_state *state) { for (usize i = 0; i < sizeof(positions) / sizeof(positions[0]); i++) { engine_draw_model(&(state->objects[0]), positions[i]); } + for (usize i = 0; i < sizeof(thing) / sizeof(thing[0]); i++) { + engine_draw_model(&(state->objects[2]), thing[i]); + } // Move the camera // ... all of this should be easily selectable in the engine @@ -311,17 +341,8 @@ StateType mainstate_update(f64 dt, mainstate_state *state) { t[0] += dir[0] * speed[2]; t[2] -= dir[0] * speed[0]; - //state->cam_pos[0] += t[0]; - //state->cam_pos[2] -= t[2]; - glm_vec3_add(state->cam_pos, t, state->cam_pos); - INFO(" > speed: %.1f %.1f %.1f", - t[0], - t[1], - t[2]); - INFO(""); - glm_vec3_copy(state->cam_pos, state->c.pos); glm_vec3_scale(state->cam_speed, 0.85, state->cam_speed); @@ -341,12 +362,6 @@ StateType mainstate_update(f64 dt, mainstate_state *state) { a[2] = a[2] * (1.0 - f) + f*b[2]; glm_vec3_copy(a, state->c.dir); - //INFO("rotation[%.2f] (%.1f): %.1f %.1f %.1f", - // state->cam_dir_dt, - // f, - // state->c.dir[0], - // state->c.dir[1], - // state->c.dir[2]); } //delay(50); -- cgit v1.3