fixed windows build
[andemo] / src / sanegl.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6
7 #include "opengl.h"
8
9 #ifdef GLDEF
10 #undef GLDEF
11 #endif
12 #include "sanegl.h"
13
14 #define MMODE_IDX(x)    ((x) - GL_MODELVIEW)
15 #define MAT_STACK_SIZE  32
16 #define MAT_IDENT       {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
17
18 #define MAX_VERTS       512
19
20 static void gl_draw_immediate(void);
21
22 typedef struct { float x, y; } vec2_t;
23 typedef struct { float x, y, z; } vec3_t;
24 typedef struct { float x, y, z, w; } vec4_t;
25
26 static int mm_idx = 0;
27 static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}};
28 static int stack_top[3];
29 static float mat_mvp[16];
30 static int mvp_valid;
31 static int prim = -1;
32
33 static vec3_t cur_normal;
34 static vec4_t cur_color, cur_attrib;
35 static vec2_t cur_texcoord;
36
37 static vec4_t *vert_arr, *col_arr, *attr_arr;
38 static vec3_t *norm_arr;
39 static vec2_t *texc_arr;
40 /*static unsigned int vbuf, cbuf, nbuf, tbuf, abuf;*/
41 static int vloc, nloc, cloc, tloc, aloc = -1;
42
43 static int num_verts, vert_calls;
44 static int cur_prog;
45
46
47 void gl_matrix_mode(int mm)
48 {
49         mm_idx = MMODE_IDX(mm);
50 }
51
52 void gl_push_matrix(void)
53 {
54         int top = stack_top[mm_idx];
55
56         memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float));
57         stack_top[mm_idx]++;
58         mvp_valid = 0;
59 }
60
61 void gl_pop_matrix(void)
62 {
63         stack_top[mm_idx]--;
64         mvp_valid = 0;
65 }
66
67 void gl_load_identity(void)
68 {
69         static const float idmat[] = MAT_IDENT;
70         int top = stack_top[mm_idx];
71         float *mat = mat_stack[mm_idx][top];
72
73         memcpy(mat, idmat, sizeof idmat);
74         mvp_valid = 0;
75 }
76
77 void gl_load_matrixf(const float *m)
78 {
79         int top = stack_top[mm_idx];
80         float *mat = mat_stack[mm_idx][top];
81
82         memcpy(mat, m, 16 * sizeof *mat);
83         mvp_valid = 0;
84 }
85
86 #define M(i, j) ((i << 2) + j)
87
88 void gl_mult_matrixf(const float *m2)
89 {
90         int i, j;
91         int top = stack_top[mm_idx];
92         float *m1 = mat_stack[mm_idx][top];
93         float res[16];
94
95         for(i=0; i<4; i++) {
96                 for(j=0; j<4; j++) {
97                         res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] +
98                                                 m1[M(i,1)] * m2[M(1,j)] +
99                                                 m1[M(i,2)] * m2[M(2,j)] +
100                                                 m1[M(i,3)] * m2[M(3,j)];
101                 }
102         }
103
104         memcpy(m1, res, sizeof res);
105         mvp_valid = 0;
106 }
107
108 void gl_translatef(float x, float y, float z)
109 {
110         float mat[] = MAT_IDENT;
111
112         mat[12] = x;
113         mat[13] = y;
114         mat[14] = z;
115
116         gl_mult_matrixf(mat);
117 }
118
119 void gl_rotatef(float angle, float x, float y, float z)
120 {
121         float mat[] = MAT_IDENT;
122
123         float angle_rad = M_PI * angle / 180.0;
124         float sina = sin(angle_rad);
125         float cosa = cos(angle_rad);
126         float one_minus_cosa = 1.0 - cosa;
127         float nxsq = x * x;
128         float nysq = y * y;
129         float nzsq = z * z;
130
131         mat[0] = nxsq + (1.0 - nxsq) * cosa;
132         mat[4] = x * y * one_minus_cosa - z * sina;
133         mat[8] = x * z * one_minus_cosa + y * sina;
134         mat[1] = x * y * one_minus_cosa + z * sina;
135         mat[5] = nysq + (1.0 - nysq) * cosa;
136         mat[9] = y * z * one_minus_cosa - x * sina;
137         mat[2] = x * z * one_minus_cosa - y * sina;
138         mat[6] = y * z * one_minus_cosa + x * sina;
139         mat[10] = nzsq + (1.0 - nzsq) * cosa;
140
141         gl_mult_matrixf(mat);
142 }
143
144 void gl_scalef(float x, float y, float z)
145 {
146         float mat[] = MAT_IDENT;
147
148         mat[0] = x;
149         mat[5] = y;
150         mat[10] = z;
151
152         gl_mult_matrixf(mat);
153 }
154
155 void gl_ortho(float left, float right, float bottom, float top, float znear, float zfar)
156 {
157         float mat[] = MAT_IDENT;
158
159         float dx = right - left;
160         float dy = top - bottom;
161         float dz = zfar - znear;
162
163         float tx = -(right + left) / dx;
164         float ty = -(top + bottom) / dy;
165         float tz = -(zfar + znear) / dz;
166
167         float sx = 2.0 / dx;
168         float sy = 2.0 / dy;
169         float sz = -2.0 / dz;
170
171         mat[0] = sx;
172         mat[5] = sy;
173         mat[10] = sz;
174         mat[12] = tx;
175         mat[13] = ty;
176         mat[14] = tz;
177
178         gl_mult_matrixf(mat);
179 }
180
181 void gl_frustum(float left, float right, float bottom, float top, float znear, float zfar)
182 {
183         float mat[] = MAT_IDENT;
184
185         float dx = right - left;
186         float dy = top - bottom;
187         float dz = zfar - znear;
188
189         float a = (right + left) / dx;
190         float b = (top + bottom) / dy;
191         float c = -(zfar + znear) / dz;
192         float d = -2.0 * zfar * znear / dz;
193
194         mat[0] = 2.0 * znear / dx;
195         mat[5] = 2.0 * znear / dy;
196         mat[8] = a;
197         mat[9] = b;
198         mat[10] = c;
199         mat[11] = -1.0;
200         mat[14] = d;
201
202         gl_mult_matrixf(mat);
203 }
204
205 void glu_perspective(float vfov, float aspect, float znear, float zfar)
206 {
207         float vfov_rad = M_PI * vfov / 180.0;
208         float x = znear * tan(vfov_rad / 2.0);
209         gl_frustum(-aspect * x, aspect * x, -x, x, znear, zfar);
210 }
211
212 void gl_apply_xform(unsigned int prog)
213 {
214         int loc, mvidx, pidx, tidx, mvtop, ptop, ttop;
215
216         mvidx = MMODE_IDX(GL_MODELVIEW);
217         pidx = MMODE_IDX(GL_PROJECTION);
218         tidx = MMODE_IDX(GL_TEXTURE);
219
220         mvtop = stack_top[mvidx];
221         ptop = stack_top[pidx];
222         ttop = stack_top[tidx];
223
224         assert(prog);
225
226         if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
227                 glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]);
228         }
229
230         if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
231                 glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]);
232         }
233
234         if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
235                 glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]);
236         }
237
238         if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
239                 float nmat[9];
240
241                 nmat[0] = mat_stack[mvidx][mvtop][0];
242                 nmat[1] = mat_stack[mvidx][mvtop][1];
243                 nmat[2] = mat_stack[mvidx][mvtop][2];
244                 nmat[3] = mat_stack[mvidx][mvtop][4];
245                 nmat[4] = mat_stack[mvidx][mvtop][5];
246                 nmat[5] = mat_stack[mvidx][mvtop][6];
247                 nmat[6] = mat_stack[mvidx][mvtop][8];
248                 nmat[7] = mat_stack[mvidx][mvtop][9];
249                 nmat[8] = mat_stack[mvidx][mvtop][10];
250                 glUniformMatrix3fv(loc, 1, 0, nmat);
251         }
252
253         if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
254                 if(!mvp_valid) {
255                         /* TODO calc mvp */
256                 }
257                 glUniformMatrix4fv(loc, 1, 0, mat_mvp);
258         }
259 }
260
261
262 /* immediate mode rendering */
263 void gl_begin(int p)
264 {
265         if(!vert_arr) {
266                 vert_arr = malloc(MAX_VERTS * sizeof *vert_arr);
267                 norm_arr = malloc(MAX_VERTS * sizeof *norm_arr);
268                 texc_arr = malloc(MAX_VERTS * sizeof *texc_arr);
269                 col_arr = malloc(MAX_VERTS * sizeof *col_arr);
270                 attr_arr = malloc(MAX_VERTS * sizeof *attr_arr);
271                 assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr);
272         }
273
274         prim = p;
275         num_verts = vert_calls = 0;
276
277         glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
278         assert(cur_prog);
279
280         gl_apply_xform(cur_prog);
281
282         vloc = glGetAttribLocation(cur_prog, "attr_vertex");
283         nloc = glGetAttribLocation(cur_prog, "attr_normal");
284         cloc = glGetAttribLocation(cur_prog, "attr_color");
285         tloc = glGetAttribLocation(cur_prog, "attr_texcoord");
286 }
287
288 void gl_end(void)
289 {
290         if(num_verts > 0) {
291                 gl_draw_immediate();
292         }
293         aloc = -1;
294 }
295
296 static void gl_draw_immediate(void)
297 {
298         int glprim;
299
300         if(vloc == -1) {
301                 fprintf(stderr, "gl_draw_immediate call with vloc == -1\n");
302                 return;
303         }
304
305         glprim = prim == GL_QUADS ? GL_TRIANGLES : prim;
306
307         glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr);
308         glEnableVertexAttribArray(vloc);
309
310         if(nloc != -1) {
311                 glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr);
312                 glEnableVertexAttribArray(nloc);
313         }
314
315         if(cloc != -1) {
316                 glVertexAttribPointer(cloc, 4, GL_FLOAT, 1, 0, col_arr);
317                 glEnableVertexAttribArray(cloc);
318         }
319
320         if(tloc != -1) {
321                 glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr);
322                 glEnableVertexAttribArray(tloc);
323         }
324
325         if(aloc != -1) {
326                 glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr);
327                 glEnableVertexAttribArray(aloc);
328         }
329
330         glDrawArrays(glprim, 0, num_verts);
331
332         glDisableVertexAttribArray(vloc);
333         if(nloc != -1) {
334                 glDisableVertexAttribArray(nloc);
335         }
336         if(cloc != -1) {
337                 glDisableVertexAttribArray(cloc);
338         }
339         if(tloc != -1) {
340                 glDisableVertexAttribArray(tloc);
341         }
342         if(aloc != -1) {
343                 glDisableVertexAttribArray(aloc);
344         }
345 }
346
347
348 void gl_vertex2f(float x, float y)
349 {
350         gl_vertex4f(x, y, 0.0f, 1.0f);
351 }
352
353 void gl_vertex3f(float x, float y, float z)
354 {
355         gl_vertex4f(x, y, z, 1.0f);
356 }
357
358 void gl_vertex4f(float x, float y, float z, float w)
359 {
360         int i, buffer_full;
361
362         if(prim == GL_QUADS && vert_calls % 4 == 3) {
363                 for(i=0; i<2; i++) {
364                         if(aloc != -1) {
365                                 attr_arr[num_verts] = attr_arr[num_verts - 3 + i];
366                         }
367                         if(cloc != -1) {
368                                 col_arr[num_verts] = col_arr[num_verts - 3 + i];
369                         }
370                         if(tloc != -1) {
371                                 texc_arr[num_verts] = texc_arr[num_verts - 3 + i];
372                         }
373                         if(nloc != -1) {
374                                 norm_arr[num_verts] = norm_arr[num_verts - 3 + i];
375                         }
376                         vert_arr[num_verts] = vert_arr[num_verts - 3 + i];
377                         num_verts++;
378                 }
379         }
380
381         vert_arr[num_verts].x = x;
382         vert_arr[num_verts].y = y;
383         vert_arr[num_verts].z = z;
384         vert_arr[num_verts].w = w;
385
386         if(cloc != -1) {
387                 col_arr[num_verts] = cur_color;
388         }
389         if(nloc != -1) {
390                 norm_arr[num_verts] = cur_normal;
391         }
392         if(tloc != -1) {
393                 texc_arr[num_verts] = cur_texcoord;
394         }
395         if(aloc != -1) {
396                 attr_arr[num_verts] = cur_attrib;
397         }
398
399         vert_calls++;
400         num_verts++;
401
402         if(prim == GL_QUADS) {
403                 /* leave space for 6 more worst-case and don't allow flushes mid-quad */
404                 buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0;
405         } else {
406                 buffer_full = num_verts >= MAX_VERTS - prim;
407         }
408
409         if(buffer_full) {
410                 gl_draw_immediate();
411                 gl_begin(prim); /* reset everything */
412         }
413 }
414
415
416 void gl_normal3f(float x, float y, float z)
417 {
418         cur_normal.x = x;
419         cur_normal.y = y;
420         cur_normal.z = z;
421 }
422
423
424 void gl_color3f(float r, float g, float b)
425 {
426         cur_color.x = r;
427         cur_color.y = g;
428         cur_color.z = b;
429         cur_color.w = 1.0f;
430 }
431
432 void gl_color4f(float r, float g, float b, float a)
433 {
434         cur_color.x = r;
435         cur_color.y = g;
436         cur_color.z = b;
437         cur_color.w = a;
438 }
439
440
441 void gl_texcoord1f(float s)
442 {
443         cur_texcoord.x = s;
444         cur_texcoord.y = 0.0f;
445 }
446
447 void gl_texcoord2f(float s, float t)
448 {
449         cur_texcoord.x = s;
450         cur_texcoord.y = t;
451 }
452
453 void gl_vertex_attrib2f(int loc, float x, float y)
454 {
455         aloc = loc;
456         cur_attrib.x = x;
457         cur_attrib.y = y;
458         cur_attrib.z = 0.0f;
459         cur_attrib.w = 1.0f;
460 }
461
462 void gl_vertex_attrib3f(int loc, float x, float y, float z)
463 {
464         aloc = loc;
465         cur_attrib.x = x;
466         cur_attrib.y = y;
467         cur_attrib.z = z;
468         cur_attrib.w = 1.0f;
469 }
470
471 void gl_vertex_attrib4f(int loc, float x, float y, float z, float w)
472 {
473         aloc = loc;
474         cur_attrib.x = x;
475         cur_attrib.y = y;
476         cur_attrib.z = z;
477         cur_attrib.w = w;
478 }