A simple map viewer

atmos_layer.rs 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. use ::std::ffi::CStr;
  2. use buffer::{Buffer, DrawMode};
  3. use context::Context;
  4. use orthografic_view::OrthograficView;
  5. use program::{Program, UniformId};
  6. use std::f32::consts::PI;
  7. use vertex_attrib::VertexAttribParams;
  8. #[derive(Debug)]
  9. pub struct AtmosLayer {
  10. buffer: Buffer,
  11. program: Program,
  12. scale_uniform: UniformId,
  13. }
  14. impl AtmosLayer {
  15. pub fn new(cx: &mut Context) -> AtmosLayer {
  16. let vertex_data = {
  17. let mut vertex_data: Vec<f32> = Vec::with_capacity(17 * 4);
  18. let radius_a = 0.75;
  19. let radius_b = 1.25;
  20. for x in 0..17 {
  21. let angle = x as f32 * (PI * 2.0 / 16.0);
  22. vertex_data.extend(&[
  23. angle.cos() * radius_a,
  24. angle.sin() * radius_a,
  25. angle.cos() * radius_b,
  26. angle.sin() * radius_b,
  27. ]);
  28. }
  29. vertex_data
  30. };
  31. let buffer = Buffer::new(cx, &vertex_data, vertex_data.len() / 2);
  32. let mut program = Program::new(
  33. cx,
  34. include_bytes!("../shader/atmos.vert"),
  35. include_bytes!("../shader/atmos.frag"),
  36. ).unwrap();
  37. program.add_attribute(
  38. cx,
  39. CStr::from_bytes_with_nul(b"position\0").unwrap(),
  40. &VertexAttribParams::new(2, 2, 0)
  41. );
  42. let scale_uniform = program.get_uniform_id(cx, CStr::from_bytes_with_nul(b"scale\0").unwrap()).unwrap();
  43. AtmosLayer {
  44. buffer,
  45. program,
  46. scale_uniform,
  47. }
  48. }
  49. // Has to be called once before one or multiple calls to `draw`.
  50. pub fn prepare_draw(&mut self, cx: &mut Context) {
  51. self.program.enable_vertex_attribs(cx);
  52. self.program.set_vertex_attribs(cx, &self.buffer);
  53. }
  54. pub fn draw(
  55. &mut self,
  56. cx: &mut Context,
  57. ortho: &OrthograficView,
  58. ) {
  59. let (scale_x, scale_y) = {
  60. let diam = ortho.diameter_physical_pixels();
  61. ((diam / ortho.viewport_size.x) as f32, (diam / ortho.viewport_size.y) as f32)
  62. };
  63. self.program.set_uniform_2f(cx, self.scale_uniform, scale_x, scale_y);
  64. self.buffer.draw(cx, &self.program, DrawMode::TriangleStrip);
  65. }
  66. }