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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! A rectangular viewport and associated 2D and 3D cameras.

use ::{std, glium};

use ::{graphics, Camera2d, Camera3d};

/// A viewport defined by a `glium::Rect` structure, with associated 2D and 3D
/// cameras
pub struct Viewport {
  /// Describes the viewport position and size.
  ///
  /// `left` and `bottom` are the number of pixels between the left and bottom
  /// of the screen with the left and bottom borders of the viewport rectangle.
  pub rect                : glium::Rect,
  pub camera2d            : Camera2d,
  pub camera3d            : Camera3d
}

pub struct Builder {
  rect            : glium::Rect,
  /// Set 3D camera projection to orthographic with the given zoom
  orthographic_3d : Option <f32>,
  pose_3d         : Option <graphics::Pose3d <f32>>,
  zoom_2d         : Option <f32>
}

impl Viewport {
  /// Create a new viewport with the given `Rect`
  ///
  /// # Panics
  ///
  /// Width or height are zero:
  ///
  /// ```should_panic
  /// # extern crate gl_utils;
  /// # extern crate cgmath;
  /// # extern crate glium;
  /// # fn main () {
  /// # use gl_utils::render::Viewport;
  /// let mut viewport = Viewport::new (glium::Rect {
  ///   left: 0, bottom: 0, width: 0, height: 0
  /// });
  /// # }
  /// ```
  ///
  /// Width or height are greater than `u16::MAX` (65535):
  ///
  /// ```should_panic
  /// # extern crate gl_utils;
  /// # extern crate cgmath;
  /// # extern crate glium;
  /// # fn main () {
  /// # use gl_utils::render::Viewport;
  /// let mut viewport = Viewport::new (glium::Rect {
  ///   left: 0, bottom: 0, width: 65536, height: 65536
  /// });
  /// # }
  /// ```
  pub fn new (rect : glium::Rect) -> Self {
    assert!(rect.width  <= std::u16::MAX as u32);
    assert!(rect.height <= std::u16::MAX as u32);
    Viewport {
      rect,
      camera2d: Camera2d::new (rect.width as u16, rect.height as u16),
      camera3d: Camera3d::new (rect.width as u16, rect.height as u16)
    }
  }

  /// Also initializes `self.camera3d` with position and orientation
  pub fn with_pose_3d (
    rect : glium::Rect,
    pose : graphics::Pose3d <f32>
  ) -> Self {
    assert!(rect.width  <= std::u16::MAX as u32);
    assert!(rect.height <= std::u16::MAX as u32);
    Viewport {
      rect,
      camera2d: Camera2d::new (rect.width as u16, rect.height as u16),
      camera3d: Camera3d::with_pose (
        rect.width as u16, rect.height as u16,
        pose
      )
    }
  }

  /// Should be called when screen resolution changes.
  ///
  /// # Panics
  ///
  /// Width and height must be less than or equal to `u16::MAX` (65535):
  ///
  /// ```should_panic
  /// # extern crate gl_utils;
  /// # extern crate cgmath;
  /// # extern crate glium;
  /// # fn main () {
  /// # use gl_utils::render::Viewport;
  /// # let mut viewport = Viewport::new (glium::Rect {
  /// #   left: 0, bottom: 0, width: 320, height: 240
  /// # });
  /// viewport.set_rect (glium::Rect {
  ///   left:   0,       bottom: 0,
  ///   width:  100_000, height: 100_000 // panics
  /// });
  /// # }
  /// ```
  ///
  /// Width or height are zero:
  ///
  /// ```should_panic
  /// # extern crate gl_utils;
  /// # extern crate cgmath;
  /// # extern crate glium;
  /// # fn main () {
  /// # use gl_utils::render::Viewport;
  /// # let mut viewport = Viewport::new (glium::Rect {
  /// #   left: 0, bottom: 0, width: 320, height: 240
  /// # });
  /// viewport.set_rect (glium::Rect {
  ///   left:   0, bottom: 0,
  ///   width:  0, height: 0 // panics
  /// });
  /// # }
  /// ```
  pub fn set_rect (&mut self, rect : glium::Rect) {
    assert!(rect.width  <= std::u16::MAX as u32);
    assert!(rect.height <= std::u16::MAX as u32);
    self.rect = rect;
    self.camera2d
      .set_viewport_dimensions (rect.width as u16, rect.height as u16);
    self.camera3d
      .set_viewport_dimensions (rect.width as u16, rect.height as u16);
  }
}

impl Builder {
  #[inline]
  pub fn new (rect : glium::Rect) -> Self {
    Builder {
      rect,
      orthographic_3d: None,
      pose_3d:         None,
      zoom_2d:         None
    }
  }
  pub fn with_zoom_2d (self, zoom : f32) -> Self {
    Builder { zoom_2d: Some (zoom), .. self }
  }
  /// Changes the 3D camera projection from perspective (default) to orthographic
  pub fn orthographic_3d (self, zoom : f32) -> Self {
    Builder { orthographic_3d: Some (zoom), .. self }
  }
  pub fn with_pose_3d (self, pose_3d : graphics::Pose3d <f32>) -> Self {
    Builder { pose_3d: Some (pose_3d), .. self }
  }
  #[inline]
  pub fn build (self) -> Viewport {
    let mut viewport = if let Some (pose_3d) = self.pose_3d {
      Viewport::with_pose_3d (self.rect, pose_3d)
    } else {
      Viewport::new (self.rect)
    };
    if let Some (zoom) = self.orthographic_3d {
      viewport.camera3d.projection3d.to_orthographic (zoom);
    }
    if let Some (zoom) = self.zoom_2d {
      viewport.camera2d.set_zoom (zoom);
    }
    viewport
  }
}