@@ -4,10 +4,11 @@ use minifb::{Key, KeyRepeat};
44
55use 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+ } ;
1112use re:: front:: { Frame , minifb:: Window } ;
1213use 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 ( )
0 commit comments