]> git.tdb.fi Git - libs/gl.git/blobdiff - source/camera.cpp
Refresh lighting and culling uniforms if the camera changes in pop_state
[libs/gl.git] / source / camera.cpp
index fca3ea33d504773d90e073c598175a33d9be2f46..ae35f632438df2d9a7a8a416f838deabde6b8320 100644 (file)
@@ -7,6 +7,7 @@ namespace GL {
 
 Camera::Camera():
        fov(Geometry::Angle<float>::from_turns(0.125)),
+       height(0),
        aspect(4.0/3.0),
        clip_near(0.1),
        clip_far(10),
@@ -26,6 +27,15 @@ void Camera::set_field_of_view(const Geometry::Angle<float> &f)
        update_projection_matrix();
 }
 
+void Camera::set_orthographic(float w, float h)
+{
+       fov = Geometry::Angle<float>::zero();
+       height = h;
+       if(w)
+               aspect = w/h;
+       update_projection_matrix();
+}
+
 void Camera::set_aspect(float a)
 {
        aspect = a;
@@ -46,6 +56,12 @@ void Camera::set_frustum_axis(float x, float y)
        update_projection_matrix();
 }
 
+void Camera::set_frustum_rotation(const Geometry::Angle<float> &r)
+{
+       rotate = r;
+       update_projection_matrix();
+}
+
 void Camera::set_position(const Vector3 &p)
 {
        position = p;
@@ -72,7 +88,7 @@ void Camera::look_at(const Vector3 &p)
 Vector3 Camera::project(const Vector4 &p) const
 {
        Vector4 r = proj_matrix*(view_matrix*p);
-       return Vector3(r)/r.w;
+       return r.slice<3>(0)/r.w;
 }
 
 Vector3 Camera::project(const Vector3 &p) const
@@ -83,10 +99,15 @@ Vector3 Camera::project(const Vector3 &p) const
 Vector4 Camera::unproject(const Vector4 &p) const
 {
        Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f);
-       r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
+       r = object_matrix*Vector4(r.x/r.w, r.y/r.w, r.z/r.w, p.w);
        return r;
 }
 
+Vector3 Camera::unproject(const Vector3 &p) const
+{
+       return unproject(Vector4(p.x, p.y, p.z, 1.0f)).slice<3>(0);
+}
+
 void Camera::apply() const
 {
        MatrixStack::projection() = proj_matrix;
@@ -95,23 +116,27 @@ void Camera::apply() const
 
 void Camera::update_projection_matrix()
 {
-       float frustum_h = tan(fov/2.0f)*clip_near;
+       float frustum_h = (fov!=Geometry::Angle<float>::zero() ? tan(fov/2.0f)*clip_near : height/2);
        float frustum_w = frustum_h*aspect;
        float left = frustum_w*(frustum_x-1.0f);
        float right = frustum_w*(frustum_x+1.0f);
        float bottom = frustum_h*(frustum_y-1.0f);
        float top = frustum_h*(frustum_y+1.0f);
-       proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
+       if(fov>Geometry::Angle<float>::zero())
+               proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
+       else
+               proj_matrix = Matrix::ortho(left, right, bottom, top, clip_near, clip_far);
+       proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix;
 }
 
 void Camera::update_object_matrix()
 {
        Vector3 right_dir = normalize(cross(look_dir, up_dir));
        Vector4 columns[4];
-       columns[0] = Vector4(right_dir, 0.0f);
-       columns[1] = Vector4(cross(right_dir, look_dir), 0.0f);
-       columns[2] = Vector4(-look_dir, 0.0f);
-       columns[3] = Vector4(position, 1.0f);
+       columns[0] = compose(right_dir, 0.0f);
+       columns[1] = compose(cross(right_dir, look_dir), 0.0f);
+       columns[2] = compose(-look_dir, 0.0f);
+       columns[3] = compose(position, 1.0f);
        object_matrix = Matrix::from_columns(columns);
        view_matrix = invert(object_matrix);
 }