Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bytemuck = { version = "1", features = ["derive"] }
egui = { version = "0.34", optional = true, default-features = true }
egui-wgpu = { version = "0.34", optional = true }
image = { version = "0.25", default-features = false, features = ["default-formats"] }
log = "0.4"
web-time = "1"
kiss3d-macro = { version = "0.36.0", path = "kiss3d-macro" }
glamx = { version = "0.2", features = ["bytemuck"] }
Expand Down
8 changes: 4 additions & 4 deletions src/camera/sidescroll2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ impl Camera2d for PanZoomCamera2d {

self.last_cursor_pos = curr_pos;
}
WindowEvent::Scroll(_, off, modifiers) => {
if self.zoom_modifier.is_none() || self.zoom_modifier == Some(modifiers) {
self.handle_scroll(off as f32)
}
WindowEvent::Scroll(_, off, modifiers)
if (self.zoom_modifier.is_none() || self.zoom_modifier == Some(modifiers)) =>
{
self.handle_scroll(off as f32)
}
WindowEvent::FramebufferSize(w, h) => {
self.proj = Mat3::from_cols(
Expand Down
94 changes: 45 additions & 49 deletions src/loader/mtl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,53 @@ pub fn parse(string: &str) -> Vec<MtlMaterial> {

for (l, line) in string.lines().enumerate() {
let mut words = obj::split_words(line);
let tag = words.next();

match tag {
None => {}
Some(w) => {
if !w.is_empty() && w.as_bytes()[0] != b'#' {
let mut p = obj::split_words(line).peekable();
let _ = p.next();

if p.peek().is_none() {
continue;
}

match w {
// texture name
"newmtl" => {
let old = mem::replace(
&mut curr_material,
MtlMaterial::new_default(parse_name(l, words)),
);

if !old.name.is_empty() {
res.push(old);
}
}
// ambient color
"Ka" => curr_material.ambient = parse_color(l, words),
// diffuse color
"Kd" => curr_material.diffuse = parse_color(l, words),
// specular color
"Ks" => curr_material.specular = parse_color(l, words),
// shininess
"Ns" => curr_material.shininess = parse_scalar(l, words),
// alpha
"d" => curr_material.alpha = parse_scalar(l, words),
// ambient map
"map_Ka" => curr_material.ambient_texture = Some(parse_name(l, words)),
// diffuse texture map
"map_Kd" => curr_material.diffuse_texture = Some(parse_name(l, words)),
// specular texture map
"map_Ks" => curr_material.specular_texture = Some(parse_name(l, words)),
// specular texture map
"map_d" | "map_opacity" => {
curr_material.opacity_map = Some(parse_name(l, words))
}
_ => {
println!("Warning: unknown line {} ignored: `{}'", l, line);
}
}
let Some(w) = words.next() else {
continue;
};
if w.is_empty() || w.as_bytes()[0] == b'#' {
continue;
}

let mut p = obj::split_words(line).peekable();
let _ = p.next();

if p.peek().is_none() {
continue;
}

match w {
// texture name
"newmtl" => {
let old = mem::replace(
&mut curr_material,
MtlMaterial::new_default(parse_name(l, words)),
);

if !old.name.is_empty() {
res.push(old);
}
}
// ambient color
"Ka" => curr_material.ambient = parse_color(l, words),
// diffuse color
"Kd" => curr_material.diffuse = parse_color(l, words),
// specular color
"Ks" => curr_material.specular = parse_color(l, words),
// shininess
"Ns" => curr_material.shininess = parse_scalar(l, words),
// alpha
"d" => curr_material.alpha = parse_scalar(l, words),
// ambient map
"map_Ka" => curr_material.ambient_texture = Some(parse_name(l, words)),
// diffuse texture map
"map_Kd" => curr_material.diffuse_texture = Some(parse_name(l, words)),
// specular texture map
"map_Ks" => curr_material.specular_texture = Some(parse_name(l, words)),
// specular texture map
"map_d" | "map_opacity" => curr_material.opacity_map = Some(parse_name(l, words)),
_ => {
log::warn!("unknown line {} ignored: `{}'", l, line);
}
}
}

Expand Down
113 changes: 54 additions & 59 deletions src/loader/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn error(line: usize, err: &str) -> ! {
}

fn warn(line: usize, err: &str) {
println!("At line {}: {}", line, err)
log::warn!("at line {}: {}", line, err)
}

/// Parses an OBJ file and returns the meshes it contains.
Expand Down Expand Up @@ -141,68 +141,67 @@ pub fn parse(

for (l, line) in string.lines().enumerate() {
let mut words = split_words(line);
let tag = words.next();
match tag {
None => {}
Some(w) => {
if !w.is_empty() && w.as_bytes()[0] != b'#' {
match w {
"v" => coords.push(parse_v_or_vn(l, words)),
"vn" => {
if !ignore_normals {
normals.push(parse_v_or_vn(l, words))
}
}
"f" => parse_f(
l,
words,
&coords[..],
&uvs[..],
&normals[..],
&mut ignore_uvs,
&mut ignore_normals,
&mut groups_ids,
curr_group,
),
"vt" => {
if !ignore_uvs {
uvs.push(parse_vt(l, words))
}
}
"g" => {
curr_group = parse_g(l, words, basename, &mut groups, &mut groups_ids);
let _ = curr_mtl
.as_ref()
.map(|mtl| group2mtl.insert(curr_group, mtl.clone()));
}
"mtllib" => parse_mtllib(l, words, mtl_base_dir, &mut mtllib),
"usemtl" => {
curr_group = parse_usemtl(
l,
words,
curr_group,
&mtllib,
&mut group2mtl,
&mut groups,
&mut groups_ids,
&mut curr_mtl,
)
}
_ => {
println!("Warning: unknown line {} ignored: `{}'", l, line);
}
}
let Some(w) = words.next() else {
continue;
};
if w.is_empty() || w.as_bytes()[0] == b'#' {
continue;
}

match w {
"v" => coords.push(parse_v_or_vn(l, words)),
"vn" => {
if !ignore_normals {
normals.push(parse_v_or_vn(l, words))
}
}
"f" => parse_f(
l,
words,
&coords[..],
&uvs[..],
&normals[..],
&mut ignore_uvs,
&mut ignore_normals,
&mut groups_ids,
curr_group,
),
"vt" => {
if !ignore_uvs {
uvs.push(parse_vt(l, words))
}
}
"g" => {
curr_group = parse_g(l, words, basename, &mut groups, &mut groups_ids);
let _ = curr_mtl
.as_ref()
.map(|mtl| group2mtl.insert(curr_group, mtl.clone()));
}
"mtllib" => parse_mtllib(l, words, mtl_base_dir, &mut mtllib),
"usemtl" => {
curr_group = parse_usemtl(
l,
words,
curr_group,
&mtllib,
&mut group2mtl,
&mut groups,
&mut groups_ids,
&mut curr_mtl,
)
}
_ => {
log::warn!("unknown line {} ignored: `{}'", l, line);
}
}
}

if uvs.is_empty() && ignore_uvs {
println!("Warning: some texture coordinates are missing. Dropping texture coordinates infos for every vertex.");
log::warn!("some texture coordinates are missing. Dropping texture coordinates infos for every vertex.");
}

if normals.is_empty() && ignore_normals {
println!("Warning: some normals are missing. Dropping normals infos for every vertex.");
log::warn!("some normals are missing. Dropping normals infos for every vertex.");
}

reformat(
Expand Down Expand Up @@ -532,11 +531,7 @@ fn reformat(
)));

let mut meshes = Vec::new();
for ((fs, name), mtl) in resfs
.into_iter()
.zip(names.into_iter())
.zip(mtls.into_iter())
{
for ((fs, name), mtl) in resfs.into_iter().zip(names).zip(mtls) {
if !fs.is_empty() {
let fs = Arc::new(RwLock::new(GPUVec::new(
fs,
Expand Down
4 changes: 3 additions & 1 deletion src/resource/framebuffer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ impl OffscreenBuffers {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});

Expand Down
8 changes: 8 additions & 0 deletions src/window/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ impl Canvas {
self.canvas.copy_frame_to_readback(frame)
}

/// Copies an arbitrary texture into the readback texture used by `read_pixels`.
///
/// The source texture must have `COPY_SRC` usage and the same size and
/// format as the surface.
pub fn copy_texture_to_readback(&self, src: &wgpu::Texture) {
self.canvas.copy_texture_to_readback(src)
}

/// Reads pixels from the readback texture into the provided buffer.
/// Returns RGB data (3 bytes per pixel).
pub fn read_pixels(&self, out: &mut Vec<u8>, x: usize, y: usize, width: usize, height: usize) {
Expand Down
12 changes: 5 additions & 7 deletions src/window/egui_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,11 @@ impl Window {
modifiers: self.get_egui_modifiers(),
});
}
WindowEvent::Char(ch) => {
if !ch.is_control() {
self.egui_context
.raw_input
.events
.push(egui::Event::Text(ch.to_string()));
}
WindowEvent::Char(ch) if !ch.is_control() => {
self.egui_context
.raw_input
.events
.push(egui::Event::Text(ch.to_string()));
}
WindowEvent::Key(key, action, _modifiers) => {
if let Some(egui_key) = self.translate_key_to_egui(key) {
Expand Down
Loading
Loading