From e2b14f3dfbbfcdab12bf79559806576050231cea Mon Sep 17 00:00:00 2001 From: _ <> Date: Mon, 17 Feb 2020 02:00:41 +0000 Subject: [PATCH] :recycle: Pull out shader source code to text files --- shaders/pumpkin-frag.glsl | 27 ++++++++++++++++++ shaders/pumpkin-vert.glsl | 22 ++++++++++++++ src/bin/pumpkin.rs | 60 ++------------------------------------- src/shader.rs | 11 ++++++- 4 files changed, 61 insertions(+), 59 deletions(-) create mode 100644 shaders/pumpkin-frag.glsl create mode 100644 shaders/pumpkin-vert.glsl diff --git a/shaders/pumpkin-frag.glsl b/shaders/pumpkin-frag.glsl new file mode 100644 index 0000000..8fc60b3 --- /dev/null +++ b/shaders/pumpkin-frag.glsl @@ -0,0 +1,27 @@ +#define lowp +#define mediump +#define highp +#line 0 +uniform lowp sampler2D uni_texture; +uniform lowp vec3 uni_albedo; +uniform lowp vec3 uni_object_space_light; +uniform lowp vec3 uni_min_bright; +uniform lowp vec3 uni_min_albedo; + +//varying lowp vec4 vary_color; +varying lowp vec3 vary_normal; +varying mediump vec2 vary_uv; + +void main (void) { + lowp vec3 normal = normalize (vary_normal); + + lowp vec3 albedo = uni_albedo * max (uni_min_albedo, texture2D (uni_texture, vary_uv).rgb); + //lowp vec3 albedo = vec3 (vary_uv, 0.0); + + lowp float diffuse_factor = dot (normal, uni_object_space_light); + lowp vec3 sun = max (diffuse_factor, 0.0) * vec3 (0.95, 0.9, 0.85); + lowp vec3 sky = (diffuse_factor * 0.45 + 0.55) * vec3 (0.05, 0.1, 0.15); + lowp vec3 diffuse_color = albedo * max (uni_min_bright, (sun + sky)); + + gl_FragColor = vec4 (sqrt (diffuse_color), 1.0); +} diff --git a/shaders/pumpkin-vert.glsl b/shaders/pumpkin-vert.glsl new file mode 100644 index 0000000..191b090 --- /dev/null +++ b/shaders/pumpkin-vert.glsl @@ -0,0 +1,22 @@ +#define lowp +#define mediump +#define highp +#line 0 +uniform highp mat4 uni_mvp; + +attribute highp vec4 attr_pos; +attribute mediump vec2 attr_uv; +attribute lowp vec3 attr_normal; + +varying mediump vec2 vary_uv; +varying lowp vec3 vary_normal; + +void main (void) { + vary_uv = attr_uv; + + lowp vec4 light_color = vec4 (1.0); + + vary_normal = attr_normal; + + gl_Position = uni_mvp * attr_pos; +} diff --git a/src/bin/pumpkin.rs b/src/bin/pumpkin.rs index cf3d77d..f65c3f5 100644 --- a/src/bin/pumpkin.rs +++ b/src/bin/pumpkin.rs @@ -26,62 +26,6 @@ where V: Into )) } -const VERT_SHADER_SRC: &str = -" -#define lowp -#define mediump -#define highp -#line 0 -uniform highp mat4 uni_mvp; - -attribute highp vec4 attr_pos; -attribute mediump vec2 attr_uv; -attribute lowp vec3 attr_normal; - -varying mediump vec2 vary_uv; -varying lowp vec3 vary_normal; - -void main (void) { - vary_uv = attr_uv; - - lowp vec4 light_color = vec4 (1.0); - - vary_normal = attr_normal; - - gl_Position = uni_mvp * attr_pos; -}"; - -const FRAG_SHADER_SRC: &str = -" -#define lowp -#define mediump -#define highp -#line 0 -uniform lowp sampler2D uni_texture; -uniform lowp vec3 uni_albedo; -uniform lowp vec3 uni_object_space_light; -uniform lowp vec3 uni_min_bright; -uniform lowp vec3 uni_min_albedo; - -//varying lowp vec4 vary_color; -varying lowp vec3 vary_normal; -varying mediump vec2 vary_uv; - -void main (void) { - lowp vec3 normal = normalize (vary_normal); - - lowp vec3 albedo = uni_albedo * max (uni_min_albedo, texture2D (uni_texture, vary_uv).rgb); - //lowp vec3 albedo = vec3 (vary_uv, 0.0); - - lowp float diffuse_factor = dot (normal, uni_object_space_light); - lowp vec3 sun = max (diffuse_factor, 0.0) * vec3 (0.95, 0.9, 0.85); - lowp vec3 sky = (diffuse_factor * 0.45 + 0.55) * vec3 (0.05, 0.1, 0.15); - lowp vec3 diffuse_color = albedo * max (uni_min_bright, (sun + sky)); - - gl_FragColor = vec4 (sqrt (diffuse_color), 1.0); -} -"; - const KEY_LEFT: usize = 0; const KEY_RIGHT: usize = KEY_LEFT + 1; const KEY_UP: usize = KEY_RIGHT + 1; @@ -183,8 +127,8 @@ fn main () { // And I'm still not actually checking errors - Just checkmarking // that I know where they are. - let vert_shader = ShaderObject::new (gl::VERTEX_SHADER, VERT_SHADER_SRC).unwrap (); - let frag_shader = ShaderObject::new (gl::FRAGMENT_SHADER, FRAG_SHADER_SRC).unwrap (); + let vert_shader = ShaderObject::from_file (gl::VERTEX_SHADER, "shaders/pumpkin-vert.glsl").unwrap (); + let frag_shader = ShaderObject::from_file (gl::FRAGMENT_SHADER, "shaders/pumpkin-frag.glsl").unwrap (); let shader_program = ShaderProgram::new (&vert_shader, &frag_shader).unwrap (); diff --git a/src/shader.rs b/src/shader.rs index 1c597b5..9a9e9a4 100644 --- a/src/shader.rs +++ b/src/shader.rs @@ -11,7 +11,8 @@ impl ShaderObject { self.id } - pub fn new (shader_type: u32, source: &str) -> Result + pub fn from_source (shader_type: u32, source: &[u8]) + -> Result { let id = unsafe { gl::CreateShader (shader_type) @@ -54,6 +55,14 @@ impl ShaderObject { Err (info) } } + + pub fn from_file

(shader_type: u32, filename: P) + -> Result + where P: AsRef + { + let src = crate::file::load_small_file (filename, 1024 * 1024); + Self::from_source (shader_type, &src) + } } impl Drop for ShaderObject {