unifinished port to glow for shader.rs
parent
93217e1a66
commit
adc2c93af2
11
src/main.rs
11
src/main.rs
|
@ -7,7 +7,7 @@ const SCR_WIDTH: u32 = 1600;
|
||||||
const SCR_HEIGHT: u32 = 900;
|
const SCR_HEIGHT: u32 = 900;
|
||||||
const TITLE: &str = "GLFWtest";
|
const TITLE: &str = "GLFWtest";
|
||||||
|
|
||||||
use glow;
|
use glow::*;
|
||||||
|
|
||||||
use std::sync::mpsc::Receiver;
|
use std::sync::mpsc::Receiver;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
@ -56,13 +56,18 @@ fn main() {
|
||||||
let mut lastFrame: f32 = 0.0;
|
let mut lastFrame: f32 = 0.0;
|
||||||
|
|
||||||
//gl::load_with(|ptr| window.get_proc_address(ptr) as *const _);
|
//gl::load_with(|ptr| window.get_proc_address(ptr) as *const _);
|
||||||
let gl = unsafe{glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _);};
|
let gl = {
|
||||||
|
|
||||||
|
unsafe{
|
||||||
|
let glue = glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _);
|
||||||
|
glue
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let (ourshader, ourModel) = unsafe {
|
let (ourshader, ourModel) = unsafe {
|
||||||
gl::Enable(gl::DEPTH_TEST);
|
gl::Enable(gl::DEPTH_TEST);
|
||||||
|
|
||||||
let ourShader = shader::shader::new("model");
|
let ourShader = shader::shader::new("model", gl);
|
||||||
|
|
||||||
let ourModel = model::Model::new("resources/models/TestCarModel/CarW4.obj");
|
let ourModel = model::Model::new("resources/models/TestCarModel/CarW4.obj");
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
|
|
||||||
use gl;
|
use glow::*;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::ffi::{CString, CStr};
|
use std::ffi::{CString, CStr};
|
||||||
use std::str;
|
use std::str;
|
||||||
use cgmath::prelude::*;
|
use cgmath::prelude::*;
|
||||||
/// Shader Struct for creating and using shaders in the Context of engine
|
/// Shader Struct for creating and using shaders in the Context of engine
|
||||||
///
|
///
|
||||||
pub struct shader {
|
pub struct shader<T: glow::HasContext> {
|
||||||
|
|
||||||
pub ID: gl::types::GLuint,
|
pub ID: T::Program,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
impl shader
|
impl shader<T>
|
||||||
{
|
{
|
||||||
/// Shader Constructor, will read, Compile and Create GLSL Program for use
|
/// Shader Constructor, will read, Compile and Create GLSL Program for use
|
||||||
///
|
///
|
||||||
|
@ -21,74 +21,51 @@ use cgmath::prelude::*;
|
||||||
/// `new(String::from("Example"))`
|
/// `new(String::from("Example"))`
|
||||||
/// if the String given was "Example" the Program expects both shaders to be in the directory "resources/shaders/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"
|
/// with the vertex shader being called "shader.vert" and fragment "shader.frag"
|
||||||
pub fn new(path: &str) -> shader
|
pub fn new(path: &str, gl: glow::Context) -> shader<T>
|
||||||
{
|
{
|
||||||
//read file contents
|
//read file contents
|
||||||
let VERT_SHADER = fs::read_to_string(format!("resources/shaders/{path}/shader.vert")).unwrap();
|
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();
|
let FRAG_SHADER = fs::read_to_string(format!("resources/shaders/{path}/shader.frag")).unwrap();
|
||||||
|
|
||||||
//create vertex shader
|
//create vertex shader
|
||||||
let vertex_shader = unsafe {gl::CreateShader(gl::VERTEX_SHADER) };
|
let vertex_shader = unsafe {gl.create_shader(glow::VERTEX_SHADER).unwrap() };
|
||||||
unsafe {
|
unsafe {
|
||||||
gl::ShaderSource(vertex_shader, 1, &VERT_SHADER.as_bytes().as_ptr().cast(), &VERT_SHADER.len().try_into().unwrap());
|
gl.shader_source(vertex_shader, &VERT_SHADER);
|
||||||
gl::CompileShader(vertex_shader);
|
gl.compile_shader(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());
|
// create fragment shader
|
||||||
v.set_len(log_len.try_into().unwrap());
|
let fragment_shader = unsafe{gl.create_shader(glow::FRAGMENT_SHADER).unwrap()};
|
||||||
panic!("Vertex Shader Compile Error: {}", String::from_utf8_lossy(&v));
|
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 fragment_shader = gl::CreateShader(gl::FRAGMENT_SHADER);
|
let shader_program = gl.create_program().unwrap();
|
||||||
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.attach_shader(shader_program, vertex_shader);
|
||||||
gl::GetShaderiv(fragment_shader, gl::COMPILE_STATUS, &mut success);
|
gl.attach_shader(shader_program, fragment_shader);
|
||||||
if success == 0 {
|
gl.link_program(shader_program);
|
||||||
let mut v: Vec<u8> = Vec::with_capacity(1024);
|
if !gl.get_program_link_status(shader_program) {
|
||||||
let mut log_len = 0_i32;
|
panic!("Error, Linking Shader Program {}", gl.get_program_info_log(shader_program));
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
gl.detach_shader(shader_program, vertex_shader);
|
||||||
|
gl.detach_shader(shader_program, fragment_shader);
|
||||||
|
gl.delete_shader(vertex_shader);
|
||||||
|
gl.delete_shader(fragment_shader);
|
||||||
|
|
||||||
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 {
|
shader {
|
||||||
ID: shader_program,
|
ID: shader_program,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// return program
|
||||||
|
|
||||||
/// uses Shader
|
/// uses Shader
|
||||||
pub fn Use(&self)
|
pub fn Use(&self)
|
||||||
{
|
{
|
||||||
|
@ -132,11 +109,7 @@ use cgmath::prelude::*;
|
||||||
pub fn setMat4(&self, name: &CStr, value: &cgmath::Matrix4<f32>)
|
pub fn setMat4(&self, name: &CStr, value: &cgmath::Matrix4<f32>)
|
||||||
{unsafe {gl::UniformMatrix4fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}}
|
{unsafe {gl::UniformMatrix4fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use glfw;
|
use glfw;
|
||||||
|
|
Loading…
Reference in New Issue