added Shader module and fixed some code
parent
8990ca3acf
commit
49268dbabf
17
src/main.rs
17
src/main.rs
|
@ -8,6 +8,7 @@ const ScreenWidth: u32 = 480;
|
|||
const ScreenHeight: u32 = 320;
|
||||
const TITLE: &str = "GLFWtest";
|
||||
|
||||
|
||||
mod shader;
|
||||
|
||||
fn main() {
|
||||
|
@ -37,12 +38,20 @@ fn main() {
|
|||
}
|
||||
|
||||
while !window.should_close() {
|
||||
glfw.poll_events();
|
||||
for(_, event) in glfw::flush_messages(&events) {
|
||||
glfw.poll_events();
|
||||
for (_, event) in glfw::flush_messages(&events) {
|
||||
glfw_handle_event(&mut window, event);
|
||||
}}
|
||||
}
|
||||
|
||||
clear_color(0.3, 0.4, 0.6, 1.0);
|
||||
|
||||
unsafe {
|
||||
gl::Clear(gl::COLOR_BUFFER_BIT);
|
||||
}
|
||||
|
||||
window.swap_buffers();
|
||||
}
|
||||
|
||||
window.swap_buffers();
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
|
||||
use gl;
|
||||
use std::fs;
|
||||
/// Shader Struct for creating and using shaders in the Context of engine
|
||||
///
|
||||
pub struct shader {
|
||||
|
||||
ID: gl::types::GLuint,
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl shader
|
||||
{
|
||||
/// Shader Constructor, will read, Compile and Create GLSL Program for use
|
||||
///
|
||||
/// # Example
|
||||
/// `new(String::from("Example"))`
|
||||
/// if the String given was "Example" the Program expects both shaders to be in the directory "resources/shaders/Example/"
|
||||
/// with the vertex shader being called "shader.vert" and fragment "shader.frag"
|
||||
/// # TODO
|
||||
/// add a way to interact with uniforms
|
||||
pub fn new(path: &str) -> shader
|
||||
{
|
||||
//read file contents
|
||||
let VERT_SHADER = fs::read_to_string(format!("resources/shaders/{path}/shader.vert")).unwrap();
|
||||
let FRAG_SHADER = std::fs::read_to_string(format!("resources/shaders/{path}/shader.frag")).unwrap();
|
||||
|
||||
//create vertex shader
|
||||
let vertex_shader = unsafe {gl::CreateShader(gl::VERTEX_SHADER) };
|
||||
unsafe {
|
||||
gl::ShaderSource(vertex_shader, 1, &VERT_SHADER.as_bytes().as_ptr().cast(), &VERT_SHADER.len().try_into().unwrap());
|
||||
gl::CompileShader(vertex_shader);
|
||||
}
|
||||
//error Checking
|
||||
let mut success = 0;
|
||||
unsafe {
|
||||
gl::GetShaderiv(vertex_shader, gl::COMPILE_STATUS, &mut success);
|
||||
if success == 0 {
|
||||
let mut log_len = 0_i32;
|
||||
|
||||
let mut v: Vec<u8> = Vec::with_capacity(1024);
|
||||
gl::GetShaderInfoLog(vertex_shader, 1024, &mut log_len, v.as_mut_ptr().cast());
|
||||
v.set_len(log_len.try_into().unwrap());
|
||||
panic!("Vertex Shader Compile Error: {}", String::from_utf8_lossy(&v));
|
||||
}
|
||||
|
||||
let fragment_shader = gl::CreateShader(gl::FRAGMENT_SHADER);
|
||||
gl::ShaderSource(fragment_shader, 1, &FRAG_SHADER.as_bytes().as_ptr().cast(), &FRAG_SHADER.len().try_into().unwrap());
|
||||
gl::CompileShader(fragment_shader);
|
||||
|
||||
let mut success = 0;
|
||||
gl::GetShaderiv(fragment_shader, gl::COMPILE_STATUS, &mut success);
|
||||
if success == 0 {
|
||||
let mut v: Vec<u8> = Vec::with_capacity(1024);
|
||||
let mut log_len = 0_i32;
|
||||
gl::GetShaderInfoLog(fragment_shader, 1024, &mut log_len, v.as_mut_ptr().cast());
|
||||
v.set_len(log_len.try_into().unwrap());
|
||||
panic!("Fragment Shader Compile Error: {}", String::from_utf8_lossy(&v));
|
||||
}
|
||||
|
||||
let shader_program = gl::CreateProgram();
|
||||
|
||||
gl::AttachShader(shader_program, vertex_shader);
|
||||
gl::AttachShader(shader_program, fragment_shader);
|
||||
gl::LinkProgram(shader_program);
|
||||
|
||||
let mut success = 0;
|
||||
gl::GetProgramiv(shader_program, gl::LINK_STATUS, &mut success);
|
||||
if success == 0 {
|
||||
let mut v: Vec<u8> = Vec::with_capacity(1024);
|
||||
let mut log_len = 0_i32;
|
||||
gl::GetProgramInfoLog(shader_program, 1024, &mut log_len, v.as_mut_ptr().cast());
|
||||
v.set_len(log_len.try_into().unwrap());
|
||||
panic!("Program Link Error: {}", String::from_utf8_lossy(&v));
|
||||
}
|
||||
|
||||
gl::DetachShader(shader_program, vertex_shader);
|
||||
gl::DetachShader(shader_program, fragment_shader);
|
||||
gl::DeleteShader(vertex_shader);
|
||||
gl::DeleteShader(fragment_shader);
|
||||
|
||||
|
||||
|
||||
// return program
|
||||
shader {
|
||||
ID: shader_program,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// uses Shader
|
||||
pub fn Use(&self)
|
||||
{
|
||||
unsafe {gl::UseProgram(self.ID);}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue