Skip to content

Commit 3037eda

Browse files
committed
Add basis parameter to Normal3
1 parent c5c4f37 commit 3037eda

File tree

10 files changed

+84
-72
lines changed

10 files changed

+84
-72
lines changed

core/src/geom/mesh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<A> Builder<A> {
205205
/// This is an eager operation, that is, only vertices *currently* added
206206
/// to the builder are transformed. The attribute type of the result is
207207
/// `Normal3`; the vertex type it accepts is changed accordingly.
208-
pub fn with_vertex_normals(self) -> Builder<Normal3> {
208+
pub fn with_vertex_normals(self) -> Builder<Normal3<Model>> {
209209
let Mesh { verts, faces } = self.mesh;
210210

211211
// Compute weighted face normals...

core/src/geom/prim.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ pub struct Sphere<B = ()>(pub Point3<B>, pub f32);
6666

6767
/// A surface normal in 3D.
6868
// TODO Use distinct type rather than alias
69-
pub type Normal3 = Vec3;
69+
pub type Normal3<B> = Vec3<B>;
70+
7071
/// A surface normal in 2D.
7172
pub type Normal2 = Vec2;
7273

@@ -239,7 +240,7 @@ impl<A, B> Tri<Vertex3<A, B>> {
239240
/// ]);
240241
/// assert_eq!(tri.normal(), vec3(0.0, SQRT_2 / 2.0, -SQRT_2 / 2.0));
241242
/// ```
242-
pub fn normal(&self) -> Normal3 {
243+
pub fn normal(&self) -> Normal3<B> {
243244
let [t, u] = self.tangents();
244245
// TODO normal with basis
245246
t.cross(&u).normalize_or_zero().to()
@@ -383,7 +384,7 @@ impl<B> Plane3<B> {
383384
/// assert_eq!(p.offset(), 3.0);
384385
///
385386
/// ```
386-
pub fn from_point_and_normal(pt: Point3<B>, n: Normal3) -> Self {
387+
pub fn from_point_and_normal(pt: Point3<B>, n: Normal3<B>) -> Self {
387388
let n = n.normalize();
388389
let d = pt.to_vec().dot(&n.to());
389390
Plane::new(n.x(), n.y(), n.z(), d)
@@ -400,7 +401,7 @@ impl<B> Plane3<B> {
400401
/// assert_eq!(<Plane3>::XY.normal(), Vec3::Z);
401402
/// assert_eq!(<Plane3>::YZ.normal(), Vec3::X);
402403
#[inline]
403-
pub fn normal(&self) -> Normal3 {
404+
pub fn normal(&self) -> Normal3<B> {
404405
self.abc().normalize().to()
405406
}
406407

core/src/render/tex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub const fn uv(u: f32, v: f32) -> TexCoord {
8080
/// 1 +------+------+------+
8181
///
8282
/// ```
83-
pub fn cube_map(pos: Vec3, dir: Normal3) -> TexCoord {
83+
pub fn cube_map(pos: Vec3, dir: Normal3<()>) -> TexCoord {
8484
// -1.0..1.0 -> 0.0..1.0
8585
let [x, y, z] = (0.5 * pos + splat(0.5))
8686
.clamp(&splat(0.0), &splat(1.0))

demos/src/bin/crates.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ fn main() {
3535
},
3636
);
3737
let crate_shader = shader::new(
38-
|v: Vertex3<(Normal3, TexCoord)>, mvp: &ProjMat3<_>| {
38+
|v: Vertex3<(Normal3<_>, TexCoord)>, mvp: &ProjMat3<_>| {
3939
vertex(mvp.apply(&v.pos), v.attrib)
4040
},
41-
|frag: Frag<(Normal3, TexCoord)>| {
41+
|frag: Frag<(Normal3<_>, TexCoord)>| {
4242
let (n, uv) = frag.var;
4343
let kd = lerp(n.dot(&light_dir).max(0.0), 0.4, 1.0);
4444
let col = SamplerClamp.sample(&tex, uv);
@@ -136,7 +136,7 @@ fn main() {
136136
.expect("should run");
137137
}
138138

139-
fn crates() -> Vec<Obj<(Normal3, TexCoord)>> {
139+
fn crates() -> Vec<Obj<(Normal3<Model>, TexCoord)>> {
140140
let obj = Obj::new(Cube { side_len: 2.0 }.build());
141141

142142
let mut res = vec![];

demos/src/bin/curses.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main() {
4949
|v: Vertex3<_>, mvp: &ProjMat3<Model>| {
5050
vertex(mvp.apply(&v.pos), v.attrib)
5151
},
52-
|frag: Frag<Normal3>| {
52+
|frag: Frag<Normal3<Model>>| {
5353
let [x, y, z] = (frag.var / 2.0 + splat(0.5)).0;
5454
rgb(x, y, z).to_color4()
5555
},

demos/src/bin/solids.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use minifb::{Key, KeyRepeat};
44

55
use re::prelude::*;
66

7-
use re::core::geom::Polyline;
8-
use re::core::math::{ProjMat3, ProjVec3, color::gray};
9-
use re::core::render::cam::Fov;
10-
7+
use re::core::{
8+
geom::Polyline,
9+
math::{ProjMat3, ProjVec3, color::gray},
10+
render::cam::{Fov, Transform},
11+
};
1112
use re::front::{Frame, minifb::Window};
1213
use re::geom::{io::parse_obj, solids::*};
1314

@@ -61,15 +62,17 @@ fn main() {
6162
.perspective(Fov::Equiv35mm(28.0), 0.1..1000.0)
6263
.viewport(pt2(10, h - 10)..pt2(w - 10, 10));
6364

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

68-
fn vtx_shader(v: VertexIn, (mvp, spin): Uniform) -> VertexOut {
68+
type NormalMat = Mat3<Model, View, 3>;
69+
type Uniform<'a> = (&'a ProjMat3<Model>, &'a NormalMat);
70+
71+
fn vtx_shader(v: VertexIn, (mvp, n): Uniform) -> VertexOut {
6972
// Transform vertex normal
70-
let norm = spin.apply(&v.attrib.to());
73+
let norm = n.apply(&v.attrib);
7174
// Calculate diffuse shading
72-
let diffuse = (norm.z() + 0.2).max(0.2) * 0.8;
75+
let diffuse = (-norm.z() + 0.2).max(0.2) * 0.8;
7376
// Visualize normal by mapping to RGB values
7477
let [r, g, b] = (0.45 * (v.attrib + splat(1.1))).0;
7578
let col = diffuse * rgb(r, g, b);
@@ -106,12 +109,17 @@ fn main() {
106109
.to::<ModelToWorld>()
107110
.then(&cam.world_to_project());
108111

112+
let normal: NormalMat = spin
113+
.to()
114+
.linear()
115+
.then(&cam.transform.world_to_view().linear());
116+
109117
let object = &objects[carousel.idx % objects.len()];
110118

111119
Batch {
112120
prims: object.faces.clone(),
113121
verts: object.verts.clone(),
114-
uniform: (&model_view_project, &spin),
122+
uniform: (&model_view_project, &normal),
115123
shader: shader,
116124
viewport: cam.viewport,
117125
target: frame.buf,
@@ -125,7 +133,7 @@ fn main() {
125133

126134
// Creates the 14 objects exhibited.
127135
#[rustfmt::skip]
128-
fn objects_n(res: u32) -> [Mesh<Normal3>; 14] {
136+
fn objects_n(res: u32) -> [Mesh<Normal3<Model>>; 14] {
129137
let segments = res;
130138
let sectors = 2 * res;
131139

@@ -158,7 +166,7 @@ fn objects_n(res: u32) -> [Mesh<Normal3>; 14] {
158166
}
159167

160168
// Creates a Lathe mesh.
161-
fn lathe(secs: u32) -> Mesh<Normal3> {
169+
fn lathe(secs: u32) -> Mesh<Normal3<Model>> {
162170
let pts = [
163171
(pt2(0.75, -0.5), vec2(1.0, 1.0)),
164172
(pt2(0.55, -0.25), vec2(1.0, 0.5)),
@@ -174,7 +182,7 @@ fn lathe(secs: u32) -> Mesh<Normal3> {
174182
}
175183

176184
// Loads the Utah teapot model.
177-
fn teapot() -> Mesh<Normal3> {
185+
fn teapot() -> Mesh<Normal3<Model>> {
178186
parse_obj(*include_bytes!("../../assets/teapot.obj"))
179187
.unwrap()
180188
.transform(
@@ -187,7 +195,7 @@ fn teapot() -> Mesh<Normal3> {
187195
}
188196

189197
// Loads the Stanford bunny model.
190-
fn bunny() -> Mesh<Normal3> {
198+
fn bunny() -> Mesh<Normal3<Model>> {
191199
parse_obj::<()>(*include_bytes!("../../assets/bunny.obj"))
192200
.unwrap()
193201
.transform(&scale(splat(0.12)).then(&translate(-Vec3::Y)).to())
@@ -196,7 +204,7 @@ fn bunny() -> Mesh<Normal3> {
196204
}
197205

198206
// Loads the Stanford dragon model.
199-
fn dragon() -> Mesh<Normal3> {
207+
fn dragon() -> Mesh<Normal3<Model>> {
200208
static DRAGON: &[u8] = include_bytes!("../../assets/dragon.obj");
201209
parse_obj::<()>(DRAGON.iter().copied())
202210
.unwrap()

geom/src/io.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub enum Error {
7979
pub struct Obj {
8080
faces: Vec<Tri<Indices>>,
8181
coords: Vec<Point3<Model>>,
82-
norms: Vec<Normal3>,
82+
norms: Vec<Normal3<Model>>,
8383
texcs: Vec<TexCoord>,
8484
}
8585

@@ -235,7 +235,7 @@ impl TryFrom<Obj> for Builder<()> {
235235
o.try_into_with(|_| Some(()))
236236
}
237237
}
238-
impl TryFrom<Obj> for Builder<Normal3> {
238+
impl TryFrom<Obj> for Builder<Normal3<Model>> {
239239
type Error = Error;
240240

241241
fn try_from(o: Obj) -> Result<Self> {
@@ -255,7 +255,7 @@ impl TryFrom<Obj> for Builder<TexCoord> {
255255
o.try_into_with(|i| i.uv.map(|ti| o.texcs[ti]))
256256
}
257257
}
258-
impl TryFrom<Obj> for Builder<(Normal3, TexCoord)> {
258+
impl TryFrom<Obj> for Builder<(Normal3<Model>, TexCoord)> {
259259
type Error = Error;
260260

261261
fn try_from(o: Obj) -> Result<Self> {
@@ -335,7 +335,9 @@ fn parse_vector<'a>(
335335
Ok(vec3(x, y, z))
336336
}
337337

338-
fn parse_normal<'a>(i: &mut impl Iterator<Item = &'a str>) -> Result<Normal3> {
338+
fn parse_normal<'a>(
339+
i: &mut impl Iterator<Item = &'a str>,
340+
) -> Result<Normal3<Model>> {
339341
Ok(parse_vector(i)?.to())
340342
}
341343

@@ -506,7 +508,7 @@ v 0.0 -2.0 0.0
506508
v 1.0 2.0 3.0
507509
vn 0.0 0.0 -1.0";
508510

509-
let m: Mesh<Normal3> = parse_obj(input).unwrap().build();
511+
let m: Mesh<Normal3<_>> = parse_obj(input).unwrap().build();
510512

511513
assert_eq!(m.faces.len(), 2);
512514
assert_eq!(m.verts.len(), 5);
@@ -585,7 +587,7 @@ v 0.0 -2.0 0.0
585587
vt 0.0 -1.0
586588
vn 0.0 0.0 1.0";
587589

588-
let m = &parse_obj::<(Normal3, TexCoord)>(input)
590+
let m = &parse_obj::<(Normal3<_>, TexCoord)>(input)
589591
.unwrap()
590592
.build();
591593
assert_eq!(m.faces.len(), 2);

geom/src/solids.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use retrofire_core::math::{Lerp, Vec3};
1313
pub use lathe::*;
1414

1515
pub use platonic::*;
16+
use retrofire_core::render::Model;
1617

1718
pub trait Build<A>: Sized {
1819
fn build(self) -> Mesh<A>;
@@ -24,8 +25,8 @@ pub trait Build<A>: Sized {
2425

2526
pub struct Icosphere(pub f32, pub u8);
2627

27-
impl Build<Normal3> for Icosphere {
28-
fn build(self) -> Mesh<Normal3> {
28+
impl Build<Normal3<Model>> for Icosphere {
29+
fn build(self) -> Mesh<Normal3<Model>> {
2930
#[derive(Default)]
3031
struct Tessellator {
3132
coords: Vec<Vec3>,
@@ -70,7 +71,7 @@ impl Build<Normal3> for Icosphere {
7071
}
7172

7273
let verts = recurser.coords.iter().map(|&p| {
73-
vertex((p.normalize() * self.0).to().to_pt(), p.normalize())
74+
vertex((p.normalize() * self.0).to().to_pt(), p.normalize().to())
7475
});
7576

7677
Mesh::new(recurser.faces, verts)

0 commit comments

Comments
 (0)