From f2ddde88a80334dd90b819d2e8a009b25e15e4d9 Mon Sep 17 00:00:00 2001 From: Able Date: Tue, 29 Mar 2022 05:58:46 -0500 Subject: [PATCH] Shader Work --- assets/GrassTop.png | Bin 0 -> 123 bytes assets/shade.frag | 10 +++++++++- assets/shade.vert | 8 ++++++++ assets/shaders/grass/grass.frag | 4 ++++ assets/shaders/grass/grass.vert | 4 ++++ assets/shaders/water/water.frag | 4 ++++ assets/shaders/water/water.vert | 4 ++++ src/main.rs | 32 +++++++++++++++++++++++--------- 8 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 assets/GrassTop.png create mode 100644 assets/shaders/grass/grass.frag create mode 100644 assets/shaders/grass/grass.vert create mode 100644 assets/shaders/water/water.frag create mode 100644 assets/shaders/water/water.vert diff --git a/assets/GrassTop.png b/assets/GrassTop.png new file mode 100644 index 0000000000000000000000000000000000000000..e53b61bab084ceac5b15087770b38e2067311ce0 GIT binary patch literal 123 zcmeAS@N?(olHy`uVBq!ia0vp^93afW1|*O0@9PFq4xTQKArj%qQ+$(u{&$`-v+?oQ zc(#(L6K=B_1GmijYj5T`Bgw5stwChri~|fT3YQ%kY=k%LWX)kcld-6_aSbEGr_M-g WtAj~vfo3syy85}Sb4qApU;qG;At&bm literal 0 HcmV?d00001 diff --git a/assets/shade.frag b/assets/shade.frag index 968b39f..a09cc1a 100644 --- a/assets/shade.frag +++ b/assets/shade.frag @@ -1,10 +1,18 @@ #version 140 in vec2 v_tex_coords; +in vec3 v_frag_color; +in float v_sun_intensity; + + out vec4 color; + uniform sampler2D tex; void main() { - color = texture(tex, v_tex_coords); + vec4 sun_color = vec4(v_sun_intensity , v_sun_intensity , 0.0, 1.0); + + + color = texture(tex, v_tex_coords) * vec4(v_frag_color, 1.0) * sun_color; } \ No newline at end of file diff --git a/assets/shade.vert b/assets/shade.vert index 275eac1..8109deb 100644 --- a/assets/shade.vert +++ b/assets/shade.vert @@ -2,11 +2,19 @@ in vec2 position; in vec2 tex_coords; +in vec4 light_color; + +out vec3 v_frag_color; out vec2 v_tex_coords; +out float v_sun_intensity; uniform mat4 matrix; void main() { v_tex_coords = tex_coords; + + v_frag_color = vec3(light_color.rgb); + v_sun_intensity = light_color.a; + gl_Position = matrix * vec4(position, 0.0, 1.0); } \ No newline at end of file diff --git a/assets/shaders/grass/grass.frag b/assets/shaders/grass/grass.frag new file mode 100644 index 0000000..c8c1aec --- /dev/null +++ b/assets/shaders/grass/grass.frag @@ -0,0 +1,4 @@ +#version 140 + +void main() { +} \ No newline at end of file diff --git a/assets/shaders/grass/grass.vert b/assets/shaders/grass/grass.vert new file mode 100644 index 0000000..c8c1aec --- /dev/null +++ b/assets/shaders/grass/grass.vert @@ -0,0 +1,4 @@ +#version 140 + +void main() { +} \ No newline at end of file diff --git a/assets/shaders/water/water.frag b/assets/shaders/water/water.frag new file mode 100644 index 0000000..c8c1aec --- /dev/null +++ b/assets/shaders/water/water.frag @@ -0,0 +1,4 @@ +#version 140 + +void main() { +} \ No newline at end of file diff --git a/assets/shaders/water/water.vert b/assets/shaders/water/water.vert new file mode 100644 index 0000000..c8c1aec --- /dev/null +++ b/assets/shaders/water/water.vert @@ -0,0 +1,4 @@ +#version 140 + +void main() { +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 51f52cb..60e764a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ fn main() { let display = glium::Display::new(wb, cb, &event_loop).unwrap(); let image = image::load( - Cursor::new(&include_bytes!("../assets/opengl.png")), + Cursor::new(&include_bytes!("../assets/GrassTop.png")), image::ImageFormat::Png, ) .unwrap() @@ -38,16 +38,28 @@ fn main() { struct Vertex { position: [f32; 2], tex_coords: [f32; 2], + light_color: [f32; 4], } - implement_vertex!(Vertex, position, tex_coords); + implement_vertex!(Vertex, position, tex_coords, light_color); - #[rustfmt::skip] - let vertex1 = Vertex { position: [-0.5, -0.5], tex_coords: [0.0, 0.0] }; - #[rustfmt::skip] - let vertex2 = Vertex { position: [ 0.0, 0.5], tex_coords: [0.0, 1.0] }; - #[rustfmt::skip] - let vertex3 = Vertex { position: [ 0.5, -0.25], tex_coords: [1.0, 0.0] }; + let vertex1 = Vertex { + position: [-0.5, -0.5], + tex_coords: [0.0, 0.0], + light_color: [1.0, 1.0, 1.0, 0.0], + }; + + let vertex2 = Vertex { + position: [0.0, 0.5], + tex_coords: [0.0, 1.0], + light_color: [1.0, 0.0, 0.0, 1.0], + }; + + let vertex3 = Vertex { + position: [0.5, -0.25], + tex_coords: [1.0, 0.0], + light_color: [0.0, 1.0, 0.0, 0.5], + }; let shape = vec![vertex1, vertex2, vertex3]; @@ -122,7 +134,7 @@ fn main() { } let mut target = display.draw(); - target.clear_color(0.0, 0.0, 1.0, 0.0); + target.clear_color(0.0, 0.0, 1.0, 1.0); let uniforms = uniform! { matrix: [ @@ -132,6 +144,8 @@ fn main() { [ t , 0.0, 0.0, 1.0f32], ], tex: &texture, + + }; target