1 // Matrix Stack Implementation
7 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
9 # include "gl_core_3_3.h"
11 # include "gl_compat_4_4.h"
17 #if defined(ECERE_NO3D) || defined(ECERE_VANILLA)
25 FillBytesBy4(this, 0, sizeof(Matrix) >> 2);
26 m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
29 void Transpose(Matrix source)
34 m[j][i] = source.m[i][j];
37 void Multiply(Matrix a, Matrix b)
39 // We need a full matrix multiplication for the Projection matrix
40 m[0][0]=a.m[0][0]*b.m[0][0] + a.m[0][1]*b.m[1][0] + a.m[0][2]*b.m[2][0] + a.m[0][3]*b.m[3][0];
41 m[0][1]=a.m[0][0]*b.m[0][1] + a.m[0][1]*b.m[1][1] + a.m[0][2]*b.m[2][1] + a.m[0][3]*b.m[3][1];
42 m[0][2]=a.m[0][0]*b.m[0][2] + a.m[0][1]*b.m[1][2] + a.m[0][2]*b.m[2][2] + a.m[0][3]*b.m[3][2];
43 m[0][3]=a.m[0][0]*b.m[0][3] + a.m[0][1]*b.m[1][3] + a.m[0][2]*b.m[2][3] + a.m[0][3]*b.m[3][3];
45 m[1][0]=a.m[1][0]*b.m[0][0] + a.m[1][1]*b.m[1][0] + a.m[1][2]*b.m[2][0] + a.m[1][3]*b.m[3][0];
46 m[1][1]=a.m[1][0]*b.m[0][1] + a.m[1][1]*b.m[1][1] + a.m[1][2]*b.m[2][1] + a.m[1][3]*b.m[3][1];
47 m[1][2]=a.m[1][0]*b.m[0][2] + a.m[1][1]*b.m[1][2] + a.m[1][2]*b.m[2][2] + a.m[1][3]*b.m[3][2];
48 m[1][3]=a.m[1][0]*b.m[0][3] + a.m[1][1]*b.m[1][3] + a.m[1][2]*b.m[2][3] + a.m[1][3]*b.m[3][3];
50 m[2][0]=a.m[2][0]*b.m[0][0] + a.m[2][1]*b.m[1][0] + a.m[2][2]*b.m[2][0] + a.m[2][3]*b.m[3][0];
51 m[2][1]=a.m[2][0]*b.m[0][1] + a.m[2][1]*b.m[1][1] + a.m[2][2]*b.m[2][1] + a.m[2][3]*b.m[3][1];
52 m[2][2]=a.m[2][0]*b.m[0][2] + a.m[2][1]*b.m[1][2] + a.m[2][2]*b.m[2][2] + a.m[2][3]*b.m[3][2];
53 m[2][3]=a.m[2][0]*b.m[0][3] + a.m[2][1]*b.m[1][3] + a.m[2][2]*b.m[2][3] + a.m[2][3]*b.m[3][3];
55 m[3][0]=a.m[3][0]*b.m[0][0] + a.m[3][1]*b.m[1][0] + a.m[3][2]*b.m[2][0] + a.m[3][3]*b.m[3][0];
56 m[3][1]=a.m[3][0]*b.m[0][1] + a.m[3][1]*b.m[1][1] + a.m[3][2]*b.m[2][1] + a.m[3][3]*b.m[3][1];
57 m[3][2]=a.m[3][0]*b.m[0][2] + a.m[3][1]*b.m[1][2] + a.m[3][2]*b.m[2][2] + a.m[3][3]*b.m[3][2];
58 m[3][3]=a.m[3][0]*b.m[0][3] + a.m[3][1]*b.m[1][3] + a.m[3][2]*b.m[2][3] + a.m[3][3]*b.m[3][3];
63 public enum MatrixMode { modelView = 0x1700, projection = 0x1701, texture = 0x1702 };
67 public double glmsGetNearPlane()
72 public void glmsSetNearPlane(double value)
77 /*static */Matrix matrixStack[3][32];
78 /*static */int matrixIndex[3];
79 /*static */int curStack = 0;
81 static void LoadCurMatrix()
83 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
86 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
87 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
88 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
89 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
93 shader_LoadMatrixf((MatrixMode) (0x1700 + curStack), m);
99 public void glmsLoadMatrix(Matrix matrix)
103 (float)matrix.m[0][0], (float)matrix.m[0][1], (float)matrix.m[0][2], (float)matrix.m[0][3],
104 (float)matrix.m[1][0], (float)matrix.m[1][1], (float)matrix.m[1][2], (float)matrix.m[1][3],
105 (float)matrix.m[2][0], (float)matrix.m[2][1], (float)matrix.m[2][2], (float)matrix.m[2][3],
106 (float)matrix.m[3][0], (float)matrix.m[3][1], (float)matrix.m[3][2], (float)matrix.m[3][3]
109 shader_LoadMatrixf((MatrixMode) (0x1700 + curStack), m);
115 public void glmsLoadIdentity()
117 matrixStack[curStack][matrixIndex[curStack]].Identity();
121 public void glmsPushMatrix()
123 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
125 matrixIndex[curStack]++;
126 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
130 public void glmsPopMatrix()
132 if(matrixIndex[curStack] > 0)
134 matrixIndex[curStack]--;
139 public void glmsLoadMatrixd(double * i)
141 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
145 public void glmsOrtho( double l, double r, double b, double t, double n, double f )
149 (2 / (r - l)), 0, 0, 0,
150 0, (2 / (t - b)), 0, 0,
151 0, 0, (-2 / (f - n)), 0,
152 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
155 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
156 matrixStack[curStack][matrixIndex[curStack]] = res;
160 public void glmsFrustum( double l, double r, double b, double t, double n, double f )
170 double A = ((r + l) / (r - l));
171 double B = ((t + b) / (t - b));
172 double C = (-(f + n) / (f - n));
173 double D = (-2*f*n/(f-n));
176 (2.0*n / (r - l)), 0, 0, 0,
177 0, (2.0*n / (t - b)), 0, 0,
182 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
183 matrixStack[curStack][matrixIndex[curStack]] = res;
188 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
189 public void glmsRotated( double a, double b, double c, double d )
194 q.RotationAxis({(float)b,(float)c,(float)-d}, a );
195 m.RotationQuaternion(q);
196 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
197 matrixStack[curStack][matrixIndex[curStack]] = r;
200 public void glmsScaled( double a, double b, double c )
206 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
207 matrixStack[curStack][matrixIndex[curStack]] = r;
211 public void glmsTranslated( double a, double b, double c )
217 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
218 matrixStack[curStack][matrixIndex[curStack]] = r;
222 public void glmsMultMatrixd( double * i )
225 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
226 matrixStack[curStack][matrixIndex[curStack]] = r;
231 public void glmsMatrixMode(MatrixMode mode)
233 curStack = (mode == modelView) ? 0 : (mode == projection) ? 1 : 2;