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


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 Feb 28 2022 23:48:55