V-ART
camera.cpp
Go to the documentation of this file.
1 
5 #include "vart/camera.h"
6 #include "vart/transform.h"
7 
8 #ifdef WIN32
9 #include <windows.h>
10 #endif
11 #ifdef VART_OGL
12 #include <GL/glu.h>
13 #endif
14 
15 //?
16 #include <iostream>
17 
18 using namespace std;
19 
21  projectionType = PERSPECTIVE;
22  fovY = 60;
23  aspectRatio = 1.0f;
24  vvLeft = -1.0;
25  vvRight = 1.0;
26  vvTop = 1.0;
27  vvBottom = -1.0;
28  nearPlaneDistance = 0.5f;
29  farPlaneDistance = 100.0f;
30 
31  //~ viewHeight = 0;
32  //~ viewWidth = 0;
33 }
34 
35 VART::Camera::Camera(const VART::Point4D& position, const VART::Point4D& focus, const VART::Point4D& upVec)
36 {
37  projectionType = PERSPECTIVE;
38  fovY = 60;
39  vvLeft = -1.0;
40  vvRight = 1.0;
41  vvTop = 1.0;
42  vvBottom = -1.0;
43  nearPlaneDistance = 0.5f;
44  farPlaneDistance = 100.0f;
45  aspectRatio = 1.0f;
46  location = position;
47  target = focus;
48  up = upVec;
49  // Make sure "up" is orthogonal to "front" and normalized
50  VART::Point4D front = focus - position;
51  up.Normalize();
52  front.Normalize();
53  VART::Point4D left = up.CrossProduct(front);
54  up = front.CrossProduct(left);
55 }
56 
58 {
59  description = cam.description;
60  projectionType = cam.projectionType;
61  vvLeft = cam.vvLeft;
62  vvRight = cam.vvRight;
63  vvTop = cam.vvTop;
64  vvBottom = cam.vvBottom;
65  nearPlaneDistance = cam.nearPlaneDistance;
66  farPlaneDistance = cam.farPlaneDistance;
67  fovY = cam.fovY;
68  location = cam.location;
69  target = cam.target;
70  up = cam.up;
71  aspectRatio = cam.aspectRatio;
72  //~ viewHeight = cam.viewHeight;
73  //~ viewWidth = cam.viewWidth;
74  //~ winBottomLeft = cam.winBottomLeft;
75  //~ winTopRight = cam.winTopRight;
76 }
77 
79 {
80  description = cam.description;
81  projectionType = cam.projectionType;
82  vvLeft = cam.vvLeft;
83  vvRight = cam.vvRight;
84  vvTop = cam.vvTop;
85  vvBottom = cam.vvBottom;
86  nearPlaneDistance = cam.nearPlaneDistance;
87  farPlaneDistance = cam.farPlaneDistance;
88  fovY = cam.fovY;
89  location = cam.location;
90  target = cam.target;
91  up = cam.up;
92  aspectRatio = cam.aspectRatio;
93  return (*this);
94 }
95 
96 void VART::Camera::SetDescription(const string& descriptionValue){
97  description = descriptionValue;
98 }
99 
100 //~ VART::Point2D VART::Camera::GetWinBottomLeft() const {
101  //~ return winBottomLeft;
102 //~ }
103 
104 //~ void VART::Camera::SetWinBottomLeft(VART::Point2D winBottomLeftValue){
105  //~ winBottomLeft = winBottomLeftValue;
106 //~ }
107 
108 // FixMe: This method should be const
109 //~ void VART::Camera::GetWinTopRight(float &x, float &y) {
110  //~ x = winTopRight.vpGetX();
111  //~ y = winTopRight.vpGetY();
112 //~ }
113 
114 //~ void VART::Camera::SetWinTopRight(float x, float y) {
115  //~ winTopRight.vpSetX(x);
116  //~ winTopRight.vpSetY(y);
117 //~ }
118 
119 //~ VART::Point2D VART::Camera::GetWinTopRight() const {
120  //~ return winTopRight;
121 //~ }
122 
123 //~ void VART::Camera::SetWinTopRight(VART::Point2D winTopRightValue){
124  //~ winTopRight = winTopRightValue;
125 //~ }
126 
127 // FixMe: This method should be const
128 //~ VART::Point2D VART::Camera::GetWinLeftRight() {
129  //~ VART::Point2D point(winBottomLeft.vpGetX(), winTopRight.vpGetY());
130  //~ return point;
131 //~ }
132 
133 //~ void VART::Camera::SetWinLeftRight(VART::Point2D winLeftRightValue){
134  //~ winBottomLeft.vpSetY(winLeftRightValue.vpGetX());
135  //~ winTopRight.vpSetY(winLeftRightValue.vpGetY());
136 //~ }
137 
138 // FixMe: This method should be const
139 //~ VART::Point2D VART::Camera::GetWinBottomTop() {
140  //~ VART::Point2D point(winBottomLeft.vpGetX(), winTopRight.vpGetX());
141  //~ return point;
142 //~ }
143 
144 //~ void VART::Camera::SetWinBottomTop(VART::Point2D winBottomTopValue){
145  //~ winBottomLeft.vpSetX(winBottomTopValue.vpGetX());
146  //~ winTopRight.vpSetX(winBottomTopValue.vpGetY());
147 //~ }
148 
150  return location;
151 }
152 
153 void VART::Camera::SetLocation(const VART::Point4D& locationValue){
154  location = locationValue;
155 }
156 
158  return target;
159 }
160 
161 void VART::Camera::SetTarget(const VART::Point4D& targetValue){
162  target = targetValue;
163 }
164 
166  return up;
167 }
168 
169 void VART::Camera::SetUp(const VART::Point4D& upValue){
170  up = upValue;
171 }
172 
173 float VART::Camera::GetFovY() const {
174  return fovY;
175 }
176 
177 void VART::Camera::SetFovY(float f) {
178  fovY = f;
179 }
180 
181 void VART::Camera::LeftVector(Point4D* resultPtr) const {
182  VART::Point4D front = target - location;
183  front.Normalize();
184  *resultPtr = up.CrossProduct(front);
185 }
186 
187 void VART::Camera::FrontVector(Point4D* resultPtr) const {
188  VART::Point4D front = target - location;
189  front.Normalize();
190  *resultPtr = front;
191 }
192 
194  double halfHeight = newValue / 2;
195  double halfWidth = halfHeight * aspectRatio;
196  vvTop = halfHeight;
197  vvBottom = -halfHeight;
198  vvLeft = -halfWidth;
199  vvRight = halfWidth;
200 }
201 
202 void VART::Camera::ScaleVisibleVolume(float horScale, float verScale) {
203  vvTop *= verScale;
204  vvBottom *= verScale;
205  vvLeft *= horScale;
206  vvRight *= horScale;
207 }
208 
209 void VART::Camera::YawAroundTarget(float radians) {
210  VART::Transform trans;
211  trans.MakeRotation(target, up, radians);
212  trans.ApplyTo(&location);
213 }
214 
215 void VART::Camera::Yaw(float radians) {
216  VART::Transform trans;
217  trans.MakeRotation(location, up, radians);
218  trans.ApplyTo(&target);
219 }
220 
221 void VART::Camera::Roll(float radians) {
222  VART::Transform trans;
223  VART::Point4D front = target - location;
224 
225  front.Normalize();
226  trans.MakeRotation(location, front, radians);
227  trans.ApplyTo(&up);
228 }
229 
230 void VART::Camera::PitchAroundTarget(float radians) {
231  VART::Transform trans;
232  VART::Point4D left;
233  VART::Point4D front = target - location;
234  front.Normalize();
235  left = up.CrossProduct(front);
236  trans.MakeRotation(target, left, radians);
237  trans.ApplyTo(&location);
238  trans.ApplyTo(&up);
239 }
240 
241 void VART::Camera::MoveForward(double distance) {
242  VART::Transform trans;
243  VART::Point4D front = target - location;
244  front.Normalize();
245  front *= distance;
246  trans.MakeTranslation(front);
247  trans.ApplyTo(&location);
248  trans.ApplyTo(&target);
249 }
250 
251 void VART::Camera::MoveSideways(double distance) {
252  VART::Transform trans;
253  VART::Point4D right;
254 
255  trans.MakeRotation(up,-1.5707963267948966192313216916398);
256  right = trans * (target - location);
257 
258  right.Normalize();
259  right *= distance;
260  trans.MakeTranslation(right);
261  trans.ApplyTo(&location);
262  trans.ApplyTo(&target);
263 }
264 
265 void VART::Camera::MoveUp(double distance) {
266  VART::Transform trans;
267  VART::Point4D translation(up);
268 
269  translation.Normalize();
270  translation *= distance;
271  trans.MakeTranslation(translation);
272  trans.ApplyTo(&location);
273  trans.ApplyTo(&target);
274 }
275 
276 bool VART::Camera::DrawOGL() const {
277 #ifdef VART_OGL
278  // It seems that setting the projection matrix at every rendering cycle is a
279  // good idea because an application may have multiple cameras and multiple viewers.
280  glMatrixMode(GL_PROJECTION);
281  glLoadIdentity();
282  if (projectionType == PERSPECTIVE)
283  gluPerspective(fovY, aspectRatio, nearPlaneDistance, farPlaneDistance);
284  else
285  glOrtho(vvLeft, vvRight, vvBottom, vvTop, nearPlaneDistance, farPlaneDistance);
286  glMatrixMode(GL_MODELVIEW);
287  glLoadIdentity();
288  gluLookAt(location.GetX(),location.GetY(),location.GetZ(),
289  target.GetX(), target.GetY(), target.GetZ(),
290  up.GetX(), up.GetY(), up.GetZ());
291  return true;
292 #else
293  return false;
294 #endif
295 }
296 
298 {
299 #ifdef VART_OGL
300  // It seems that setting the projection matrix at every rendering cycle is a
301  // good idea because an application may have multiple cameras and multiple viewers.
302  glMatrixMode(GL_PROJECTION);
303  if (projectionType == PERSPECTIVE)
304  gluPerspective(fovY, aspectRatio, nearPlaneDistance, farPlaneDistance);
305  else
306  glOrtho(vvLeft, vvRight, vvBottom, vvTop, nearPlaneDistance, farPlaneDistance);
307  glMatrixMode(GL_MODELVIEW);
308  glLoadIdentity();
309  gluLookAt(location.GetX(),location.GetY(),location.GetZ(),
310  target.GetX(), target.GetY(), target.GetZ(),
311  up.GetX(), up.GetY(), up.GetZ());
312 #else
313  cerr << "Error! Camera::SetMatrices() unimplemented for non OpenGL systems!" << endl;
314 #endif
315 }
316 
317 ostream& VART::operator<<(ostream& output, const VART::Camera& cam)
318 {
319  output << "Camera(" << cam.description << ": pos"<< cam.location << " target" << cam.target
320  << " up" << cam.up << " near(" << cam.nearPlaneDistance << ") far(" << cam.farPlaneDistance
321  << "))";
322  return output;
323 }
void MoveForward(double distance)
Moves the camera forward (relative to the camera).
Definition: camera.cpp:241
float GetFovY() const
Returns the vertical field of view (in degrees).
Definition: camera.cpp:173
void SetMatrices() const
Loads camera transform into rendering engine.
Definition: camera.cpp:297
Point4D location
Position where the camera is at.
Definition: camera.h:185
Points and vectors using homogeneous coordinates.
Definition: point4d.h:22
void MakeRotation(const Point4D &refVec, const float radians)
Turns transform into a rotation (around some reference vetor).
Definition: transform.cpp:171
void ApplyTo(Point4D *ptPoint) const
Applies tranformation to a point.
Definition: transform.cpp:149
void Roll(float radians)
Rolls the Camera around itself.
Definition: camera.cpp:221
void MakeTranslation(const Point4D &translationVector)
Turns transform into a translation.
Definition: transform.cpp:56
Header file for V-ART class "Camera".
float fovY
Vertical angle of frustum. Used if projectionType == PERSPECTIVE.
Definition: camera.h:163
void Normalize()
Normalizes the point/vector.
Definition: point4d.cpp:88
void SetDescription(const std::string &descriptionValue)
Sets the description associated with the camera.
Definition: camera.cpp:96
std::ostream & operator<<(std::ostream &output, const Joint::DofID &dofId)
void LeftVector(Point4D *resultPtr) const
Computes the vector pointing left.
Definition: camera.cpp:181
void ScaleVisibleVolume(float horScale, float verScale)
Scales the visible volume.
Definition: camera.cpp:202
Point4D GetTarget() const
Returns the camera target (where it is looking at).
Definition: camera.cpp:157
Geometric transformations.
Definition: transform.h:24
Point4D CrossProduct(const Point4D &p) const
Computes the cross product between "this" and "p".
Definition: point4d.cpp:121
float farPlaneDistance
Distance to far plane.
Definition: camera.h:169
void SetTarget(const Point4D &targetValue)
Sets the camera target (where it is looking at).
Definition: camera.cpp:161
void SetUp(const Point4D &upValue)
Sets the camera up vector.
Definition: camera.cpp:169
void SetFovY(float f)
Sets the vertical field of view (in degrees).
Definition: camera.cpp:177
bool DrawOGL() const
Positions a camera using OpenGL commands.
Definition: camera.cpp:276
Virtual camera.
Definition: camera.h:16
void YawAroundTarget(float radians)
Rotates the Camera around the axis defined by the target and up vector.
Definition: camera.cpp:209
void SetLocation(const Point4D &locationValue)
Sets the camera location.
Definition: camera.cpp:153
Point4D up
Up direction, relative to the camera.
Definition: camera.h:190
Header file for V-ART class "Transform".
void Yaw(float radians)
Rotates the Camera around itself (location and up vector).
Definition: camera.cpp:215
Point4D GetUp() const
Returns the camera up vector.
Definition: camera.cpp:165
void MoveUp(double distance)
Moves the camera up and down.
Definition: camera.cpp:265
void SetVisibleVolumeHeight(double newValue)
Sets the visible volume by given height.
Definition: camera.cpp:193
double vvRight
Visible Volume right coordinate (in world coordinates). Used if projectionType is ORTHOGRAPHIC...
Definition: camera.h:176
void PitchAroundTarget(float radians)
Rotates the Camera around the axis defined by the target and left vector.
Definition: camera.cpp:230
void MoveSideways(double distance)
Moves the camera sideways.
Definition: camera.cpp:251
double vvBottom
Visible Volume bottom coordinate (in world coordinates). Used if projectionType is ORTHOGRAPHIC...
Definition: camera.h:182
float aspectRatio
Viewing aspect ratio (width/height).
Definition: camera.h:165
Camera & operator=(const Camera &cam)
Definition: camera.cpp:78
Point4D GetLocation() const
Returns the camera location (its position).
Definition: camera.cpp:149
Point4D target
Position where the camera is looking at.
Definition: camera.h:187
float nearPlaneDistance
Distance to near plane.
Definition: camera.h:167
double vvTop
Visible Volume top coordinate (in world coordinates). Used if projectionType is ORTHOGRAPHIC.
Definition: camera.h:179
double vvLeft
Visible Volume left coordinate (in world coordinates). Used if projectionType is ORTHOGRAPHIC.
Definition: camera.h:173
void FrontVector(Point4D *resultPtr) const
Computes the vector pointing ahead.
Definition: camera.cpp:187
ProjectionType projectionType
Indicates whether a perspective ou orthographic projection should be used.
Definition: camera.h:161