]> git.tdb.fi Git - libs/gl.git/commitdiff
Make better use of matrix operations in Camera
authorMikko Rasa <tdb@tdb.fi>
Mon, 25 Nov 2013 14:21:57 +0000 (16:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 25 Nov 2013 14:23:16 +0000 (16:23 +0200)
source/camera.cpp
source/camera.h

index 274604f27f32e74833925d29828ec1628d22f36a..0afa033c1348bf43a4fae8f57072c798a625e79d 100644 (file)
@@ -42,21 +42,19 @@ void Camera::set_frustum_axis(float x, float y)
 void Camera::set_position(const Vector3 &p)
 {
        position = p;
-       compute_matrix();
+       update_object_matrix();
 }
 
 void Camera::set_up_direction(const Vector3 &u)
 {
        up_dir = normalize(u);
-
-       compute_matrix();
+       update_object_matrix();
 }
 
 void Camera::set_look_direction(const Vector3 &l)
 {
        look_dir = normalize(l);
-
-       compute_matrix();
+       update_object_matrix();
 }
 
 void Camera::look_at(const Vector3 &p)
@@ -66,14 +64,8 @@ void Camera::look_at(const Vector3 &p)
 
 Vector3 Camera::project(const Vector4 &p) const
 {
-       float frustum_h = tan(fov/2.0f);
-       float frustum_w = frustum_h*aspect;
-       float z_range = clip_far-clip_near;
-
-       Vector4 eye = matrix*p;
-
-       return Vector3(eye.x/frustum_w/-eye.z, eye.y/frustum_h/-eye.z,
-               (clip_far+clip_near)/z_range+2*clip_far*clip_near/(eye.z*z_range));
+       Vector4 r = proj_matrix*(view_matrix*p);
+       return Vector3(r)/r.w;
 }
 
 Vector3 Camera::project(const Vector3 &p) const
@@ -83,18 +75,9 @@ Vector3 Camera::project(const Vector3 &p) const
 
 Vector4 Camera::unproject(const Vector4 &p) const
 {
-       float frustum_h = tan(fov/2.0f);
-       float frustum_w = frustum_h*aspect;
-       float z_range = clip_far-clip_near;
-
-       float z = (2*clip_far*clip_near)/(p.z*z_range-(clip_far+clip_near))-matrix[14]*p.w;
-       float x = p.x*-z*frustum_w-matrix[12]*p.w;
-       float y = p.y*-z*frustum_h-matrix[13]*p.w;
-
-       return Vector4(matrix[0]*x+matrix[1]*y+matrix[2]*z,
-               matrix[4]*x+matrix[5]*y+matrix[6]*z,
-               matrix[8]*x+matrix[9]*y+matrix[10]*z,
-               p.w);
+       Vector4 r = invert(proj_matrix)*LinAl::Vector<double, 4>(p.x, p.y, p.z, 1.0f);
+       r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
+       return r;
 }
 
 void Camera::apply() const
@@ -106,36 +89,19 @@ void Camera::apply() const
        float bottom = frustum_h*(frustum_y-1.0f);
        float top = frustum_h*(frustum_y+1.0f);
        MatrixStack::projection() = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
-       MatrixStack::modelview() = matrix;
+       MatrixStack::modelview() = view_matrix;
 }
 
-void Camera::compute_matrix()
+void Camera::update_object_matrix()
 {
        Vector3 right_dir = normalize(cross(look_dir, up_dir));
-       double mdata[16];
-
-       mdata[0] = right_dir.x;
-       mdata[4] = right_dir.y;
-       mdata[8] = right_dir.z;
-
-       mdata[1] = right_dir.y*look_dir.z-right_dir.z*look_dir.y;
-       mdata[5] = right_dir.z*look_dir.x-right_dir.x*look_dir.z;
-       mdata[9] = right_dir.x*look_dir.y-right_dir.y*look_dir.x;
-
-       mdata[2] = -look_dir.x;
-       mdata[6] = -look_dir.y;
-       mdata[10] = -look_dir.z;
-
-       mdata[12] = -position.x*mdata[0]-position.y*mdata[4]-position.z*mdata[8];
-       mdata[13] = -position.x*mdata[1]-position.y*mdata[5]-position.z*mdata[9];
-       mdata[14] = -position.x*mdata[2]-position.y*mdata[6]-position.z*mdata[10];
-
-       mdata[3] = 0;
-       mdata[7] = 0;
-       mdata[11] = 0;
-       mdata[15] = 1;
-
-       matrix = mdata;
+       LinAl::Vector<double, 4> columns[4];
+       columns[0] = LinAl::Vector<double, 4>(right_dir, 0.0f);
+       columns[1] = LinAl::Vector<double, 4>(cross(right_dir, look_dir), 0.0f);
+       columns[2] = LinAl::Vector<double, 4>(-look_dir, 0.0f);
+       columns[3] = LinAl::Vector<double, 4>(position, 1.0f);
+       object_matrix = Matrix::from_columns(columns);
+       view_matrix = invert(object_matrix);
 }
 
 } // namespace GL
index a0e4d6f65fb3215a5d8f64e2fbc1fef58347377f..634557607c112abdd619e622203cfafe1b9eddd0 100644 (file)
@@ -20,7 +20,8 @@ private:
        Vector3 position;
        Vector3 look_dir;
        Vector3 up_dir;
-       Matrix matrix;
+       Matrix view_matrix;
+       Matrix object_matrix;
 
 public:
        Camera();
@@ -41,7 +42,17 @@ public:
        const Vector3 &get_position() const { return position; }
        const Vector3 &get_look_direction() const { return look_dir; }
        const Vector3 &get_up_direction() const { return up_dir; }
-       const Matrix &get_matrix() const { return matrix; }
+
+       /** Deprecated alias for get_view_matrix. */
+       const Matrix &get_matrix() const { return get_view_matrix(); }
+
+       /** Returns the view matrix, used to transform coordinates from world space
+       to eye space. */
+       const Matrix &get_view_matrix() const { return view_matrix; }
+
+       /** Returns the object matrix, used to transform coordinates from eye space
+       to world space. */
+       const Matrix &get_object_matrix() const { return object_matrix; }
 
        Vector3 project(const Vector4 &) const;
        Vector3 project(const Vector3 &) const;
@@ -50,7 +61,7 @@ public:
        void apply() const;
 
 private:
-       void compute_matrix();
+       void update_object_matrix();
 };
 
 } // namespace GL