| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include "Camera.h"
- #include "Types.h"
- #include "MathUtils.h"
- namespace crown
- {
- //-----------------------------------------------------------------------------
- Camera::Camera(const Vec3& position, float fov, float aspect) :
- m_position(position),
- m_look_at(0, 0, -1),
- m_up(0, 1, 0),
- m_rot_factor(0.0f, 0.0f),
- m_angle_x(0.0f),
- m_angle_y(0.0f),
- m_FOV(fov),
- m_aspect(aspect),
- m_near(0.1f),
- m_far(1000.0f)
- {
- update_projection_matrix();
- update_view_matrix();
- update_frustum();
- }
- //-----------------------------------------------------------------------------
- const Vec3& Camera::position() const
- {
- return m_position;
- }
- //-----------------------------------------------------------------------------
- void Camera::set_position(const Vec3& position)
- {
- m_position = position;
- update_view_matrix();
- }
- //-----------------------------------------------------------------------------
- const Vec3& Camera::look_at() const
- {
- return m_look_at;
- }
- //-----------------------------------------------------------------------------
- void Camera::set_look_at(const Vec3& lookat)
- {
- m_look_at = lookat;
- update_view_matrix();
- }
- //-----------------------------------------------------------------------
- void Camera::set_rotation(const float x, const float y)
- {
- Vec3 right(1, 0, 0);
- Vec3 look;
- look.x = 0.0f;
- look.y = math::sin(x);
- look.z = -math::cos(x);
- Vec3 up = right.cross(look);
- up.normalize();
- Mat3 m;
- m.build_rotation_y(y);
- look = m * look;
- m_up = m * up;
- set_look_at(look);
- }
- //-----------------------------------------------------------------------------
- const Vec3& Camera::up() const
- {
- return m_up;
- }
- //-----------------------------------------------------------------------------
- float Camera::fov() const
- {
- return m_FOV;
- }
- //-----------------------------------------------------------------------------
- void Camera::set_fov(float fov)
- {
- m_FOV = fov;
- update_projection_matrix();
- }
- //-----------------------------------------------------------------------------
- float Camera::aspect() const
- {
- return m_aspect;
- }
- //-----------------------------------------------------------------------------
- void Camera::set_aspect(float aspect)
- {
- m_aspect = aspect;
- update_projection_matrix();
- }
- //-----------------------------------------------------------------------------
- float Camera::near_clip_distance() const
- {
- return m_near;
- }
- //-----------------------------------------------------------------------------
- void Camera::set_near_clip_distance(float near)
- {
- m_near = near;
- update_projection_matrix();
- }
- //-----------------------------------------------------------------------------
- float Camera::far_clip_distance() const
- {
- return m_far;
- }
- //-----------------------------------------------------------------------------
- void Camera::set_far_clip_distance(float far)
- {
- m_far = far;
- update_projection_matrix();
- }
- //-----------------------------------------------------------------------------
- const Mat4& Camera::projection_matrix() const
- {
- return m_projection;
- }
- //-----------------------------------------------------------------------------
- const Mat4& Camera::view_matrix() const
- {
- return m_view;
- }
- //-----------------------------------------------------------------------------
- const Frustum& Camera::frustum() const
- {
- return m_frustum;
- }
- //-----------------------------------------------------------------------------
- void Camera::update_projection_matrix()
- {
- m_projection.build_projection_perspective_rh(m_FOV, m_aspect, m_near, m_far);
- }
- //-----------------------------------------------------------------------------
- void Camera::update_view_matrix()
- {
- m_view.build_look_at_rh(m_position, m_position + m_look_at, m_up);
- }
- //-----------------------------------------------------------------------------
- void Camera::update_frustum()
- {
- m_frustum.from_matrix(m_projection * m_view);
- }
- //-----------------------------------------------------------------------------
- void Camera::move_forward(float meters)
- {
- set_position(m_position + m_look_at * meters);
- }
- //-----------------------------------------------------------------------
- void Camera::move_backward(float meters)
- {
- set_position(m_position + m_look_at * -meters);
- }
- //-----------------------------------------------------------------------
- void Camera::strafe_left(float meters)
- {
- Vec3 left = m_up.cross(m_look_at);
- left.normalize();
- set_position(m_position + left * meters);
- }
- //-----------------------------------------------------------------------
- void Camera::strafe_right(float meters)
- {
- Vec3 left = m_up.cross(m_look_at);
- left.normalize();
- set_position(m_position + left * -meters);
- }
- } // namespace crown
|