diff options
Diffstat (limited to 'engine/src/opengl/shader.rs')
-rw-r--r-- | engine/src/opengl/shader.rs | 101 |
1 files changed, 62 insertions, 39 deletions
diff --git a/engine/src/opengl/shader.rs b/engine/src/opengl/shader.rs index 36dc1a4..a626fc7 100644 --- a/engine/src/opengl/shader.rs +++ b/engine/src/opengl/shader.rs @@ -159,82 +159,105 @@ impl Program } } - pub fn set_uniform_matrix_4fv(&mut self, name: &CStr, matrix: &Matrix<f32, 4, 4>) + pub fn set_uniform(&mut self, name: &CStr, var: &impl UniformVariable) { - let uniform_location = - unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) }; + let location = UniformLocation(unsafe { + gl::GetUniformLocation(self.program, name.as_ptr().cast()) + }); + + var.set(self, location); + } + + fn get_info_log(&self) -> String + { + let mut buf = vec![gl::types::GLchar::default(); 512]; unsafe { - gl::ProgramUniformMatrix4fv( + #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] + gl::GetProgramInfoLog( self.program, - uniform_location, - 1, - gl::FALSE, - matrix.as_ptr(), + buf.len() as gl::types::GLsizei, + null_mut(), + buf.as_mut_ptr(), ); } + + let info_log = unsafe { CStr::from_ptr(buf.as_ptr()) }; + + unsafe { String::from_utf8_unchecked(info_log.to_bytes().to_vec()) } } +} - pub fn set_uniform_vec_3fv(&mut self, name: &CStr, vec: &Vec3<f32>) +impl Drop for Program +{ + fn drop(&mut self) { - let uniform_location = - unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) }; - unsafe { - gl::ProgramUniform3fv(self.program, uniform_location, 1, vec.as_ptr()); + gl::DeleteProgram(self.program); } } +} - pub fn set_uniform_1fv(&mut self, name: &CStr, num: f32) - { - let uniform_location = - unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) }; +pub trait UniformVariable +{ + fn set(&self, program: &mut Program, uniform_location: UniformLocation); +} +impl UniformVariable for f32 +{ + fn set(&self, program: &mut Program, uniform_location: UniformLocation) + { unsafe { - gl::ProgramUniform1fv(self.program, uniform_location, 1, &num); + gl::ProgramUniform1f(program.program, uniform_location.0, *self); } } +} - pub fn set_uniform_1i(&mut self, name: &CStr, num: i32) +impl UniformVariable for i32 +{ + fn set(&self, program: &mut Program, uniform_location: UniformLocation) { - let uniform_location = - unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) }; - unsafe { - gl::ProgramUniform1i(self.program, uniform_location, num); + gl::ProgramUniform1i(program.program, uniform_location.0, *self); } } +} - fn get_info_log(&self) -> String +impl UniformVariable for Vec3<f32> +{ + fn set(&self, program: &mut Program, uniform_location: UniformLocation) { - let mut buf = vec![gl::types::GLchar::default(); 512]; - unsafe { - #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] - gl::GetProgramInfoLog( - self.program, - buf.len() as gl::types::GLsizei, - null_mut(), - buf.as_mut_ptr(), + gl::ProgramUniform3f( + program.program, + uniform_location.0, + self.x, + self.y, + self.z, ); } - - let info_log = unsafe { CStr::from_ptr(buf.as_ptr()) }; - - unsafe { String::from_utf8_unchecked(info_log.to_bytes().to_vec()) } } } -impl Drop for Program +impl UniformVariable for Matrix<f32, 4, 4> { - fn drop(&mut self) + fn set(&self, program: &mut Program, uniform_location: UniformLocation) { unsafe { - gl::DeleteProgram(self.program); + gl::ProgramUniformMatrix4fv( + program.program, + uniform_location.0, + 1, + gl::FALSE, + self.as_ptr(), + ); } } } +#[derive(Debug)] +pub struct UniformLocation(gl::types::GLint); + /// Shader error. #[derive(Debug, thiserror::Error)] pub enum Error |