Extract mesh tracing into the lib crate
This commit is contained in:
parent
0d212cb59c
commit
fca01e05f4
|
|
@ -1,5 +1,6 @@
|
|||
use glam::*;
|
||||
use refraction::mesh_loader::{load_mesh, Face};
|
||||
use refraction::mesh_loader::load_mesh;
|
||||
use refraction::mesh_tracer::{trace_to_mesh, Mesh};
|
||||
use show_image::{ImageInfo, ImageView, WindowOptions};
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
|
|
@ -59,44 +60,6 @@ fn ypr_to_mat(ypr: Vec3) -> Mat3 {
|
|||
m_roll * m_pitch * m_yaw
|
||||
}
|
||||
|
||||
type Mesh = [Face];
|
||||
|
||||
struct TraceResult {
|
||||
distance: f32,
|
||||
normal: Vec3,
|
||||
}
|
||||
|
||||
fn trace_to_mesh(mesh: &Mesh, base: Vec3, ray: Vec3) -> Option<TraceResult> {
|
||||
let mut ret: Option<TraceResult> = None;
|
||||
let mut dist = f32::INFINITY;
|
||||
for f in mesh {
|
||||
let fs = (0..3).map(|k| edge_dist(f.vertices[k], f.vertices[(k + 1) % 3], base, ray));
|
||||
if fs.into_iter().all(|f| f >= 0.0) {
|
||||
let m = mat3(
|
||||
f.vertices[1] - f.vertices[0],
|
||||
f.vertices[2] - f.vertices[0],
|
||||
-ray,
|
||||
);
|
||||
let m = m.inverse();
|
||||
let rel = m * (base - f.vertices[0]);
|
||||
if rel.z > dist {
|
||||
continue;
|
||||
}
|
||||
dist = rel.z;
|
||||
ret = Some(TraceResult {
|
||||
distance: rel.z,
|
||||
normal: f.normal,
|
||||
});
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
struct Location {
|
||||
pos: Vec3,
|
||||
rot: Vec4,
|
||||
}
|
||||
|
||||
fn render(mesh: &Mesh, camera: impl Fn(Vec2) -> (Vec3, Vec3)) -> Image {
|
||||
let bkg = vec3(0.0, 0.0, 0.0);
|
||||
let mut img = Image {
|
||||
|
|
@ -159,8 +122,3 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn edge_dist(a: Vec3, b: Vec3, base: Vec3, dir: Vec3) -> f32 {
|
||||
// Note: given that the input is not arbitrary but comes from a cartesian product of certain (a, b) pairs and certain (base, dir) pairs, this can be optimized from Cnm to an+bm+cnm with c<C.
|
||||
mat3(b - a, base - a, -dir).determinant()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
pub mod mesh_loader;
|
||||
pub mod mesh_tracer;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
use crate::mesh_loader::Face;
|
||||
use glam::{mat3, Vec3};
|
||||
|
||||
pub type Mesh = [Face];
|
||||
|
||||
pub struct TraceResult {
|
||||
pub distance: f32,
|
||||
pub normal: Vec3,
|
||||
}
|
||||
|
||||
pub fn trace_to_mesh(mesh: &Mesh, base: Vec3, ray: Vec3) -> Option<TraceResult> {
|
||||
let mut ret: Option<TraceResult> = None;
|
||||
let mut dist = f32::INFINITY;
|
||||
for f in mesh {
|
||||
let fs = (0..3).map(|k| edge_dist(f.vertices[k], f.vertices[(k + 1) % 3], base, ray));
|
||||
if fs.into_iter().all(|f| f >= 0.0) {
|
||||
let m = mat3(
|
||||
f.vertices[1] - f.vertices[0],
|
||||
f.vertices[2] - f.vertices[0],
|
||||
-ray,
|
||||
);
|
||||
let m = m.inverse();
|
||||
let rel = m * (base - f.vertices[0]);
|
||||
if rel.z > dist {
|
||||
continue;
|
||||
}
|
||||
dist = rel.z;
|
||||
ret = Some(TraceResult {
|
||||
distance: rel.z,
|
||||
normal: f.normal,
|
||||
});
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
fn edge_dist(a: Vec3, b: Vec3, base: Vec3, dir: Vec3) -> f32 {
|
||||
// Note: given that the input is not arbitrary but comes from a cartesian product of certain (a, b) pairs and certain (base, dir) pairs, this can be optimized from Cnm to an+bm+cnm with c<C.
|
||||
mat3(b - a, base - a, -dir).determinant()
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user