59 #define ACG_GLMATRIX_C 65 #include "GLMatrixT.hh" 78 template<
typename Scalar>
81 scale( Scalar _x, Scalar _y, Scalar _z,
91 if (_mult_from == MULT_FROM_RIGHT) *
this *= m;
99 template<
typename Scalar>
112 if (_mult_from == MULT_FROM_RIGHT) *
this *= m;
120 template<
typename Scalar>
129 Scalar ca = cos(_angle * (M_PI/180.0));
130 Scalar sa = sin(_angle * (M_PI/180.0));
135 m(1,1) = m(2,2) = ca; m(2,1) = sa; m(1,2) = -sa;
139 m(0,0) = m(2,2) = ca; m(0,2) = sa; m(2,0) = -sa;
143 m(0,0) = m(1,1) = ca; m(1,0) = sa; m(0,1) = -sa;
148 if (_mult_from == MULT_FROM_RIGHT) *
this *= m;
158 template <
typename Scalar>
161 rotate( Scalar angle, Scalar x, Scalar y, Scalar z,
166 Scalar xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
168 mag = sqrt( x*x + y*y + z*z );
173 s = sin( angle * ( Scalar(M_PI) / Scalar(180.) ) );
174 c = cos( angle * ( Scalar(M_PI) / Scalar(180.) ) );
191 m(0,0) = (one_c * xx) + c;
192 m(0,1) = (one_c * xy) - zs;
193 m(0,2) = (one_c * zx) + ys;
196 m(1,0) = (one_c * xy) + zs;
197 m(1,1) = (one_c * yy) + c;
198 m(1,2) = (one_c * yz) - xs;
201 m(2,0) = (one_c * zx) - ys;
202 m(2,1) = (one_c * yz) + xs;
203 m(2,2) = (one_c * zz) + c;
211 if (_mult_from == MULT_FROM_RIGHT) *
this *= m;
219 template<
typename Scalar>
237 m(0,0)=x[0]; m(0,1)=x[1]; m(0,2)=x[2]; m(0,3)=0.0;
238 m(1,0)=y[0]; m(1,1)=y[1]; m(1,2)=y[2]; m(1,3)=0.0;
239 m(2,0)=z[0]; m(2,1)=z[1]; m(2,2)=z[2]; m(2,3)=0.0;
240 m(3,0)=0.0; m(3,1)=0.0; m(3,2)=0.0; m(3,3)=1.0;
243 translate(-eye[0], -eye[1], -eye[2]);
250 template<
typename Scalar>
268 m(0,0)=x[0]; m(0,1)=y[0]; m(0,2)=z[0]; m(0,3)=eye[0];
269 m(1,0)=x[1]; m(1,1)=y[1]; m(1,2)=z[1]; m(1,3)=eye[1];
270 m(2,0)=x[2]; m(2,1)=y[2]; m(2,2)=z[2]; m(2,3)=eye[2];
271 m(3,0)=0.0; m(3,1)=0.0; m(3,2)=0.0; m(3,3)=1.0;
280 template<
typename Scalar>
284 Scalar near_plane, Scalar far_plane)
286 Scalar ymax = near_plane * tan( fovY * M_PI / 360.0 );
288 Scalar xmin = ymin * aspect;
289 Scalar xmax = ymax * aspect;
290 frustum(xmin, xmax, ymin, ymax, near_plane, far_plane);
297 template<
typename Scalar>
301 Scalar near_plane, Scalar far_plane)
303 Scalar ymax = near_plane * tan( fovY * M_PI / 360.0 );
305 Scalar xmin = ymin * aspect;
306 Scalar xmax = ymax * aspect;
307 inverse_frustum(xmin, xmax, ymin, ymax, near_plane, far_plane);
314 template<
typename Scalar>
318 Scalar bottom, Scalar top,
319 Scalar near_plane, Scalar far_plane )
321 assert(near_plane > 0.0 && far_plane > near_plane);
324 Scalar x, y, a, b, c, d;
326 x = (2.0*near_plane) / (right-left);
327 y = (2.0*near_plane) / (top-bottom);
328 a = (right+left) / (right-left);
329 b = (top+bottom) / (top-bottom);
330 c = -(far_plane+near_plane) / ( far_plane-near_plane);
331 d = -(2.0*far_plane*near_plane) / (far_plane-near_plane);
333 m(0,0) = x; m(0,1) = 0.0F; m(0,2) = a; m(0,3) = 0.0F;
334 m(1,0) = 0.0F; m(1,1) = y; m(1,2) = b; m(1,3) = 0.0F;
335 m(2,0) = 0.0F; m(2,1) = 0.0F; m(2,2) = c; m(2,3) = d;
336 m(3,0) = 0.0F; m(3,1) = 0.0F; m(3,2) = -1.0F; m(3,3) = 0.0F;
345 template<
typename Scalar>
349 Scalar bottom, Scalar top,
350 Scalar near_plane, Scalar far_plane)
352 assert(near_plane > 0.0 && far_plane > near_plane);
355 Scalar x, y, a, b, c, d;
357 x = (right-left) / (2.0*near_plane);
358 y = (top-bottom) / (2.0*near_plane);
359 a = (right+left) / (2.0*near_plane);
360 b = (top+bottom) / (2.0*near_plane);
361 c = (far_plane+near_plane) / (2.0*far_plane*near_plane);
362 d = (near_plane-far_plane) / (2.0*far_plane*near_plane);
364 m(0,0)=x; m(0,1)= 0.0F; m(0,2)= 0.0F; m(0,3)= a;
365 m(1,0)=0.0F; m(1,1)= y; m(1,2)= 0.0F; m(1,3)= b;
366 m(2,0)=0.0F; m(2,1)= 0.0F; m(2,2)= 0.0F; m(2,3)= -1.0;
367 m(3,0)=0.0F; m(3,1)= 0.0F; m(3,2)= d; m(3,3)= c;
376 template<
typename Scalar>
380 Scalar bottom, Scalar top,
381 Scalar near_plane, Scalar far_plane)
385 Scalar r_l = right-left;
386 Scalar t_b = top - bottom;
387 Scalar f_n = far_plane - near_plane;
391 m(1,0) = m(0,1) = m(0,2) =
392 m(2,0) = m(2,1) = m(1,2) =
393 m(3,0) = m(3,1) = m(3,2) = 0.0;
399 m(0,3) = -(right + left) / r_l;
400 m(1,3) = -(top + bottom) / t_b;
401 m(2,3) = -(far_plane + near_plane) / f_n;
411 template<
typename Scalar>
415 Scalar bottom, Scalar top,
416 Scalar near_plane, Scalar far_plane)
420 m(1,0) = m(0,1) = m(0,2) =
421 m(2,0) = m(2,1) = m(1,2) =
422 m(3,0) = m(3,1) = m(3,2) = 0.0;
425 m(0,0) = 0.5 * (right - left);
426 m(1,1) = 0.5 * (top - bottom);
427 m(2,2) = -0.5 * (far_plane - near_plane);
429 m(0,3) = 0.5 * (right + left);
430 m(1,3) = 0.5 * (top + bottom);
431 m(2,3) = -0.5 * (far_plane + near_plane);
441 template<
typename Scalar>
446 Scalar a = (*this)(2,2);
447 Scalar b = (*this)(2,3);
454 template<
typename Scalar>
459 Scalar a = (*this)(2,2);
460 Scalar b = (*this)(2,3);
467 template<
typename Scalar>
504 for (
int i = 0; i < nnz; ++i)
506 if ( checkEpsilon( (*
this)(nze[i*2],nze[i*2+1]) ) )
511 if ( !checkEpsilon( (*
this)(3,2) + 1.0 ) )
515 for (
int i = 0; i < nz; ++i)
517 if ( !checkEpsilon( (*
this)(ze[i*2],ze[i*2+1]) ) )
526 template<
typename Scalar>
535 for (
int i = 0; i < 3; ++i)
537 if ( checkEpsilon( (*
this)(i,i) ) )
542 if ( !checkEpsilon( (*
this)(3,3) - 1.0 ) )
546 if ( checkEpsilon( (*
this)(2,3) ) )
568 for (
int i = 0; i < nz; ++i)
570 if ( !checkEpsilon( (*
this)(ze[i*2],ze[i*2+1]) ) )
579 template<
typename Scalar>
585 return extract_planes_perspective();
588 return extract_planes_ortho();
void inverse_lookAt(const Vec3 &eye, const Vec3 ¢er, const Vec3 &up)
multiply self from left with inverse lookAt matrix
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
void inverse_frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
VectorT< Scalar, 2 > extract_planes() const
detect type of projection matrix and extract near and far clipping planes
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
void identity()
setup an identity matrix
void inverse_ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse orthographic projection matrix
VectorT< Scalar, 2 > extract_planes_perspective() const
extract near and far clipping planes from a perspective projection matrix
4x4 matrix implementing OpenGL commands.
void perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
VectorT< Scalar, 2 > extract_planes_ortho() const
extract near and far clipping planes from an orthographic projection matrix
Namespace providing different geometric functions concerning angles.
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
void frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
bool isOrtho() const
check if the matrix is an orthographic projection matrix
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
void inverse_perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
void lookAt(const Vec3 &eye, const Vec3 ¢er, const Vec3 &up)
bool isPerspective() const
check if the matrix is a perspective projection matrix
void ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with orthographic projection matrix