TinyGL 0.4.1 for MinGW
matrix.c
Go to the documentation of this file.
1#include "zgl.h"
2
3void gl_print_matrix( const float *m)
4{
5 int i;
6
7 for (i=0;i<4;i++) {
8 fprintf(stderr,"%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
9 }
10}
11
12static inline void gl_matrix_update(GLContext *c)
13{
15}
16
17
19{
20 int mode=p[1].i;
21 switch(mode) {
22 case GL_MODELVIEW:
23 c->matrix_mode=0;
24 break;
25 case GL_PROJECTION:
26 c->matrix_mode=1;
27 break;
28 case GL_TEXTURE:
29 c->matrix_mode=2;
30 break;
31 default:
32 assert(0);
33 }
34}
35
37{
38 M4 *m;
39 int i;
40
41 GLParam *q;
42
44 q=p+1;
45
46 for(i=0;i<4;i++) {
47 m->m[0][i]=q[0].f;
48 m->m[1][i]=q[1].f;
49 m->m[2][i]=q[2].f;
50 m->m[3][i]=q[3].f;
51 q+=4;
52 }
53
54 gl_matrix_update(c);
55}
56
58{
59
61
62 gl_matrix_update(c);
63}
64
66{
67 M4 m;
68 int i;
69
70 GLParam *q;
71 q=p+1;
72
73 for(i=0;i<4;i++) {
74 m.m[0][i]=q[0].f;
75 m.m[1][i]=q[1].f;
76 m.m[2][i]=q[2].f;
77 m.m[3][i]=q[3].f;
78 q+=4;
79 }
80
82
83 gl_matrix_update(c);
84}
85
86
88{
89 int n=c->matrix_mode;
90 M4 *m;
91
92 assert( (c->matrix_stack_ptr[n] - c->matrix_stack[n] + 1 )
93 < c->matrix_stack_depth_max[n] );
94
95 m=++c->matrix_stack_ptr[n];
96
97 gl_M4_Move(&m[0],&m[-1]);
98
99 gl_matrix_update(c);
100}
101
103{
104 int n=c->matrix_mode;
105
106 assert( c->matrix_stack_ptr[n] > c->matrix_stack[n] );
107 c->matrix_stack_ptr[n]--;
108 gl_matrix_update(c);
109}
110
111
113{
114 M4 m;
115 float u[3];
116 float angle;
117 int dir_code;
118
119 angle = p[1].f * M_PI / 180.0;
120 u[0]=p[2].f;
121 u[1]=p[3].f;
122 u[2]=p[4].f;
123
124 /* simple case detection */
125 dir_code = ((u[0] != 0)<<2) | ((u[1] != 0)<<1) | (u[2] != 0);
126
127 switch(dir_code) {
128 case 0:
129 gl_M4_Id(&m);
130 break;
131 case 4:
132 if (u[0] < 0) angle=-angle;
133 gl_M4_Rotate(&m,angle,0);
134 break;
135 case 2:
136 if (u[1] < 0) angle=-angle;
137 gl_M4_Rotate(&m,angle,1);
138 break;
139 case 1:
140 if (u[2] < 0) angle=-angle;
141 gl_M4_Rotate(&m,angle,2);
142 break;
143 default:
144 {
145 float cost, sint;
146
147 /* normalize vector */
148 float len = u[0]*u[0]+u[1]*u[1]+u[2]*u[2];
149 if (len == 0.0f) return;
150 len = 1.0f / sqrt(len);
151 u[0] *= len;
152 u[1] *= len;
153 u[2] *= len;
154
155 /* store cos and sin values */
156 cost=cos(angle);
157 sint=sin(angle);
158
159 /* fill in the values */
160 m.m[3][0]=m.m[3][1]=m.m[3][2]=
161 m.m[0][3]=m.m[1][3]=m.m[2][3]=0.0f;
162 m.m[3][3]=1.0f;
163
164 /* do the math */
165 m.m[0][0]=u[0]*u[0]+cost*(1-u[0]*u[0]);
166 m.m[1][0]=u[0]*u[1]*(1-cost)-u[2]*sint;
167 m.m[2][0]=u[2]*u[0]*(1-cost)+u[1]*sint;
168 m.m[0][1]=u[0]*u[1]*(1-cost)+u[2]*sint;
169 m.m[1][1]=u[1]*u[1]+cost*(1-u[1]*u[1]);
170 m.m[2][1]=u[1]*u[2]*(1-cost)-u[0]*sint;
171 m.m[0][2]=u[2]*u[0]*(1-cost)-u[1]*sint;
172 m.m[1][2]=u[1]*u[2]*(1-cost)+u[0]*sint;
173 m.m[2][2]=u[2]*u[2]+cost*(1-u[2]*u[2]);
174 }
175 }
176
178
179 gl_matrix_update(c);
180}
181
183{
184 float *m;
185 float x=p[1].f,y=p[2].f,z=p[3].f;
186
187 m=&c->matrix_stack_ptr[c->matrix_mode]->m[0][0];
188
189 m[0] *= x; m[1] *= y; m[2] *= z;
190 m[4] *= x; m[5] *= y; m[6] *= z;
191 m[8] *= x; m[9] *= y; m[10] *= z;
192 m[12] *= x; m[13] *= y; m[14] *= z;
193 gl_matrix_update(c);
194}
195
197{
198 float *m;
199 float x=p[1].f,y=p[2].f,z=p[3].f;
200
201 m=&c->matrix_stack_ptr[c->matrix_mode]->m[0][0];
202
203 m[3] = m[0] * x + m[1] * y + m[2] * z + m[3];
204 m[7] = m[4] * x + m[5] * y + m[6] * z + m[7];
205 m[11] = m[8] * x + m[9] * y + m[10] * z + m[11];
206 m[15] = m[12] * x + m[13] * y + m[14] * z + m[15];
207
208 gl_matrix_update(c);
209}
210
211
213{
214 float *r;
215 M4 m;
216 float left=p[1].f;
217 float right=p[2].f;
218 float bottom=p[3].f;
219 float top=p[4].f;
220 float near=p[5].f;
221 float farp=p[6].f;
222 float x,y,A,B,C,D;
223
224 x = (2.0*near) / (right-left);
225 y = (2.0*near) / (top-bottom);
226 A = (right+left) / (right-left);
227 B = (top+bottom) / (top-bottom);
228 C = -(farp+near) / ( farp-near);
229 D = -(2.0*farp*near) / (farp-near);
230
231 r=&m.m[0][0];
232 r[0]= x; r[1]=0; r[2]=A; r[3]=0;
233 r[4]= 0; r[5]=y; r[6]=B; r[7]=0;
234 r[8]= 0; r[9]=0; r[10]=C; r[11]=D;
235 r[12]= 0; r[13]=0; r[14]=-1; r[15]=0;
236
238
239 gl_matrix_update(c);
240}
241
#define M_PI
Definition: gears.c:24
@ GL_PROJECTION
Definition: gl.h:95
@ GL_MODELVIEW
Definition: gl.h:94
@ GL_TEXTURE
Definition: gl.h:96
void glopScale(GLContext *c, GLParam *p)
Definition: matrix.c:182
void glopFrustum(GLContext *c, GLParam *p)
Definition: matrix.c:212
void glopMultMatrix(GLContext *c, GLParam *p)
Definition: matrix.c:65
void glopTranslate(GLContext *c, GLParam *p)
Definition: matrix.c:196
void glopRotate(GLContext *c, GLParam *p)
Definition: matrix.c:112
void glopLoadIdentity(GLContext *c, GLParam *p)
Definition: matrix.c:57
void glopMatrixMode(GLContext *c, GLParam *p)
Definition: matrix.c:18
void glopPushMatrix(GLContext *c, GLParam *p)
Definition: matrix.c:87
void gl_print_matrix(const float *m)
Definition: matrix.c:3
void glopLoadMatrix(GLContext *c, GLParam *p)
Definition: matrix.c:36
void glopPopMatrix(GLContext *c, GLParam *p)
Definition: matrix.c:102
Definition: zgl.h:159
M4 * matrix_stack[3]
Definition: zgl.h:192
int matrix_model_projection_updated
Definition: zgl.h:198
int matrix_mode
Definition: zgl.h:191
M4 * matrix_stack_ptr[3]
Definition: zgl.h:193
int matrix_stack_depth_max[3]
Definition: zgl.h:194
Definition: zmath.h:6
float m[4][4]
Definition: zmath.h:7
Definition: zgl.h:97
int i
Definition: zgl.h:100
float f
Definition: zgl.h:99
void gl_M4_Rotate(M4 *a, float t, int u)
Definition: zmath.c:206
void gl_M4_MulLeft(M4 *c, M4 *b)
Definition: zmath.c:45
void gl_M4_Move(M4 *a, M4 *b)
Definition: zmath.c:63
void gl_M4_Id(M4 *a)
Definition: zmath.c:12