| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /*
- 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"
- #include "Quaternion.h"
- #include "Unit.h"
- #include "SceneGraph.h"
- #include "Assert.h"
- #include "Vector4.h"
- #include "Frustum.h"
- namespace crown
- {
- //-----------------------------------------------------------------------------
- Camera::Camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far)
- : m_scene_graph(sg)
- , m_node(node)
- , m_projection_type(type)
- , m_near(near)
- , m_far(far)
- {
- update_projection_matrix();
- }
- //-----------------------------------------------------------------------------
- Vector3 Camera::local_position() const
- {
- return m_scene_graph.local_position(m_node);
- }
- //-----------------------------------------------------------------------------
- Quaternion Camera::local_rotation() const
- {
- return m_scene_graph.local_rotation(m_node);
- }
- //-----------------------------------------------------------------------------
- Matrix4x4 Camera::local_pose() const
- {
- return m_scene_graph.local_pose(m_node);
- }
- //-----------------------------------------------------------------------------
- Vector3 Camera::world_position() const
- {
- return m_scene_graph.world_position(m_node);
- }
- //-----------------------------------------------------------------------------
- Quaternion Camera::world_rotation() const
- {
- return m_scene_graph.world_rotation(m_node);
- }
- //-----------------------------------------------------------------------------
- Matrix4x4 Camera::world_pose() const
- {
- return m_scene_graph.world_pose(m_node);
- }
- //-----------------------------------------------------------------------------
- void Camera::set_local_position(Unit* unit, const Vector3& pos)
- {
- unit->set_local_position(m_node, pos);
- }
- //-----------------------------------------------------------------------------
- void Camera::set_local_rotation(Unit* unit, const Quaternion& rot)
- {
- unit->set_local_rotation(m_node, rot);
- }
- //-----------------------------------------------------------------------------
- void Camera::set_local_pose(Unit* unit, const Matrix4x4& pose)
- {
- unit->set_local_pose(m_node, pose);
- }
- //-----------------------------------------------------------------------
- void Camera::set_projection_type(ProjectionType::Enum type)
- {
- m_projection_type = type;
- update_projection_matrix();
- }
- //-----------------------------------------------------------------------
- ProjectionType::Enum Camera::projection_type() const
- {
- return m_projection_type;
- }
- //-----------------------------------------------------------------------
- const Matrix4x4& Camera::projection_matrix() const
- {
- return m_projection;
- }
- //-----------------------------------------------------------------------------
- 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();
- }
- //-----------------------------------------------------------------------------
- void Camera::set_orthographic_metrics(float left, float right, float bottom, float top)
- {
- m_left = left;
- m_right = right;
- m_bottom = bottom;
- m_top = top;
- update_projection_matrix();
- }
- //-----------------------------------------------------------------------------
- void Camera::set_viewport_metrics(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
- {
- m_view_x = x;
- m_view_y = y;
- m_view_width = width;
- m_view_height = height;
- }
- //-----------------------------------------------------------------------------
- Vector3 Camera::screen_to_world(const Vector3& pos)
- {
- using namespace matrix4x4;
- Matrix4x4 world_inv = world_pose();
- invert(world_inv);
- Matrix4x4 mvp = m_projection * world_inv;
- invert(mvp);
- Vector4 ndc( (2 * (pos.x - 0)) / m_view_width - 1,
- (2 * (m_view_height - pos.y)) / m_view_height - 1,
- (2 * pos.z) - 1, 1);
- Vector4 tmp = mvp * ndc;
- tmp *= 1.0 / tmp.w;
- return Vector3(tmp.x, tmp.y, tmp.z);
- }
- //-----------------------------------------------------------------------------
- Vector3 Camera::world_to_screen(const Vector3& pos)
- {
- using namespace matrix4x4;
- Matrix4x4 world_inv = world_pose();
- invert(world_inv);
- Vector3 ndc = (m_projection * world_inv) * pos;
- return Vector3( (m_view_x + m_view_width * (ndc.x + 1.0)) / 2.0,
- (m_view_y + m_view_height * (ndc.y + 1.0)) / 2.0,
- (ndc.z + 1.0) / 2.0);
- }
- //-----------------------------------------------------------------------------
- void Camera::update_projection_matrix()
- {
- switch (m_projection_type)
- {
- case ProjectionType::ORTHOGRAPHIC:
- {
- matrix4x4::set_orthographic_rh(m_projection, m_left, m_right, m_bottom, m_top, m_near, m_far);
- break;
- }
- case ProjectionType::PERSPECTIVE:
- {
- matrix4x4::set_perspective_rh(m_projection, m_FOV, m_aspect, m_near, m_far);
- break;
- }
- default:
- {
- CE_FATAL("Oops, unknown projection type");
- break;
- }
- }
- }
- //-----------------------------------------------------------------------------
- void Camera::update_frustum()
- {
- // TODO
- //m_frustum.from_matrix(m_projection * m_view);
- }
- } // namespace crown
|