Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
660 khor 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
/** @file Mat4x4f.h
8
 * @brief 4x4 float matrix class
9
 */
10
 
11
#ifndef __CGLA_MAT4X4_H__
12
#define __CGLA_MAT4X4_H__
13
 
14
#include "ExceptionStandard.h"
15
#include "CGLA.h"
16
#include "Vec3f.h"
17
#include "Vec3Hf.h"
18
#include "Vec4f.h"
19
#include "ArithSqMat4x4Float.h"
20
 
21
 
22
namespace CGLA 
23
{
24
 
25
  /** \brief 4x4 float matrix.
26
      This class is useful for transformations such as perspective projections 
27
      or translation where 3x3 matrices do not suffice. */
28
  class Mat4x4f: public ArithSqMat4x4Float<Vec4f, Mat4x4f>
29
    {
30
    public:
31
 
32
      /// Construct a Mat4x4f from four Vec4f vectors
33
      Mat4x4f(Vec4f _a, Vec4f _b, Vec4f _c, Vec4f _d): 
34
	ArithSqMat4x4Float<Vec4f, Mat4x4f> (_a,_b,_c,_d) {}
35
 
36
      /// Construct the NaN matrix
37
      Mat4x4f() {}
38
 
39
      /// Construct a matrix with identical elements.
40
      explicit Mat4x4f(float a) : ArithSqMat4x4Float<Vec4f, Mat4x4f> (a) {}
41
    };
42
 
43
  /// Create a rotation _matrix. Rotates about one of the major axes.
44
  Mat4x4f rotation_Mat4x4f(CGLA::Axis axis, float angle);
45
 
46
  /// Create a translation matrix
47
  Mat4x4f translation_Mat4x4f(const Vec3f&);
48
 
49
  /// Create a scaling matrix.
50
  Mat4x4f scaling_Mat4x4f(const Vec3f&);
51
 
52
    /// Creates a perspective projection similar to gluPerspective
53
    /// Description from gluPerspective: perspective_Mat4x4f specifies a viewing frustum into
54
    /// the world coordinate system. In general, the aspect ratio in perspective_Mat4x4f
55
    /// should match the aspect ratio of the associated viewport. For example, aspect = 2.0
56
    /// means the viewer's angle of view is twice as wide in x as it is in y. If the viewport
57
    /// is twice as wide as it is tall, it displays the image without distortion.
58
    Mat4x4f perspective_Mat4x4f(float fovy, float aspect, float zNear, float zFar);
59
 
60
    /// Creates a perspective matrix similar to glFrustum
61
    Mat4x4f frustum_Mat4x4f(float  	left,
62
                            float  	right,
63
                            float  	bottom,
64
                            float  	top,
65
                            float  	nearVal,
66
                            float  	farVal);
67
 
68
    /// Creates an orthographic projection matrix (similar to glOrtho)
69
    Mat4x4f ortho_Mat4x4f(float left,
70
                          float right,
71
                          float bottom,
72
                          float top,
73
                          float nearVal,
74
                          float farVal);
75
 
76
    /// Creates a 2D orthographic projection matrix (similar to gluOrtho2D)
77
    Mat4x4f ortho2D_Mat4x4f(float left, float right, float bottom, float top);
78
 
79
    /// Creates a view matrix similar to gluLookAt
80
    Mat4x4f lookAt_Mat4x4f(const Vec3f& eye, const Vec3f& at, const Vec3f& up);
81
 
82
  /// Create an identity matrix.
83
  inline Mat4x4f identity_Mat4x4f()
84
    {
85
      return Mat4x4f(Vec4f(1.0f,0.0f,0.0f,0.0f), 
86
		     Vec4f(0.0f,1.0f,0.0f,0.0f), 
87
		     Vec4f(0.0f,0.0f,1.0f,0.0f), 
88
		     Vec4f(0.0f,0.0f,0.0f,1.0f));
89
    }
90
 
91
  /** Compute inverse assuming that the upper-left 3x3 sub-matrix is
92
      orthonormal (which is the case if the transformation is only
93
      a concatenation of rotations and translations).
94
  */
95
  inline Mat4x4f invert_ortho(const Mat4x4f& m)
96
  {
97
    Vec3f rx(m[0][0], m[1][0], m[2][0]);
98
    Vec3f ry(m[0][1], m[1][1], m[2][1]);
99
    Vec3f rz(m[0][2], m[1][2], m[2][2]);
100
    Vec3f t(m[0][3], m[1][3], m[2][3]);
101
 
102
    return Mat4x4f(Vec4f(rx, -dot(t, rx)),
103
		   Vec4f(ry, -dot(t, ry)),
104
		   Vec4f(rz, -dot(t, rz)),
105
		   Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
106
  }
107
}
108
#endif
109
 
110
 
111
 
112
 
113
 
114
 
115