camera.cc
Go to the documentation of this file.
1 /*
2  * camera.cc
3  * Stage
4  *
5  * Created by Alex Couture-Beil on 06/06/08.
6  *
7  */
8 
9 
10 #include "stage.hh"
11 #include "worldfile.hh"
12 using namespace Stg;
13 
14 #include <iostream>
15 
16 // Perspective Camera
18  Camera(),
19  _z_near( 0.2 ), _z_far( 40.0 ),
20  _vert_fov( 70 ), _horiz_fov( 70 ),
21  _aspect( 1.0 )
22 {
23  setPitch( 70.0 );
24 }
25 
26 void PerspectiveCamera::move( double x, double y, double z )
27 {
28  (void)z; // avoid warning about unused var
29 
30  //scale relative to zoom level
31  x *= _z / 100.0;
32  y *= _z / 100.0;
33 
34  //adjust for yaw angle
35  _x += cos( dtor( _yaw ) ) * x;
36  _x += -sin( dtor( _yaw ) ) * y;
37 
38  _y += sin( dtor( _yaw ) ) * x;
39  _y += cos( dtor( _yaw ) ) * y;
40  }
41 
42 void PerspectiveCamera::Draw( void ) const
43 {
44  glMatrixMode (GL_MODELVIEW);
45  glLoadIdentity ();
46 
47  glRotatef( - _pitch, 1.0, 0.0, 0.0 );
48  glRotatef( - _yaw, 0.0, 0.0, 1.0 );
49 
50  glTranslatef( - _x, - _y, - _z );
51  //zooming needs to happen in the Projection code (don't use glScale for zoom)
52 }
53 
55 {
56 // SetProjection( pixels_width/pixels_height );
57 
58  glMatrixMode (GL_PROJECTION);
59  glLoadIdentity ();
60 
61  double top = tan( dtor( _vert_fov ) / 2.0 ) * _z_near;
62  double bottom = -top;
63  double right = tan( dtor( _horiz_fov ) / 2.0 ) * _z_near;
64  double left = -right;
65 
66  right *= _aspect;
67  left *= _aspect;
68 
69  glFrustum( left, right, bottom, top, _z_near, _z_far );
70 
71  glMatrixMode (GL_MODELVIEW);
72 
73 }
74 
76 {
77 }
78 
79 
80 void PerspectiveCamera::strafe( double amount )
81 {
82  _x += cos( dtor( _yaw ) ) * amount;
83  _y += sin( dtor( _yaw ) ) * amount;
84 }
85 
86 void PerspectiveCamera::forward( double amount )
87 {
88  _x += -sin( dtor( _yaw ) ) * amount;
89  _y += cos( dtor( _yaw ) ) * amount;
90 }
91 
92 void PerspectiveCamera::Load( Worldfile* wf, int sec )
93 {
94  wf->ReadTuple( sec, "pcam_loc", 0, 3, "lll", &_x, &_y, &_z );
95  wf->ReadTuple( sec, "pcam_angle", 0, 2, "aa", &_pitch, &_yaw );
96 }
97 
98 void PerspectiveCamera::Save( Worldfile* wf, int sec )
99 {
100  wf->WriteTuple( sec, "pcam_loc", 0, 3, "lll", _x, _y, _z );
101  wf->WriteTuple( sec, "pcam_angle", 0, 2, "aa", _pitch, _yaw );
102 }
103 
104 
105 
106 
108 //Ortho camera
110 void OrthoCamera::Draw( void ) const
111 {
112  glMatrixMode (GL_MODELVIEW);
113  glLoadIdentity ();
114 
115  glRotatef( - _pitch, 1.0, 0.0, 0.0 );
116  glRotatef( - _yaw, 0.0, 0.0, 1.0 );
117 
118  glTranslatef( - _x, - _y, 0.0 );
119  //zooming needs to happen in the Projection code (don't use glScale for zoom)
120 
121 }
122 
123 void OrthoCamera::SetProjection( void ) const
124 {
125  glMatrixMode (GL_PROJECTION);
126  glLoadIdentity ();
127 
128  glOrtho( -_pixels_width/2.0 / _scale, _pixels_width/2.0 / _scale,
129  -_pixels_height/2.0 / _scale, _pixels_height/2.0 / _scale,
130  _y_min * _scale * 2, _y_max * _scale * 2 );
131 
132  glMatrixMode (GL_MODELVIEW);
133 }
134 
135 void OrthoCamera::SetProjection( double pixels_width, double pixels_height, double y_min, double y_max )
136 {
137  _pixels_width = pixels_width;
138  _pixels_height = pixels_height;
139  _y_min = y_min;
140  _y_max = y_max;
141  SetProjection();
142 }
143 
144 void OrthoCamera::move( double x, double y ) {
145  //convert screen points into world points
146  x = x / ( _scale );
147  y = y / ( _scale );
148 
149  //adjust for pitch angle
150  y = y / cos( dtor( _pitch ) );
151 
152  //don't allow huge values
153  if( y > 100 )
154  y = 100;
155  else if( y < -100 )
156  y = -100;
157 
158  //adjust for yaw angle
159  double yaw = -dtor( _yaw );
160  _x += cos( yaw ) * x;
161  _y += -sin( yaw ) * x;
162 
163  _x += sin( yaw ) * y;
164  _y += cos( yaw ) * y;
165 }
166 
167 //TODO re-evaluate the way the camera is shifted when the mouse zooms - it might be possible to simplify
168 void OrthoCamera::scale( double scale, double shift_x, double w, double shift_y, double h )
169 {
170  double to_scale = -scale;
171  const double old_scale = _scale;
172 
173  //TODO setting up the factor can use some work
174  double factor = 1.0 + fabs( to_scale ) / 25;
175  if( factor < 1.1 )
176  factor = 1.1; //this must be greater than 1.
177  else if( factor > 2.5 )
178  factor = 2.5;
179 
180  //convert the shift distance to the range [-0.5, 0.5]
181  shift_x = shift_x / w - 0.5;
182  shift_y = shift_y / h - 0.5;
183 
184  //adjust the shift values based on the factor (this represents how much the positions grows/shrinks)
185  shift_x *= factor - 1.0;
186  shift_y *= factor - 1.0;
187 
188  if( to_scale > 0 ) {
189  //zoom in
190  _scale *= factor;
191  move( shift_x * w, - shift_y * h );
192  }
193  else {
194  //zoom out
195  _scale /= factor;
196  if( _scale < 1 ) {
197  _scale = 1;
198  } else {
199  //shift camera to follow where mouse zoomed out
200  move( - shift_x * w / old_scale * _scale,
201  shift_y * h / old_scale * _scale );
202  }
203  }
204 }
205 
206 void OrthoCamera::Load( Worldfile* wf, int sec )
207 {
208  wf->ReadTuple( sec, "center", 0, 2, "ff", &_x, &_y );
209  wf->ReadTuple( sec, "rotate", 0, 2, "ff", &_pitch, &_yaw );
210  setScale( wf->ReadFloat(sec, "scale", scale() ) );
211 }
212 
213 void OrthoCamera::Save( Worldfile* wf, int sec )
214 {
215  wf->WriteTuple( sec, "center", 0, 2, "ff", _x, _y );
216  wf->WriteTuple( sec, "rotate", 0, 2, "ff", _pitch, _yaw );
217 
218  // wf->WriteTupleFloat( sec, "center", 0, x() );
219  // wf->WriteTupleFloat( sec, "center", 1, y() );
220  // wf->WriteTupleFloat( sec, "rotate", 0, pitch() );
221  // wf->WriteTupleFloat( sec, "rotate", 1, yaw() );
222 
223  wf->WriteFloat(sec, "scale", scale() );
224 }
225 
float scale
Definition: make_rsn.c:26
void Save(Worldfile *wf, int sec)
Definition: camera.cc:98
void update(void)
Definition: camera.cc:75
double dtor(double d)
Definition: stage.hh:151
The Stage library uses its own namespace.
Definition: canvas.hh:8
virtual void SetProjection(void) const
Definition: camera.cc:123
double _yaw
Definition: stage.hh:1368
double scale() const
Definition: stage.hh:1488
double _z
Definition: stage.hh:1369
double _x
Definition: stage.hh:1369
void Load(Worldfile *wf, int sec)
Definition: camera.cc:92
void forward(double amount)
Definition: camera.cc:86
int ReadTuple(const int entity, const char *name, const unsigned int first, const unsigned int num, const char *format,...)
Definition: worldfile.cc:1506
double y(void) const
Definition: stage.hh:1382
double yaw(void) const
Definition: stage.hh:1378
double _pitch
Definition: stage.hh:1367
void Save(Worldfile *wf, int sec)
Definition: camera.cc:213
void WriteTuple(const int entity, const char *name, const unsigned int first, const unsigned int count, const char *format,...)
Definition: worldfile.cc:1573
double z(void) const
Definition: stage.hh:1383
virtual void Draw() const
Definition: camera.cc:110
void WriteFloat(int entity, const char *name, double value)
Definition: worldfile.cc:1418
double _y
Definition: stage.hh:1369
double x(void) const
Definition: stage.hh:1381
virtual void SetProjection(void) const
Definition: camera.cc:54
void strafe(double amount)
Definition: camera.cc:80
double ReadFloat(int entity, const char *name, double value)
Definition: worldfile.cc:1434
void Load(Worldfile *wf, int sec)
Definition: camera.cc:206
virtual void Draw(void) const
Definition: camera.cc:42
void setPitch(double pitch)
Definition: stage.hh:1422
void move(double x, double y, double z)
Definition: camera.cc:26
void move(double x, double y)
Definition: camera.cc:144


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09