This commit is contained in:
aiden 2023-06-27 20:03:42 +01:00
parent a5473377a8
commit de86f40394
Signed by: aiden
GPG Key ID: EFA9C74AEBF806E0
4 changed files with 70 additions and 11 deletions

@ -184,6 +184,7 @@ fn real_main() -> Result<(), &'static str> {
frames += 1;
if total_elapsed >= 1.0 {
println!("frames in the past {total_elapsed}s: {frames:?}");
println!("camera at {:?}", state.camera.position);
frames = 0;
total_elapsed = 0.0;
}

@ -1,6 +1,6 @@
use std::{path::Path, assert_eq};
use crate::texture;
use crate::texture::Texture;
use image;
use wgpu::util::DeviceExt;
@ -11,9 +11,9 @@ fn load_texture(
file_name: &str,
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> texture::Texture {
) -> Texture {
let bytes = load_bytes(file_name);
return texture::Texture::from_image(device, queue, &(image::load_from_memory(&(bytes)).unwrap()), Some(file_name));
return Texture::from_image(device, queue, &(image::load_from_memory(&(bytes)).unwrap()), Some(file_name));
}
#[repr(C)]
@ -24,7 +24,7 @@ pub struct Vertex {
}
pub struct Material {
pub diffuse_texture: texture::Texture,
pub diffuse_texture: Texture,
pub bind_group: wgpu::BindGroup,
}
@ -53,8 +53,12 @@ pub fn load_obj(
let mut materials = Vec::new();
for m in obj_materials.unwrap() {
let x = format!("models/ruby/{}", m.diffuse_texture.unwrap()); // fixme
let diffuse_texture = load_texture(&(x), device, queue);
let diffuse_texture = if let Some(x) = m.diffuse_texture {
let x = format!("models/ruby/{}", x); // fixme
load_texture(&(x), device, queue)
} else {
Texture::solid(device, queue, 0xff0000ff, None)
};
let bind_group = device.create_bind_group(&(wgpu::BindGroupDescriptor {
layout,
entries: &[

@ -18,7 +18,7 @@ pub struct State {
render_pipeline: wgpu::RenderPipeline,
player: Player,
camera: Camera,
pub camera: Camera,
camera_uniform: CameraUniform,
camera_bind_group: wgpu::BindGroup,
@ -120,8 +120,8 @@ impl State {
}));*/
let player = Player {
position: (1.0, 0.25, -1.0).into(),
rot_x: Deg(0.0),
position: (-0.275, 1.25, -1.0).into(),
rot_x: Deg(90.0),
};
let mut camera = Camera::new(size, Deg(0.0));
camera.update_pos(&(player));
@ -180,7 +180,7 @@ impl State {
label: Some("texture_bind_group_layout"),
})));
let skin = obj::load_obj("models/skin.obj", &(device), &(queue), &(texture_bind_group_layout));
let skin = obj::load_obj("models/untitled.obj", &(device), &(queue), &(texture_bind_group_layout));
let render_pipeline_layout = device.create_pipeline_layout(&(wgpu::PipelineLayoutDescriptor {
label: Some("render_pipeline_layout"),
@ -411,7 +411,9 @@ impl State {
// and the player should also rotate so that
// its back is facing the camera.)
for mesh in &(skin.meshes) {
render_pass.set_bind_group(1, &(skin.materials[mesh.material].bind_group), &[]);
if skin.materials.len() > mesh.material {
render_pass.set_bind_group(1, &(skin.materials[mesh.material].bind_group), &[]);
}
render_pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
render_pass.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
render_pass.draw_indexed(0..mesh.num_elements, 0, 0..1);

@ -5,6 +5,58 @@ pub struct Texture {
}
impl Texture {
pub fn solid(
device: &wgpu::Device,
queue: &wgpu::Queue,
rgba: u32,
label: Option<&str>
) -> Self {
let size = wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 1,
};
let texture = device.create_texture(&(wgpu::TextureDescriptor {
label,
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
}));
queue.write_texture(
wgpu::ImageCopyTexture {
aspect: wgpu::TextureAspect::All,
texture: &(texture),
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
},
&(rgba.to_be_bytes()),
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(1 * 4),
rows_per_image: Some(1),
},
size
);
let view = texture.create_view(&(wgpu::TextureViewDescriptor::default()));
let sampler = device.create_sampler(&(wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Nearest,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
}));
return Self { texture, view, sampler };
}
pub fn from_image(
device: &wgpu::Device,
queue: &wgpu::Queue,