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 # More implementations of `Light`
19 intrude import cameras_cache
25 # Sun-like light projecting parallel rays
27 super LightCastingShadows
29 # Angle to the light source, around the X axis
30 var pitch
= 0.0 is writable
32 # Angle to the light source, around the Y axis
33 var yaw
= 0.0 is writable
35 # Depth texture width, in world coordinates
36 var width
= 100.0 is writable
38 # Depth texture height, in world coordinates
39 var height
= 100.0 is writable
41 # Viewport depth, centered on `app.world_camera`
42 var depth
= 500.0 is writable
44 redef var camera
= new ParallelLightCamera(app
.display
.as(not null), self) is lazy
47 private class ParallelLightCamera
50 var light
: ParallelLight
52 # Rotation matrix produced by the current rotation of the camera
53 fun rotation_matrix
: Matrix
55 var view
= new Matrix.identity
(4)
56 view
.rotate
(light
.yaw
, 0.0, 1.0, 0.0)
57 view
.rotate
(light
.pitch
, 1.0, 0.0, 0.0)
61 private fun create_mvp_matrix
: Matrix
63 var near
= -light
.depth
/2.0
64 var far
= light
.depth
/2.0
66 var view
= new Matrix.identity
(4)
67 view
.translate
(-position
.x
, -position
.y
, -position
.z
)
68 view
= view
* rotation_matrix
69 var projection
= new Matrix.orthogonal
(-light
.width
/2.0, light
.width
/2.0,
70 -light
.height
/2.0, light
.height
/2.0,
72 return view
* projection
77 var m
= mvp_matrix_cache
78 if m
== null or check_position_changed
then
85 private var pitch_cache
= 0.0
86 private var yaw_cache
= 0.0
87 private var width_cache
= 0.0
88 private var height_cache
= 0.0
89 private var depth_cache
= 0.0
91 redef fun check_position_changed
93 if super then return true
95 if light
.pitch
!= pitch_cache
or
96 light
.yaw
!= yaw_cache
or
97 light
.width
!= width_cache
or
98 light
.height
!= height_cache
or
99 light
.depth
!= depth_cache
then
100 pitch_cache
= light
.pitch
101 yaw_cache
= light
.yaw
102 width_cache
= light
.width
103 height_cache
= light
.height
104 depth_cache
= light
.depth