Compare commits
3 Commits
72ac25ea9d
...
0df79f7509
Author | SHA1 | Date |
---|---|---|
Milk.H | 0df79f7509 | |
Milk.H | adc2c93af2 | |
Milk.H | 93217e1a66 |
|
@ -11,5 +11,6 @@ image = "0.24.5"
|
|||
gl = "0.14.0"
|
||||
cgmath = "0.18.0"
|
||||
tobj = "2.0.0"
|
||||
glow = "0.12.1"
|
||||
[dependencies.glfw]
|
||||
version = "*"
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -7,6 +7,7 @@ const SCR_WIDTH: u32 = 1600;
|
|||
const SCR_HEIGHT: u32 = 900;
|
||||
const TITLE: &str = "GLFWtest";
|
||||
|
||||
use glow::*;
|
||||
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::ptr;
|
||||
|
@ -54,12 +55,12 @@ fn main() {
|
|||
let mut deltaTime: f32; // time between current frame and last frame
|
||||
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 = std::rc::Rc::new(glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _));
|
||||
let (ourshader, ourModel) = unsafe {
|
||||
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");
|
||||
|
||||
|
@ -81,8 +82,7 @@ fn main() {
|
|||
|
||||
unsafe {
|
||||
gl::Enable(gl::DEPTH_TEST);
|
||||
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
|
||||
gl::ClearColor(0.0, 0.0, 0.0, 0.0);
|
||||
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
|
||||
|
||||
// outline
|
||||
|
||||
|
|
167
src/shader.rs
167
src/shader.rs
|
@ -1,14 +1,16 @@
|
|||
|
||||
use gl;
|
||||
use glow::*;
|
||||
use std::fs;
|
||||
use std::ffi::{CString, CStr};
|
||||
use std::rc::Rc;
|
||||
use std::str;
|
||||
use cgmath::prelude::*;
|
||||
/// Shader Struct for creating and using shaders in the Context of engine
|
||||
///
|
||||
pub struct shader {
|
||||
|
||||
pub ID: gl::types::GLuint,
|
||||
pub ID: NativeProgram,
|
||||
gl: Rc<glow::Context>
|
||||
}
|
||||
|
||||
|
||||
|
@ -21,122 +23,113 @@ use cgmath::prelude::*;
|
|||
/// `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"
|
||||
pub fn new(path: &str) -> shader
|
||||
pub fn new(path: &str, gl: Rc<glow::Context>) -> 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();
|
||||
let FRAG_SHADER = fs::read_to_string(format!("resources/shaders/{path}/shader.frag")).unwrap();
|
||||
|
||||
//create vertex shader
|
||||
let vertex_shader = unsafe {gl::CreateShader(gl::VERTEX_SHADER) };
|
||||
let vertex_shader = unsafe {gl.create_shader(glow::VERTEX_SHADER).unwrap() };
|
||||
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;
|
||||
gl.shader_source(vertex_shader, &VERT_SHADER);
|
||||
gl.compile_shader(vertex_shader);
|
||||
|
||||
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));
|
||||
}
|
||||
// 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 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 shader_program = gl.create_program().unwrap();
|
||||
|
||||
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));
|
||||
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);
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
ID: shader_program,
|
||||
gl: Rc::clone(&gl),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// return program
|
||||
|
||||
/// uses Shader
|
||||
pub fn Use(&self)
|
||||
pub fn Use(&self, gl: glow::Context)
|
||||
{
|
||||
unsafe {gl::UseProgram(self.ID);}
|
||||
unsafe {gl.use_program(Some(self.ID));}
|
||||
}
|
||||
|
||||
// BOILERPLATE JUMPSCARE
|
||||
pub fn setBool(&self, name: &CStr, value: bool )
|
||||
{unsafe {gl::Uniform1i(gl::GetUniformLocation(self.ID, name.as_ptr()), value as i32);}}
|
||||
/*
|
||||
pub fn setBool(&self, name: &str, value: bool )
|
||||
{unsafe {self.gl.uniform_1_bool(self.gl.get_uniform_location(self.ID, name).as_ref(), value);}}
|
||||
*/
|
||||
pub fn setInt(&self, name: &str, value: i32 )
|
||||
{unsafe {self.gl.uniform_1_i32(self.gl.get_uniform_location(self.ID, name).as_ref(), value);}}
|
||||
|
||||
pub fn setInt(&self, name: &CStr, value: u32 )
|
||||
{unsafe {gl::Uniform1i(gl::GetUniformLocation(self.ID, name.as_ptr()), value as i32);}}
|
||||
pub fn setFloat(&self, name: &str, value: f32 )
|
||||
{unsafe {self.gl.uniform_1_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), value);}}
|
||||
|
||||
pub fn setFloat(&self, name: &CStr, value: f32 )
|
||||
{unsafe {gl::Uniform1f(gl::GetUniformLocation(self.ID, name.as_ptr()), value as f32);}}
|
||||
pub fn setVector2(&self, name: &str, value: cgmath::Vector2<f32> )
|
||||
{unsafe {self.gl.uniform_2_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1]);}}
|
||||
|
||||
pub fn setVec2(&self, name: &CStr, value: &cgmath::Vector2<f32> )
|
||||
{unsafe {gl::Uniform2fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, value.as_ptr());}}
|
||||
pub fn setVector2d(&self, name: &str, x: f32, y: f32)
|
||||
{unsafe {self.gl.uniform_2_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), x, y);}}
|
||||
|
||||
pub fn setVector2d(&self, name: &CStr, x: f32, y: f32)
|
||||
{unsafe {gl::Uniform2f(gl::GetUniformLocation(self.ID, name.as_ptr()), x, y);}}
|
||||
pub fn setVector3(&self, name: &str, value: cgmath::Vector3<f32> )
|
||||
{unsafe {self.gl.uniform_3_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1], value[2]);}}
|
||||
|
||||
pub fn setVector3(&self, name: &CStr, value: cgmath::Vector3<f32> )
|
||||
{unsafe {gl::Uniform3fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, value.as_ptr());}}
|
||||
pub fn setVector3d(&self, name: &str, x: f32, y: f32, z: f32 )
|
||||
{unsafe {self.gl.uniform_3_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), x, y, z);}}
|
||||
|
||||
pub fn setVector3d(&self, name: &CStr, x: f32, y: f32, z: f32 )
|
||||
{unsafe {gl::Uniform3f(gl::GetUniformLocation(self.ID, name.as_ptr()), x, y, z);}}
|
||||
pub fn setVector4(&self, name: &str, value: &cgmath::Vector4<f32> )
|
||||
{unsafe {self.gl.uniform_4_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1], value[2], value[3]);}}
|
||||
|
||||
pub fn setVector4(&self, name: &CStr, value: &cgmath::Vector4<f32> )
|
||||
{unsafe {gl::Uniform4fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, value.as_ptr());}}
|
||||
pub fn setVector4d(&self, name: &str, x: f32, y: f32, z: f32, w: f32)
|
||||
{unsafe {self.gl.uniform_4_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), x, y, z, w);}}
|
||||
|
||||
pub fn setVector4d(&self, name: &CStr, x: f32, y: f32, z: f32, w: f32)
|
||||
{unsafe {gl::Uniform4f(gl::GetUniformLocation(self.ID, name.as_ptr()), x, y, z, w);}}
|
||||
//pub fn setMat2(&self, name: &str, value: cgmath::Matrix2<f32>)
|
||||
|
||||
pub fn setMat2(&self, name: &CStr, value: &cgmath::Matrix2<f32>)
|
||||
{unsafe {gl::UniformMatrix2fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}}
|
||||
|
||||
pub fn setMat3(&self, name: &CStr, value: &cgmath::Matrix3<f32>)
|
||||
{unsafe {gl::UniformMatrix3fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}}
|
||||
|
||||
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());}}
|
||||
pub fn setMat3(&self, 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
|
||||
{
|
||||
self.gl.uniform_matrix_3_f32_slice(self.gl.get_uniform_location(self.ID, name).as_ref(), false, nums.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setMat4(&self, 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
|
||||
{
|
||||
self.gl.uniform_matrix_4_f32_slice(self.gl.get_uniform_location(self.ID, name).as_ref(), false, nums.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use glfw;
|
||||
|
|
Loading…
Reference in New Issue