Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Sep 18, 2024
1 parent 95bdaa1 commit 767dad0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 58 deletions.
41 changes: 22 additions & 19 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Config {
.help("Colors for rendering the mesh using the Phong reflection model. Requires 3 colors as rgb hex values: ambient, diffuse, and specular. Defaults to blue.")
.short('m')
.long("material")
.value_names(&["ambient","diffuse","specular"])
.value_names(["ambient","diffuse","specular"])
)
.arg(
clap::Arg::new("background")
Expand Down Expand Up @@ -138,17 +138,21 @@ impl Config {
.expect("IMG_FILE not provided");
match matches.get_one::<String>("format") {
Some(x) => c.format = match_format(x),
None => match Path::new(&c.img_filename).extension() {
Some(ext) => c.format = match_format(ext.to_str().unwrap()),
_ => (),
},
None => {
if let Some(ext) = Path::new(&c.img_filename).extension() {
c.format = match_format(ext.to_str().unwrap());
}
}
};
matches
.get_one::<String>("size")
.map(|x| c.width = x.parse::<u32>().expect("Invalid size"));
matches
.get_one::<String>("size")
.map(|x| c.height = x.parse::<u32>().expect("Invalid size"));

if let Some(x) = matches.get_one::<String>("size") {
c.width = x.parse::<u32>().expect("Invalid size");
}

if let Some(x) = matches.get_one::<String>("size") {
c.height = x.parse::<u32>().expect("Invalid size");
}

c.visible = matches.contains_id("visible");
c.verbosity = matches.get_count("verbosity") as usize;
if let Some(materials) = matches.get_many::<String>("material") {
Expand All @@ -159,17 +163,16 @@ impl Config {
specular: iter.next().unwrap_or([0.0, 0.0, 0.0]),
};
}
matches
.get_one::<String>("background")
.map(|x| c.background = html_to_rgba(x));
match matches.get_one::<String>("aamethod") {
Some(x) => match x.as_str() {
if let Some(x) = matches.get_one::<String>("background") {
c.background = html_to_rgba(x);
}
if let Some(x) = matches.get_one::<String>("aamethod") {
match x.as_str() {
"none" => c.aamethod = AAMethod::None,
"fxaa" => c.aamethod = AAMethod::FXAA,
_ => unreachable!(),
},
None => (),
};
}
}
c.recalc_normals = matches.contains_id("recalc_normals");

c
Expand Down
20 changes: 10 additions & 10 deletions src/fxaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ struct SpriteVertex {
implement_vertex!(SpriteVertex, position, i_tex_coords);

impl FxaaSystem {
pub fn new<F: ?Sized>(facade: &F) -> FxaaSystem
pub fn new<F>(facade: &F) -> FxaaSystem
where
F: Facade,
F: Facade + ?Sized,
{
FxaaSystem {
context: facade.get_context().clone(),
Expand Down Expand Up @@ -63,7 +63,7 @@ impl FxaaSystem {
index_buffer: glium::index::IndexBuffer::new(
facade,
glium::index::PrimitiveType::TriangleStrip,
&[1 as u16, 2, 0, 3],
&[1u16, 2, 0, 3],
)
.unwrap(),

Expand Down Expand Up @@ -92,7 +92,7 @@ where
let mut target_depth = system.target_depth.borrow_mut();

{
let clear = if let &Some(ref tex) = &*target_color {
let clear = if let Some(ref tex) = *target_color {
tex.get_width() != target_dimensions.0
|| tex.get_height().unwrap() != target_dimensions.1
} else {
Expand All @@ -104,7 +104,7 @@ where
}

{
let clear = if let &Some(ref tex) = &*target_depth {
let clear = if let Some(ref tex) = *target_depth {
tex.get_dimensions() != target_dimensions
} else {
false
Expand All @@ -117,8 +117,8 @@ where
if target_color.is_none() {
let texture = glium::texture::Texture2d::empty(
&system.context,
target_dimensions.0 as u32,
target_dimensions.1 as u32,
target_dimensions.0,
target_dimensions.1,
)
.unwrap();
*target_color = Some(texture);
Expand All @@ -129,8 +129,8 @@ where
let texture = glium::framebuffer::DepthRenderBuffer::new(
&system.context,
glium::texture::DepthFormat::I24,
target_dimensions.0 as u32,
target_dimensions.1 as u32,
target_dimensions.0,
target_dimensions.1,
)
.unwrap();
*target_depth = Some(texture);
Expand All @@ -143,7 +143,7 @@ where
);

let uniforms = uniform! {
tex: &*target_color,
tex: target_color,
enabled: if enabled { 1i32 } else { 0i32 },
resolution: (target_dimensions.0 as f32, target_dimensions.1 as f32)
};
Expand Down
44 changes: 22 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ const CAM_POSITION: cgmath::Point3<f32> = cgmath::Point3 {
};

fn print_matrix(m: [[f32; 4]; 4]) {
for i in 0..4 {
debug!(
"{:.3}\t{:.3}\t{:.3}\t{:.3}",
m[i][0], m[i][1], m[i][2], m[i][3]
);
for row in &m {
debug!("{:.3}\t{:.3}\t{:.3}\t{:.3}", row[0], row[1], row[2], row[3]);
}
debug!("");
}
Expand Down Expand Up @@ -175,7 +172,7 @@ where
let pixel_shader_src = include_str!("shaders/model.frag");

// TODO: Cache program binary
let program = glium::Program::from_source(display, &vertex_shader_src, &pixel_shader_src, None);
let program = glium::Program::from_source(display, vertex_shader_src, pixel_shader_src, None);
let program = match program {
Ok(p) => p,
Err(glium::CompilationError(err, _)) => {
Expand Down Expand Up @@ -246,7 +243,7 @@ where
target
.draw(
(&vertex_buf, &normal_buf),
&indices,
indices,
&program,
&uniforms,
&params,
Expand All @@ -261,9 +258,8 @@ where
let pixels: glium::texture::RawImage2d<u8> = texture.read();
let img = image::ImageBuffer::from_raw(config.width, config.height, pixels.data.into_owned())
.unwrap();
let img = image::DynamicImage::ImageRgba8(img).flipv();

img
image::DynamicImage::ImageRgba8(img).flipv()
}

pub fn render_to_window(config: Config) -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -292,13 +288,14 @@ pub fn render_to_window(config: Config) -> Result<(), Box<dyn Error>> {
.unwrap();

match ev {
glutin::event::Event::WindowEvent { event, .. }
if event == glutin::event::WindowEvent::CloseRequested =>
{
glutin::event::Event::WindowEvent {
event: glutin::event::WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
return;
}
glutin::event::Event::NewEvents(cause) if cause == glutin::event::StartCause::Init => {
glutin::event::Event::NewEvents(glutin::event::StartCause::Init) => {
render_pipeline(&display, &config, &mesh, &mut framebuffer, &texture);
}
_ => (),
Expand Down Expand Up @@ -330,13 +327,11 @@ pub fn render_to_image(config: &Config) -> Result<image::DynamicImage, Box<dyn E
// =========================
let mesh = Mesh::load(&config.stl_filename, config.recalc_normals)?;

let img: image::DynamicImage;

// Create GL context
// =================
// 1. If not visible create a headless context.
// 2. If headless context creation fails, create a normal context with a hidden window.
match create_headless_display(&config) {
let img: image::DynamicImage = match create_headless_display(config) {
Ok(display) => {
let texture = glium::Texture2d::empty(&display, config.width, config.height).unwrap();
let depthtexture =
Expand All @@ -348,14 +343,14 @@ pub fn render_to_image(config: &Config) -> Result<image::DynamicImage, Box<dyn E
&depthtexture,
)
.unwrap();
img = render_pipeline(&display, &config, &mesh, &mut framebuffer, &texture);
render_pipeline(&display, config, &mesh, &mut framebuffer, &texture)
}
Err(e) => {
warn!(
"Unable to create headless GL context. Trying hidden window instead. Reason: {:?}",
e
);
let (display, _) = create_normal_display(&config)?;
let (display, _) = create_normal_display(config)?;
let texture = glium::Texture2d::empty(&display, config.width, config.height).unwrap();
let depthtexture =
glium::texture::DepthTexture2d::empty(&display, config.width, config.height)
Expand All @@ -366,15 +361,15 @@ pub fn render_to_image(config: &Config) -> Result<image::DynamicImage, Box<dyn E
&depthtexture,
)
.unwrap();
img = render_pipeline(&display, &config, &mesh, &mut framebuffer, &texture);
render_pipeline(&display, config, &mesh, &mut framebuffer, &texture)
}
};

Ok(img)
}

pub fn render_to_file(config: &Config) -> Result<(), Box<dyn Error>> {
let img = render_to_image(&config)?;
let img = render_to_image(config)?;

// Choose output
// Write to stdout if user did not specify a file
Expand Down Expand Up @@ -438,6 +433,11 @@ pub fn render_to_file(config: &Config) -> Result<(), Box<dyn Error>> {
///
/// render_to_buffer(buf_ptr, width, height, stl_filename_c);
/// ```
///
/// # Safety
///
/// * `buf_ptr` _must_ point to a valid initialized buffer, at least `width * height * 4` bytes long.
/// * `stl_filename_c` must point to a valid null-terminated string.
#[no_mangle]
pub unsafe extern "C" fn render_to_buffer(
buf_ptr: *mut u8,
Expand Down Expand Up @@ -476,8 +476,8 @@ pub unsafe extern "C" fn render_to_buffer(
// Setup configuration for the renderer
let config = Config {
stl_filename: stl_filename_str.to_string(),
width: width,
height: height,
width,
height,
..Default::default()
};

Expand Down
8 changes: 3 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ fn main() {
error!("Application error: {}", e);
process::exit(1);
}
} else {
if let Err(e) = stl_thumb::render_to_file(&config) {
error!("Application error: {}", e);
process::exit(1);
}
} else if let Err(e) = stl_thumb::render_to_file(&config) {
error!("Application error: {}", e);
process::exit(1);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,7 @@ impl Mesh {
let scale = 2.0 / longest;
info!("Scale:\t{}", scale);
let scale_matrix = cgmath::Matrix4::from_scale(scale);
let matrix = scale_matrix * translation_matrix;
matrix
scale_matrix * translation_matrix
}
}

Expand Down

0 comments on commit 767dad0

Please sign in to comment.