added Shader object and Renderer Module
parent
22e47de409
commit
decb65d0ce
|
@ -0,0 +1,2 @@
|
|||
|
||||
pub mod shader;
|
|
@ -0,0 +1,135 @@
|
|||
#![allow(non_snake_case)]
|
||||
use glow::*;
|
||||
use std::fs;
|
||||
use std::str;
|
||||
|
||||
|
||||
|
||||
/// Shader Struct for creating and using shaders in the Context of engine
|
||||
///
|
||||
#[derive(Clone)]
|
||||
pub struct Shader {
|
||||
|
||||
pub ID: NativeProgram,
|
||||
|
||||
pub path: String
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl Shader
|
||||
{
|
||||
/// Shader Constructor, will read, Compile and Create a GLSL Program for use
|
||||
/// currently panics if the files can't be found or the shader is wrong
|
||||
///
|
||||
///
|
||||
/// # Example
|
||||
/// `let shader = Shader::new("Example", &gl);`
|
||||
/// if the String given was "Example" the Program expects both shaders to be in the directory "data/shaders/Example/"
|
||||
/// with the vertex shader being called "shader.vert" and fragment "shader.frag"
|
||||
pub fn new(path: &str, gl: &glow::Context) -> Shader
|
||||
{
|
||||
//read file contents
|
||||
let VERT_SHADER = fs::read_to_string(format!("/data/shaders/{path}/shader.vert")).unwrap();
|
||||
let FRAG_SHADER = fs::read_to_string(format!("/data/shaders/{path}/shader.frag")).unwrap();
|
||||
|
||||
//create vertex shader
|
||||
let vertex_shader = unsafe {gl.create_shader(glow::VERTEX_SHADER).unwrap() };
|
||||
unsafe {
|
||||
gl.shader_source(vertex_shader, &VERT_SHADER);
|
||||
gl.compile_shader(vertex_shader);
|
||||
|
||||
}
|
||||
// create fragment shader
|
||||
let fragment_shader = unsafe{gl.create_shader(glow::FRAGMENT_SHADER).unwrap()};
|
||||
unsafe {
|
||||
gl.shader_source(fragment_shader, &FRAG_SHADER);
|
||||
gl.compile_shader(fragment_shader);
|
||||
if !gl.get_shader_compile_status(fragment_shader) {
|
||||
panic!("Error, Compiling Fragment Shader: {}", gl.get_shader_info_log(fragment_shader));
|
||||
}
|
||||
|
||||
let shader_program = gl.create_program().unwrap();
|
||||
|
||||
gl.attach_shader(shader_program, vertex_shader);
|
||||
gl.attach_shader(shader_program, fragment_shader);
|
||||
gl.link_program(shader_program);
|
||||
if !gl.get_program_link_status(shader_program) {
|
||||
panic!("Error, Linking Shader Program {}", gl.get_program_info_log(shader_program));
|
||||
}
|
||||
gl.detach_shader(shader_program, vertex_shader);
|
||||
gl.detach_shader(shader_program, fragment_shader);
|
||||
gl.delete_shader(vertex_shader);
|
||||
gl.delete_shader(fragment_shader);
|
||||
|
||||
Shader {
|
||||
ID: shader_program,
|
||||
path: path.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// return program
|
||||
|
||||
/// uses Shader
|
||||
///
|
||||
pub fn Use(&self, gl: &glow::Context)
|
||||
{
|
||||
unsafe {gl.use_program(Some(self.ID));}
|
||||
}
|
||||
|
||||
// BOILERPLATE JUMPSCARE
|
||||
//pub fn set_bool(&self, gl: &glow::Context name: &str, value: bool )
|
||||
//{unsafe {gl.uniform_1_bool(gl.get_uniform_location(self.ID, name).as_ref(), value);}}
|
||||
|
||||
pub fn set_int(&self, gl: &glow::Context, name: &str, value: i32 )
|
||||
{unsafe {gl.uniform_1_i32(gl.get_uniform_location(self.ID, name).as_ref(), value);}}
|
||||
|
||||
pub fn set_float(&self, gl: &glow::Context, name: &str, value: f32 )
|
||||
{unsafe {gl.uniform_1_f32(gl.get_uniform_location(self.ID, name).as_ref(), value);}}
|
||||
|
||||
pub fn set_vector2(&self, gl: &glow::Context, name: &str, value: cgmath::Vector2<f32> )
|
||||
{unsafe {gl.uniform_2_f32(gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1]);}}
|
||||
|
||||
pub fn set_vector2d(&self, gl: &glow::Context, name: &str, x: f32, y: f32)
|
||||
{unsafe {gl.uniform_2_f32(gl.get_uniform_location(self.ID, name).as_ref(), x, y);}}
|
||||
|
||||
pub fn set_vector3(&self, gl: &glow::Context, name: &str, value: cgmath::Vector3<f32> )
|
||||
{unsafe {gl.uniform_3_f32(gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1], value[2]);}}
|
||||
|
||||
pub fn set_vector3d(&self, gl: &glow::Context, name: &str, x: f32, y: f32, z: f32 )
|
||||
{unsafe {gl.uniform_3_f32(gl.get_uniform_location(self.ID, name).as_ref(), x, y, z);}}
|
||||
|
||||
pub fn set_vector4(&self, gl: &glow::Context, name: &str, value: &cgmath::Vector4<f32> )
|
||||
{unsafe {gl.uniform_4_f32(gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1], value[2], value[3]);}}
|
||||
|
||||
pub fn set_vector4d(&self, gl: &glow::Context, name: &str, x: f32, y: f32, z: f32, w: f32)
|
||||
{unsafe {gl.uniform_4_f32(gl.get_uniform_location(self.ID, name).as_ref(), x, y, z, w);}}
|
||||
//pub fn setMat2(&self, name: &str, value: cgmath::Matrix2<f32>)
|
||||
|
||||
pub fn set_mat3(&self, gl: &glow::Context, name: &str, value: cgmath::Matrix3<f32>)
|
||||
{
|
||||
let nums = vec![value.x.x, value.x.y, value.x.z,
|
||||
value.y.x, value.y.y, value.y.z,
|
||||
value.z.x, value.z.y, value.z.z];
|
||||
unsafe
|
||||
{
|
||||
gl.uniform_matrix_3_f32_slice(gl.get_uniform_location(self.ID, name).as_ref(), false, nums.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_mat4(&self, gl: &glow::Context, name: &str, value: &cgmath::Matrix4<f32>)
|
||||
{
|
||||
let nums = vec![value.x.x, value.x.y, value.x.z, value.x.w,
|
||||
value.y.x, value.y.y, value.y.z, value.y.w,
|
||||
value.z.x, value.z.y, value.z.z, value.z.w,
|
||||
value.w.x, value.w.y, value.w.z, value.w.w];
|
||||
unsafe
|
||||
{
|
||||
gl.uniform_matrix_4_f32_slice(gl.get_uniform_location(self.ID, name).as_ref(), false, nums.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -17,6 +17,7 @@ use glutin::{prelude::*, context::Version};
|
|||
use glutin_winit::{self, GlWindow};
|
||||
use glutin::display::GetGlDisplay;
|
||||
|
||||
pub mod Renderer;
|
||||
fn main() {
|
||||
|
||||
// NOTE initialization code here
|
||||
|
|
Loading…
Reference in New Issue