diff options
Diffstat (limited to 'engine')
-rw-r--r-- | engine/src/opengl/shader.rs | 101 | ||||
-rw-r--r-- | engine/src/renderer/opengl.rs | 61 |
2 files changed, 92 insertions, 70 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 diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs index 43ec16c..18b0041 100644 --- a/engine/src/renderer/opengl.rs +++ b/engine/src/renderer/opengl.rs @@ -452,11 +452,11 @@ fn apply_transformation_matrices( ) { gl_shader_program - .set_uniform_matrix_4fv(c"model", &create_transformation_matrix(transformation)); + .set_uniform(c"model", &create_transformation_matrix(transformation)); let view_matrix = create_view_matrix(camera, &camera_pos.position); - gl_shader_program.set_uniform_matrix_4fv(c"view", &view_matrix); + gl_shader_program.set_uniform(c"view", &view_matrix); #[allow(clippy::cast_precision_loss)] let proj_matrix = match &camera.projection { @@ -469,7 +469,7 @@ fn apply_transformation_matrices( } }; - gl_shader_program.set_uniform_matrix_4fv(c"projection", &proj_matrix); + gl_shader_program.set_uniform(c"projection", &proj_matrix); } fn apply_light<PointLightHolder>( @@ -494,7 +494,7 @@ fn apply_light<PointLightHolder>( ); for (dir_light_index, dir_light) in directional_lights.iter().enumerate() { - gl_shader_program.set_uniform_vec_3fv( + gl_shader_program.set_uniform( &create_light_uniform_name( "directional_lights", dir_light_index, @@ -514,10 +514,10 @@ fn apply_light<PointLightHolder>( // There probably won't be more than 2147483648 directional lights #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] gl_shader_program - .set_uniform_1i(c"directional_light_cnt", directional_lights.len() as i32); + .set_uniform(c"directional_light_cnt", &(directional_lights.len() as i32)); for (point_light_index, point_light) in point_lights.iter().enumerate() { - gl_shader_program.set_uniform_vec_3fv( + gl_shader_program.set_uniform( &create_light_uniform_name("point_lights", point_light_index, "position"), &point_light.position, ); @@ -539,24 +539,23 @@ fn apply_light<PointLightHolder>( // There probably won't be more than 2147483648 point lights #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] - gl_shader_program.set_uniform_1i(c"point_light_cnt", point_lights.len() as i32); + gl_shader_program.set_uniform(c"point_light_cnt", &(point_lights.len() as i32)); - gl_shader_program.set_uniform_vec_3fv( + gl_shader_program.set_uniform( c"material.ambient", - &if material_flags.use_ambient_color { + &Vec3::from(if material_flags.use_ambient_color { material.ambient.clone() } else { global_light.ambient.clone() - } - .into(), + }), ); gl_shader_program - .set_uniform_vec_3fv(c"material.diffuse", &material.diffuse.clone().into()); + .set_uniform(c"material.diffuse", &Vec3::from(material.diffuse.clone())); #[allow(clippy::cast_possible_wrap)] gl_shader_program - .set_uniform_vec_3fv(c"material.specular", &material.specular.clone().into()); + .set_uniform(c"material.specular", &Vec3::from(material.specular.clone())); let texture_map = material .textures @@ -566,26 +565,26 @@ fn apply_light<PointLightHolder>( .collect::<HashMap<_, _>>(); #[allow(clippy::cast_possible_wrap)] - gl_shader_program.set_uniform_1i( + gl_shader_program.set_uniform( c"material.ambient_map", - *texture_map.get(&material.ambient_map).unwrap() as i32, + &(*texture_map.get(&material.ambient_map).unwrap() as i32), ); #[allow(clippy::cast_possible_wrap)] - gl_shader_program.set_uniform_1i( + gl_shader_program.set_uniform( c"material.diffuse_map", - *texture_map.get(&material.diffuse_map).unwrap() as i32, + &(*texture_map.get(&material.diffuse_map).unwrap() as i32), ); #[allow(clippy::cast_possible_wrap)] - gl_shader_program.set_uniform_1i( + gl_shader_program.set_uniform( c"material.specular_map", - *texture_map.get(&material.specular_map).unwrap() as i32, + &(*texture_map.get(&material.specular_map).unwrap() as i32), ); - gl_shader_program.set_uniform_1fv(c"material.shininess", material.shininess); + gl_shader_program.set_uniform(c"material.shininess", &material.shininess); - gl_shader_program.set_uniform_vec_3fv(c"view_pos", &camera_pos.position); + gl_shader_program.set_uniform(c"view_pos", &camera_pos.position); } fn set_light_attenuation_uniforms( @@ -595,27 +594,27 @@ fn set_light_attenuation_uniforms( light: &PointLight, ) { - gl_shader_program.set_uniform_1fv( + gl_shader_program.set_uniform( &create_light_uniform_name( light_array, light_index, "attenuation_props.constant", ), - light.attenuation_params.constant, + &light.attenuation_params.constant, ); - gl_shader_program.set_uniform_1fv( + gl_shader_program.set_uniform( &create_light_uniform_name(light_array, light_index, "attenuation_props.linear"), - light.attenuation_params.linear, + &light.attenuation_params.linear, ); - gl_shader_program.set_uniform_1fv( + gl_shader_program.set_uniform( &create_light_uniform_name( light_array, light_index, "attenuation_props.quadratic", ), - light.attenuation_params.quadratic, + &light.attenuation_params.quadratic, ); } @@ -626,14 +625,14 @@ fn set_light_phong_uniforms( light: &impl Light, ) { - gl_shader_program.set_uniform_vec_3fv( + gl_shader_program.set_uniform( &create_light_uniform_name(light_array, light_index, "phong.diffuse"), - &light.diffuse().clone().into(), + &Vec3::from(light.diffuse().clone()), ); - gl_shader_program.set_uniform_vec_3fv( + gl_shader_program.set_uniform( &create_light_uniform_name(light_array, light_index, "phong.specular"), - &light.specular().clone().into(), + &Vec3::from(light.specular().clone()), ); } |