@@ -7,8 +7,8 @@ use re::prelude::*;
77use re:: core:: {
88 geom:: Polyline ,
99 math:: { ProjMat3 , ProjVec3 , color:: gray} ,
10- render:: cam:: Fov ,
11- render:: { Model , ModelToWorld , shader} ,
10+ render:: cam:: { Fov , Transform } ,
11+ render:: { Model , ModelToWorld , View , shader} ,
1212} ;
1313use re:: front:: { Frame , minifb:: Window } ;
1414use re:: geom:: { io:: read_obj, solids:: * } ;
@@ -63,15 +63,17 @@ fn main() {
6363 . perspective ( Fov :: Equiv35mm ( 28.0 ) , 0.1 ..1000.0 )
6464 . viewport ( pt2 ( 10 , h - 10 ) ..pt2 ( w - 10 , 10 ) ) ;
6565
66- type VertexIn = Vertex3 < Normal3 > ;
66+ type VertexIn = Vertex3 < Normal3 < Model > > ;
6767 type VertexOut = Vertex < ProjVec3 , Color3f > ;
68- type Uniform < ' a > = ( & ' a ProjMat3 < Model > , & ' a Mat4 ) ;
6968
70- fn vtx_shader ( v : VertexIn , ( mvp, spin) : Uniform ) -> VertexOut {
69+ type NormalMat = Mat3 < Model , View , 3 > ;
70+ type Uniform < ' a > = ( & ' a ProjMat3 < Model > , & ' a NormalMat ) ;
71+
72+ fn vtx_shader ( v : VertexIn , ( mvp, n) : Uniform ) -> VertexOut {
7173 // Transform vertex normal
72- let norm = spin . apply ( & v. attrib ) ;
74+ let norm = n . apply ( & v. attrib ) ;
7375 // Calculate diffuse shading
74- let diffuse = ( norm. z ( ) + 0.2 ) . max ( 0.2 ) * 0.8 ;
76+ let diffuse = ( - norm. z ( ) + 0.2 ) . max ( 0.2 ) * 0.8 ;
7577 // Visualize normal by mapping to RGB values
7678 let [ r, g, b] = ( 0.45 * ( v. attrib + splat ( 1.1 ) ) ) . 0 ;
7779 let col = diffuse * rgb ( r, g, b) ;
@@ -108,12 +110,17 @@ fn main() {
108110 . to :: < ModelToWorld > ( )
109111 . then ( & cam. world_to_project ( ) ) ;
110112
113+ let normal: NormalMat = spin
114+ . to ( )
115+ . linear ( )
116+ . then ( & cam. transform . world_to_view ( ) . linear ( ) ) ;
117+
111118 let object = & objects[ carousel. idx % objects. len ( ) ] ;
112119
113120 Batch {
114121 prims : object. faces . clone ( ) ,
115122 verts : object. verts . clone ( ) ,
116- uniform : ( & model_view_project, & spin ) ,
123+ uniform : ( & model_view_project, & normal ) ,
117124 shader : shader,
118125 viewport : cam. viewport ,
119126 target : frame. buf ,
@@ -127,7 +134,7 @@ fn main() {
127134
128135// Creates the 14 objects exhibited.
129136#[ rustfmt:: skip]
130- fn objects_n ( res : u32 ) -> [ Mesh < Normal3 > ; 14 ] {
137+ fn objects_n ( res : u32 ) -> [ Mesh < Normal3 < Model > > ; 14 ] {
131138 let segments = res;
132139 let sectors = 2 * res;
133140
@@ -160,7 +167,7 @@ fn objects_n(res: u32) -> [Mesh<Normal3>; 14] {
160167}
161168
162169// Creates a Lathe mesh.
163- fn lathe ( secs : u32 ) -> Mesh < Normal3 > {
170+ fn lathe ( secs : u32 ) -> Mesh < Normal3 < Model > > {
164171 let pts = [
165172 ( pt2 ( 0.75 , -0.5 ) , vec2 ( 1.0 , 1.0 ) ) ,
166173 ( pt2 ( 0.55 , -0.25 ) , vec2 ( 1.0 , 0.5 ) ) ,
@@ -176,10 +183,10 @@ fn lathe(secs: u32) -> Mesh<Normal3> {
176183}
177184
178185// Loads the Utah teapot model.
179- fn teapot ( ) -> Mesh < Normal3 > {
186+ fn teapot ( ) -> Mesh < Normal3 < Model > > {
180187 static TEAPOT : & [ u8 ] = include_bytes ! ( "../../assets/teapot.obj" ) ;
181188 read_obj ( TEAPOT )
182- . unwrap ( )
189+ . expect ( "valid .obj, included in binary" )
183190 . transform (
184191 & scale ( splat ( 0.4 ) )
185192 . then ( & translate ( -0.5 * Vec3 :: Y ) )
@@ -189,20 +196,20 @@ fn teapot() -> Mesh<Normal3> {
189196}
190197
191198// Loads the Stanford bunny model.
192- fn bunny ( ) -> Mesh < Normal3 > {
199+ fn bunny ( ) -> Mesh < Normal3 < Model > > {
193200 static BUNNY : & [ u8 ] = include_bytes ! ( "../../assets/bunny.obj" ) ;
194201 read_obj :: < ( ) > ( BUNNY )
195- . unwrap ( )
202+ . expect ( "valid .obj, included in binary" )
196203 . transform ( & scale ( splat ( 0.12 ) ) . then ( & translate ( -Vec3 :: Y ) ) . to ( ) )
197204 . with_vertex_normals ( )
198205 . build ( )
199206}
200207
201208// Loads the Stanford dragon model.
202- fn dragon ( ) -> Mesh < Normal3 > {
209+ fn dragon ( ) -> Mesh < Normal3 < Model > > {
203210 static DRAGON : & [ u8 ] = include_bytes ! ( "../../assets/dragon.obj" ) ;
204211 read_obj :: < ( ) > ( DRAGON )
205- . unwrap ( )
212+ . expect ( "valid .obj, included in binary" )
206213 . with_vertex_normals ( )
207214 . transform (
208215 & scale ( splat ( 0.18 ) )
0 commit comments