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
use graphics::{node, Node,NodeT};
use color::{Rgb, ToRgb, ToRgba};
use glin;
use graphics;
use na::*;
use angle::*;
use math;

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

pub struct SpotLight{
    node: Node,
    attenuation_constant: f32,
    attenuation_linear: f32,
    attenuation_quadratic: f32,
    color: Rgb<f32>,
    radius: f32,
    cutoff: f32,
    strength: f32,
    spot_angle: Deg<f32>,
    direction: Vec3,
    spot_blend: f32,
}

pub struct Data{
    world_position: Vec3,
    eye_position: Vec3,
    color: Rgb<f32>,
    attenuation_constant: f32,
    attenuation_linear: f32,
    attenuation_quadratic: f32,
    radius: f32,
    strength: f32,
    cutoff: f32,
    spot_angle: Rad<f32>,
    spot_cos_cutoff: f32,
    spot_cos_inner_cutoff: f32,
    spot_direction: Vec3,
    spot_blend: f32,
    matrix: Mat4,
}


impl SpotLight{
    pub fn new() -> SpotLight{
        SpotLight{
            node: Node::identity(),
            attenuation_constant: 1.0,
            attenuation_linear: 0.1,
            attenuation_quadratic: 0.01,
            color: rgb!(1.0f32,1.0f32,1.0f32),
            radius: 1.0,
            cutoff: 45.0,
            strength: 1.0,
            spot_angle: Deg(45.),
            direction: Vec3::z(),
            spot_blend: 1.,
        }
    }

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

    pub fn set_att_constant(&mut self, att: f32){
        self.attenuation_constant = att;
    }

    pub fn set_att_linear(&mut self, att: f32){
        self.attenuation_linear = att;
    }

    pub fn set_att_quadratic(&mut self, att: f32){
        self.attenuation_quadratic = att;
    }

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

    pub fn set_spot_angle(&mut self, angle: Deg<f32>){
        self.spot_angle = angle;
    }

    pub fn set_spot_blend(&mut self, blend: f32){
        self.spot_blend = blend;
    }

    pub fn spot_angle(&self) -> Deg<f32>{
        self.spot_angle
    }

    pub fn data(&self, trafo: &graphics::Mvp) -> Data{
        let p = trafo.view() * self.global_position().to_homogeneous();
        let viewport = math::Rect{pos: pnt2!(0), width: 1, height: 1};
        let projection = graphics::Projection::new_perspective(viewport, self.spot_angle, graphics::CoordinateOrigin::CenterUp)
            .into_parts().1;
        let view = self.inv_local_transformation();
        Data{
            world_position: self.global_position().xyz().to_vec(),
            eye_position: vec3(p.x, p.y, p.z),
            color: self.color,
            attenuation_constant: self.attenuation_constant,
            attenuation_linear: self.attenuation_linear,
            attenuation_quadratic: self.attenuation_quadratic,
            radius: self.radius,
            strength: self.strength,
            cutoff: self.cutoff,
            spot_angle: self.spot_angle.to_rad(),
            spot_cos_cutoff: self.spot_angle.cos(),
            spot_cos_inner_cutoff: (self.spot_angle * (1. - self.spot_blend)).cos(),
            spot_direction: self.direction,
            spot_blend: self.spot_blend,
            matrix: projection * view,
        }
    }
}

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

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

    fn update_with_parent_flags(&mut self, parent: Option<&Node>, flags: node::Flags) -> bool{
        let changed = self.node.update_with_parent_flags(parent, flags);
        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()));
        changed
    }
}

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

    fn uniforms(&self, trafo: &graphics::Mvp, _texture_bind_offset: &mut u32) -> Vec<glin::program::Uniform>{
        let p = trafo.view() * self.global_position().to_homogeneous();
        // let direction = self.global_position().xyz() + self.direction;
		// auto direction4 = renderer.getCurrentViewMatrix() * glm::vec4(direction,1.0);
		// direction = direction4.xyz() / direction4.w;
		// direction = direction - lightEyePosition.xyz();
        let viewport = math::Rect{pos: pnt2!(0), width: 1, height: 1};
        let projection = graphics::Projection::new_perspective(viewport, self.spot_angle, graphics::CoordinateOrigin::CenterUp)
            .into_parts().1;
        let view = self.inv_local_transformation();
        uniforms!{
            type: 2f32,
            eye_position: vec3(p.x, p.y, p.z),
            world_position: self.global_position().xyz().to_vec(),
            color: self.color,
            constantAttenuation: self.attenuation_constant,
            linearAttenuation: self.attenuation_linear,
            quadraticAttenuation: self.attenuation_quadratic,
            radius: self.radius,
            strength: self.strength,
            cutoff: self.cutoff,
            spot_angle: self.spot_angle.to_rad().value(),
            spot_cos_cutoff: self.spot_angle.cos(),
            spot_cos_inner_cutoff: (self.spot_angle * (1. - self.spot_blend)).cos(),
            spot_direction: self.direction,
            spot_blend: self.spot_blend,
            matrix: projection * view,
        }
    }
}

geometry_cache!(graphics::Vertex3DNormal, graphics::sphere(0.1,20,20));

impl Render3d for SpotLight{
    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();
        renderer
            .draw_vao(geom.full_range(), material.program(renderer), &material.uniforms(renderer));
    }
}