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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use std::f32;
use glfw::{self, Key, Action, WindowEvent};
use gl;
use na::{self, Point3, Point2, Vector2, Vector3, Matrix4, Isometry3, Perspective3};
use resource::ShaderUniform;
use camera::Camera;

#[path = "../error.rs"]
mod error;

/// First-person camera mode.
///
///   * Left button press + drag - look around
///   * Right button press + drag - translates the camera position on the plane orthogonal to the
///   view direction
///   * Scroll in/out - zoom in/out
#[derive(Debug)]
pub struct FirstPersonStereo {
    /// The camera position
    eye:        Point3<f32>,
    eye_left:   Point3<f32>,
    eye_right:  Point3<f32>,

    /// Inter Pupilary Distance
    ipd:        f32,

    /// Yaw of the camera (rotation along the y axis).
    yaw:        f32,
    /// Pitch of the camera (rotation along the x axis).
    pitch:      f32,

    /// Increment of the yaw per unit mouse movement. The default value is 0.005.
    yaw_step:   f32,
    /// Increment of the pitch per unit mouse movement. The default value is 0.005.
    pitch_step: f32,
    /// Increment of the translation per arrow press. The default value is 0.1.
    move_step:  f32,

    /// Low level datas
    projection:      Perspective3<f32>,
    proj_view:       Matrix4<f32>,
    proj_view_left:  Matrix4<f32>,
    proj_view_right: Matrix4<f32>,
    inverse_proj_view:   Matrix4<f32>,
    last_cursor_pos: Point2<f32>
}

impl FirstPersonStereo {
    /// Creates a first person camera with default sensitivity values.
    pub fn new(eye: Point3<f32>, at: Point3<f32>, ipd: f32) -> FirstPersonStereo {
        FirstPersonStereo::new_with_frustrum(f32::consts::PI / 4.0, 0.1, 1024.0, eye, at, ipd)
    }

    /// Creates a new first person camera with default sensitivity values.
    pub fn new_with_frustrum(fov:    f32,
                             znear:  f32,
                             zfar:   f32,
                             eye:    Point3<f32>,
                             at:     Point3<f32>,
                             ipd:    f32) -> FirstPersonStereo {
        let mut res = FirstPersonStereo {
            eye:           Point3::new(0.0, 0.0, 0.0),
            // left & right are initially wrong, don't take ipd into accound
            eye_left:      Point3::new(0.0, 0.0, 0.0),
            eye_right:     Point3::new(0.0, 0.0, 0.0),
            ipd:           ipd,
            yaw:           0.0,
            pitch:         0.0,
            yaw_step:      0.005,
            pitch_step:    0.005,
            move_step:     0.5,
            projection: Perspective3::new(800.0 / 600.0, fov, znear, zfar),
            proj_view:  na::zero(),
            inverse_proj_view:   na::zero(),
            last_cursor_pos: Point2::origin(),
            proj_view_left:  na::zero(),
            proj_view_right: na::zero(),
        };

        res.look_at(eye, at);

        res
    }


    /// Changes the orientation and position of the camera to look at the specified point.
    pub fn look_at(&mut self, eye: Point3<f32>, at: Point3<f32>) {
        let dist  = na::norm(&(eye - at));

        let pitch = ((at.y - eye.y) / dist).acos();
        let yaw   = (at.z - eye.z).atan2(at.x - eye.x);

        self.eye   = eye;
        self.yaw   = yaw;
        self.pitch = pitch;
        self.update_projviews();
    }

    /// The point the camera is looking at.
    pub fn at(&self) -> Point3<f32> {
        let ax = self.eye.x + self.yaw.cos() * self.pitch.sin();
        let ay = self.eye.y + self.pitch.cos();
        let az = self.eye.z + self.yaw.sin() * self.pitch.sin();

        Point3::new(ax, ay, az)
    }

    fn update_restrictions(&mut self) {
        if self.pitch <= 0.0001 {
            self.pitch = 0.0001
        }

        let _pi: f32 = f32::consts::PI;
        if self.pitch > _pi - 0.0001 {
            self.pitch = _pi - 0.0001
        }
    }

    #[doc(hidden)]
    pub fn handle_left_button_displacement(&mut self, dpos: &Vector2<f32>) {
        self.yaw   = self.yaw   + dpos.x * self.yaw_step;
        self.pitch = self.pitch + dpos.y * self.pitch_step;

        self.update_restrictions();
        self.update_projviews();
    }

    fn update_eyes_location(&mut self) {
        // left and right are on a line perpendicular to both up and the target
        // up is always y
        let dir       = na::normalize(&(self.at() - self.eye));
        let tangent   = na::normalize(&Vector3::y().cross(&dir));
        self.eye_left = self.eye - tangent * (self.ipd / 2.0);
        self.eye_right = self.eye + tangent * (self.ipd / 2.0);
        //println(fmt!("eye_left = %f,%f,%f", self.eye_left.x as float, self.eye_left.y as float, self.eye_left.z as float));
        //println(fmt!("eye_right = %f,%f,%f", self.eye_right.x as float, self.eye_right.y as float, self.eye_right.z as float));
        // TODO: verify with an assert or something that the distance between the eyes is ipd, just to make me feel good.
    }

