Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more custom rendering #3

Open
wants to merge 2 commits into
base: projection_example_reimagine
Choose a base branch
from
Open
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
155 changes: 114 additions & 41 deletions crates/parry3d/examples/project3d_animated.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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, Mat4, Vec2, Vec3};
use macroquad::math::{vec2, vec3, vec4, Mat4, Vec2, Vec3, Vec4};
use macroquad::miniquad;
use macroquad::quad_gl::camera::{Camera, Projection};
use macroquad::quad_gl::models::CpuMesh;
use macroquad::quad_gl::QuadGl;
Expand Down Expand Up @@ -38,20 +39,40 @@ async fn main_loop(ctx: macroquad::Context) {

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 mesh = quad_gl.mesh(cpu_mesh, None);

mesh.nodes[0].materials[0].shader = Shader::new(
let mut mesh = ctx.mesh(cpu_mesh, None);

let sphere = Sphere::new(0.1, 18, 10);
let sphere_vertices_count = sphere.vertices.len();
let mut sphere_mesh = ctx.mesh(
CpuMesh {
vertices: sphere.vertices,
uvs: vec![vec2(0.0, 0.0); sphere_vertices_count],
normals: sphere.normals,
indices: sphere.indices,
},
None,
);
let shader = Shader::new(
ctx.quad_ctx.lock().unwrap().as_mut(),
vec![],
vec![("MyColor".to_string(), miniquad::UniformType::Float4, 1)],
Some(FRAGMENT),
None,
);
// TODO: remove depth test to be able to show the sphere inside?

mesh.nodes[0].materials[0].shader = shader.clone();
mesh.nodes[0].materials[0]
.shader
.set_uniform("MyColor", &[1., 1., 1., 0.5]);
sphere_mesh.nodes[0].materials[0].shader = shader.clone();
sphere_mesh.nodes[0].materials[0]
.shader
.set_uniform("MyColor", &[1., 0., 0.0, 1.0]);

scene.add_model(&mesh);
let sphere_handle = scene.add_model(&sphere_mesh);
let trimesh = TriMesh::with_flags(points, indices, TriMeshFlags::ORIENTED);
let mut canvas = ctx.new_canvas();

Expand Down Expand Up @@ -103,23 +124,20 @@ async fn main_loop(ctx: macroquad::Context) {
color::YELLOW
};

gizmos_add_line(
false,
point_to_project,
mquad_from_na(projected_point.point),
scene.set_translation(&sphere_handle, point_to_project);

gizmos_add_line(false, point_to_project, point_to_project);
/*
let (proj, view) = camera.proj_view();
let mvp = proj * view;
let point = mvp.project_point3(point_to_project);
let depth = point_to_project.distance(camera.position);
let point = vec2(
(point.x / 2. + 0.5) * screen_width(),
(0.5 - point.y / 2.) * screen_height(),
);
// 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);
canvas.draw_circle(point.x, point.y, 0.5 * depth * depth, color);
*/

gizmos_add_line(
false,
Expand Down Expand Up @@ -151,8 +169,7 @@ async fn main_loop(ctx: macroquad::Context) {

scene.draw(&camera);
draw_gizmos(&camera);

canvas.draw();
ctx.blit_canvas(&mut canvas);

next_frame().await
}
Expand All @@ -165,12 +182,12 @@ fn mquad_mesh_from_parry(indices: &Vec<[u32; 3]>, points: &Vec<Point3<Real>>) ->
let mesh = compute_mesh_with_normals_per_face(&m_vertices, &m_indices);

let nb_vertices = mesh.vertices.len();
let cpu_mesh = CpuMesh(
mesh.vertices,
vec![vec2(0.0, 0.0); nb_vertices],
mesh.normals,
mesh.indices,
);
let cpu_mesh = CpuMesh {
vertices: mesh.vertices,
uvs: vec![vec2(0.0, 0.0); nb_vertices],
normals: mesh.normals,
indices: mesh.indices,
};
cpu_mesh
}

Expand Down Expand Up @@ -211,22 +228,78 @@ pub fn compute_mesh_with_normals_per_face(vertices: &Vec<Vec3>, indices: &Vec<u1
}
}

const VERTEX: &str = r#"
#include "common_vertex.glsl"

void vertex() {
}
"#;

const FRAGMENT: &str = r#"
varying vec3 out_normal;
uniform vec4 MyColor;

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.3);
float lightning = max(dot(norm, lightDir), 0.0);
vec3 finalColor = lightning * MyColor.rgb + vec3(0.3) * MyColor.rgb;

gl_FragColor = vec4(diffuse,0.5);
gl_FragColor = vec4(finalColor, MyColor.a);
}
"#;

struct Sphere {
vertices: Vec<Vec3>,
normals: Vec<Vec3>,
indices: Vec<u16>,
}

impl Sphere {
fn new(radius: f32, sector_count: usize, stack_count: usize) -> Self {
let mut vertices: Vec<Vec3> = Vec::new();
let mut normals: Vec<Vec3> = Vec::new();
let mut indices: Vec<u16> = Vec::new();

// Step 1: Generate vertices and normals
for i in 0..=stack_count {
let stack_angle =
std::f32::consts::PI / 2.0 - i as f32 * std::f32::consts::PI / stack_count as f32;
let xy = radius * stack_angle.cos();
let z = radius * stack_angle.sin();

for j in 0..=sector_count {
let sector_angle = j as f32 * 2.0 * std::f32::consts::PI / sector_count as f32;

let x = xy * sector_angle.cos();
let y = xy * sector_angle.sin();

let vertex = Vec3::new(x, y, z);
vertices.push(vertex);

// Normalize the vertex to get the normal
let normal = vertex.normalize();
normals.push(normal);
}
}

// Step 2: Generate indices
for i in 0..stack_count {
let k1 = i * (sector_count + 1);
let k2 = k1 + sector_count + 1;

for j in 0..sector_count {
if i != 0 {
indices.push((k1 + j) as u16);
indices.push((k2 + j) as u16);
indices.push((k1 + j + 1) as u16);
}

if i != (stack_count - 1) {
indices.push((k1 + j + 1) as u16);
indices.push((k2 + j) as u16);
indices.push((k2 + j + 1) as u16);
}
}
}

Self {
vertices,
normals,
indices,
}
}
}