From 44c81cc34f6fe8b09cb67b0ec2e8e661d6cd3c89 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 31 Jul 2024 12:14:04 +0200 Subject: [PATCH 1/6] example for projection (+ bug detected) --- crates/parry3d/Cargo.toml | 17 +- crates/parry3d/examples/project3d_animated.rs | 157 ++++++++++++++++++ 2 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 crates/parry3d/examples/project3d_animated.rs diff --git a/crates/parry3d/Cargo.toml b/crates/parry3d/Cargo.toml index 62545af3..99c037f1 100644 --- a/crates/parry3d/Cargo.toml +++ b/crates/parry3d/Cargo.toml @@ -22,11 +22,23 @@ workspace = true [features] default = ["required-features", "std"] required-features = ["dim3", "f32"] -std = ["nalgebra/std", "slab", "rustc-hash", "simba/std", "arrayvec/std", "spade", "thiserror"] +std = [ + "nalgebra/std", + "slab", + "rustc-hash", + "simba/std", + "arrayvec/std", + "spade", + "thiserror", +] dim3 = [] f32 = [] serde-serialize = ["serde", "nalgebra/serde-serialize", "bitflags/serde"] -rkyv-serialize = ["rkyv/validation", "nalgebra/rkyv-serialize", "simba/rkyv-serialize"] +rkyv-serialize = [ + "rkyv/validation", + "nalgebra/rkyv-serialize", + "simba/rkyv-serialize", +] bytemuck-serialize = ["bytemuck", "nalgebra/convert-bytemuck"] simd-stable = ["simba/wide", "simd-is-enabled"] @@ -76,3 +88,4 @@ obj = { version = "0.10.2", optional = true } oorandom = "11" ptree = "0.4.0" rand = { version = "0.8" } +macroquad = "*" diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs new file mode 100644 index 00000000..6c64486c --- /dev/null +++ b/crates/parry3d/examples/project3d_animated.rs @@ -0,0 +1,157 @@ +use macroquad::models::Vertex; +use macroquad::prelude::*; +use nalgebra::{Point3, UnitVector3, Vector3}; +use parry3d::math::{Isometry, Real}; +use parry3d::query::{IntersectResult, PointQuery}; +use parry3d::shape::TriMesh; + +fn build_diamond(position: &Isometry) -> (Vec>, Vec<[u32; 3]>) { + // Two tetrahedrons sharing a face + let points = vec![ + position * Point3::new(0.0, 2.0, 0.0), + position * Point3::new(-2.0, -1.0, 0.0), + position * Point3::new(0.0, 0.0, 2.0), + position * Point3::new(2.0, -1.0, 0.0), + position * Point3::new(0.0, 0.0, -2.0), + ]; + + let indices = vec![ + [0u32, 1, 2], + [0, 2, 3], + [1, 2, 3], + [0, 1, 4], + [0, 4, 3], + [1, 4, 3], + ]; + + (points, indices) +} + +#[macroquad::main("parry3d::query::PlaneIntersection")] +async fn main() { + // + // This is useful to test for https://github.com/dimforge/parry/pull/248 + let _points = vec![ + Point3::from([0.0, 0.0, 0.0]), + Point3::from([0.0, 0.0, 1.0]), + Point3::from([1.0, 0.0, 0.0]), + Point3::from([1.0, 0.0, 1.0]), + ]; + let _indices: Vec<[u32; 3]> = vec![[0, 1, 2], [1, 3, 2]]; + // + // + + let (points, indices) = build_diamond(&Isometry::identity()); + + let mesh = Mesh { + vertices: points + .iter() + .map(|p| Vertex { + position: mquad_from_na(*p), + uv: Vec2::new(p.x, p.y), + color: Color::new(0.9, 0.9, 0.9, 0.7), + }) + .collect(), + indices: indices.iter().flatten().map(|v| *v as u16).collect(), + texture: None, + }; + let trimesh = TriMesh::new(points, indices); + + for _i in 1.. { + clear_background(BLACK); + + let elapsed_time = get_time() as f32; + let slow_elapsed_time = elapsed_time / 3.0; + + let sin = (elapsed_time / 3f32).sin(); + let bias = 1.5 * sin.abs(); + let rotation = Quat::from_axis_angle( + Vec3::new(slow_elapsed_time.sin(), slow_elapsed_time.cos(), 1f32), + (elapsed_time * 50f32).to_radians(), + ); + let up_plane_vector = rotation * Vec3::Y; + let point_to_project = up_plane_vector * bias; + let projected_point = trimesh.project_point( + &Isometry::identity(), + &na_from_mquad(point_to_project), + true, + ); + + let slow_elapsed_time = slow_elapsed_time / 2.0; + // Going 3d! + set_camera(&Camera3D { + position: Vec3::new( + slow_elapsed_time.sin() * 5.0, + slow_elapsed_time.sin(), + slow_elapsed_time.cos() * 5.0, + ), + up: Vec3::new(0f32, 1f32, 0f32), + target: Vec3::new(0.5f32, 0f32, 0.5f32), + ..Default::default() + }); + + /* + * + * Render the projection + * + */ + let color = if projected_point.is_inside { + RED + } else { + YELLOW + }; + + draw_line_3d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + draw_sphere(point_to_project, 0.1, None, color); + + draw_line_3d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + + // fixed point inside + let point_to_project = Vec3::ZERO; + let projected_point = trimesh.project_point( + &Isometry::identity(), + &na_from_mquad(point_to_project), + true, + ); + let color = if projected_point.is_inside { + RED + } else { + YELLOW + }; + draw_line_3d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + draw_sphere(point_to_project, 0.1, None, color); + + // Mesh is rendered in the back. + draw_mesh(&mesh); + + next_frame().await + } +} + +fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { + for i in 0..polygon.len() { + let a = polygon[i].0; + let b = polygon[i].1; + draw_line_3d(a, b, color); + } +} + +fn mquad_from_na(a: Point3) -> Vec3 { + Vec3::new(a.x, a.y, a.z) +} + +fn na_from_mquad(a: Vec3) -> Point3 { + Point3::new(a.x, a.y, a.z) +} From 9526e753eac701448961756acc152cb14523d6ef Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 31 Jul 2024 20:47:24 +0200 Subject: [PATCH 2/6] simpler project example --- crates/parry3d/examples/project3d_animated.rs | 66 +++++-------------- 1 file changed, 18 insertions(+), 48 deletions(-) diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs index 6c64486c..f3e9e27b 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project3d_animated.rs @@ -1,31 +1,9 @@ use macroquad::models::Vertex; use macroquad::prelude::*; -use nalgebra::{Point3, UnitVector3, Vector3}; +use nalgebra::{Point3, Vector3}; use parry3d::math::{Isometry, Real}; -use parry3d::query::{IntersectResult, PointQuery}; -use parry3d::shape::TriMesh; - -fn build_diamond(position: &Isometry) -> (Vec>, Vec<[u32; 3]>) { - // Two tetrahedrons sharing a face - let points = vec![ - position * Point3::new(0.0, 2.0, 0.0), - position * Point3::new(-2.0, -1.0, 0.0), - position * Point3::new(0.0, 0.0, 2.0), - position * Point3::new(2.0, -1.0, 0.0), - position * Point3::new(0.0, 0.0, -2.0), - ]; - - let indices = vec![ - [0u32, 1, 2], - [0, 2, 3], - [1, 2, 3], - [0, 1, 4], - [0, 4, 3], - [1, 4, 3], - ]; - - (points, indices) -} +use parry3d::query::PointQuery; +use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags}; #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { @@ -38,10 +16,8 @@ async fn main() { Point3::from([1.0, 0.0, 1.0]), ]; let _indices: Vec<[u32; 3]> = vec![[0, 1, 2], [1, 3, 2]]; - // - // - let (points, indices) = build_diamond(&Isometry::identity()); + let (points, indices) = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh(); let mesh = Mesh { vertices: points @@ -55,22 +31,24 @@ async fn main() { indices: indices.iter().flatten().map(|v| *v as u16).collect(), texture: None, }; - let trimesh = TriMesh::new(points, indices); - + let trimesh = TriMesh::with_flags( + points.clone(), + indices.clone(), + TriMeshFlags::ORIENTED + | TriMeshFlags::DELETE_BAD_TOPOLOGY_TRIANGLES + | TriMeshFlags::FIX_INTERNAL_EDGES, + ); for _i in 1.. { clear_background(BLACK); let elapsed_time = get_time() as f32; let slow_elapsed_time = elapsed_time / 3.0; - let sin = (elapsed_time / 3f32).sin(); - let bias = 1.5 * sin.abs(); - let rotation = Quat::from_axis_angle( - Vec3::new(slow_elapsed_time.sin(), slow_elapsed_time.cos(), 1f32), - (elapsed_time * 50f32).to_radians(), - ); - let up_plane_vector = rotation * Vec3::Y; - let point_to_project = up_plane_vector * bias; + let point_to_project = Vec3::new( + slow_elapsed_time.sin(), + slow_elapsed_time.cos() * 1.5, + (elapsed_time - slow_elapsed_time).cos(), + ) * slow_elapsed_time.sin().abs(); let projected_point = trimesh.project_point( &Isometry::identity(), &na_from_mquad(point_to_project), @@ -126,13 +104,13 @@ async fn main() { } else { YELLOW }; + draw_sphere(point_to_project, 0.1, None, color); + draw_line_3d( point_to_project, mquad_from_na(projected_point.point), color, ); - draw_sphere(point_to_project, 0.1, None, color); - // Mesh is rendered in the back. draw_mesh(&mesh); @@ -140,14 +118,6 @@ async fn main() { } } -fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { - for i in 0..polygon.len() { - let a = polygon[i].0; - let b = polygon[i].1; - draw_line_3d(a, b, color); - } -} - fn mquad_from_na(a: Point3) -> Vec3 { Vec3::new(a.x, a.y, a.z) } From 113046d3637974b2419e8e75abb69a5d40be8acb Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 31 Jul 2024 21:08:45 +0200 Subject: [PATCH 3/6] use lissajous trajectory + small polish pass --- crates/parry3d/examples/project3d_animated.rs | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs index f3e9e27b..be8260fd 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project3d_animated.rs @@ -1,3 +1,5 @@ +use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; + use macroquad::models::Vertex; use macroquad::prelude::*; use nalgebra::{Point3, Vector3}; @@ -5,6 +7,16 @@ use parry3d::math::{Isometry, Real}; use parry3d::query::PointQuery; use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags}; +fn lissajous_3d(t: f32) -> Vec3 { + // Some hardcoded parameters to have a pleasing lissajous trajectory. + let (a, b, c, delta_x, delta_y, delta_z) = (3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6); + + let x = (a * t + delta_x).sin(); + let y = (b * t + delta_y).sin(); + let z = (c * t + delta_z).sin(); + Vec3::new(x, y, z) * 0.75f32 +} + #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { // @@ -31,31 +43,20 @@ async fn main() { indices: indices.iter().flatten().map(|v| *v as u16).collect(), texture: None, }; - let trimesh = TriMesh::with_flags( - points.clone(), - indices.clone(), - TriMeshFlags::ORIENTED - | TriMeshFlags::DELETE_BAD_TOPOLOGY_TRIANGLES - | TriMeshFlags::FIX_INTERNAL_EDGES, - ); + let trimesh = TriMesh::with_flags(points, indices, TriMeshFlags::ORIENTED); for _i in 1.. { clear_background(BLACK); let elapsed_time = get_time() as f32; let slow_elapsed_time = elapsed_time / 3.0; - let point_to_project = Vec3::new( - slow_elapsed_time.sin(), - slow_elapsed_time.cos() * 1.5, - (elapsed_time - slow_elapsed_time).cos(), - ) * slow_elapsed_time.sin().abs(); + let point_to_project = lissajous_3d(slow_elapsed_time); let projected_point = trimesh.project_point( &Isometry::identity(), &na_from_mquad(point_to_project), true, ); - let slow_elapsed_time = slow_elapsed_time / 2.0; // Going 3d! set_camera(&Camera3D { position: Vec3::new( @@ -63,8 +64,8 @@ async fn main() { slow_elapsed_time.sin(), slow_elapsed_time.cos() * 5.0, ), - up: Vec3::new(0f32, 1f32, 0f32), - target: Vec3::new(0.5f32, 0f32, 0.5f32), + up: Vec3::Y, + target: Vec3::ZERO, ..Default::default() }); @@ -111,7 +112,7 @@ async fn main() { mquad_from_na(projected_point.point), color, ); - // Mesh is rendered in the back. + // Mesh is rendered in the back, so we can see the other graphics elements draw_mesh(&mesh); next_frame().await From 5ff2b684dca55313513642408bfa0db2936f0700 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Thu, 8 Aug 2024 09:24:14 +0200 Subject: [PATCH 4/6] use reimagine macroquad branch --- crates/parry2d/Cargo.toml | 25 ++- crates/parry3d/Cargo.toml | 2 +- crates/parry3d/examples/project3d_animated.rs | 200 ++++++++++++++---- 3 files changed, 185 insertions(+), 42 deletions(-) diff --git a/crates/parry2d/Cargo.toml b/crates/parry2d/Cargo.toml index 19b7e19d..554943dc 100644 --- a/crates/parry2d/Cargo.toml +++ b/crates/parry2d/Cargo.toml @@ -22,11 +22,28 @@ workspace = true [features] default = ["required-features", "std"] required-features = ["dim2", "f32"] -std = ["nalgebra/std", "slab", "rustc-hash", "simba/std", "arrayvec/std", "spade", "thiserror"] +std = [ + "nalgebra/std", + "slab", + "rustc-hash", + "simba/std", + "arrayvec/std", + "spade", + "thiserror", +] dim2 = [] f32 = [] -serde-serialize = ["serde", "nalgebra/serde-serialize", "arrayvec/serde", "bitflags/serde"] -rkyv-serialize = ["rkyv/validation", "nalgebra/rkyv-serialize", "simba/rkyv-serialize"] +serde-serialize = [ + "serde", + "nalgebra/serde-serialize", + "arrayvec/serde", + "bitflags/serde", +] +rkyv-serialize = [ + "rkyv/validation", + "nalgebra/rkyv-serialize", + "simba/rkyv-serialize", +] bytemuck-serialize = ["bytemuck", "nalgebra/convert-bytemuck"] simd-stable = ["simba/wide", "simd-is-enabled"] simd-nightly = ["simba/portable_simd", "simd-is-enabled"] @@ -73,4 +90,4 @@ simba = { version = "0.9", default-features = false } oorandom = "11" ptree = "0.4.0" rand = { version = "0.8" } -macroquad = "0.4" \ No newline at end of file +macroquad = { git = "https://github.com/not-fl3/macroquad.git", branch = "reimagine" } diff --git a/crates/parry3d/Cargo.toml b/crates/parry3d/Cargo.toml index 99c037f1..58d94379 100644 --- a/crates/parry3d/Cargo.toml +++ b/crates/parry3d/Cargo.toml @@ -88,4 +88,4 @@ obj = { version = "0.10.2", optional = true } oorandom = "11" ptree = "0.4.0" rand = { version = "0.8" } -macroquad = "*" +macroquad = { git = "https://github.com/not-fl3/macroquad.git", branch = "reimagine" } diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs index be8260fd..0622fc5b 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project3d_animated.rs @@ -1,7 +1,21 @@ use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; -use macroquad::models::Vertex; -use macroquad::prelude::*; +use macroquad::gizmos::{draw_gizmos, gizmos_add_line, init_gizmos}; +use macroquad::math::{vec2, vec3, Vec2, Vec3}; +use macroquad::miniquad; +use macroquad::quad_gl::camera::{Camera, Projection}; +use macroquad::quad_gl::models::CpuMesh; +use macroquad::quad_gl::QuadGl; +use macroquad::{ + quad_gl::{ + camera::Environment, + color::{self, Color}, + scene::Shader, + ui::{hash, widgets}, + }, + window::next_frame, +}; + use nalgebra::{Point3, Vector3}; use parry3d::math::{Isometry, Real}; use parry3d::query::PointQuery; @@ -17,8 +31,13 @@ fn lissajous_3d(t: f32) -> Vec3 { Vec3::new(x, y, z) * 0.75f32 } -#[macroquad::main("parry3d::query::PlaneIntersection")] -async fn main() { +fn main() { + macroquad::start(Default::default(), |ctx| main_loop(ctx)); +} + +async fn main_loop(ctx: macroquad::Context) { + init_gizmos(&ctx); + // // This is useful to test for https://github.com/dimforge/parry/pull/248 let _points = vec![ @@ -31,24 +50,61 @@ async fn main() { let (points, indices) = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh(); - let mesh = Mesh { - vertices: points - .iter() - .map(|p| Vertex { - position: mquad_from_na(*p), - uv: Vec2::new(p.x, p.y), - color: Color::new(0.9, 0.9, 0.9, 0.7), - }) - .collect(), - indices: indices.iter().flatten().map(|v| *v as u16).collect(), - texture: None, - }; + let mut scene = ctx.new_scene(); + + let quad_gl = QuadGl::new(ctx.quad_ctx.clone()); + + let cpu_mesh = mquad_mesh_from_parry(&indices, &points); + + // No clone on CpuMesh :'( + let cpu_mesh2 = mquad_mesh_from_parry(&indices, &points); + + let mut mesh = quad_gl.mesh(cpu_mesh, None); + + mesh.nodes[0].materials[0].shader = Shader::new( + ctx.quad_ctx.lock().unwrap().as_mut(), + vec![], + Some(FRAGMENT), + Some(VERTEX), + ); + + scene.add_model(&mesh); let trimesh = TriMesh::with_flags(points, indices, TriMeshFlags::ORIENTED); + let mut canvas = ctx.new_canvas(); + + let mut camera = Camera { + environment: Environment::SolidColor(color::BLACK), + depth_enabled: true, + projection: Projection::Perspective, + position: vec3(0., 1.5, 4.), + up: vec3(0., 1., 0.), + target: vec3(0., 0., 0.), + z_near: 0.1, + z_far: 1500.0, + ..Default::default() + }; + + // FIXME: that's framerate dependent + let mut elapsed_time = 0.0f32; for _i in 1.. { - clear_background(BLACK); + elapsed_time += 0.1; + ctx.clear_screen(color::BLACK); + canvas.clear(); + + // To show vertices winding order, I display each edge of each faces sequentially. + let current_edge_index = (elapsed_time / 3.0) as usize; + let current_face_index = current_edge_index / 3; + let first_vertice_index = current_face_index * 3; + let first_point = first_vertice_index + current_edge_index % 3; + let second_point = first_vertice_index + (current_edge_index + 1) % 3; + // No color lines :( + gizmos_add_line( + false, + cpu_mesh2.0[first_point % cpu_mesh2.0.len()], + cpu_mesh2.0[second_point % cpu_mesh2.0.len()], + ); - let elapsed_time = get_time() as f32; - let slow_elapsed_time = elapsed_time / 3.0; + let slow_elapsed_time = elapsed_time / 10.0; let point_to_project = lissajous_3d(slow_elapsed_time); let projected_point = trimesh.project_point( @@ -57,27 +113,25 @@ async fn main() { true, ); - // Going 3d! - set_camera(&Camera3D { - position: Vec3::new( - slow_elapsed_time.sin() * 5.0, - slow_elapsed_time.sin(), - slow_elapsed_time.cos() * 5.0, - ), - up: Vec3::Y, - target: Vec3::ZERO, - ..Default::default() - }); + camera.position = Vec3::new( + slow_elapsed_time.sin() * 5.0, + slow_elapsed_time.sin() * 1.5, + slow_elapsed_time.cos() * 5.0, + ); + + camera.position = Vec3::new(-1.0, 2.5, -4.0); /* * * Render the projection * */ + + /* let color = if projected_point.is_inside { - RED + color::RED } else { - YELLOW + color::YELLOW }; draw_line_3d( @@ -101,9 +155,9 @@ async fn main() { true, ); let color = if projected_point.is_inside { - RED + color::RED } else { - YELLOW + color::YELLOW }; draw_sphere(point_to_project, 0.1, None, color); @@ -111,14 +165,35 @@ async fn main() { point_to_project, mquad_from_na(projected_point.point), color, - ); - // Mesh is rendered in the back, so we can see the other graphics elements - draw_mesh(&mesh); + );*/ + + ctx.root_ui().draw(&mut canvas); + + scene.draw(&camera); + draw_gizmos(&camera); + canvas.draw(); next_frame().await } } +fn mquad_mesh_from_parry(indices: &Vec<[u32; 3]>, points: &Vec>) -> CpuMesh { + let m_indices = indices.iter().flatten().map(|v| *v as u16).collect(); + let m_vertices = points.iter().map(|p| mquad_from_na(*p)).collect(); + + let mesh = compute_mesh_with_normals_per_face(&m_vertices, &m_indices); + + let nb_vertices = mesh.vertices.len(); + dbg!(&mesh.vertices, &mesh.indices); + let cpu_mesh = CpuMesh( + mesh.vertices, + vec![vec2(0.0, 0.0); nb_vertices], + mesh.normals, + mesh.indices, + ); + cpu_mesh +} + fn mquad_from_na(a: Point3) -> Vec3 { Vec3::new(a.x, a.y, a.z) } @@ -126,3 +201,54 @@ fn mquad_from_na(a: Point3) -> Vec3 { fn na_from_mquad(a: Vec3) -> Point3 { Point3::new(a.x, a.y, a.z) } + +pub struct MeshData { + pub vertices: Vec, + pub indices: Vec, + pub normals: Vec, +} + +pub fn compute_mesh_with_normals_per_face(vertices: &Vec, indices: &Vec) -> MeshData { + let mut result_vertices: Vec = Vec::::new(); + let mut normals: Vec = Vec::::new(); + for indices in indices.chunks(3) { + let v0 = vertices[indices[0] as usize]; + let v1 = vertices[indices[1] as usize]; + let v2 = vertices[indices[2] as usize]; + + let normal = (v0 - v2).cross(v1 - v2).normalize(); + + for &i in indices.iter() { + result_vertices.push(vertices[i as usize]); + normals.push(normal); + } + } + MeshData { + vertices: result_vertices, + indices: (0..vertices.len() * 3) + .into_iter() + .map(|i| i as u16) + .collect(), + normals, + } +} + +const VERTEX: &str = r#" +#include "common_vertex.glsl" + +void vertex() { +} +"#; + +const FRAGMENT: &str = r#" +varying vec3 out_normal; + +void main() { + vec3 norm = normalize(out_normal); + vec3 lightDir = normalize(vec3(1.0, -1.0, 0.5)); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = diff * vec3(1.0) + vec3(0.2,0.2,0.2); + + gl_FragColor = vec4(diffuse,1.); +} +"#; From 858bddf727f78f48b5bda6609895ec5153e55ac7 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Thu, 8 Aug 2024 10:18:39 +0200 Subject: [PATCH 5/6] fix faces not displayed --- crates/parry3d/examples/project3d_animated.rs | 46 ++++++------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs index 0622fc5b..5ea65ded 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project3d_animated.rs @@ -38,26 +38,21 @@ fn main() { async fn main_loop(ctx: macroquad::Context) { init_gizmos(&ctx); - // - // This is useful to test for https://github.com/dimforge/parry/pull/248 - let _points = vec![ - Point3::from([0.0, 0.0, 0.0]), - Point3::from([0.0, 0.0, 1.0]), - Point3::from([1.0, 0.0, 0.0]), - Point3::from([1.0, 0.0, 1.0]), - ]; - let _indices: Vec<[u32; 3]> = vec![[0, 1, 2], [1, 3, 2]]; - let (points, indices) = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh(); let mut scene = ctx.new_scene(); let quad_gl = QuadGl::new(ctx.quad_ctx.clone()); - let cpu_mesh = mquad_mesh_from_parry(&indices, &points); + let mut cpu_mesh = mquad_mesh_from_parry(&indices, &points); // No clone on CpuMesh :'( - let cpu_mesh2 = mquad_mesh_from_parry(&indices, &points); + let cpu_mesh2 = CpuMesh( + cpu_mesh.0.clone(), + cpu_mesh.1.clone(), + cpu_mesh.2.clone(), + cpu_mesh.3.clone(), + ); let mut mesh = quad_gl.mesh(cpu_mesh, None); @@ -65,7 +60,7 @@ async fn main_loop(ctx: macroquad::Context) { ctx.quad_ctx.lock().unwrap().as_mut(), vec![], Some(FRAGMENT), - Some(VERTEX), + None, ); scene.add_model(&mesh); @@ -91,19 +86,6 @@ async fn main_loop(ctx: macroquad::Context) { ctx.clear_screen(color::BLACK); canvas.clear(); - // To show vertices winding order, I display each edge of each faces sequentially. - let current_edge_index = (elapsed_time / 3.0) as usize; - let current_face_index = current_edge_index / 3; - let first_vertice_index = current_face_index * 3; - let first_point = first_vertice_index + current_edge_index % 3; - let second_point = first_vertice_index + (current_edge_index + 1) % 3; - // No color lines :( - gizmos_add_line( - false, - cpu_mesh2.0[first_point % cpu_mesh2.0.len()], - cpu_mesh2.0[second_point % cpu_mesh2.0.len()], - ); - let slow_elapsed_time = elapsed_time / 10.0; let point_to_project = lissajous_3d(slow_elapsed_time); @@ -119,7 +101,7 @@ async fn main_loop(ctx: macroquad::Context) { slow_elapsed_time.cos() * 5.0, ); - camera.position = Vec3::new(-1.0, 2.5, -4.0); + //camera.position = Vec3::new(-1.0, 2.5, -4.0); /* * @@ -171,6 +153,7 @@ async fn main_loop(ctx: macroquad::Context) { scene.draw(&camera); draw_gizmos(&camera); + canvas.draw(); next_frame().await @@ -184,7 +167,6 @@ fn mquad_mesh_from_parry(indices: &Vec<[u32; 3]>, points: &Vec>) -> let mesh = compute_mesh_with_normals_per_face(&m_vertices, &m_indices); let nb_vertices = mesh.vertices.len(); - dbg!(&mesh.vertices, &mesh.indices); let cpu_mesh = CpuMesh( mesh.vertices, vec![vec2(0.0, 0.0); nb_vertices], @@ -223,12 +205,10 @@ pub fn compute_mesh_with_normals_per_face(vertices: &Vec, indices: &Vec Date: Thu, 8 Aug 2024 11:47:55 +0200 Subject: [PATCH 6/6] show projection (missing sphere + color) --- crates/parry3d/examples/project3d_animated.rs | 66 +++++++++---------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs index 5ea65ded..f49a8627 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project3d_animated.rs @@ -1,20 +1,16 @@ use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; use macroquad::gizmos::{draw_gizmos, gizmos_add_line, init_gizmos}; -use macroquad::math::{vec2, vec3, Vec2, Vec3}; -use macroquad::miniquad; +use macroquad::math::{vec2, vec3, Mat4, Vec2, Vec3}; use macroquad::quad_gl::camera::{Camera, Projection}; use macroquad::quad_gl::models::CpuMesh; use macroquad::quad_gl::QuadGl; -use macroquad::{ - quad_gl::{ - camera::Environment, - color::{self, Color}, - scene::Shader, - ui::{hash, widgets}, - }, - window::next_frame, +use macroquad::quad_gl::{ + camera::Environment, + color::{self}, + scene::Shader, }; +use macroquad::window::{next_frame, screen_height, screen_width}; use nalgebra::{Point3, Vector3}; use parry3d::math::{Isometry, Real}; @@ -23,12 +19,12 @@ use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags}; fn lissajous_3d(t: f32) -> Vec3 { // Some hardcoded parameters to have a pleasing lissajous trajectory. - let (a, b, c, delta_x, delta_y, delta_z) = (3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6); + let (a, b, c, delta_x, delta_y, delta_z) = (1.0, 3.0, 2.0, FRAC_PI_4, FRAC_PI_2, FRAC_PI_4); let x = (a * t + delta_x).sin(); let y = (b * t + delta_y).sin(); let z = (c * t + delta_z).sin(); - Vec3::new(x, y, z) * 0.75f32 + Vec3::new(x, y, z) * 0.8f32 } fn main() { @@ -38,21 +34,13 @@ fn main() { async fn main_loop(ctx: macroquad::Context) { init_gizmos(&ctx); - let (points, indices) = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh(); + let (points, indices) = Cuboid::new(Vector3::new(0.5, 0.5, 0.8)).to_trimesh(); let mut scene = ctx.new_scene(); let quad_gl = QuadGl::new(ctx.quad_ctx.clone()); - let mut cpu_mesh = mquad_mesh_from_parry(&indices, &points); - - // No clone on CpuMesh :'( - let cpu_mesh2 = CpuMesh( - cpu_mesh.0.clone(), - cpu_mesh.1.clone(), - cpu_mesh.2.clone(), - cpu_mesh.3.clone(), - ); + let cpu_mesh = mquad_mesh_from_parry(&indices, &points); let mut mesh = quad_gl.mesh(cpu_mesh, None); @@ -109,24 +97,34 @@ async fn main_loop(ctx: macroquad::Context) { * */ - /* let color = if projected_point.is_inside { color::RED } else { color::YELLOW }; - draw_line_3d( + gizmos_add_line( + false, point_to_project, mquad_from_na(projected_point.point), - color, ); - draw_sphere(point_to_project, 0.1, None, color); - - draw_line_3d( + // not working + //let point = camera + // .proj_view() + // .0 + // .project_point3(point_to_project.xy); + //let point = vec2( + // (point.x / 2. + 0.5) * screen_width(), + // (0.5 - point.y / 2.) * screen_height(), + //); + + //canvas.draw_circle(point.x, point.y, 10.0, color); + //draw_sphere(point_to_project, 0.1, None, color); + + gizmos_add_line( + false, point_to_project, mquad_from_na(projected_point.point), - color, ); // fixed point inside @@ -141,13 +139,13 @@ async fn main_loop(ctx: macroquad::Context) { } else { color::YELLOW }; - draw_sphere(point_to_project, 0.1, None, color); + //draw_sphere(point_to_project, 0.1, None, color); - draw_line_3d( + gizmos_add_line( + false, point_to_project, mquad_from_na(projected_point.point), - color, - );*/ + ); ctx.root_ui().draw(&mut canvas); @@ -229,6 +227,6 @@ void main() { float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * vec3(1.0) + vec3(0.3); - gl_FragColor = vec4(diffuse,1.); + gl_FragColor = vec4(diffuse,0.5); } "#;