Subversion Repositories gelsvn

Rev

Rev 595 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 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
 
2 bj 7
#include "Mat4x4f.h"
8
 
5 jab 9
namespace CGLA 
10
{
2 bj 11
 
12
	Mat4x4f rotation_Mat4x4f(Axis axis, float angle)
13
	{
10 jab 14
		Mat4x4f m(0.0f);
2 bj 15
 
16
		switch(axis)
17
			{
18
			case XAXIS:
501 jrf 19
				m[0][0] = 1.0f;
2 bj 20
				m[1][1] = cos(angle);
21
				m[1][2] = sin(angle);
22
				m[2][1] = -sin(angle);
23
				m[2][2] = cos(angle);
501 jrf 24
				m[3][3] = 1.0f;
2 bj 25
				break;
26
			case YAXIS:
27
				m[0][0] = cos(angle);
28
				m[0][2] = -sin(angle);
29
				m[2][0] = sin(angle);
30
				m[2][2] = cos(angle);
501 jrf 31
				m[1][1] = 1.0f;
32
				m[3][3] = 1.0f;
2 bj 33
				break;
34
			case ZAXIS:
35
				m[0][0] = cos(angle);
36
				m[0][1] = sin(angle);
37
				m[1][0] = -sin(angle);
38
				m[1][1] = cos(angle);
501 jrf 39
				m[2][2] = 1.0f;
40
				m[3][3] = 1.0f;
2 bj 41
				break;
42
			}
43
		return m;
44
	}
45
 
46
	Mat4x4f translation_Mat4x4f(const Vec3f& v)
47
	{
10 jab 48
		Mat4x4f m(0.0f);
2 bj 49
 
501 jrf 50
		m[0][0] = 1.0f;
51
		m[1][1] = 1.0f;
52
		m[2][2] = 1.0f;
53
		m[3][3] = 1.0f;
2 bj 54
 
55
		m[0][3] = v[0];
56
		m[1][3] = v[1];
57
		m[2][3] = v[2];
58
 
59
		return m;
60
	}
61
 
62
	Mat4x4f scaling_Mat4x4f(const Vec3f& v)
63
	{
10 jab 64
		Mat4x4f m(0.0f);
2 bj 65
 
66
		m[0][0] = v[0];
67
		m[1][1] = v[1];
68
		m[2][2] = v[2];
501 jrf 69
		m[3][3] = 1.0f;
2 bj 70
 
71
		return m;
72
	}
624 mono 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
 
2 bj 139
}