1 // Matrix Stack Implementation
7 #if defined(__ANDROID__) || defined(__ODROID__)
9 #elif defined(__EMSCRIPTEN__)
10 #include <GLES2/gl2.h>
13 # include "gl_core_3_3.h"
15 # include "gl_compat_4_4.h"
22 #if defined(ECERE_NO3D) || defined(ECERE_VANILLA)
30 FillBytesBy4(this, 0, sizeof(Matrix) >> 2);
31 m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
34 void Transpose(Matrix source)
39 m[j][i] = source.m[i][j];
42 void Multiply(Matrix a, Matrix b)
44 // We need a full matrix multiplication for the Projection matrix
45 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];
46 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];
47 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];
48 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];
50 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];
51 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];
52 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];
53 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];
55 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];
56 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];
57 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];
58 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];
60 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];
61 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];
62 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];
63 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];
68 public enum MatrixMode { modelView = 0x1700, projection = 0x1701, texture = 0x1702 };
72 public double glmsGetNearPlane()
77 public void glmsSetNearPlane(double value)
82 /*static */Matrix matrixStack[3][32];
83 /*static */int matrixIndex[3];
84 /*static */int curStack = 0;
86 static void LoadCurMatrix()
88 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
91 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
92 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
93 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
94 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
98 shader_LoadMatrixf((MatrixMode) (0x1700 + curStack), m);
104 public void glmsLoadMatrix(Matrix matrix)
108 (float)matrix.m[0][0], (float)matrix.m[0][1], (float)matrix.m[0][2], (float)matrix.m[0][3],
109 (float)matrix.m[1][0], (float)matrix.m[1][1], (float)matrix.m[1][2], (float)matrix.m[1][3],
110 (float)matrix.m[2][0], (float)matrix.m[2][1], (float)matrix.m[2][2], (float)matrix.m[2][3],
111 (float)matrix.m[3][0], (float)matrix.m[3][1], (float)matrix.m[3][2], (float)matrix.m[3][3]
114 shader_LoadMatrixf((MatrixMode) (0x1700 + curStack), m);
120 public void glmsLoadIdentity()
122 matrixStack[curStack][matrixIndex[curStack]].Identity();
126 public void glmsPushMatrix()
128 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
130 matrixIndex[curStack]++;
131 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
135 public void glmsPopMatrix()
137 if(matrixIndex[curStack] > 0)
139 matrixIndex[curStack]--;
144 public void glmsLoadMatrixd(double * i)
146 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
150 public void glmsOrtho( double l, double r, double b, double t, double n, double f )
154 (2 / (r - l)), 0, 0, 0,
155 0, (2 / (t - b)), 0, 0,
156 0, 0, (-2 / (f - n)), 0,
157 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
160 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
161 matrixStack[curStack][matrixIndex[curStack]] = res;
165 public void glmsFrustum( double l, double r, double b, double t, double n, double f )
175 double A = ((r + l) / (r - l));
176 double B = ((t + b) / (t - b));
177 double C = (-(f + n) / (f - n));
178 double D = (-2*f*n/(f-n));
181 (2.0*n / (r - l)), 0, 0, 0,
182 0, (2.0*n / (t - b)), 0, 0,
187 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
188 matrixStack[curStack][matrixIndex[curStack]] = res;
193 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
194 public void glmsRotated( double a, double b, double c, double d )
199 q.RotationAxis({(float)b,(float)c,(float)-d}, a );
200 m.RotationQuaternion(q);
201 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
202 matrixStack[curStack][matrixIndex[curStack]] = r;
205 public void glmsScaled( double a, double b, double c )
211 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
212 matrixStack[curStack][matrixIndex[curStack]] = r;
216 public void glmsTranslated( double a, double b, double c )
222 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
223 matrixStack[curStack][matrixIndex[curStack]] = r;
227 public void glmsMultMatrixd( double * i )
230 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
231 matrixStack[curStack][matrixIndex[curStack]] = r;
236 public void glmsMatrixMode(MatrixMode mode)
238 curStack = (mode == modelView) ? 0 : (mode == projection) ? 1 : 2;