3d model loading

This commit is contained in:
Werner
2021-11-05 12:36:09 -03:00
parent 7792887fba
commit c5dbf96177
14 changed files with 746 additions and 577 deletions

View File

@ -1,5 +1,6 @@
use anyhow::*;
use image::GenericImageView;
use std::path::Path;
pub struct Texture {
pub texture: wgpu::Texture,
@ -9,6 +10,18 @@ pub struct Texture {
impl Texture {
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
pub fn load<P: AsRef<Path>>(
device: &wgpu::Device,
queue: &wgpu::Queue,
path: P,
) -> Result<Self> {
// Needed to appease the borrow checker
let path_copy = path.as_ref().to_path_buf();
let label = path_copy.to_str();
let img = image::open(path)?;
Self::from_image(device, queue, &img, label)
}
pub fn from_bytes(
device: &wgpu::Device,
@ -26,7 +39,7 @@ impl Texture {
img: &image::DynamicImage,
label: Option<&str>,
) -> Result<Self> {
let rgba = img.as_rgba8().unwrap();
let rgba = img.to_rgba8();
let dimensions = img.dimensions();
let size = wgpu::Extent3d {
@ -52,7 +65,7 @@ impl Texture {
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
},
rgba,
&rgba,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: std::num::NonZeroU32::new(4 * dimensions.0),