Advertisement
Nachasic

Raylib-rs Lights

Dec 27th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.58 KB | None | 0 0
  1. mod lights {
  2.     use std::sync::atomic;
  3.     use std::os::raw::c_void;
  4.     use raylib::prelude::*;
  5.  
  6.     const MAX_LIGHTS: usize = 4;
  7.  
  8.     #[derive(Clone, Copy)]
  9.     pub enum LightType {
  10.         DIRECTIONAL_LIGHT,
  11.         POINT_LIGHT
  12.     }
  13.  
  14.     impl ShaderV for LightType {
  15.         const UNIFORM_TYPE: ShaderUniformDataType = ShaderUniformDataType::UNIFORM_INT;
  16.         unsafe fn value(&self) -> *const c_void {
  17.             match self {
  18.                 LightType::DIRECTIONAL_LIGHT => 0 as *const i32 as *const c_void,
  19.                 LightType::POINT_LIGHT => 1 as *const i32 as *const c_void
  20.             }
  21.         }
  22.     }
  23.  
  24.     pub struct Light {
  25.         light_type: LightType,
  26.         position: Vector3,
  27.         target: Vector3,
  28.         color: Color,
  29.         is_enabled: i32,
  30.  
  31.         // Shader locations
  32.         enabled_loc: i32,
  33.         type_loc: i32,
  34.         position_loc: i32,
  35.         target_loc: i32,
  36.         color_loc: i32
  37.     }
  38.  
  39.     // Keep track of the number of lights
  40.     static LIGHTS_AMOUNT: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
  41.  
  42.     impl Light {
  43.  
  44.         fn get_lights_amount() -> usize {
  45.             LIGHTS_AMOUNT.load(atomic::Ordering::SeqCst)
  46.         }
  47.  
  48.         fn increment_lights_amount() {
  49.             LIGHTS_AMOUNT.fetch_add(1, atomic::Ordering::SeqCst);
  50.         }
  51.  
  52.         pub fn new (
  53.             light_type: LightType,
  54.             position: Vector3,
  55.             target: Vector3,
  56.             color: Color,
  57.             is_enabled: bool,
  58.             shader:&mut Shader) -> Option<Light> {
  59.                 // Don't make more lights if we've at the limit
  60.                 if Light::get_lights_amount() == MAX_LIGHTS {
  61.                     return None
  62.                 };
  63.  
  64.                 let light = Light {
  65.                     light_type, position, color, target,
  66.                     is_enabled: match is_enabled {
  67.                         true => 1,
  68.                         false => 0
  69.                     },
  70.                     enabled_loc: shader.get_shader_location("lights[x].enabled\0"),
  71.                     type_loc: shader.get_shader_location("lights[x].type\0"),
  72.                     position_loc: shader.get_shader_location("lights[x].position\0"),
  73.                     target_loc: shader.get_shader_location("lights[x].target\0"),
  74.                     color_loc: shader.get_shader_location("lights[x].color\0")
  75.                 };
  76.  
  77.                 light.update_light_values(shader);
  78.                 Light::increment_lights_amount();
  79.  
  80.                 Some(light)
  81.         }
  82.  
  83.         fn update_light_values(&self, shader: &mut Shader) {
  84.             // Send to shader light enabled state and type
  85.             shader.set_shader_value(self.enabled_loc, self.is_enabled);
  86.             shader.set_shader_value(self.type_loc, self.light_type);
  87.  
  88.             // Sent to shader light position values
  89.             let position: [f32; 3] = [self.position.x, self.position.y, self.position.z];
  90.             shader.set_shader_value(self.position_loc, position);
  91.  
  92.             // Send to shader light target position values
  93.             let target_position: [f32; 3] = [self.target.x, self.target.y, self.target.z];
  94.             shader.set_shader_value(self.target_loc, target_position);
  95.  
  96.             // Send to shader light color values
  97.             let color: [f32; 4] = [
  98.                 self.color.r as f32 / 255.0,
  99.                 self.color.g as f32 / 255.0,
  100.                 self.color.b as f32 / 255.0,
  101.                 self.color.a as f32 / 255.0
  102.             ];
  103.             shader.set_shader_value(self.color_loc, color);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement