4 * Some useful matrix functions.
18 * Pretty-print the given matrix.
21 PrintMatrix(const float p
[16])
23 printf("[ %6.3f %6.3f %6.3f %6.3f ]\n", p
[0], p
[4], p
[8], p
[12]);
24 printf("[ %6.3f %6.3f %6.3f %6.3f ]\n", p
[1], p
[5], p
[9], p
[13]);
25 printf("[ %6.3f %6.3f %6.3f %6.3f ]\n", p
[2], p
[6], p
[10], p
[14]);
26 printf("[ %6.3f %6.3f %6.3f %6.3f ]\n", p
[3], p
[7], p
[11], p
[15]);
31 * Build a glFrustum matrix.
34 Frustum(float left
, float right
, float bottom
, float top
, float nearZ
, float farZ
, float *m
)
36 float x
= (2.0F
*nearZ
) / (right
-left
);
37 float y
= (2.0F
*nearZ
) / (top
-bottom
);
38 float a
= (right
+left
) / (right
-left
);
39 float b
= (top
+bottom
) / (top
-bottom
);
40 float c
= -(farZ
+nearZ
) / ( farZ
-nearZ
);
41 float d
= -(2.0F
*farZ
*nearZ
) / (farZ
-nearZ
);
43 #define M(row,col) m[col*4+row]
44 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
45 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
46 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
47 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
53 * Build a glOrtho marix.
56 Ortho(float left
, float right
, float bottom
, float top
, float nearZ
, float farZ
, float *m
)
58 #define M(row,col) m[col*4+row]
59 M(0,0) = 2.0F
/ (right
-left
);
62 M(0,3) = -(right
+left
) / (right
-left
);
65 M(1,1) = 2.0F
/ (top
-bottom
);
67 M(1,3) = -(top
+bottom
) / (top
-bottom
);
71 M(2,2) = -2.0F
/ (farZ
-nearZ
);
72 M(2,3) = -(farZ
+nearZ
) / (farZ
-nearZ
);
83 * Decompose a projection matrix to determine original glFrustum or
87 DecomposeProjection( const float *m
,
89 float *leftOut
, float *rightOut
,
90 float *botOut
, float *topOut
,
91 float *nearOut
, float *farOut
)
96 const float x
= m
[0]; /* 2N / (R-L) */
97 const float y
= m
[5]; /* 2N / (T-B) */
98 const float a
= m
[8]; /* (R+L) / (R-L) */
99 const float b
= m
[9]; /* (T+B) / (T-B) */
100 const float c
= m
[10]; /* -(F+N) / (F-N) */
101 const float d
= m
[14]; /* -2FN / (F-N) */
103 /* These equations found with simple algebra, knowing the arithmetic
104 * use to set up a typical perspective projection matrix in OpenGL.
106 const float nearZ
= -d
/ (1.0 - c
);
107 const float farZ
= (c
- 1.0) * nearZ
/ (c
+ 1.0);
108 const float left
= nearZ
* (a
- 1.0) / x
;
109 const float right
= 2.0 * nearZ
/ x
+ left
;
110 const float bottom
= nearZ
* (b
- 1.0) / y
;
111 const float top
= 2.0 * nearZ
/ y
+ bottom
;
123 const float x
= m
[0]; /* 2 / (R-L) */
124 const float y
= m
[5]; /* 2 / (T-B) */
125 const float z
= m
[10]; /* -2 / (F-N) */
126 const float a
= m
[12]; /* -(R+L) / (R-L) */
127 const float b
= m
[13]; /* -(T+B) / (T-B) */
128 const float c
= m
[14]; /* -(F+N) / (F-N) */
129 /* again, simple algebra */
130 const float right
= -(a
- 1.0) / x
;
131 const float left
= right
- 2.0 / x
;
132 const float top
= -(b
- 1.0) / y
;
133 const float bottom
= top
- 2.0 / y
;
134 const float farZ
= (c
- 1.0) / z
;
135 const float nearZ
= farZ
+ 2.0 / z
;
151 main(int argc
, char *argv
[])
154 float l
, r
, b
, t
, n
, f
;
165 printf(" Frustum(%f, %f, %f, %f, %f, %f\n",l
+1, r
+1.2, b
+.5, t
+.3, n
, f
);
166 Frustum(l
+1, r
+1.2, b
+.5, t
+.3, n
, f
, p
);
167 DecomposeProjection(p
, &persp
, &l
, &r
, &b
, &t
, &n
, &f
);
168 printf("glFrustum(%f, %f, %f, %f, %f, %f)\n",
172 printf("Ortho(-1, 1, -1, 1, 10, 84)\n");
173 Ortho(-1, 1, -1, 1, 10, 84, m
);
175 DecomposeProjection(m
, &persp
, &l
, &r
, &b
, &t
, &n
, &f
);
176 printf("Ortho(%f, %f, %f, %f, %f, %f) %d\n", l
, r
, b
, t
, n
, f
, persp
);