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
use graphics::{node, Node,NodeT}; use color::{Rgb, ToRgb, ToRgba}; use glin; use graphics; use na::*; use ::Renderer; use ::Material; use renderer::{Render3d, Renderer3d}; use basic_material; 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, } pub struct Data{ world_position: Vec3, eye_position: Vec3, color: Rgb<f32>, attenuation_constant: f32, attenuation_linear: f32, attenuation_quadratic: f32, strength: f32, radius: f32, cutoff: f32, } 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, trafo: &graphics::Mvp) -> Data{ let p = trafo.view() * self.global_position().to_homogeneous(); 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, strength: self.strength, radius: self.radius, cutoff: self.cutoff, } } } impl NodeT for PointLight{ 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{ self.node.update_with_parent_flags(parent, flags) } } impl ::Light for PointLight{ fn ty(&self) -> &str{ "POINT_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(); uniforms!{ type: 0f32, 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, cutoff: self.cutoff, strength: self.strength } } } geometry_cache!(graphics::Vertex3DNormal, graphics::sphere(0.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) .create(); renderer .draw_vao(geom.full_range(), material.program(renderer), &material.uniforms(renderer)); } }