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
use graphics::{node, Node, NodeRef, NodeMut};
use color::{Rgb, ToRgb, ToRgba};
use glin;
use graphics;
use na::*;
use super::{Renderer, Render3d, Renderer3d,
basic_material, ShadowMap, Light};
use glin::RenderSurface;
pub struct PointLight{
node: Node,
attenuation_constant: f32,
attenuation_linear: f32,
attenuation_quadratic: f32,
color: Rgb<f32>,
radius: f32,
cutoff: f32,
strength: f32,
}
#[allow(dead_code)]
#[repr(C)]
pub struct Data{
world_position: Vec3,
radius: f32,
color: Rgb<f32>,
attenuation_constant: f32,
attenuation_linear: f32,
attenuation_quadratic: f32,
strength: f32,
cutoff: f32,
shadow_map_idx: [i32;4],
}
impl PointLight{
pub fn new() -> PointLight{
PointLight{
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,
}
}
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_radius(&mut self, radius: f32){
self.radius = radius;
}
pub fn data(&self) -> Data{
Data{
world_position: self.global_position().xyz().to_vec(),
color: self.color,
attenuation_constant: self.attenuation_constant,
attenuation_linear: self.attenuation_linear,
attenuation_quadratic: self.attenuation_quadratic,
strength: self.strength,
radius: self.radius,
cutoff: self.cutoff,
shadow_map_idx: [-1; 4],
}
}
}
impl NodeRef for PointLight{
fn node(&self) -> &Node{
&self.node
}
}
impl NodeMut for PointLight{
fn node_mut(&mut self) -> &mut Node{
&mut self.node
}
fn update_with_parent_flags(&mut self, parent: Option<&Node>, flags: node::Flags) -> bool{
self.node.update_with_parent_flags(parent, flags)
}
}
impl Light for PointLight{
fn ty(&self) -> &str{
"POINT_LIGHT"
}
fn uniforms(&self, _shadow_map_idx_offset: &mut usize) -> Vec<glin::program::Uniform>{
uniforms!{
type: 0f32,
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,
cutoff: self.cutoff,
strength: self.strength
}
}
fn shadow_maps(&self) -> Vec<&dyn ShadowMap>{
vec![]
}
}
geometry_cache!(graphics::Vertex3DNormal, graphics::sphere(1.,20,20));
impl Render3d for PointLight{
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 * (self.strength / 5.)))
.create();
let model = self.node().clone() * Node::new(origin(), one(), vec3(self.radius, self.radius, self.radius));
renderer
.with_model(model)
.draw_vao_with_material(geom.full_range(), &material);
}
}