[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[953] | 2 | /* |
---|
[3611] | 3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
[953] | 4 | * |
---|
[3611] | 5 | * Authors: |
---|
| 6 | * Wei Qiao <qiaow@purdue.edu> |
---|
[953] | 7 | */ |
---|
[3611] | 8 | #ifndef NV_CAMERA_H |
---|
| 9 | #define NV_CAMERA_H |
---|
[953] | 10 | |
---|
[3492] | 11 | #include <vrmath/Matrix4x4d.h> |
---|
| 12 | #include <vrmath/Vector3f.h> |
---|
[2877] | 13 | |
---|
| 14 | #include "config.h" |
---|
[953] | 15 | |
---|
[3611] | 16 | namespace nv { |
---|
| 17 | |
---|
| 18 | class Camera |
---|
[2836] | 19 | { |
---|
[1215] | 20 | public: |
---|
[3630] | 21 | enum AxisDirection { |
---|
| 22 | X_POS = 1, |
---|
| 23 | Y_POS = 2, |
---|
| 24 | Z_POS = 3, |
---|
| 25 | X_NEG = -1, |
---|
| 26 | Y_NEG = -2, |
---|
| 27 | Z_NEG = -3 |
---|
| 28 | }; |
---|
[1215] | 29 | |
---|
[3630] | 30 | Camera(int x, int y, int width, int height); |
---|
| 31 | |
---|
[3611] | 32 | ~Camera() |
---|
[2836] | 33 | {} |
---|
| 34 | |
---|
[3630] | 35 | void setUpdir(AxisDirection dir) |
---|
[2836] | 36 | { |
---|
[4899] | 37 | setUpDirMatrix(dir); |
---|
[3630] | 38 | _mvDirty = true; |
---|
[1215] | 39 | } |
---|
[2836] | 40 | |
---|
[3630] | 41 | void setPosition(const vrmath::Vector3f& pos) |
---|
[2836] | 42 | { |
---|
[3630] | 43 | _position = pos; |
---|
| 44 | _mvDirty = true; |
---|
[1215] | 45 | } |
---|
[2836] | 46 | |
---|
[3630] | 47 | const vrmath::Vector3f& getPosition() const |
---|
[2836] | 48 | { |
---|
[3630] | 49 | return _position; |
---|
[1215] | 50 | } |
---|
[2836] | 51 | |
---|
[3630] | 52 | const vrmath::Vector3f& getFocalPoint() const |
---|
[2836] | 53 | { |
---|
[3630] | 54 | return _focalPoint; |
---|
[1215] | 55 | } |
---|
[2836] | 56 | |
---|
[3936] | 57 | double getDistance() const |
---|
[2836] | 58 | { |
---|
[3630] | 59 | return vrmath::Vector3f(_focalPoint - _position).length(); |
---|
[1215] | 60 | } |
---|
[2836] | 61 | |
---|
[3630] | 62 | vrmath::Vector3f getDirectionOfProjection() const |
---|
[2836] | 63 | { |
---|
[3630] | 64 | vrmath::Vector3f dir = _focalPoint - _position; |
---|
| 65 | dir = dir.normalize(); |
---|
| 66 | return dir; |
---|
[1215] | 67 | } |
---|
| 68 | |
---|
[3630] | 69 | vrmath::Vector3f getViewPlaneNormal() const |
---|
| 70 | { |
---|
| 71 | vrmath::Vector3f dir = _position - _focalPoint; |
---|
| 72 | dir = dir.normalize(); |
---|
| 73 | return dir; |
---|
| 74 | } |
---|
[2836] | 75 | |
---|
[3630] | 76 | void pan(float x, float y, bool absolute = true); |
---|
[2836] | 77 | |
---|
[3630] | 78 | void zoom(float z, bool absolute = true); |
---|
| 79 | |
---|
| 80 | void orient(double *quat); |
---|
| 81 | |
---|
| 82 | void orient(float angleX, float angleY, float angleZ); |
---|
| 83 | |
---|
| 84 | void setFov(float fov) |
---|
[2836] | 85 | { |
---|
[3362] | 86 | _fov = fov; |
---|
[1236] | 87 | } |
---|
[2836] | 88 | |
---|
[3630] | 89 | float getFov() const |
---|
[2836] | 90 | { |
---|
[3362] | 91 | return _fov; |
---|
[1236] | 92 | } |
---|
[2836] | 93 | |
---|
[3492] | 94 | void reset(const vrmath::Vector3f& bboxMin, |
---|
| 95 | const vrmath::Vector3f& bboxMax, |
---|
| 96 | bool resetOrientation = false); |
---|
| 97 | |
---|
| 98 | void resetClippingRange(const vrmath::Vector3f& bboxMin, |
---|
| 99 | const vrmath::Vector3f& bboxMax); |
---|
[1236] | 100 | |
---|
[3630] | 101 | void setViewport(int x, int y, int width, int height) |
---|
[2836] | 102 | { |
---|
[3630] | 103 | _viewport[0] = x; |
---|
| 104 | _viewport[1] = y; |
---|
| 105 | _viewport[2] = width; |
---|
| 106 | _viewport[3] = height; |
---|
[1215] | 107 | } |
---|
[2836] | 108 | |
---|
[2854] | 109 | /** |
---|
[2877] | 110 | * \brief Make the camera setting active, this has to be |
---|
[2854] | 111 | * called before drawing things |
---|
| 112 | */ |
---|
[2836] | 113 | void initialize(); |
---|
| 114 | |
---|
[3492] | 115 | void print() const; |
---|
| 116 | |
---|
[3630] | 117 | vrmath::Matrix4x4d& getModelViewMatrix() |
---|
| 118 | { |
---|
| 119 | computeModelViewMatrix(); |
---|
| 120 | return _modelViewMatrix; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | void getProjectionMatrix(vrmath::Matrix4x4d& mat); |
---|
| 124 | |
---|
| 125 | const vrmath::Matrix4x4d& getUpDirMatrix() const; |
---|
| 126 | |
---|
| 127 | void windowToWorldCoords(double x, double y, double z, vrmath::Vector3f& objPos); |
---|
| 128 | |
---|
| 129 | void worldToWindowCoords(double x, double y, double z, vrmath::Vector3f& winPos); |
---|
| 130 | |
---|
[2836] | 131 | private: |
---|
[3630] | 132 | void computeModelViewMatrix(); |
---|
[3492] | 133 | |
---|
[3630] | 134 | void setUpDirMatrix(AxisDirection dir); |
---|
| 135 | |
---|
| 136 | AxisDirection _updir; |
---|
| 137 | |
---|
| 138 | double _zoomRatio; |
---|
| 139 | float _pan[2]; |
---|
| 140 | /// Position of the camera in the scene |
---|
| 141 | vrmath::Vector3f _position; |
---|
| 142 | vrmath::Vector3f _focalPoint; |
---|
| 143 | |
---|
| 144 | /// Model transform for z-up scene |
---|
| 145 | vrmath::Matrix4x4d _updirMatrix; |
---|
| 146 | /// Camera orientation |
---|
| 147 | vrmath::Matrix4x4d _rotationMatrix; |
---|
| 148 | /// Full camera matrix |
---|
| 149 | bool _mvDirty; |
---|
| 150 | vrmath::Matrix4x4d _modelViewMatrix; |
---|
| 151 | |
---|
[3362] | 152 | /// Field of view (vertical angle in degrees) |
---|
| 153 | float _fov; |
---|
| 154 | /// Near, far z clipping |
---|
| 155 | float _near, _far; |
---|
[2877] | 156 | |
---|
[3630] | 157 | // x, y, width, height |
---|
| 158 | int _viewport[4]; |
---|
[4936] | 159 | }; |
---|
[953] | 160 | |
---|
[3611] | 161 | } |
---|
| 162 | |
---|
[953] | 163 | #endif |
---|