solid
This commit is contained in:
parent
a5473377a8
commit
de86f40394
@ -184,6 +184,7 @@ fn real_main() -> Result<(), &'static str> {
|
|||||||
frames += 1;
|
frames += 1;
|
||||||
if total_elapsed >= 1.0 {
|
if total_elapsed >= 1.0 {
|
||||||
println!("frames in the past {total_elapsed}s: {frames:?}");
|
println!("frames in the past {total_elapsed}s: {frames:?}");
|
||||||
|
println!("camera at {:?}", state.camera.position);
|
||||||
frames = 0;
|
frames = 0;
|
||||||
total_elapsed = 0.0;
|
total_elapsed = 0.0;
|
||||||
}
|
}
|
||||||
|
16
src/obj.rs
16
src/obj.rs
@ -1,6 +1,6 @@
|
|||||||
use std::{path::Path, assert_eq};
|
use std::{path::Path, assert_eq};
|
||||||
|
|
||||||
use crate::texture;
|
use crate::texture::Texture;
|
||||||
use image;
|
use image;
|
||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
|
|
||||||
@ -11,9 +11,9 @@ fn load_texture(
|
|||||||
file_name: &str,
|
file_name: &str,
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
) -> texture::Texture {
|
) -> Texture {
|
||||||
let bytes = load_bytes(file_name);
|
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)]
|
#[repr(C)]
|
||||||
@ -24,7 +24,7 @@ pub struct Vertex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Material {
|
pub struct Material {
|
||||||
pub diffuse_texture: texture::Texture,
|
pub diffuse_texture: Texture,
|
||||||
pub bind_group: wgpu::BindGroup,
|
pub bind_group: wgpu::BindGroup,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,8 +53,12 @@ pub fn load_obj(
|
|||||||
|
|
||||||
let mut materials = Vec::new();
|
let mut materials = Vec::new();
|
||||||
for m in obj_materials.unwrap() {
|
for m in obj_materials.unwrap() {
|
||||||
let x = format!("models/ruby/{}", m.diffuse_texture.unwrap()); // fixme
|
let diffuse_texture = if let Some(x) = m.diffuse_texture {
|
||||||
let diffuse_texture = load_texture(&(x), device, queue);
|
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 {
|
let bind_group = device.create_bind_group(&(wgpu::BindGroupDescriptor {
|
||||||
layout,
|
layout,
|
||||||
entries: &[
|
entries: &[
|
||||||
|
12
src/state.rs
12
src/state.rs
@ -18,7 +18,7 @@ pub struct State {
|
|||||||
render_pipeline: wgpu::RenderPipeline,
|
render_pipeline: wgpu::RenderPipeline,
|
||||||
|
|
||||||
player: Player,
|
player: Player,
|
||||||
camera: Camera,
|
pub camera: Camera,
|
||||||
camera_uniform: CameraUniform,
|
camera_uniform: CameraUniform,
|
||||||
camera_bind_group: wgpu::BindGroup,
|
camera_bind_group: wgpu::BindGroup,
|
||||||
|
|
||||||
@ -120,8 +120,8 @@ impl State {
|
|||||||
}));*/
|
}));*/
|
||||||
|
|
||||||
let player = Player {
|
let player = Player {
|
||||||
position: (1.0, 0.25, -1.0).into(),
|
position: (-0.275, 1.25, -1.0).into(),
|
||||||
rot_x: Deg(0.0),
|
rot_x: Deg(90.0),
|
||||||
};
|
};
|
||||||
let mut camera = Camera::new(size, Deg(0.0));
|
let mut camera = Camera::new(size, Deg(0.0));
|
||||||
camera.update_pos(&(player));
|
camera.update_pos(&(player));
|
||||||
@ -180,7 +180,7 @@ impl State {
|
|||||||
label: Some("texture_bind_group_layout"),
|
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 {
|
let render_pipeline_layout = device.create_pipeline_layout(&(wgpu::PipelineLayoutDescriptor {
|
||||||
label: Some("render_pipeline_layout"),
|
label: Some("render_pipeline_layout"),
|
||||||
@ -411,7 +411,9 @@ impl State {
|
|||||||
// and the player should also rotate so that
|
// and the player should also rotate so that
|
||||||
// its back is facing the camera.)
|
// its back is facing the camera.)
|
||||||
for mesh in &(skin.meshes) {
|
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_vertex_buffer(0, mesh.vertex_buffer.slice(..));
|
||||||
render_pass.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
|
render_pass.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
|
||||||
render_pass.draw_indexed(0..mesh.num_elements, 0, 0..1);
|
render_pass.draw_indexed(0..mesh.num_elements, 0, 0..1);
|
||||||
|
@ -5,6 +5,58 @@ pub struct Texture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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(
|
pub fn from_image(
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
|
Loading…
Reference in New Issue
Block a user