Skip to content

Commit 574e5b0

Browse files
committed
WIP use debug vis in solids
1 parent 6c0e914 commit 574e5b0

File tree

2 files changed

+124
-34
lines changed

2 files changed

+124
-34
lines changed

core/src/render/debug.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,11 @@ pub fn face_normal<A, B: Debug + Default>(
9999
/// from the origin point of the basis.
100100
pub fn basis<S, D>(m: Mat4<S, D>) -> DbgBatch<D> {
101101
let xyz = m.linear();
102-
let x = xyz.col_vec(0);
103-
let y = xyz.col_vec(1);
104-
let z = xyz.col_vec(2);
105102
let o = m.origin();
106103

107-
let mut b = ray(o, x);
108-
b.append(ray(o, y));
109-
b.append(ray(o, z));
104+
let mut b = ray(o, xyz.col_vec(0));
105+
b.append(ray(o, xyz.col_vec(1)));
106+
b.append(ray(o, xyz.col_vec(2)));
110107
b
111108
}
112109

@@ -186,7 +183,7 @@ pub fn sphere<B>(o: Point3<B>, r: f32) -> DbgBatch<B> {
186183
}
187184

188185
impl<B> DbgBatch<B> {
189-
fn new(prims: &[Edge<usize>], verts: &[Vertex3<Color4f, B>]) -> Self {
186+
pub fn new(prims: &[Edge<usize>], verts: &[Vertex3<Color4f, B>]) -> Self {
190187
DbgBatch::<B>::default()
191188
.primitives(prims)
192189
.vertices(verts)

demos/src/bin/solids.rs

Lines changed: 120 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use minifb::{Key, KeyRepeat};
44

55
use re::prelude::*;
66

7-
use re::core::geom::Polyline;
7+
use re::core::geom::{Edge, Polyline, Ray};
88
use re::core::math::{ProjMat3, ProjVec3, color::gray};
9-
use re::core::render::cam::Fov;
9+
use re::core::render::{cam::Fov, debug::DbgBatch};
1010

1111
use re::front::{Frame, minifb::Window};
1212
use re::geom::{io::parse_obj, solids::*};
@@ -62,7 +62,7 @@ fn main() {
6262
.viewport(pt2(10, h - 10)..pt2(w - 10, 10));
6363

6464
type VertexIn = Vertex3<Normal3>;
65-
type VertexOut = Vertex<ProjVec3, Color3f>;
65+
type VertexOut = Vertex<ProjVec3, Color4f>;
6666
type Uniform<'a> = (&'a ProjMat3<Model>, &'a Mat4);
6767

6868
fn vtx_shader(v: VertexIn, (mvp, spin): Uniform) -> VertexOut {
@@ -71,12 +71,11 @@ fn main() {
7171
// Calculate diffuse shading
7272
let diffuse = (norm.z() + 0.2).max(0.2) * 0.8;
7373
// Visualize normal by mapping to RGB values
74-
let [r, g, b] = (0.45 * (v.attrib + splat(1.1))).0;
75-
let col = diffuse * rgb(r, g, b);
74+
let col = diffuse * debug::dir_to_rgb(norm);
7675
vertex(mvp.apply(&v.pos), col)
7776
}
7877

79-
fn frag_shader(f: Frag<Color3f>) -> Color4 {
78+
fn frag_shader(f: Frag<Color4f>) -> Color4 {
8079
f.var.to_color4()
8180
}
8281

@@ -86,6 +85,8 @@ fn main() {
8685

8786
let translate = translate(-3.0 * Vec3::Z);
8887
let mut carousel = Carousel::default();
88+
let mut debug: u8 = 0;
89+
let mut hide_model = false;
8990

9091
win.run(|frame| {
9192
let Frame { t, dt, win, .. } = frame;
@@ -94,30 +95,79 @@ fn main() {
9495
if win.imp.is_key_pressed(Key::Space, KeyRepeat::No) {
9596
carousel.start();
9697
}
98+
if win.imp.is_key_pressed(Key::D, KeyRepeat::No) {
99+
debug = (debug + 1) % 6;
100+
}
101+
if win.imp.is_key_pressed(Key::H, KeyRepeat::No) {
102+
hide_model = !hide_model;
103+
}
97104

98105
let theta = rads(t.as_secs_f32());
99106
let spin = rotate_x(theta * 0.37).then(&rotate_y(theta * 0.51));
100107
let carouse = carousel.update(dt.as_secs_f32());
101108

102109
// Compose transform stack
103-
let model_view_project: ProjMat3<Model> = spin
110+
let mvp: ProjMat3<Model> = spin
104111
.then(&translate)
105112
.then(&carouse)
106113
.to::<ModelToWorld>()
107114
.then(&cam.world_to_project());
108115

109116
let object = &objects[carousel.idx % objects.len()];
110117

111-
Batch {
112-
prims: object.faces.clone(),
113-
verts: object.verts.clone(),
114-
uniform: (&model_view_project, &spin),
115-
shader: shader,
116-
viewport: cam.viewport,
117-
target: frame.buf,
118-
ctx: &*frame.ctx,
118+
if !hide_model {
119+
Batch {
120+
prims: object.faces.clone(),
121+
verts: object.verts.clone(),
122+
uniform: (&mvp, &spin),
123+
shader: shader,
124+
viewport: cam.viewport,
125+
target: frame.buf,
126+
ctx: &*frame.ctx,
127+
}
128+
.render();
129+
}
130+
131+
let mut dbg_batch = DbgBatch::default();
132+
if debug > 0 {
133+
let bbox = debug::bbox(&object.verts);
134+
dbg_batch.append(bbox);
135+
}
136+
if debug > 1 {
137+
let sphere = debug::sphere::<Model>(pt3(0.0, 0.0, 0.0), 1.0);
138+
dbg_batch.append(sphere);
139+
}
140+
if debug > 2 {
141+
let basis = debug::basis(Mat4::<_>::identity());
142+
dbg_batch.append(basis);
119143
}
120-
.render();
144+
if debug > 3 {
145+
// Wireframe faces
146+
let edges: Vec<_> = object
147+
.faces
148+
.iter()
149+
.flat_map(|tri| tri.edges().map(|Edge(a, b)| Edge(*a, *b)))
150+
.collect();
151+
let verts: Vec<_> = object
152+
.verts
153+
.iter()
154+
.map(|v| vertex(v.pos, gray(1.0f32).to_rgba()))
155+
.collect();
156+
157+
dbg_batch.append(DbgBatch::new(&edges, &verts));
158+
}
159+
if debug > 4 {
160+
for Tri(vs) in object.faces() {
161+
let norm = debug::face_normal(Tri(vs.map(|&v| v)));
162+
dbg_batch.append(norm);
163+
}
164+
}
165+
dbg_batch
166+
.uniform(&mvp)
167+
.viewport(cam.viewport)
168+
.context(&*frame.ctx)
169+
.target(&mut frame.buf)
170+
.render();
121171

122172
Continue(())
123173
});
@@ -135,6 +185,7 @@ fn objects_n(res: u32) -> [Mesh<Normal3>; 14] {
135185
let major_sectors = 3 * res;
136186
let minor_sectors = 2 * res;
137187
[
188+
lathe(sectors),
138189
// The five Platonic solids
139190
Tetrahedron.build(),
140191
Cube { side_len: 1.25 }.build(),
@@ -143,7 +194,6 @@ fn objects_n(res: u32) -> [Mesh<Normal3>; 14] {
143194
Icosahedron.build(),
144195

145196
// Surfaces of revolution
146-
lathe(sectors),
147197
Sphere { radius: 1.0, sectors, segments, }.build(),
148198
Cylinder { radius: 0.8, sectors, segments, capped: true }.build(),
149199
Cone { base_radius: 1.1, apex_radius: 0.3, sectors, segments, capped: true }.build(),
@@ -159,16 +209,59 @@ fn objects_n(res: u32) -> [Mesh<Normal3>; 14] {
159209

160210
// Creates a Lathe mesh.
161211
fn lathe(secs: u32) -> Mesh<Normal3> {
162-
let pts = [
163-
(pt2(0.75, -0.5), vec2(1.0, 1.0)),
164-
(pt2(0.55, -0.25), vec2(1.0, 0.5)),
165-
(pt2(0.5, 0.0), vec2(1.0, 0.0)),
166-
(pt2(0.55, 0.25), vec2(1.0, -0.5)),
167-
(pt2(0.75, 0.5), vec2(1.0, -1.0)),
168-
]
169-
.map(|(p, n)| vertex(p, n.normalize()));
170-
171-
Lathe::new(Polyline::new(pts), secs, pts.len() as u32)
212+
let _pts: [Vertex<Point2, Normal2>; _] = [
213+
// _________
214+
// |
215+
// /
216+
// ____/
217+
// /
218+
// |
219+
// \______
220+
// _______\
221+
222+
// Base
223+
vertex(pt2(0.5, -0.6), vec2(1.0, 0.0)),
224+
vertex(pt2(0.45, -0.55), vec2(0.0, 1.0)),
225+
// Neck
226+
vertex(pt2(0.15, -0.5), vec2(1.0, 1.0)),
227+
vertex(pt2(0.1, -0.45), vec2(1.0, 0.0)),
228+
vertex(pt2(0.1, 0.0), vec2(1.0, 0.0)),
229+
vertex(pt2(0.15, 0.05), vec2(1.0, -1.0)),
230+
// Bowl outer
231+
vertex(pt2(0.4, 0.1), vec2(1.0, -0.5)),
232+
vertex(pt2(0.5, 0.2), vec2(1.0, 0.0)),
233+
vertex(pt2(0.5, 0.3), vec2(1.0, 0.0)),
234+
vertex(pt2(0.4, 0.6), vec2(1.0, 0.1)),
235+
// Bowl inner
236+
vertex(pt2(0.35, 0.6), vec2(-1.0, 0.1)),
237+
vertex(pt2(0.4, 0.25), vec2(-1.0, 0.0)),
238+
vertex(pt2(0.2, 0.15), vec2(-0.2, 1.0)),
239+
vertex(pt2(0.0, 0.1), vec2(0.0, 1.0)),
240+
];
241+
242+
let curve = BezierSpline::from_rays(
243+
[
244+
Ray(pt2(0.5, -0.6), vec2(0.0, 0.1)),
245+
Ray(pt2(0.4, -0.55), vec2(-0.1, 0.0)),
246+
Ray(pt2(0.1, -0.4), vec2(0.0, 0.1)),
247+
Ray(pt2(0.1, 0.0), vec2(0.0, 0.1)),
248+
Ray(pt2(0.3, 0.2), vec2(0.1, 0.0)),
249+
Ray(pt2(0.5, 0.4), vec2(0.0, 0.1)),
250+
Ray(pt2(0.4, 1.0), vec2(-0.1, 0.0)),
251+
Ray(pt2(0.48, 0.4), vec2(0.0, -0.1)),
252+
Ray(pt2(0.0, 0.05), vec2(-0.1, 0.0)),
253+
], //.map(|Ray(pos, dir)| Ray(vertex(pos, dir.perp().normalize()))),
254+
);
255+
256+
let curve: Vec<_> = curve
257+
.approximate(0.05)
258+
.0
259+
.into_iter()
260+
.map(|p| vertex(p, vec2(1.0, 1.0)))
261+
.collect();
262+
263+
let n = curve.len();
264+
Lathe::new(Polyline(curve), secs, 2 * n as u32)
172265
.capped(true)
173266
.build()
174267
}

0 commit comments

Comments
 (0)