aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/Mesa_6.4.2/progs/util/matrix.c
blob: 8be2c3111f4e436e9dd04fc9b38e7985ebb1983f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * matrix.c
 *
 * Some useful matrix functions.
 *
 * Brian Paul
 * 10 Feb 2004
 */



#include <stdio.h>
#include <stdlib.h>
#include <math.h>


/**
 * Pretty-print the given matrix.
 */
void
PrintMatrix(const float p[16])
{
   printf("[ %6.3f %6.3f %6.3f %6.3f ]\n", p[0], p[4], p[8], p[12]);
   printf("[ %6.3f %6.3f %6.3f %6.3f ]\n", p[1], p[5], p[9], p[13]);
   printf("[ %6.3f %6.3f %6.3f %6.3f ]\n", p[2], p[6], p[10], p[14]);
   printf("[ %6.3f %6.3f %6.3f %6.3f ]\n", p[3], p[7], p[11], p[15]);
}


/**
 * Build a glFrustum matrix.
 */
void
Frustum(float left, float right, float bottom, float top, float nearZ, float farZ, float *m)
{
   float x = (2.0F*nearZ) / (right-left);
   float y = (2.0F*nearZ) / (top-bottom);
   float a = (right+left) / (right-left);
   float b = (top+bottom) / (top-bottom);
   float c = -(farZ+nearZ) / ( farZ-nearZ);
   float d = -(2.0F*farZ*nearZ) / (farZ-nearZ);

#define M(row,col)  m[col*4+row]
   M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = a;      M(0,3) = 0.0F;
   M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = b;      M(1,3) = 0.0F;
   M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = c;      M(2,3) = d;
   M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = -1.0F;  M(3,3) = 0.0F;
#undef M
}


/**
 * Build a glOrtho marix.
 */
void
Ortho(float left, float right, float bottom, float top, float nearZ, float farZ, float *m)
{
#define M(row,col)  m[col*4+row]
   M(0,0) = 2.0F / (right-left);
   M(0,1) = 0.0F;
   M(0,2) = 0.0F;
   M(0,3) = -(right+left) / (right-left);

   M(1,0) = 0.0F;
   M(1,1) = 2.0F / (top-bottom);
   M(1,2) = 0.0F;
   M(1,3) = -(top+bottom) / (top-bottom);

   M(2,0) = 0.0F;
   M(2,1) = 0.0F;
   M(2,2) = -2.0F / (farZ-nearZ);
   M(2,3) = -(farZ+nearZ) / (farZ-nearZ);

   M(3,0) = 0.0F;
   M(3,1) = 0.0F;
   M(3,2) = 0.0F;
   M(3,3) = 1.0F;
#undef M
}


/**
 * Decompose a projection matrix to determine original glFrustum or
 * glOrtho parameters.
 */
void
DecomposeProjection( const float *m,
                     int *isPerspective,
                     float *leftOut, float *rightOut,
                     float *botOut, float *topOut,
                     float *nearOut, float *farOut)
{
   if (m[15] == 0.0) {
      /* perspective */
      float p[16];
      const float x = m[0];  /* 2N / (R-L) */
      const float y = m[5];  /* 2N / (T-B) */
      const float a = m[8];  /* (R+L) / (R-L) */
      const float b = m[9];  /* (T+B) / (T-B) */
      const float c = m[10]; /* -(F+N) / (F-N) */
      const float d = m[14]; /* -2FN / (F-N) */

      /* These equations found with simple algebra, knowing the arithmetic
       * use to set up a typical perspective projection matrix in OpenGL.
       */
      const float nearZ = -d / (1.0 - c);
      const float farZ = (c - 1.0) * nearZ / (c + 1.0);
      const float left = nearZ * (a - 1.0) / x;
      const float right = 2.0 * nearZ / x + left;
      const float bottom = nearZ * (b - 1.0) / y;
      const float top = 2.0 * nearZ / y + bottom;

      *isPerspective = 1;
      *leftOut = left;
      *rightOut = right;
      *botOut = bottom;
      *topOut = top;
      *nearOut = nearZ;
      *farOut = farZ;
   }
   else {
      /* orthographic */
      const float x = m[0];  /*  2 / (R-L) */
      const float y = m[5];  /*  2 / (T-B) */
      const float z = m[10]; /* -2 / (F-N) */
      const float a = m[12]; /* -(R+L) / (R-L) */
      const float b = m[13]; /* -(T+B) / (T-B) */
      const float c = m[14]; /* -(F+N) / (F-N) */
      /* again, simple algebra */
      const float right  = -(a - 1.0) / x;
      const float left   = right - 2.0 / x;
      const float top    = -(b - 1.0) / y;
      const float bottom = top - 2.0 / y;
      const float farZ   = (c - 1.0) / z;
      const float nearZ  = farZ + 2.0 / z;

      *isPerspective = 0;
      *leftOut = left;
      *rightOut = right;
      *botOut = bottom;
      *topOut = top;
      *nearOut = nearZ;
      *farOut = farZ;
   }
}


#if 0
/* test harness */
int
main(int argc, char *argv[])
{
   float m[16], p[16];
   float l, r, b, t, n, f;
   int persp;
   int i;

#if 0
   l = -.9;
   r = 1.2;
   b = -0.5;
   t = 1.4;
   n = 30;
   f = 84;
   printf("  Frustum(%f, %f, %f, %f, %f, %f\n",l+1, r+1.2, b+.5, t+.3, n, f);
   Frustum(l+1, r+1.2, b+.5, t+.3, n, f, p);
   DecomposeProjection(p, &persp, &l, &r, &b, &t, &n, &f);
   printf("glFrustum(%f, %f, %f, %f, %f, %f)\n",
          l, r, b, t, n, f);
   PrintMatrix(p);
#else
   printf("Ortho(-1, 1, -1, 1, 10, 84)\n");
   Ortho(-1, 1, -1, 1, 10, 84, m);
   PrintMatrix(m);
   DecomposeProjection(m, &persp, &l, &r, &b, &t, &n, &f);
   printf("Ortho(%f, %f, %f, %f, %f, %f) %d\n", l, r, b, t, n, f, persp);
#endif

   return 0;
}
#endif