glVertex  5.5.2
glvertex_cam.h
Go to the documentation of this file.
1 // (c) by Stefan Roettger, licensed under MIT license
2 
5 #ifndef GLVERTEX_CAM_H
6 #define GLVERTEX_CAM_H
7 
8 #include "glvertex.h"
9 
11 class lgl_Cam
12 {
13 public:
14 
17  : perspective_(false),
18  left_(-1), right_(1), bottom_(-1), top_(1), front_(-1), back_(1),
19  fovy_(0), aspect_(0), nearp_(0), farp_(0),
20  eye_(vec3(0,0,0)), lookat_(vec3(0,0,-1)), up_(vec3(0,1,0)), alt_up_(vec3(0,0,-1)),
21  base_(0), lefteye_(true),
22  halted_(false)
23  {}
24 
25  ~lgl_Cam()
26  {}
27 
29  void setOrthographic(double left, double right, double bottom, double top, double front, double back)
30  {
31  perspective_ = false;
32 
33  left_ = left;
34  right_ = right;
35  bottom_ = bottom;
36  top_ = top;
37  front_ = front;
38  back_ = back;
39  }
40 
42  void setPerspective(double fovy, double aspect, double nearp, double farp)
43  {
44  perspective_ = true;
45 
46  fovy_ = fovy;
47  aspect_ = aspect;
48  nearp_ = nearp;
49  farp_ = farp;
50  }
51 
53  void setPerspective(double fovy, double nearp, double farp)
54  {
55  setPerspective(fovy, 0, nearp, farp);
56  }
57 
59  void setAspect(double aspect)
60  {
61  aspect_ = aspect;
62  }
63 
65  void setEye(vec3 eye, vec3 lookat, vec3 up)
66  {
67  if (halted_) return;
68 
69  up_ = up.normalize();
70  setEye(eye, lookat);
71  }
72 
74  void setEye(vec3 eye, vec3 lookat)
75  {
76  if (halted_) return;
77 
78  eye_ = eye;
79  lookat_ = lookat;
80 
81  vec3 dir = getDirection();
82  double lambda = dir.dot(up_);
83 
84  if (fabs(lambda) < 1-0.001)
85  alt_up_ = (up_-lambda*dir).normalize();
86  }
87 
89  vec3 getEye(bool left = true) const
90  {
91  vec3 right = getRight();
92  if (left) right = -right;
93  if (!lefteye_) right = -right;
94 
95  return(eye_+0.5*base_*right);
96  }
97 
99  vec3 getEyeManip(bool left = true) const
100  {
101  vec3 right = getRight();
102  if (left) right = -right;
103  if (!lefteye_) right = -right;
104 
105  vec4 eye = eye_+0.5*base_*right;
106 
107  mat4 M = lglGetManip();
108  mat4 V = getViewMatrix();
109 
110  eye = (M*V).invert() * V * eye;
111 
112  return(eye);
113  }
114 
116  vec3 getLookAt() const
117  {
118  return(lookat_);
119  }
120 
123  {
124  return((lookat_-eye_).normalize());
125  }
126 
128  vec3 getRight() const
129  {
130  return(getDirection().cross(getUp()).normalize());
131  }
132 
134  vec3 getUp() const
135  {
136  if (fabs(getDirection().dot(up_)) < 1-0.001)
137  return(up_);
138  else
139  return(alt_up_);
140  }
141 
143  vec3 unprojectViewport(double x, double y)
144  {
145  if (perspective_)
146  {
147  double mx = x-0.5;
148  double my = 0.5-y;
149 
150  double wy = tan(fovy_*PI/360);
151  double wx = aspect_*wy;
152 
153  vec3 dir = vec3(2.0*wx*mx, 2.0*wy*my, -1);
154 
155  return(dir.normalize());
156  }
157 
158  return(vec3(0,0,-1));
159  }
160 
162  vec3 unprojectViewportToWorld(double x, double y)
163  {
164  if (perspective_)
165  {
166  double mx = x-0.5;
167  double my = 0.5-y;
168 
169  double wy = tan(fovy_*PI/360);
170  double wx = aspect_*wy;
171 
172  vec3 dir = getDirection()+
173  getRight()*2.0*wx*mx+
174  getUp()*2.0*wy*my;
175 
176  return(dir.normalize());
177  }
178 
179  return(getDirection());
180  }
181 
183  bool isVisible(const vec3 &center, double radius)
184  {
185  return(glslmath::vtest_plane_sphere(getEye(), getDirection(), center, radius));
186  }
187 
189  bool isVisible(const vec3 &center, const vec3 &extent)
190  {
191  return(glslmath::vtest_plane_bbox(getEye(), getDirection(), center, extent));
192  }
193 
195  void setStereoBase(double base)
196  {
197  base_ = base;
198  }
199 
201  double getStereoBase() const
202  {
203  return(base_);
204  }
205 
207  bool isStereoCam() const
208  {
209  return(base_ != 0);
210  }
211 
213  void useLeftEye(bool left = true)
214  {
215  lefteye_ = left;
216  }
217 
219  void useRightEye(bool right = true)
220  {
221  lefteye_ = !right;
222  }
223 
225  bool isLeftEye() const
226  {
227  return(lefteye_);
228  }
229 
231  bool isRightEye() const
232  {
233  return(!lefteye_);
234  }
235 
238  {
239  if (perspective_)
240  return(mat4::perspective(fovy_, aspect_, nearp_, farp_));
241  else
242  return(mat4::ortho(left_, right_, bottom_, top_, front_, back_));
243  }
244 
246  mat4 getViewMatrix(bool left = true) const
247  {
248  return(mat4::lookat(getEye(left), getLookAt(), getUp()));
249  }
250 
252  mat4 getInverseViewMatrix(bool left = true) const
253  {
254  return(getViewMatrix(left).invert());
255  }
256 
258  mat4 getInverseTransposeViewMatrix(bool left = true) const
259  {
260  return(getViewMatrix(left).invert().transpose());
261  }
262 
264  mat4 getViewProjectionMatrix(bool left = true) const
265  {
267  mat4 V = getViewMatrix(left);
268 
269  return(P * V);
270  }
271 
273  void begin(bool left = true) const
274  {
275  lglMatrixMode(LGL_PROJECTION);
276  lglPushMatrix();
277  lglLoadIdentity();
278  if (perspective_)
279  lglPerspective(fovy_, aspect_, nearp_, farp_);
280  else
281  lglOrtho(left_, right_, bottom_, top_, front_, back_);
282  lglMatrixMode(LGL_MODELVIEW);
283  lglPushMatrix();
284  lglLookAt(getEye(left), getLookAt(), getUp());
285  }
286 
288  void end() const
289  {
290  lglMatrixMode(LGL_PROJECTION);
291  lglPopMatrix();
292  lglMatrixMode(LGL_MODELVIEW);
293  lglPopMatrix();
294  }
295 
297  void halt(bool yes = true)
298  {
299  halted_ = yes;
300  }
301 
303  void resume(bool yes = true)
304  {
305  halted_ = !yes;
306  }
307 
309  void reset()
310  {
311  if (!halted_)
312  lglResetManip();
313  }
314 
315 protected:
316 
317  bool perspective_;
318  double left_, right_, bottom_, top_, front_, back_;
319  double fovy_, aspect_, nearp_, farp_;
320  vec3 eye_, lookat_, up_, alt_up_;
321  double base_;
322  bool lefteye_;
323  bool halted_;
324 };
325 
326 #endif
lgl_Cam::getEye
vec3 getEye(bool left=true) const
get eye point
Definition: glvertex_cam.h:89
lgl_Cam::setAspect
void setAspect(double aspect)
set camera aspect
Definition: glvertex_cam.h:59
lgl_Cam::getRight
vec3 getRight() const
get right vector
Definition: glvertex_cam.h:128
lgl_Cam::isLeftEye
bool isLeftEye() const
is left eye?
Definition: glvertex_cam.h:225
lgl_Cam::lgl_Cam
lgl_Cam()
ctor
Definition: glvertex_cam.h:16
lgl_Cam::reset
void reset()
reset cam manipulator
Definition: glvertex_cam.h:309
lgl_Cam::end
void end() const
pop viewing and projection matrix
Definition: glvertex_cam.h:288
lglResetManip
void lglResetManip()
reset the manipulator matrix
Definition: glvertex_api.h:320
lgl_Cam::halt
void halt(bool yes=true)
halt cam
Definition: glvertex_cam.h:297
lglOrtho
void lglOrtho(double left, double right, double bottom, double top, double nearp=-1, double farp=1)
multiply with orthgraphic matrix (as defined by OpenGL 1.2)
Definition: glvertex_api.h:110
lglGetManip
mat4 lglGetManip()
get the manipulator matrix
Definition: glvertex_api.h:324
mat4::perspective
static mat4 perspective(double fovy, double aspect, double znear, double zfar)
create perspective matrix
Definition: glslmath.h:2712
lgl_Cam::unprojectViewport
vec3 unprojectViewport(double x, double y)
unproject viewport coordinate into view coordinate direction
Definition: glvertex_cam.h:143
vec3::dot
double dot(const vec3 &v) const
inner product
Definition: glslmath.h:429
lgl_Cam::isRightEye
bool isRightEye() const
is right eye?
Definition: glvertex_cam.h:231
vec3::normalize
vec3 normalize() const
normalize vector to unit length
Definition: glslmath.h:528
lgl_Cam::useRightEye
void useRightEye(bool right=true)
use right eye for stereo rendering
Definition: glvertex_cam.h:219
vec4
4D double vector
Definition: glslmath.h:713
lgl_Cam::unprojectViewportToWorld
vec3 unprojectViewportToWorld(double x, double y)
unproject viewport coordinate into world coordinate direction
Definition: glvertex_cam.h:162
lgl_Cam::getProjectionMatrix
mat4 getProjectionMatrix() const
get projection matrix
Definition: glvertex_cam.h:237
glvertex.h
lglPopMatrix
void lglPopMatrix()
pop matrix (as defined by OpenGL 1.2)
Definition: glvertex_api.h:78
lglLoadIdentity
void lglLoadIdentity()
load identity matrix (as defined by OpenGL 1.2)
Definition: glvertex_api.h:62
lglLookAt
void lglLookAt(const vec3 &eye, const vec3 &at, const vec3 &up=vec3(0, 1, 0))
multiply with lookat matrix (as defined by OpenGL 1.2)
Definition: glvertex_api.h:122
lgl_Cam::setOrthographic
void setOrthographic(double left, double right, double bottom, double top, double front, double back)
set camera type to orthographic
Definition: glvertex_cam.h:29
lgl_Cam::getUp
vec3 getUp() const
get up vector
Definition: glvertex_cam.h:134
lgl_Cam::getViewMatrix
mat4 getViewMatrix(bool left=true) const
get viewing matrix
Definition: glvertex_cam.h:246
lgl_Cam::getViewProjectionMatrix
mat4 getViewProjectionMatrix(bool left=true) const
get combined viewing and projection matrix
Definition: glvertex_cam.h:264
lgl_Cam::setEye
void setEye(vec3 eye, vec3 lookat, vec3 up)
set eye and lookat point and up vector
Definition: glvertex_cam.h:65
vec3
3D double vector
Definition: glslmath.h:372
lgl_Cam::setPerspective
void setPerspective(double fovy, double aspect, double nearp, double farp)
set camera type to perspective
Definition: glvertex_cam.h:42
mat4
4x4 double matrix
Definition: glslmath.h:2116
lgl_Cam::setStereoBase
void setStereoBase(double base)
set stereo base
Definition: glvertex_cam.h:195
lgl_Cam::isVisible
bool isVisible(const vec3 &center, double radius)
bounding sphere visibility test
Definition: glvertex_cam.h:183
lgl_Cam::isStereoCam
bool isStereoCam() const
is stereo cam?
Definition: glvertex_cam.h:207
lglMatrixMode
void lglMatrixMode(lgl_matrixmode_enum mode=LGL_MODELVIEW)
specify matrix mode (as defined by OpenGL 1.2)
Definition: glvertex_api.h:58
lgl_Cam::begin
void begin(bool left=true) const
push viewing and projection matrix
Definition: glvertex_cam.h:273
lgl_Cam::getDirection
vec3 getDirection() const
get viewing direction
Definition: glvertex_cam.h:122
lgl_Cam::resume
void resume(bool yes=true)
resume cam
Definition: glvertex_cam.h:303
lgl_Cam::getInverseViewMatrix
mat4 getInverseViewMatrix(bool left=true) const
get inverse viewing matrix
Definition: glvertex_cam.h:252
lglPerspective
void lglPerspective(double fovy, double aspect, double nearp, double farp)
multiply with perspective matrix (as defined by OpenGL 1.2)
Definition: glvertex_api.h:118
dot
double dot(const vec2 &a, const vec2 &b)
inner product
Definition: glslmath.h:241
lgl_Cam::useLeftEye
void useLeftEye(bool left=true)
use left eye for stereo rendering
Definition: glvertex_cam.h:213
lgl_Cam
camera convenience class
Definition: glvertex_cam.h:11
cross
vec3 cross(const vec3 &a, const vec3 &b)
cross product
Definition: glslmath.h:544
lgl_Cam::getEyeManip
vec3 getEyeManip(bool left=true) const
get manipulated eye point
Definition: glvertex_cam.h:99
lgl_Cam::getStereoBase
double getStereoBase() const
get stereo base
Definition: glvertex_cam.h:201
normalize
vec2 normalize(const vec2 &v)
normalization to unit length
Definition: glslmath.h:237
transpose
mat4 transpose(const mat4 &m)
inline transpose
Definition: glslmath.h:2627
lgl_Cam::setPerspective
void setPerspective(double fovy, double nearp, double farp)
set camera type to perspective
Definition: glvertex_cam.h:53
lglPushMatrix
void lglPushMatrix()
push matrix (as defined by OpenGL 1.2)
Definition: glvertex_api.h:74
lgl_Cam::getLookAt
vec3 getLookAt() const
get lookat point
Definition: glvertex_cam.h:116
mat4::lookat
static mat4 lookat(const vec3 &eye, const vec3 &center, const vec3 &up=vec3(0, 1, 0))
create lookat matrix
Definition: glslmath.h:2731
lgl_Cam::isVisible
bool isVisible(const vec3 &center, const vec3 &extent)
bounding box visibility test
Definition: glvertex_cam.h:189
lgl_Cam::getInverseTransposeViewMatrix
mat4 getInverseTransposeViewMatrix(bool left=true) const
get inverse transpose viewing matrix
Definition: glvertex_cam.h:258
lgl_Cam::setEye
void setEye(vec3 eye, vec3 lookat)
set eye and lookat point
Definition: glvertex_cam.h:74
mat4::ortho
static mat4 ortho(double l, double r, double b, double t, double n=-1, double f=1)
create orthographic matrix
Definition: glslmath.h:2677