mirror of
https://github.com/guilhermewerner/wgpu-renderer
synced 2025-06-18 23:04:18 +00:00
3d model loading
This commit is contained in:
@ -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),
|
||||
|
Reference in New Issue
Block a user