forked from StrafesNET/strafe-client
indexed rendering
This commit is contained in:
parent
2dba9554ea
commit
75695946a0
51
src/main.rs
51
src/main.rs
@ -12,18 +12,20 @@ struct Vertex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Entity {
|
struct Entity {
|
||||||
vertex_count: u32,
|
index_count: u32,
|
||||||
vertex_buf: wgpu::Buffer,
|
index_buf: wgpu::Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
//temp?
|
//temp?
|
||||||
struct ModelData {
|
struct ModelData {
|
||||||
transform: glam::Affine3A,
|
transform: glam::Affine3A,
|
||||||
|
vertex_buf: wgpu::Buffer,
|
||||||
entities: Vec<Entity>,
|
entities: Vec<Entity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Model {
|
struct Model {
|
||||||
transform: glam::Affine3A,
|
transform: glam::Affine3A,
|
||||||
|
vertex_buf: wgpu::Buffer,
|
||||||
entities: Vec<Entity>,
|
entities: Vec<Entity>,
|
||||||
bind_group: wgpu::BindGroup,
|
bind_group: wgpu::BindGroup,
|
||||||
model_buf: wgpu::Buffer,
|
model_buf: wgpu::Buffer,
|
||||||
@ -165,34 +167,47 @@ fn get_transform_uniform_data(transform:&glam::Affine3A) -> [f32; 4*4] {
|
|||||||
fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
|
fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
|
||||||
let data = obj::ObjData::load_buf(&source[..]).unwrap();
|
let data = obj::ObjData::load_buf(&source[..]).unwrap();
|
||||||
let mut vertices = Vec::new();
|
let mut vertices = Vec::new();
|
||||||
|
let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new();
|
||||||
for object in data.objects {
|
for object in data.objects {
|
||||||
let mut entities = Vec::<Entity>::new();
|
let mut entities = Vec::<Entity>::new();
|
||||||
for group in object.groups {
|
for group in object.groups {
|
||||||
vertices.clear();
|
let mut indices = Vec::new();
|
||||||
for poly in group.polys {
|
for poly in group.polys {
|
||||||
for end_index in 2..poly.0.len() {
|
for end_index in 2..poly.0.len() {
|
||||||
for &index in &[0, end_index - 1, end_index] {
|
for &index in &[0, end_index - 1, end_index] {
|
||||||
let obj::IndexTuple(position_id, _texture_id, normal_id) =
|
let vert = poly.0[index];
|
||||||
poly.0[index];
|
if let Some(&i)=vertex_index.get(&vert){
|
||||||
vertices.push(Vertex {
|
indices.push(i as u16);
|
||||||
pos: data.position[position_id],
|
}else{
|
||||||
normal: data.normal[normal_id.unwrap()],
|
let i=vertices.len() as u16;
|
||||||
})
|
vertices.push(Vertex {
|
||||||
|
pos: data.position[vert.0],
|
||||||
|
normal: data.normal[vert.2.unwrap()],
|
||||||
|
});
|
||||||
|
vertex_index.insert(vert,i);
|
||||||
|
indices.push(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
label: Some("Vertex"),
|
label: Some("Index"),
|
||||||
contents: bytemuck::cast_slice(&vertices),
|
contents: bytemuck::cast_slice(&indices),
|
||||||
usage: wgpu::BufferUsages::VERTEX,
|
usage: wgpu::BufferUsages::INDEX,
|
||||||
});
|
});
|
||||||
entities.push(Entity {
|
entities.push(Entity {
|
||||||
vertex_count: vertices.len() as u32,
|
index_buf,
|
||||||
vertex_buf,
|
index_count: indices.len() as u32,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("Vertex"),
|
||||||
|
contents: bytemuck::cast_slice(&vertices),
|
||||||
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
|
});
|
||||||
modeldatas.push(ModelData {
|
modeldatas.push(ModelData {
|
||||||
transform: glam::Affine3A::default(),
|
transform: glam::Affine3A::default(),
|
||||||
|
vertex_buf,
|
||||||
entities,
|
entities,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -512,6 +527,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
//all of these are being moved here
|
//all of these are being moved here
|
||||||
models.push(Model{
|
models.push(Model{
|
||||||
transform: modeldata.transform,
|
transform: modeldata.transform,
|
||||||
|
vertex_buf:modeldata.vertex_buf,
|
||||||
entities: modeldata.entities,
|
entities: modeldata.entities,
|
||||||
bind_group: model_bind_group,
|
bind_group: model_bind_group,
|
||||||
model_buf: model_buf,
|
model_buf: model_buf,
|
||||||
@ -700,10 +716,11 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
rpass.set_pipeline(&self.entity_pipeline);
|
rpass.set_pipeline(&self.entity_pipeline);
|
||||||
for model in self.models.iter() {
|
for model in self.models.iter() {
|
||||||
rpass.set_bind_group(1, &model.bind_group, &[]);
|
rpass.set_bind_group(1, &model.bind_group, &[]);
|
||||||
|
rpass.set_vertex_buffer(0, model.vertex_buf.slice(..));
|
||||||
|
|
||||||
for entity in model.entities.iter() {
|
for entity in model.entities.iter() {
|
||||||
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
|
rpass.set_index_buffer(entity.index_buf.slice(..), wgpu::IndexFormat::Uint16);
|
||||||
rpass.draw(0..entity.vertex_count, 0..1);
|
rpass.draw_indexed(0..entity.index_count, 0, 0..1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user