| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "camera.h" |
| 7 |
|
| 8 |
#include <Eigen/LU> |
| 9 |
|
| 10 |
#include <cmath> |
| 11 |
|
| 12 |
namespace Avogadro::Rendering { |
| 13 |
|
| 14 |
Camera::Camera() |
| 15 |
: m_width(0), m_height(0), m_projectionType(Perspective), |
| 16 |
m_orthographicScale(1.0), m_data(new EigenData), m_focus(NAN, NAN, NAN) |
| 17 |
{ |
| 18 |
m_data->projection.setIdentity(); |
| 19 |
m_data->modelView.setIdentity(); |
| 20 |
} |
| 21 |
|
| 22 |
Camera::Camera(const Camera& o) |
| 23 |
: m_width(o.m_width), m_height(o.m_height), |
| 24 |
m_projectionType(o.m_projectionType), |
| 25 |
m_orthographicScale(o.m_orthographicScale), m_data(new EigenData(*o.m_data)) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
Camera& Camera::operator=(const Camera& o) |
| 30 |
{ |
| 31 |
if (this != &o) { |
| 32 |
m_width = o.m_width; |
| 33 |
m_height = o.m_height; |
| 34 |
m_projectionType = o.m_projectionType; |
| 35 |
m_orthographicScale = o.m_orthographicScale; |
| 36 |
m_data = std::move(std::unique_ptr<EigenData>(new EigenData(*o.m_data))); |
| 37 |
} |
| 38 |
|
| 39 |
return *this; |
| 40 |
} |
| 41 |
|
| 42 |
Camera::~Camera() {} |
| 43 |
|
| 44 |
void Camera::translate(const Vector3f& translate_) |
| 45 |
{ |
| 46 |
m_data->modelView.translate(translate_); |
| 47 |
} |
| 48 |
|
| 49 |
void Camera::preTranslate(const Vector3f& translate_) |
| 50 |
{ |
| 51 |
m_data->modelView.pretranslate(translate_); |
| 52 |
} |
| 53 |
|
| 54 |
void Camera::rotate(float angle, const Vector3f& axis) |
| 55 |
{ |
| 56 |
m_data->modelView.rotate(Eigen::AngleAxisf(angle, axis)); |
| 57 |
} |
| 58 |
|
| 59 |
void Camera::preRotate(float angle, const Vector3f& axis) |
| 60 |
{ |
| 61 |
m_data->modelView.prerotate(Eigen::AngleAxisf(angle, axis)); |
| 62 |
} |
| 63 |
|
| 64 |
void Camera::scale(float s) |
| 65 |
{ |
| 66 |
if (m_projectionType == Perspective) |
| 67 |
m_data->modelView.scale(s); |
| 68 |
else |
| 69 |
m_orthographicScale *= s; |
| 70 |
} |
| 71 |
|
| 72 |
void Camera::lookAt(const Vector3f& eye, const Vector3f& center, |
| 73 |
const Vector3f& up) |
| 74 |
{ |
| 75 |
Vector3f f = (center - eye).normalized(); |
| 76 |
Vector3f u = up.normalized(); |
| 77 |
Vector3f s = f.cross(u).normalized(); |
| 78 |
u = s.cross(f); |
| 79 |
|
| 80 |
m_data->modelView.setIdentity(); |
| 81 |
m_data->modelView(0, 0) = s.x(); |
| 82 |
m_data->modelView(0, 1) = s.y(); |
| 83 |
m_data->modelView(0, 2) = s.z(); |
| 84 |
m_data->modelView(1, 0) = u.x(); |
| 85 |
m_data->modelView(1, 1) = u.y(); |
| 86 |
m_data->modelView(1, 2) = u.z(); |
| 87 |
m_data->modelView(2, 0) = -f.x(); |
| 88 |
m_data->modelView(2, 1) = -f.y(); |
| 89 |
m_data->modelView(2, 2) = -f.z(); |
| 90 |
m_data->modelView(0, 3) = -s.dot(eye); |
| 91 |
m_data->modelView(1, 3) = -u.dot(eye); |
| 92 |
m_data->modelView(2, 3) = f.dot(eye); |
| 93 |
} |
| 94 |
|
| 95 |
float Camera::distance(const Vector3f& point) const |
| 96 |
{ |
| 97 |
return (m_data->modelView * point).norm(); |
| 98 |
} |
| 99 |
|
| 100 |
Vector3f Camera::project(const Vector3f& point) const |
| 101 |
{ |
| 102 |
Eigen::Matrix4f mvp = |
| 103 |
m_data->projection.matrix() * m_data->modelView.matrix(); |
| 104 |
Vector4f tPoint(point.x(), point.y(), point.z(), 1.0f); |
| 105 |
tPoint = mvp * tPoint; |
| 106 |
Vector3f result( |
| 107 |
static_cast<float>(m_width) * (tPoint.x() / tPoint.w() + 1.0f) / 2.0f, |
| 108 |
static_cast<float>(m_height) * (tPoint.y() / tPoint.w() + 1.0f) / 2.0f, |
| 109 |
(tPoint.z() / tPoint.w() + 1.0f) / 2.0f); |
| 110 |
return result; |
| 111 |
} |
| 112 |
|
| 113 |
Vector3f Camera::unProject(const Vector3f& point) const |
| 114 |
{ |
| 115 |
Eigen::Matrix4f mvp = |
| 116 |
m_data->projection.matrix() * m_data->modelView.matrix(); |
| 117 |
Vector4f result(2.0f * point.x() / static_cast<float>(m_width) - 1.0f, |
| 118 |
2.0f * (static_cast<float>(m_height) - point.y()) / |
| 119 |
static_cast<float>(m_height) - |
| 120 |
1.0f, |
| 121 |
2.0f * point.z() - 1.0f, 1.0f); |
| 122 |
result = mvp.matrix().inverse() * result; |
| 123 |
return Vector3f(result.x() / result.w(), result.y() / result.w(), |
| 124 |
result.z() / result.w()); |
| 125 |
} |
| 126 |
|
| 127 |
Vector3f Camera::unProject(const Vector2f& point, |
| 128 |
const Vector3f& reference) const |
| 129 |
{ |
| 130 |
return unProject(Vector3f(point.x(), point.y(), project(reference).z())); |
| 131 |
} |
| 132 |
|
| 133 |
void Camera::calculatePerspective(float fieldOfView, float aspectRatio, |
| 134 |
float zNear, float zFar) |
| 135 |
{ |
| 136 |
m_data->projection.setIdentity(); |
| 137 |
float f = 1.0f / std::tan(fieldOfView * float(M_PI) / 360.0f); |
| 138 |
m_data->projection(0, 0) = f / aspectRatio; |
| 139 |
m_data->projection(1, 1) = f; |
| 140 |
m_data->projection(2, 2) = (zNear + zFar) / (zNear - zFar); |
| 141 |
m_data->projection(2, 3) = (2.0f * zFar * zNear) / (zNear - zFar); |
| 142 |
m_data->projection(3, 2) = -1; |
| 143 |
m_data->projection(3, 3) = 0; |
| 144 |
} |
| 145 |
|
| 146 |
void Camera::calculatePerspective(float fieldOfView, float zNear, float zFar) |
| 147 |
{ |
| 148 |
calculatePerspective( |
| 149 |
fieldOfView, static_cast<float>(m_width) / static_cast<float>(m_height), |
| 150 |
zNear, zFar); |
| 151 |
} |
| 152 |
|
| 153 |
void Camera::calculateOrthographic(float left, float right, float bottom, |
| 154 |
float top, float zNear, float zFar) |
| 155 |
{ |
| 156 |
left *= m_orthographicScale; |
| 157 |
right *= m_orthographicScale; |
| 158 |
bottom *= m_orthographicScale; |
| 159 |
top *= m_orthographicScale; |
| 160 |
m_data->projection.setIdentity(); |
| 161 |
m_data->projection(0, 0) = 2.0f / (right - left); |
| 162 |
m_data->projection(0, 3) = -(right + left) / (right - left); |
| 163 |
m_data->projection(1, 1) = 2.0f / (top - bottom); |
| 164 |
m_data->projection(1, 3) = -(top + bottom) / (top - bottom); |
| 165 |
m_data->projection(2, 2) = -2.0f / (zFar - zNear); |
| 166 |
m_data->projection(2, 3) = -(zFar + zNear) / (zFar - zNear); |
| 167 |
m_data->projection(3, 3) = 1; |
| 168 |
} |
| 169 |
|
| 170 |
void Camera::setViewport(int w, int h) |
| 171 |
{ |
| 172 |
m_width = w; |
| 173 |
m_height = h; |
| 174 |
} |
| 175 |
|
| 176 |
void Camera::setProjection(const Eigen::Affine3f& transform) |
| 177 |
{ |
| 178 |
m_data->projection = transform; |
| 179 |
} |
| 180 |
|
| 181 |
void Camera::setModelView(const Eigen::Affine3f& transform) |
| 182 |
{ |
| 183 |
m_data->modelView = transform; |
| 184 |
} |
| 185 |
|
| 186 |
void Camera::calculatePerspective(float left, float right, float bottom, |
| 187 |
float top, float zNear, float zFar) |
| 188 |
{ |
| 189 |
m_data->projection.setIdentity(); |
| 190 |
|
| 191 |
m_data->projection(0, 0) = (2.0f * zNear) / (right - left); |
| 192 |
m_data->projection(1, 1) = (2.0f * zNear) / (top - bottom); |
| 193 |
m_data->projection(0, 2) = (right + left) / (right - left); |
| 194 |
m_data->projection(1, 2) = (top + bottom) / (top - bottom); |
| 195 |
m_data->projection(2, 2) = -(zFar + zNear) / (zFar - zNear); |
| 196 |
m_data->projection(3, 2) = -1.0f; |
| 197 |
m_data->projection(2, 3) = -(2.0f * zFar * zNear) / (zFar - zNear); |
| 198 |
m_data->projection(3, 3) = 0.0f; |
| 199 |
} |
| 200 |
|
| 201 |
} // namespace Avogadro::Rendering |