forked from StrafesNET/strafe-client
Compare commits
14 Commits
load_roblo
...
master
Author | SHA1 | Date | |
---|---|---|---|
05156d1741 | |||
d72e3fcabd | |||
99d4e51ee8 | |||
7effceff03 | |||
355956e532 | |||
8aad878d49 | |||
5fb9f8b1bc | |||
a9977c0bd2 | |||
fd34fbe367 | |||
75695946a0 | |||
2dba9554ea | |||
b5880d484a | |||
0b230b7597 | |||
988f204358 |
2866
models/teapot.obj
Normal file
2866
models/teapot.obj
Normal file
File diff suppressed because it is too large
Load Diff
84
src/main.rs
84
src/main.rs
@ -8,22 +8,25 @@ const IMAGE_SIZE: u32 = 128;
|
|||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
pos: [f32; 3],
|
pos: [f32; 3],
|
||||||
|
texture: [f32; 2],
|
||||||
normal: [f32; 3],
|
normal: [f32; 3],
|
||||||
}
|
}
|
||||||
|
|
||||||
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::Mat4,
|
||||||
|
vertex_buf: wgpu::Buffer,
|
||||||
entities: Vec<Entity>,
|
entities: Vec<Entity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Model {
|
struct Model {
|
||||||
transform: glam::Affine3A,
|
transform: glam::Mat4,
|
||||||
|
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,
|
||||||
@ -104,16 +107,16 @@ impl Camera {
|
|||||||
}else{
|
}else{
|
||||||
self.fov/5.0
|
self.fov/5.0
|
||||||
};
|
};
|
||||||
let proj = perspective_rh(fov, aspect, 1.0, 200.0);
|
let proj = perspective_rh(fov, aspect, 0.5, 1000.0);
|
||||||
let view = (glam::Mat4::from_translation(self.pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32)).inverse();
|
|
||||||
let proj_inv = proj.inverse();
|
let proj_inv = proj.inverse();
|
||||||
|
let view = glam::Mat4::from_translation(self.pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32);
|
||||||
|
let view_inv = view.inverse();
|
||||||
|
|
||||||
let mut raw = [0f32; 16 * 3 + 4];
|
let mut raw = [0f32; 16 * 3 + 4];
|
||||||
raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]);
|
raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]);
|
||||||
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]);
|
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]);
|
||||||
raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view)[..]);
|
raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]);
|
||||||
raw[48..51].copy_from_slice(AsRef::<[f32; 3]>::as_ref(&self.pos));
|
raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3)));
|
||||||
raw[51] = 1.0;//cam_pos is vec4
|
|
||||||
raw
|
raw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -156,43 +159,57 @@ impl Skybox {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_transform_uniform_data(transform:&glam::Affine3A) -> [f32; 4*4] {
|
fn get_transform_uniform_data(transform:&glam::Mat4) -> [f32; 4*4] {
|
||||||
let mut raw = [0f32; 4*4];
|
let mut raw = [0f32; 4*4];
|
||||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&glam::Mat4::from(*transform))[..]);
|
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(transform)[..]);
|
||||||
raw
|
raw
|
||||||
}
|
}
|
||||||
|
|
||||||
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){
|
||||||
|
indices.push(i as u16);
|
||||||
|
}else{
|
||||||
|
let i=vertices.len() as u16;
|
||||||
vertices.push(Vertex {
|
vertices.push(Vertex {
|
||||||
pos: data.position[position_id],
|
pos: data.position[vert.0],
|
||||||
normal: data.normal[normal_id.unwrap()],
|
texture: data.texture[vert.1.unwrap()],
|
||||||
})
|
normal: data.normal[vert.2.unwrap()],
|
||||||
|
});
|
||||||
|
vertex_index.insert(vert,i);
|
||||||
|
indices.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("Index"),
|
||||||
|
contents: bytemuck::cast_slice(&indices),
|
||||||
|
usage: wgpu::BufferUsages::INDEX,
|
||||||
|
});
|
||||||
|
entities.push(Entity {
|
||||||
|
index_buf,
|
||||||
|
index_count: indices.len() as u32,
|
||||||
|
});
|
||||||
|
}
|
||||||
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
label: Some("Vertex"),
|
label: Some("Vertex"),
|
||||||
contents: bytemuck::cast_slice(&vertices),
|
contents: bytemuck::cast_slice(&vertices),
|
||||||
usage: wgpu::BufferUsages::VERTEX,
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
});
|
});
|
||||||
entities.push(Entity {
|
|
||||||
vertex_count: vertices.len() as u32,
|
|
||||||
vertex_buf,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
modeldatas.push(ModelData {
|
modeldatas.push(ModelData {
|
||||||
transform: glam::Affine3A::default(),
|
transform: glam::Mat4::default(),
|
||||||
|
vertex_buf,
|
||||||
entities,
|
entities,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -214,8 +231,10 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
let mut modeldatas = Vec::<ModelData>::new();
|
let mut modeldatas = Vec::<ModelData>::new();
|
||||||
add_obj(device,& mut modeldatas,include_bytes!("../models/teslacyberv3.0.obj"));
|
add_obj(device,& mut modeldatas,include_bytes!("../models/teslacyberv3.0.obj"));
|
||||||
add_obj(device,& mut modeldatas,include_bytes!("../models/suzanne.obj"));
|
add_obj(device,& mut modeldatas,include_bytes!("../models/suzanne.obj"));
|
||||||
|
add_obj(device,& mut modeldatas,include_bytes!("../models/teapot.obj"));
|
||||||
println!("models.len = {:?}", modeldatas.len());
|
println!("models.len = {:?}", modeldatas.len());
|
||||||
modeldatas[1].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.));
|
modeldatas[1].transform=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
|
||||||
|
modeldatas[2].transform=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
|
||||||
|
|
||||||
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
@ -336,7 +355,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
buffers: &[wgpu::VertexBufferLayout {
|
buffers: &[wgpu::VertexBufferLayout {
|
||||||
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
||||||
step_mode: wgpu::VertexStepMode::Vertex,
|
step_mode: wgpu::VertexStepMode::Vertex,
|
||||||
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3],
|
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Float32x3],
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
fragment: Some(wgpu::FragmentState {
|
fragment: Some(wgpu::FragmentState {
|
||||||
@ -489,7 +508,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//drain the modeldata vec so entities can be /moved/ to models.entities
|
//drain the modeldata vec so entities can be /moved/ to models.entities
|
||||||
let mut models = Vec::<Model>::new();
|
let mut models = Vec::<Model>::with_capacity(modeldatas.len());
|
||||||
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
||||||
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
|
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
|
||||||
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
@ -510,9 +529,10 @@ 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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -584,8 +604,8 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn move_mouse(&mut self, delta: (f64,f64)) {
|
fn move_mouse(&mut self, delta: (f64,f64)) {
|
||||||
self.camera.pitch=(self.camera.pitch as f64+delta.1/-512.) as f32;
|
self.camera.pitch=(self.camera.pitch as f64+delta.1/-2048.) as f32;
|
||||||
self.camera.yaw=(self.camera.yaw as f64+delta.0/-512.) as f32;
|
self.camera.yaw=(self.camera.yaw as f64+delta.0/-2048.) as f32;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize(
|
fn resize(
|
||||||
@ -652,6 +672,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
device,
|
device,
|
||||||
)
|
)
|
||||||
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
||||||
|
//This code only needs to run when the uniforms change
|
||||||
for model in self.models.iter() {
|
for model in self.models.iter() {
|
||||||
let model_uniforms = get_transform_uniform_data(&model.transform);
|
let model_uniforms = get_transform_uniform_data(&model.transform);
|
||||||
self.staging_belt
|
self.staging_belt
|
||||||
@ -697,10 +718,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,8 @@ fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput {
|
|||||||
|
|
||||||
struct EntityOutput {
|
struct EntityOutput {
|
||||||
@builtin(position) position: vec4<f32>,
|
@builtin(position) position: vec4<f32>,
|
||||||
@location(1) normal: vec3<f32>,
|
@location(1) texture: vec2<f32>,
|
||||||
|
@location(2) normal: vec3<f32>,
|
||||||
@location(3) view: vec3<f32>,
|
@location(3) view: vec3<f32>,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -74,11 +75,13 @@ var<uniform> r_EntityTransform: mat4x4<f32>;
|
|||||||
@vertex
|
@vertex
|
||||||
fn vs_entity(
|
fn vs_entity(
|
||||||
@location(0) pos: vec3<f32>,
|
@location(0) pos: vec3<f32>,
|
||||||
@location(1) normal: vec3<f32>,
|
@location(1) texture: vec2<f32>,
|
||||||
|
@location(2) normal: vec3<f32>,
|
||||||
) -> EntityOutput {
|
) -> EntityOutput {
|
||||||
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
|
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
|
||||||
var result: EntityOutput;
|
var result: EntityOutput;
|
||||||
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
|
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
|
||||||
|
result.texture=texture;
|
||||||
result.view = position.xyz - r_data.cam_pos.xyz;
|
result.view = position.xyz - r_data.cam_pos.xyz;
|
||||||
result.position = r_data.proj * r_data.view * position;
|
result.position = r_data.proj * r_data.view * position;
|
||||||
return result;
|
return result;
|
||||||
@ -100,10 +103,13 @@ fn fs_sky(vertex: SkyOutput) -> @location(0) vec4<f32> {
|
|||||||
fn fs_entity(vertex: EntityOutput) -> @location(0) vec4<f32> {
|
fn fs_entity(vertex: EntityOutput) -> @location(0) vec4<f32> {
|
||||||
let incident = normalize(vertex.view);
|
let incident = normalize(vertex.view);
|
||||||
let normal = normalize(vertex.normal);
|
let normal = normalize(vertex.normal);
|
||||||
let reflected = incident - 2.0 * dot(normal, incident) * normal;
|
let d = dot(normal, incident);
|
||||||
|
let reflected = incident - 2.0 * d * normal;
|
||||||
|
|
||||||
|
let dir = vec3<f32>(-1.0)+2.0*vec3<f32>(vertex.texture.x,0.0,vertex.texture.y);
|
||||||
|
let texture_color = textureSample(r_texture, r_sampler, dir).rgb;
|
||||||
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
|
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
|
||||||
return vec4<f32>(vec3<f32>(0.1) + 0.5 * reflected_color, 1.0);
|
return vec4<f32>(mix(vec3<f32>(0.1) + 0.5 * reflected_color,texture_color,1.0-pow(1.0-abs(d),2.0)), 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn modulo_euclidean (a: f32, b: f32) -> f32 {
|
fn modulo_euclidean (a: f32, b: f32) -> f32 {
|
||||||
|
Loading…
Reference in New Issue
Block a user