1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use graphics::{node, Node,NodeT};
use color::{Rgb, ToRgb, ToRgba};
use glin;
use ::ShadowMapping;
use graphics::{self, Vertex3DNormal};
use na::*;

use ::Renderer;
use ::Material;
use renderer::{Render3d, Renderer3d};
use basic_material;
use glin::RenderSurface;

pub struct DirectionalLight{
    node: Node,
    color: Rgb<f32>,
    strength: f32,
    direction: Vec3,
    projection: Mat4,
    view: Mat4,
    mvp: Mat4,
    biased_mvp: Mat4,
    radius: f32,
}

#[repr(C)]
pub struct Data{
    eye_direction: Vec4,
    world_direction: Vec4,
    half_vector: Vec3,
    radius: f32,
    color: Rgb<f32>,
    strength: f32,
    projection_matrix: Mat4,
    view_matrix: Mat4,
    matrix: Mat4,
    biased_matrix: Mat4,
}

impl DirectionalLight{
    pub fn new() -> DirectionalLight{
        let left = -10.;
        let right = 10.;
        let bottom = -10.;
        let top = 10.;
        let znear = -10.;
        let zfar = 20.;
        let projection = Ortho3::new(left, right, bottom, top, znear, zfar).to_mat();
        let direction = Vec3::z();
        let view = graphics::Node::new_look_at(origin(), direction.to_pnt(), Vec3::y())
                .inv_local_transformation();
        let mvp = projection.fast_mul(&view);
        DirectionalLight{
            node: Node::identity(),
            color: rgb!(1.0f32,1.0f32,1.0f32),
            strength: 1.0,
            direction: direction,
            projection: projection,
            view: view,
            mvp: mvp,
            biased_mvp: Mat4::new(
            	 0.5, 0.0, 0.0, 0.5,
            	 0.0, 0.5, 0.0, 0.5,
            	 0.0, 0.0, 0.5, 0.5,
            	 0.0, 0.0, 0.0, 1.0
            ).fast_mul(&mvp),
            radius: 0.1,
        }
    }

    pub fn color(&self) -> &Rgb<f32>{
        &self.color
    }

    pub fn strength(&self) -> f32{
        self.strength
    }

    pub fn radius(&self) -> f32{
        self.radius
    }

    pub fn set_color<C: ToRgb + ToRgba>(&mut self, color: &C){
        self.color = color.to_rgb();
    }

    pub fn set_strength(&mut self, strength: f32){
        self.strength = strength;
    }

    pub fn direction(&self) -> Vec3{
        self.direction
    }

    pub fn projection_matrix(&self) -> Mat4{
        self.projection
    }

    pub fn view_matrix(&self) -> Mat4{
        self.view
    }

    pub fn mvp(&self) -> Mat4{
        self.mvp
    }

    pub fn biased_mvp(&self) -> Mat4{
        self.biased_mvp
    }

    pub fn set_frustrum_from_shadow_map(&mut self, shadow_map: &ShadowMapping){
        let size = shadow_map.frustum_size();
        let left = -size / 2.;
        let right = size / 2.;
        let top = size / 2.;
        let bottom = -size / 2.;
        let znear = shadow_map.near_clip();
        let zfar = shadow_map.far_clip();
        self.set_frustrum(left, right, bottom, top, znear, zfar);
    }

    pub fn set_frustrum(&mut self, l: f32, r: f32, b: f32, t: f32, n: f32, f: f32){
        self.projection = Ortho3::new(l, r, b, t, n, f).to_mat();

        self.view = self.inv_local_transformation();
        self.mvp = self.projection.fast_mul(&self.view);
        self.biased_mvp = Mat4::new(
        	 0.5, 0.0, 0.0, 0.5,
        	 0.0, 0.5, 0.0, 0.5,
        	 0.0, 0.0, 0.5, 0.5,
        	 0.0, 0.0, 0.0, 1.0
        ).fast_mul(&self.mvp);
    }

    pub fn set_radius(&mut self, radius: f32){
        self.radius = radius;
    }

    fn eye_direction(&self, view_matrix: &Mat4) -> Vec3{
        let direction = self.direction();
        Vec3::from_homogeneous(*view_matrix * vec4(direction.x, direction.y, direction.z, 0.0)).unwrap()
    }

