♻️ Pull out shader source code to text files

main
_ 2020-02-17 02:00:41 +00:00
parent af4ef579c1
commit e2b14f3dfb
4 changed files with 61 additions and 59 deletions

27
shaders/pumpkin-frag.glsl Normal file
View File

@ -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);
}

22
shaders/pumpkin-vert.glsl Normal file
View File

@ -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;
}

View File

@ -26,62 +26,6 @@ where V: Into <Vec3>
)) ))
} }
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_LEFT: usize = 0;
const KEY_RIGHT: usize = KEY_LEFT + 1; const KEY_RIGHT: usize = KEY_LEFT + 1;
const KEY_UP: usize = KEY_RIGHT + 1; const KEY_UP: usize = KEY_RIGHT + 1;
@ -183,8 +127,8 @@ fn main () {
// And I'm still not actually checking errors - Just checkmarking // And I'm still not actually checking errors - Just checkmarking
// that I know where they are. // that I know where they are.
let vert_shader = ShaderObject::new (gl::VERTEX_SHADER, VERT_SHADER_SRC).unwrap (); let vert_shader = ShaderObject::from_file (gl::VERTEX_SHADER, "shaders/pumpkin-vert.glsl").unwrap ();
let frag_shader = ShaderObject::new (gl::FRAGMENT_SHADER, FRAG_SHADER_SRC).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 (); let shader_program = ShaderProgram::new (&vert_shader, &frag_shader).unwrap ();

View File

@ -11,7 +11,8 @@ impl ShaderObject {
self.id self.id
} }
pub fn new (shader_type: u32, source: &str) -> Result <ShaderObject, String> pub fn from_source (shader_type: u32, source: &[u8])
-> Result <ShaderObject, String>
{ {
let id = unsafe { let id = unsafe {
gl::CreateShader (shader_type) gl::CreateShader (shader_type)
@ -54,6 +55,14 @@ impl ShaderObject {
Err (info) Err (info)
} }
} }
pub fn from_file <P> (shader_type: u32, filename: P)
-> Result <ShaderObject, String>
where P: AsRef <std::path::Path>
{
let src = crate::file::load_small_file (filename, 1024 * 1024);
Self::from_source (shader_type, &src)
}
} }
impl Drop for ShaderObject { impl Drop for ShaderObject {