source: trunk/sys/TinyGL/src/matrix.c @ 122

Last change on this file since 122 was 1, checked in by alain, 7 years ago

First import

File size: 4.6 KB
Line 
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{
14  c->matrix_model_projection_updated=(c->matrix_mode<=1);
15}
16
17
18void glopMatrixMode(GLContext *c,GLParam *p)
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
36void glopLoadMatrix(GLContext *c,GLParam *p)
37{
38  M4 *m;
39  int i;
40 
41  GLParam *q;
42
43  m=c->matrix_stack_ptr[c->matrix_mode];
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
57void glopLoadIdentity(GLContext *c,GLParam *p)
58{
59
60  gl_M4_Id(c->matrix_stack_ptr[c->matrix_mode]);
61
62  gl_matrix_update(c);
63}
64
65void glopMultMatrix(GLContext *c,GLParam *p)
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
81  gl_M4_MulLeft(c->matrix_stack_ptr[c->matrix_mode],&m);
82
83  gl_matrix_update(c);
84}
85
86
87void glopPushMatrix(GLContext *c,GLParam *p)
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
102void glopPopMatrix(GLContext *c,GLParam *p)
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
112void glopRotate(GLContext *c,GLParam *p)
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
177  gl_M4_MulLeft(c->matrix_stack_ptr[c->matrix_mode],&m);
178
179  gl_matrix_update(c);
180}
181
182void glopScale(GLContext *c,GLParam *p)
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
196void glopTranslate(GLContext *c,GLParam *p)
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
212void glopFrustum(GLContext *c,GLParam *p)
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
237  gl_M4_MulLeft(c->matrix_stack_ptr[c->matrix_mode],&m);
238
239  gl_matrix_update(c);
240}
241 
Note: See TracBrowser for help on using the repository browser.