    fn update_matrices(&mut self){
        let global_rot: Mat3<f32> = Mat3::from_iterator(self.global_transformation().columns(0,3).rows(0,3).iter().map(|v| *v));
        self.direction = normalize(&(global_rot * Vec3::z()));
        // let znear = 0.1;
        // let zfar = 20.;
        // let viewport = Rect{ pos: pnt2(0, 0), width:  20, height: 20};
        // self.projection = graphics::Proj::new_ortho_clipped(viewport, graphics::CoordinateOrigin::CenterUp, znear, zfar).to_mat().1;
        self.view = self.inv_local_transformation();
        // self.view = graphics::Node::new_look_at(origin(), self.direction().to_pnt(), Vec3::y())
        //         .inv_local_transformation();
        self.mvp = self.projection.fast_mul(&self.view);
        self.biased_mvp = Mat4::new(
             0.5, 0.0, 0.0, 0.5,
             0.0, 0.5, 0.0, 0.5,
             0.0, 0.0, 0.5, 0.5,
             0.0, 0.0, 0.0, 1.0
        ).fast_mul(&self.mvp);
    }

    pub fn data(&self, trafo: &graphics::Mvp) -> Data{
        let eye_direction = vec4!(self.eye_direction(&trafo.view()), 1.);
        let half_vector = (vec3(0f32, 0., 1.) + eye_direction.xyz()).normalize();
        Data{
            eye_direction,
            world_direction: vec4!(self.direction(), 1.),
            half_vector,
            color: self.color,
            strength: self.strength,
            projection_matrix: self.projection_matrix(),
            view_matrix: self.view_matrix(),
            matrix: self.mvp(),
            biased_matrix: self.biased_mvp(),
            radius: self.radius,
        }
    }
}

impl NodeT for DirectionalLight{
    fn node(&self) -> &Node{
        &self.node
    }

    fn node_mut(&mut self) -> &mut Node{
        &mut self.node
    }

    fn set_scale(&mut self, _s: &Vec3){
        // don't ever scale
    }

    fn update_with_parent_flags(&mut self, parent: Option<&Node>, flags: node::Flags) -> bool{
        let changed = self.node.update_with_parent_flags(parent, flags);
        if changed {
            self.update_matrices();
        }
        changed
    }
}

impl ::Light for DirectionalLight{
    fn ty(&self) -> &str{
        "DIRECTIONAL_LIGHT"
    }

    fn uniforms(&self, trafo: &graphics::Mvp, _texture_bind_offset: &mut u32) -> Vec<glin::program::Uniform>{
        let eye_direction = vec4!(self.eye_direction(&trafo.view()), 1.0);
        let half_vector = (vec3(0f32, 0., 1.) + eye_direction.xyz()).normalize();
        uniforms!{
            enabled: 1f32,
            type: 1f32,
            eye_direction: eye_direction,
            world_direction: vec4!(self.direction(), 1.0),
            half_vector: half_vector,
            color: self.color,
            strength: self.strength,
            projection_matrix: self.projection_matrix(),
            view_matrix: self.view_matrix(),
            matrix: self.mvp(),
            biased_matrix: self.biased_mvp(),
            radius: self.radius,
        }
    }
}

geometry_cache!(Vertex3DNormal, {
    let mut mesh = graphics::sphere(1.,5,5);
    let last_index = mesh.len();
    mesh.push(graphics::vertex3dnormal(zero(), Vec3::y()));
    mesh.push(graphics::vertex3dnormal(vec3(0., 0., -100.), Vec3::y()));
    mesh.push(graphics::vertex3dnormal(vec3(0., 0.5, 0.), Vec3::y()));
    mesh.indices_mut().push(last_index as glin::IndexT);
    mesh.indices_mut().push((last_index+1) as glin::IndexT);
    mesh.indices_mut().push((last_index+2) as glin::IndexT);
    mesh
});

impl Render3d for DirectionalLight{
    fn render<R: RenderSurface>(&self, renderer: &Renderer<R>) where Self: Sized{
        let geom = get_geometry(renderer);
        let material = basic_material::Builder::new()
            .color(&self.color)
            .create();
        let gl = renderer.with_model(self);
        gl.draw_vao(geom.full_range(), material.program(renderer), &material.uniforms(renderer));
    }
}