1    type Kind =
2        | Orthographic
3        | Perspective
4        | OrthographicFlipY
5    
6    type Camera =
7        val up : Vector3
8        var position : Vector3 @mut
9        val direction : Vector3
10       val near : f32
11       val far : f32
12       val scale : f32
13       val kind : Kind
14       let tokens = List<Token>.new
15   
16   object Camera =
17       val default = Camera
18           up = Vector3 0 1 0
19           position = Vector3.new
20           direction = Vector3 0 -2 -1 |> normalized
21           near = 1
22           far = 200
23           scale = 0
24           kind = Kind/Perspective
25   
26       let create (kind : Kind
27                   position : Vector3
28                   direction : Vector3
29                   near : f32
30                   far : f32
31                   scale : f32) =
32            Camera =position
33                   direction = direction.normalized
34                   =near =far =scale =kind
35                   f@ default
36   
37       def create (position : Vector3) (direction : Vector3) = Camera
38           =position
39           direction = direction.normalized
40           f@ default
41   
42       def orthographic (position : Vector3
43                         direction : Vector3
44                         far : f32
45                         scale : f32) =
46           create Kind/Orthographic position direction 0 far scale
47   
48       def orthographic_flip_y (position : Vector3
49                                direction : Vector3
50                                far : f32
51                                scale : f32) =
52           create Kind/OrthographicFlipY position direction 0 far scale
53   
54       def perspective (position : Vector3
55                        direction : Vector3
56                        near : f32
57                        far : f32) =
58           create Kind/Perspective position direction near far 0
59   
60   type Camera
61       def projection (width : u32) (height : u32) =
62           case kind of
63               Kind/Orthographic | Kind/OrthographicFlipY ->
64                   let mut matrix = matrix/orthographic (width as f32 / scale)
65                                                        (height as f32 / scale)
66                                                        (far - near)
67                   if kind == Kind/OrthographicFlipY then
68                       matrix.m22 *= -1
69   
70                   matrix
71   
72               Kind/Perspective ->
73                   matrix/perspective near far (width as f32 / height as f32) 1
74   
75       def view = matrix/view position direction (Vector3 0 1 0)
76   
77       def push_token (token : Token) =
78           tokens.add token
79