You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.2 KiB

  1. use std::time::Instant;
  2. use glc::{
  3. buffer::{Buffer, Target, Usage},
  4. error::Error,
  5. };
  6. pub struct Uniform {
  7. buffer: Buffer,
  8. start_time: Instant,
  9. last_time: Instant,
  10. }
  11. #[repr(C, packed)]
  12. struct Data {
  13. time: f32,
  14. delta: f32,
  15. }
  16. impl Uniform {
  17. pub fn new() -> Result<Self, Error> {
  18. let mut buffer = Buffer::new()?;
  19. buffer.buffer_data(
  20. Usage::StaticDraw,
  21. &[Data {
  22. time: 0.0,
  23. delta: 0.0,
  24. }],
  25. )?;
  26. let now = Instant::now();
  27. Ok(Self {
  28. buffer,
  29. start_time: now,
  30. last_time: now,
  31. })
  32. }
  33. pub fn update(&mut self) -> Result<(), Error> {
  34. let now = Instant::now();
  35. let time = now.duration_since(self.start_time).as_secs_f32();
  36. let delta = now.duration_since(self.last_time).as_secs_f32();
  37. self.last_time = now;
  38. let mut data = self.buffer.map_mut::<Data>(true)?;
  39. data[0].time = time;
  40. data[0].delta = delta;
  41. Ok(())
  42. }
  43. pub fn bind(&self, index: gl::GLuint) -> Result<(), Error> {
  44. Error::checked(|| self.buffer.bind_buffer_base(Target::UniformBuffer, index))
  45. }
  46. }