a1eed2f3c5991ef6142df71fdf831948d5e827e9
1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Camera services producing Model-View-Projection matrices
19 import matrix
::projection
23 # A camera with a point of view on the world
26 # TODO make this a physical object in the world
28 # The host `GamnitDisplay`
29 var display
: GamnitDisplay
31 # Position of this camera in world space
32 var position
= new Point3d[Float](0.0, 0.0, 0.0) is writable
34 # The Model-View-Projection matrix created by this camera
36 # This method should only be called by the display at the moment
37 # of drawing to the screen.
38 fun mvp_matrix
: Matrix is abstract
41 # Simple camera with perspective oriented with Euler angles (`pitch, yaw, roll`)
45 # Rotation around the X axis (looking down or up)
46 var pitch
= 0.0 is writable
48 # Rotation around the Y axis (looking left or right)
49 var yaw
= 0.0 is writable
51 # Rotation around the Z axis
52 var roll
= 0.0 is writable
54 # Field of view in radians on the vertical axis of the screen
57 var field_of_view_y
= 0.8 is writable
59 # Clipping wall near the camera, in world dimensions
62 var near
= 0.01 is writable
64 # Clipping wall the farthest of the camera, in world dimensions
66 # Default at `10000.0` but this one should be adapted to each context.
67 var far
= 10000.0 is writable
69 # Look around sensitivity, used by `turn`
70 var sensitivity
= 0.005 is writable
72 # Apply a mouse movement (or similar) to the camera
74 # `dx` and `dy` are relative mouse movements in pixels.
75 fun turn
(dx
, dy
: Float)
77 # Moving on x, turn around the y axis
79 pitch
-= dy
*sensitivity
81 # Protect rotation around then x axis for not falling on your back
82 pitch
= pitch
.min
(pi
/2.0)
83 pitch
= pitch
.max
(-pi
/2.0)
86 # Move the camera considering the current orientation
87 fun move
(dx
, dy
, dz
: Float)
90 position
.x
-= yaw
.sin
*dz
91 position
.z
-= yaw
.cos
*dz
93 # +dx strafe to the right
94 position
.x
+= yaw
.cos
*dx
95 position
.z
-= yaw
.sin
*dx
97 # +dz move towards the sky
101 # Aim the camera at `x, y, z`
102 fun look_at
(x
, y
, z
: Float)
109 pitch
= atan2
(-dy
, dz
)
112 # Rotation matrix produced by the current rotation of the camera
113 protected fun rotation_matrix
: Matrix
115 var view
= new Matrix.identity
(4)
117 # Rotate the camera, first by looking left or right, then up or down
118 view
.rotate
(yaw
, 0.0, 1.0, 0.0)
119 view
.rotate
(pitch
, 1.0, 0.0, 0.0)
120 view
.rotate
(roll
, 0.0, 0.0, 1.0)
127 var view
= new Matrix.identity
(4)
129 # Translate the world away from the camera
130 view
.translate
(-position
.x
, -position
.y
, -position
.z
)
132 # Rotate the camera, first by looking left or right, then up or down
133 view
= view
* rotation_matrix
135 # Use a projection matrix with a depth
136 var projection
= new Matrix.perspective
(pi
*field_of_view_y
/2.0,
137 display
.aspect_ratio
, near
, far
)
139 return view
* projection
142 # Reset the camera position so that `height` world units are visible on the y axis at z=0
144 # By default, `height` is set to `display.height`.
146 # After the reset, the camera sits on the Z axis and rotation values are reset to 0.
147 # The X axis is horizontal on the screen and the Y axis is vertical.
148 # Higher values on the Z axis are closer to the camera.
149 fun reset_height
(height
: nullable Float)
151 if height
== null then height
= display
.height
.to_f
153 var opp
= height
/ 2.0
154 var angle
= field_of_view_y
/ 2.0
155 var adj
= opp
/ angle
.tan
167 # Orthogonal camera to draw UI objects with services to work with screens of different sizes
169 # X axis: left to right of the screen, from `position.x` to `position.x + width`
170 # Y axis: top to bottom of the screen, from `position.y` to `position.y + height`
171 # Z axis: far to near the camera (usually when values are higher), from `far` to `near`
175 # Clipping wall near the camera, defaults to 100.0
176 var near
= 100.0 is writable
178 # Clipping wall the farthest of the camera, defaults to -100.0
179 var far
: Float = -100.0 is writable
181 # Width in world units, defaults to the width in pixels of the screen
182 var width
: Float = display
.width
.to_f
is lazy
184 # Height in world units, defaults to the height in pixels of the screen
185 var height
: Float = display
.height
.to_f
is lazy
187 # Reset the camera position so that `height` world units are visible on the Y axis
189 # By default, `height` is set to `display.height`.
191 # This can be used to set standardized UI units independently from the screen resolution.
192 fun reset_height
(height
: nullable Float)
194 if height
== null then height
= display
.height
.to_f
197 self.width
= height
* display
.aspect_ratio
200 # Convert the position `x, y` on screen, to UI coordinates
201 fun camera_to_ui
(x
, y
: Numeric): Point[Float]
203 # FIXME this kind of method should use something like a canvas
204 # instead of being hard coded on the display.
206 var wx
= x
.to_f
* width
/ display
.width
.to_f
- position
.x
207 var wy
= y
.to_f
* height
/ display
.height
.to_f
- position
.y
208 return new Point[Float](wx
, wy
)
211 # Center of the screen, from the point of view of the camera, at z = 0
212 fun center
: Point3d[Float] do return new Point3d[Float](position
.x
+ width
/ 2.0, position
.y
- height
/ 2.0, 0.0)
214 # Anchor in the top left corner of the screen, at z = 0
215 fun top_left
: Point3d[Float] do return new Point3d[Float](position
.x
, position
.y
, 0.0)
217 # Anchor in the top right corner of the screen, at z = 0
218 fun top_right
: Point3d[Float] do return new Point3d[Float](position
.x
+ width
, position
.y
, 0.0)
220 # Anchor in the bottom left corner of the screen, at z = 0
221 fun bottom_left
: Point3d[Float] do return new Point3d[Float](position
.x
, position
.y
- height
, 0.0)
223 # Anchor in the bottom right corner of the screen, at z = 0
224 fun bottom_right
: Point3d[Float] do return new Point3d[Float](position
.x
+ width
, position
.y
- height
, 0.0)
226 # TODO cache the anchors and the matrix
230 var view
= new Matrix.identity
(4)
232 # Translate the world away from the camera
233 view
.translate
(-position
.x
, -position
.y
, -position
.z
)
235 # Use a projection matrix with a depth
236 var projection
= new Matrix.orthogonal
(0.0, width
, -height
, 0.0, near
, far
)
238 return view
* projection