| 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 |
#ifndef AVOGADRO_RENDERING_CAMERA_H |
| 7 |
#define AVOGADRO_RENDERING_CAMERA_H |
| 8 |
|
| 9 |
#include "avogadrorenderingexport.h" |
| 10 |
|
| 11 |
#include <avogadro/core/vector.h> // For vector types. |
| 12 |
|
| 13 |
#include <Eigen/Geometry> // For member variables. |
| 14 |
#include <memory> |
| 15 |
|
| 16 |
namespace Avogadro { |
| 17 |
namespace Rendering { |
| 18 |
|
| 19 |
enum Projection |
| 20 |
{ |
| 21 |
Perspective, |
| 22 |
Orthographic |
| 23 |
}; |
| 24 |
|
| 25 |
// Separate Eigen datastructures to ensure sufficient memory alignment. |
| 26 |
struct EigenData |
| 27 |
{ |
| 28 |
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 29 |
Eigen::Affine3f projection; |
| 30 |
Eigen::Affine3f modelView; |
| 31 |
}; |
| 32 |
|
| 33 |
/** |
| 34 |
* @class Camera camera.h <avogadro/rendering/camera.h> |
| 35 |
* @brief The Camera class provides utility functionality useful in camera's |
| 36 |
* used with 3D scenes. |
| 37 |
* @author Marcus D. Hanwell |
| 38 |
*/ |
| 39 |
|
| 40 |
class AVOGADRORENDERING_EXPORT Camera |
| 41 |
{ |
| 42 |
public: |
| 43 |
Camera(); |
| 44 |
Camera(const Camera& o); |
| 45 |
Camera& operator=(const Camera& o); |
| 46 |
~Camera(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Translate the camera's model view matrix using the supplied translation |
| 50 |
* vector @p translate. |
| 51 |
*/ |
| 52 |
void translate(const Vector3f& translate); |
| 53 |
|
| 54 |
/** |
| 55 |
* Pretranslate the camera's model view matrix using the supplied translation |
| 56 |
* vector @p translate. |
| 57 |
*/ |
| 58 |
void preTranslate(const Vector3f& translate); |
| 59 |
|
| 60 |
/** |
| 61 |
* Rotate the camera about the supplied @p axis by @p angle (degrees). |
| 62 |
*/ |
| 63 |
void rotate(float angle, const Vector3f& axis); |
| 64 |
|
| 65 |
/** |
| 66 |
* Prerotate the camera about the supplied @p axis by @p angle (degrees). |
| 67 |
*/ |
| 68 |
void preRotate(float angle, const Vector3f& axis); |
| 69 |
|
| 70 |
/** |
| 71 |
* Modify the matrix, to give the effect of zooming in or out. |
| 72 |
*/ |
| 73 |
void scale(float scale); |
| 74 |
|
| 75 |
/** |
| 76 |
* Set the model-view matrix to the "look at" transformation matrix. |
| 77 |
* @param eye the position of the eye/camera. |
| 78 |
* @param center the position to look at. |
| 79 |
* @param up the vector pointing up. |
| 80 |
*/ |
| 81 |
void lookAt(const Vector3f& eye, const Vector3f& center, const Vector3f& up); |
| 82 |
|
| 83 |
/** |
| 84 |
* Distance to supplied point @p point and the camera. |
| 85 |
*/ |
| 86 |
float distance(const Vector3f& point) const; |
| 87 |
|
| 88 |
/** |
| 89 |
* Projects a point from the scene to the window. |
| 90 |
*/ |
| 91 |
Vector3f project(const Vector3f& point) const; |
| 92 |
|
| 93 |
/** |
| 94 |
* Unprojects a point from the window to the scene. |
| 95 |
*/ |
| 96 |
Vector3f unProject(const Vector3f& point) const; |
| 97 |
|
| 98 |
/** |
| 99 |
* Unprojects a point from the window to the scene, using the supplied |
| 100 |
* reference point (defaults to the origin if nothing is supplied). |
| 101 |
*/ |
| 102 |
Vector3f unProject(const Vector2f& point, |
| 103 |
const Vector3f& reference = Vector3f::Zero()) const; |
| 104 |
|
| 105 |
/** |
| 106 |
* Calculate the perspective projection matrix. |
| 107 |
* @param fieldOfView angle in degrees in the y direction. |
| 108 |
* @param aspectRatio is the ratio of width to height. |
| 109 |
* @param zNear is the distance from the viewer to the near clipping plane. |
| 110 |
* @param zFar is the distance from the viewer to the far clipping plane. |
| 111 |
*/ |
| 112 |
void calculatePerspective(float fieldOfView, float aspectRatio, float zNear, |
| 113 |
float zFar); |
| 114 |
|
| 115 |
/** |
| 116 |
* Calculate the perspective projection matrix. Computes the aspect ratio |
| 117 |
* from the width and height stored by the Camera object. |
| 118 |
* @param fieldOfView angle in degrees in the y direction. |
| 119 |
* @param zNear is the distance from the viewer to the near clipping plane. |
| 120 |
* @param zFar is the distance from the viewer to the far clipping plane. |
| 121 |
*/ |
| 122 |
void calculatePerspective(float fieldOfView, float zNear, float zFar); |
| 123 |
|
| 124 |
/** |
| 125 |
* Calculate the perspective projection matrix using frustum planes |
| 126 |
* coordinates. |
| 127 |
* @param left left vertical clipping plane. |
| 128 |
* @param right right vertical clipping plane. |
| 129 |
* @param bottom bottom horizontal clipping plane. |
| 130 |
* @param top top horizontal clipping plane. |
| 131 |
* @param zNear distance to the near clipping plane. |
| 132 |
* @param zFar distance to the far clipping plane. |
| 133 |
*/ |
| 134 |
void calculatePerspective(float left, float right, float bottom, float top, |
| 135 |
float zNear, float zFar); |
| 136 |
|
| 137 |
/** |
| 138 |
* Calculate the orthographic projection matrix. |
| 139 |
* @param left left vertical clipping plane. |
| 140 |
* @param right right vertical clipping plane. |
| 141 |
* @param bottom bottom horizontal clipping plane. |
| 142 |
* @param top top horizontal clipping plane. |
| 143 |
* @param zNear distance to the near clipping plane. |
| 144 |
* @param zFar distance to the far clipping plane. |
| 145 |
*/ |
| 146 |
void calculateOrthographic(float left, float right, float bottom, float top, |
| 147 |
float zNear, float zFar); |
| 148 |
|
| 149 |
/** |
| 150 |
* Set the dimensions of the viewport in pixels. |
| 151 |
*/ |
| 152 |
void setViewport(int w, int h); |
| 153 |
|
| 154 |
/** |
| 155 |
* Get the width of the viewport in pixels. |
| 156 |
*/ |
| 157 |
int width() const { return m_width; } |
| 158 |
|
| 159 |
/** |
| 160 |
* Get the height of the viewport in pixels. |
| 161 |
*/ |
| 162 |
int height() const { return m_height; } |
| 163 |
|
| 164 |
/** |
| 165 |
* Set the model view matrix to the identity. This resets the model view |
| 166 |
* matrix. |
| 167 |
*/ |
| 168 |
void setIdentity() { m_data->modelView.setIdentity(); } |
| 169 |
|
| 170 |
/** |
| 171 |
* Set the projection transform. |
| 172 |
*/ |
| 173 |
void setProjection(const Eigen::Affine3f& transform); |
| 174 |
|
| 175 |
/** |
| 176 |
* Get a reference to the projection matrix. |
| 177 |
*/ |
| 178 |
const Eigen::Affine3f& projection() const; |
| 179 |
|
| 180 |
/** |
| 181 |
* Set the model view transform. |
| 182 |
*/ |
| 183 |
void setModelView(const Eigen::Affine3f& transform); |
| 184 |
|
| 185 |
/** Get a reference to the model view matrix. */ |
| 186 |
const Eigen::Affine3f& modelView() const; |
| 187 |
|
| 188 |
/** |
| 189 |
* Set the projection type for this camera (Perspective or Orthographic). |
| 190 |
* @param proj The projection type to use. |
| 191 |
*/ |
| 192 |
void setProjectionType(Projection proj) { m_projectionType = proj; } |
| 193 |
|
| 194 |
/** |
| 195 |
* Get the projection type the camera is using. |
| 196 |
* @return The current projection type. |
| 197 |
*/ |
| 198 |
Projection projectionType() const { return m_projectionType; } |
| 199 |
|
| 200 |
/** |
| 201 |
* Set the orthographic scale, this defaults to 1.0. Affects calculation of |
| 202 |
* the orthographic projection matrix. |
| 203 |
* @param newScale The factor to scale orthographic projection by. |
| 204 |
*/ |
| 205 |
void setOrthographicScale(float newScale) { m_orthographicScale = newScale; } |
| 206 |
|
| 207 |
/** |
| 208 |
* Get the value of the orthographic scale, defaults to 1.0. |
| 209 |
* @return The current value of the orthographic scale. |
| 210 |
*/ |
| 211 |
float orthographicScale() const { return m_orthographicScale; } |
| 212 |
|
| 213 |
/** |
| 214 |
* Set focused point. Navigation actions shall occur relative to it. |
| 215 |
* Note that this method does not cause any change in camera matrices. |
| 216 |
*/ |
| 217 |
void setFocus(const Eigen::Vector3f& newFocus) { m_focus = newFocus; } |
| 218 |
|
| 219 |
/** |
| 220 |
* Get focused point. Navigation actions shall occur relative to it. |
| 221 |
*/ |
| 222 |
Vector3f focus() const { return m_focus; } |
| 223 |
|
| 224 |
private: |
| 225 |
int m_width; |
| 226 |
int m_height; |
| 227 |
Projection m_projectionType; |
| 228 |
float m_orthographicScale; |
| 229 |
std::unique_ptr<EigenData> m_data; |
| 230 |
Eigen::Vector3f m_focus; |
| 231 |
}; |
| 232 |
|
| 233 |
inline const Eigen::Affine3f& Camera::projection() const |
| 234 |
{ |
| 235 |
return m_data->projection; |
| 236 |
} |
| 237 |
|
| 238 |
inline const Eigen::Affine3f& Camera::modelView() const |
| 239 |
{ |
| 240 |
return m_data->modelView; |
| 241 |
} |
| 242 |
|
| 243 |
} // namespace Rendering |
| 244 |
} // namespace Avogadro |
| 245 |
|
| 246 |
#endif // AVOGADRO_RENDERING_CAMERA_H |