    #[doc(hidden)]
    pub fn handle_right_button_displacement(&mut self, dpos: &Vector2<f32>) {
        let at        = self.at();
        let dir       = na::normalize(&(at - self.eye));
        let tangent   = na::normalize(&Vector3::y().cross(&dir));
        let bitangent = dir.cross(&tangent);

        self.eye = self.eye + tangent * (0.01 * dpos.x / 10.0) + bitangent * (0.01 * dpos.y / 10.0);
        // TODO: ugly - should move eye update to where eye_left & eye_right are updated
        self.update_eyes_location();
        self.update_restrictions();
        self.update_projviews();
    }

    #[doc(hidden)]
    pub fn handle_scroll(&mut self, yoff: f32) {
        let front: Vector3<f32> = self.view_transform() * Vector3::z();

        self.eye = self.eye + front * (self.move_step * yoff);

        self.update_eyes_location();
        self.update_restrictions();
        self.update_projviews();
    }

    fn update_projviews(&mut self) {
        self.proj_view = *self.projection.as_matrix() * self.view_transform().to_homogeneous();
        self.inverse_proj_view = self.proj_view.try_inverse().unwrap();
        self.proj_view_left = *self.projection.as_matrix() * self.view_transform_left().to_homogeneous();
        self.proj_view_right = *self.projection.as_matrix() * self.view_transform_right().to_homogeneous();
    }

    fn transformation_eye(&self, eye: usize) -> Matrix4<f32> {
        match eye {
            0usize => self.proj_view_left,
            1usize => self.proj_view_right,
            _      => panic!("bad eye index")
        }
    }

    /// The left eye camera view transformation
    fn view_transform_left(&self) -> Isometry3<f32> {
        Isometry3::look_at_rh(&self.eye_left, &self.at(), &Vector3::y())
    }

    /// The right eye camera view transformation
    fn view_transform_right(&self) -> Isometry3<f32> {
        Isometry3::look_at_rh(&self.eye_right, &self.at(), &Vector3::y())
    }

    /// return Inter Pupilary Distance
    pub fn ipd(&self) -> f32 {
        self.ipd
    }
    
    /// change Inter Pupilary Distance
    pub fn set_ipd(&mut self, ipd: f32) {
        self.ipd = ipd;

        self.update_eyes_location();
        self.update_restrictions();
        self.update_projviews();
    }

}

impl Camera for FirstPersonStereo {
    fn clip_planes(&self) -> (f32, f32) {
        (self.projection.znear(), self.projection.zfar())
    }

    /// The imaginary middle eye camera view transformation (i-e transformation without projection).
    fn view_transform(&self) -> Isometry3<f32> {
        Isometry3::look_at_rh(&self.eye, &self.at(), &Vector3::y())
    }

    fn handle_event(&mut self, window: &glfw::Window, event: &WindowEvent) {
        match *event {
            WindowEvent::CursorPos(x, y) => {
                let curr_pos = Point2::new(x as f32, y as f32);

                if window.get_mouse_button(glfw::MouseButtonLeft) == Action::Press {
                    let dpos = curr_pos - self.last_cursor_pos;
                    self.handle_left_button_displacement(&dpos)
                }

                if window.get_mouse_button(glfw::MouseButtonRight) == Action::Press {
                    let dpos = curr_pos - self.last_cursor_pos;
                    self.handle_right_button_displacement(&dpos)
                }

                self.last_cursor_pos = curr_pos;
            },
            WindowEvent::Scroll(_, off) => self.handle_scroll(off as f32),
            WindowEvent::FramebufferSize(w, h) => {
                self.projection.set_aspect(w as f32 / h as f32);
                self.update_projviews();
            }
            _ => { }
        }
    }

    fn eye(&self) -> Point3<f32> {
        self.eye
    }

    fn transformation(&self) -> Matrix4<f32> {
        self.proj_view
    }

    fn inverse_transformation(&self) -> Matrix4<f32> {
        self.inverse_proj_view
    }

    fn update(&mut self, window: &glfw::Window) {
        let t     = self.view_transform();
        let front = t * Vector3::z();
        let right = t * Vector3::x();

        if window.get_key(Key::Up) == Action::Press {
            self.eye = self.eye + front * self.move_step
        }

        if window.get_key(Key::Down) == Action::Press {
            self.eye = self.eye + front * (-self.move_step)
        }

        if window.get_key(Key::Right) == Action::Press {
            self.eye = self.eye + right * (-self.move_step)
        }

        if window.get_key(Key::Left) == Action::Press {
            self.eye = self.eye + right * self.move_step
        }

        self.update_eyes_location();
        self.update_restrictions();
        self.update_projviews();
    }

    fn upload(&self, pass: usize, uniform: &mut ShaderUniform<Matrix4<f32>>) {
        uniform.upload(&self.transformation_eye(pass));
    }

    fn num_passes(&self) -> usize { 2usize }

    fn start_pass(&self, pass: usize, window: &glfw::Window) {
        let (win_w, win_h) = window.get_size();
        let (x, y, w, h) = match pass {
            0usize => (0, 0, win_w / 2 , win_h),
            1usize => (win_w / 2, 0, win_w / 2, win_h),
            _      => panic!("stereo first person takes only two passes")
        };
        verify!(gl::Viewport(x, y, w, h));
        verify!(gl::Scissor(x, y, w, h));
    }

    fn render_complete(&self, window: &glfw::Window) {
        let (w, h) = window.get_size();
        verify!(gl::Viewport(0, 0, w, h));
        verify!(gl::Scissor(0, 0, w, h));
    }
}