Subversion Repositories gelsvn

Rev

Rev 595 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 595 Rev 624
1
/* ----------------------------------------------------------------------- *
1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
5
 * ----------------------------------------------------------------------- */
6
 
6
 
7
#include "Mat4x4f.h"
7
#include "Mat4x4f.h"
8
 
8
 
9
namespace CGLA 
9
namespace CGLA 
10
{
10
{
11
 
11
 
12
	Mat4x4f rotation_Mat4x4f(Axis axis, float angle)
12
	Mat4x4f rotation_Mat4x4f(Axis axis, float angle)
13
	{
13
	{
14
		Mat4x4f m(0.0f);
14
		Mat4x4f m(0.0f);
15
 
15
 
16
		switch(axis)
16
		switch(axis)
17
			{
17
			{
18
			case XAXIS:
18
			case XAXIS:
19
				m[0][0] = 1.0f;
19
				m[0][0] = 1.0f;
20
				m[1][1] = cos(angle);
20
				m[1][1] = cos(angle);
21
				m[1][2] = sin(angle);
21
				m[1][2] = sin(angle);
22
				m[2][1] = -sin(angle);
22
				m[2][1] = -sin(angle);
23
				m[2][2] = cos(angle);
23
				m[2][2] = cos(angle);
24
				m[3][3] = 1.0f;
24
				m[3][3] = 1.0f;
25
				break;
25
				break;
26
			case YAXIS:
26
			case YAXIS:
27
				m[0][0] = cos(angle);
27
				m[0][0] = cos(angle);
28
				m[0][2] = -sin(angle);
28
				m[0][2] = -sin(angle);
29
				m[2][0] = sin(angle);
29
				m[2][0] = sin(angle);
30
				m[2][2] = cos(angle);
30
				m[2][2] = cos(angle);
31
				m[1][1] = 1.0f;
31
				m[1][1] = 1.0f;
32
				m[3][3] = 1.0f;
32
				m[3][3] = 1.0f;
33
				break;
33
				break;
34
			case ZAXIS:
34
			case ZAXIS:
35
				m[0][0] = cos(angle);
35
				m[0][0] = cos(angle);
36
				m[0][1] = sin(angle);
36
				m[0][1] = sin(angle);
37
				m[1][0] = -sin(angle);
37
				m[1][0] = -sin(angle);
38
				m[1][1] = cos(angle);
38
				m[1][1] = cos(angle);
39
				m[2][2] = 1.0f;
39
				m[2][2] = 1.0f;
40
				m[3][3] = 1.0f;
40
				m[3][3] = 1.0f;
41
				break;
41
				break;
42
			}
42
			}
43
		return m;
43
		return m;
44
	}
44
	}
45
 
45
 
46
	Mat4x4f translation_Mat4x4f(const Vec3f& v)
46
	Mat4x4f translation_Mat4x4f(const Vec3f& v)
47
	{
47
	{
48
		Mat4x4f m(0.0f);
48
		Mat4x4f m(0.0f);
49
 
49
 
50
		m[0][0] = 1.0f;
50
		m[0][0] = 1.0f;
51
		m[1][1] = 1.0f;
51
		m[1][1] = 1.0f;
52
		m[2][2] = 1.0f;
52
		m[2][2] = 1.0f;
53
		m[3][3] = 1.0f;
53
		m[3][3] = 1.0f;
54
  
54
  
55
		m[0][3] = v[0];
55
		m[0][3] = v[0];
56
		m[1][3] = v[1];
56
		m[1][3] = v[1];
57
		m[2][3] = v[2];
57
		m[2][3] = v[2];
58
  
58
  
59
		return m;
59
		return m;
60
	}
60
	}
61
 
61
 
62
	Mat4x4f scaling_Mat4x4f(const Vec3f& v)
62
	Mat4x4f scaling_Mat4x4f(const Vec3f& v)
63
	{
63
	{
64
		Mat4x4f m(0.0f);
64
		Mat4x4f m(0.0f);
65
 
65
 
66
		m[0][0] = v[0];
66
		m[0][0] = v[0];
67
		m[1][1] = v[1];
67
		m[1][1] = v[1];
68
		m[2][2] = v[2];
68
		m[2][2] = v[2];
69
		m[3][3] = 1.0f;
69
		m[3][3] = 1.0f;
70
   
70
   
71
		return m;
71
		return m;
72
	}
72
	}
-
 
73
    
-
 
74
    Mat4x4f perspective_Mat4x4f(float fovy, float aspect, float zNear, float zFar){
-
 
75
        assert(zNear > 0);
-
 
76
        assert(zFar > zNear);
-
 
77
        assert(fovy > 0);
-
 
78
        float top   = tan(fovy * DEGREES_TO_RADIANS/2) * zNear;
-
 
79
        float right = top * aspect;
-
 
80
        
-
 
81
        Mat4x4f c(0);
-
 
82
        c[0][0] = zNear/right;
-
 
83
        c[1][1] = zNear/top;
-
 
84
        c[2][2] = -(zFar + zNear)/(zFar - zNear);
-
 
85
        c[2][3] = -2.0*zFar*zNear/(zFar - zNear);
-
 
86
        c[3][2] = -1.0;
-
 
87
        c[3][3] = 0.0;
-
 
88
        
-
 
89
        return c;
-
 
90
    }
-
 
91
    
-
 
92
    Mat4x4f frustum_Mat4x4f(float left, float right, float bottom, float top, float nearVal, float farVal){
-
 
93
        assert(nearVal > 0);
-
 
94
        assert(farVal > nearVal);
-
 
95
        assert(right > left);
-
 
96
        assert(top > bottom);
-
 
97
        Mat4x4f c(0);
-
 
98
        c[0][0] = 2.0 * nearVal / (right - left);
-
 
99
        c[0][2] = (right + left) / (right - left);
-
 
100
        c[1][1] = 2.0 * nearVal / (top - bottom);
-
 
101
        c[1][2] = (top + bottom) / (top - bottom);
-
 
102
        c[2][2] = -(farVal + nearVal) / (farVal - nearVal);
-
 
103
        c[2][3] = -2.0 * farVal * nearVal / (farVal - nearVal);
-
 
104
        c[3][2] = -1.0;
-
 
105
        c[3][3] = 0.0;
-
 
106
        return c;
-
 
107
    }
-
 
108
    
-
 
109
    Mat4x4f ortho_Mat4x4f(float left, float right, float bottom, float top, float nearVal, float farVal) {
-
 
110
        assert(right > left);
-
 
111
        assert(top > bottom);
-
 
112
        assert(farVal > nearVal);
-
 
113
        Mat4x4f c(0);
-
 
114
        c[0][0] = 2.0/(right - left);
-
 
115
        c[1][1] = 2.0/(top - bottom);
-
 
116
        c[2][2] = 2.0/(nearVal - farVal);
-
 
117
        c[3][3] = 1.0;
-
 
118
        c[0][3] = -(right + left)/(right - left);
-
 
119
        c[1][3] = -(top + bottom)/(top - bottom);
-
 
120
        c[2][3] = -(farVal + nearVal)/(farVal - nearVal);
-
 
121
        return c;
-
 
122
    }
-
 
123
    
-
 
124
    
-
 
125
    Mat4x4f ortho2D_Mat4x4f(float left, float right, float bottom, float top){
-
 
126
        return ortho_Mat4x4f(left, right, bottom, top, -1, 1);
-
 
127
    }
-
 
128
    
-
 
129
    Mat4x4f lookAt_Mat4x4f(const Vec3f& eye, const Vec3f& at, const Vec3f& up){
-
 
130
        Vec4f n = Vec4f(normalize(eye - at),0.0);
-
 
131
        
-
 
132
        Vec4f u = Vec4f(normalize(cross(up,Vec3f(n))),0.0);
-
 
133
        Vec4f v = Vec4f(normalize(cross(Vec3f(n),Vec3f(u))),0.0);
-
 
134
        Vec4f t = Vec4f(0.0, 0.0, 0.0, 1.0);
-
 
135
        Mat4x4f c = Mat4x4f(u, v, n, t);
-
 
136
        return c * translation_Mat4x4f( -eye );
-
 
137
    }
-
 
138
 
73
}
139
}
74
 
140