forked from beltoforion/Galaxy-Renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwzgles.c
3782 lines (3279 loc) · 104 KB
/
jwzgles.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* xscreensaver, Copyright (c) 2012 Jamie Zawinski <[email protected]>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
/* A compatibility shim to allow OpenGL 1.3 source code to work in an
OpenGLES environment, where almost every OpenGL 1.3 function has
been "deprecated".
There are two major operations going on here:
- Converting calls to glBegin + glVertex3f + glEnd to glDrawArrays
- Implementing display lists.
From an API point of view, OpenGL 1.3 and earlier code looks like this:
glLightfv (GL_LIGHT0, GL_POSITION, ...);
glLightfv (GL_LIGHT0, GL_AMBIENT, ...);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (...);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (...);
glPushMatrix ();
glRotatef (...);
glColor3f (...);
glBegin (GL_TRIANGLES);
glNormal3f (...);
glVertex3f (...);
glVertex3f (...);
glVertex3f (...);
glEnd ();
glPopMatrix ();
glFinish ();
OpenGLES broke that model by eliminating glBegin(). Instead of
iterating a sequence of vertexes, you need to pack your points into
an array first, e.g.:
GLfloat coords[] = {
0, 0, 0,
0, 1, 0,
...
};
glDrawArrays (GL_TRIANGLES, 0, 3);
The projection model (glRotatef, etc.) works the same, but glColor()
is missing. You're expected to encode that into your arrays.
Also, OpenGLES doesn't support display lists at all.
So this code shadows all of the functions that are allowed within
glBegin, builds up an array, and calls glDrawArrays at the end.
Likewise, it shadows all of the functions that are allowed within
glNewList and records those calls for later playback.
This code only handles OpenGLES 1.x, not 2.x.
OpenGLES 2.0 broke things further by eliminating the whole OpenGL
lighting model. Instead of specifying the positions and properties
of your lights using the glLight* API, now you are expected to
implement it all yourself by downloading C-like code into the GPU
directly. This is more flexible, in that if you wanted a completely
different lighting model than what OpenGL provides, you could do
that, but it leaves you needing to download boilerplate to reproduce
what used to be built in.
Incidentally, the OpenGL numbering scheme goes something like this:
OpenGL 1.0 1992
OpenGL 1.1 1997 (improved texture support)
OpenGL 1.2 1998 (nothing interesting)
OpenGL 1.3 2001 (multisampling, cubemaps)
OpenGL 1.4 2002 (added auto-mipmapping)
OpenGLES 1.0 2003 (deprecated 80% of the language; fork of OpenGL 1.3)
OpenGL 1.5 2003 (added VBOs)
OpenGLES 1.1 2004 (fork of OpenGL 1.5)
OpenGL 2.0 2004 (a political quagmire)
OpenGLES 2.0 2007 (deprecated 95% of the language; fork of OpenGL 2.0)
OpenGL 3.0 2008 (added FBOs, VAOs, deprecated 60% of the language)
Some things that are missing:
- glTexGeni, meaning no spherical environment-mapping textures.
- gluNewTess, meaning no tesselation of complex objects.
- glMap2f mesh evaluators, meaning no Utah Teapot.
- glPolygonMode with GL_LINE or GL_POINT, meaning no wireframe modes
that do hidden-surface removal.
- glSelectBuffer, meaning no mouse-hit detection on rendered objects.
- gluNewQuadric, gluCylinder, etc: rewrite your code to use tube.c, etc.
- Putting verts in a display list without a wrapping glBegin.
(I didn't realize that even worked!)
- Not every function is implemented; just the ones that I needed for
xscreensaver. However, the trivial ones are trivial to enable
as they come up. Harder ones will be harder.
As a result of that, these savers look wrong:
blocktube Uses SPHERE_MAP.
bouncingcow Uses OBJECT_LINEAR (when run with -texture)
dnalogo Uses GLUtesselator.
extrusion Uses all kinds of GLUT crap.
flyingtoasters Uses SPHERE_MAP and OBJECT_LINEAR.
jigglypuff Uses SPHERE_MAP (in chrome mode), GL_LINE (in wireframe)
jigsaw Uses GLUtesselator.
lockward Puts verts in lists without glBegin!
pinion Uses glSelectBuffer and gluPickMatrix for mouse-clicks.
pipes Uses glMap2f for the Utah Teapot.
polyhedra Uses GLUtesselator (concave objects); also Utah Teapot.
skytentacles Uses GL_LINE in -cel mode.
timetunnel Uses GL_CONSTANT_ALPHA and all kinds of other stuff.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include "jwzglesI.h"
#define STRINGIFY(X) #X
#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))
#undef Assert
#ifdef THIS_IS_UNUSED_FOR_NOW
extern void jwxyz_abort (const char *fmt, ...) __dead2;
# define Assert(C,S) do { if (!(C)) { jwxyz_abort ("%s",S); }} while(0)
#else
# define Assert(C,S) do { \
if (!(C)) { \
fprintf (stderr, "jwzgles: %s\n", S); \
abort(); \
}} while(0)
#endif
typedef struct { GLfloat x, y, z; } XYZ;
typedef struct { GLfloat x, y, z, w; } XYZW;
typedef struct { GLfloat s, t, r, q; } STRQ;
typedef struct { GLfloat r, g, b, a; } RGBA;
/* Used to record all calls to glVertex3f, glNormal3f, etc.
while inside glBegin / glEnd so that we can convert that
to a single call to glDrawArrays.
*/
typedef struct {
int mode;
int count, size; /* size of each array */
XYZW *verts; /* Arrays being built */
XYZ *norms;
STRQ *tex;
RGBA *color;
int ncount; /* How many normals, tex coords and colors were */
int tcount; /* used. We optimize based on "0, 1, or many". */
int ccount;
int materialistic; /* Whether glMaterial was called inside glBegin */
XYZ cnorm; /* Prevailing normal/texture/color while building */
STRQ ctex;
RGBA ccolor;
} vert_set;
typedef void (*list_fn_cb) (void);
/* We need this nonsense because you can't cast a double to a void*
or vice versa. They tend to be passed in different registers,
and you need to know about that because it's still 1972 here.
*/
typedef union { const void *v; GLfloat f; GLuint i; } void_int;
typedef struct { /* saved args for glDrawArrays */
int binding, size, type, stride, bytes;
void *data;
} draw_array;
typedef enum { /* shorthand describing arglist signature */
PROTO_VOID, /* no args */
PROTO_I, /* 1 int arg */
PROTO_F, /* 1 float arg */
PROTO_II, /* int, int */
PROTO_FF, /* float, float */
PROTO_IF, /* int, float */
PROTO_III, /* int, int, int */
PROTO_FFF, /* float, float, float */
PROTO_IIF, /* int, int, float */
PROTO_IIII, /* int, int, int, int */
PROTO_FFFF, /* float, float, float, float */
PROTO_IIV, /* int, int[4] */
PROTO_IFV, /* int, float[4] */
PROTO_IIIV, /* int, int, int[4] */
PROTO_IIFV, /* int, int, float[4] */
PROTO_FV16, /* float[16] */
PROTO_ARRAYS /* glDrawArrays */
} fn_proto;
typedef struct { /* A single element of a display list */
const char *name;
list_fn_cb fn; /* saved function pointer */
fn_proto proto; /* arglist prototype */
draw_array *arrays; /* args for glDrawArrays */
void_int argv[16]; /* args for everything else */
} list_fn;
typedef struct { /* a display list: saved activity within glNewList */
int id;
int size, count;
list_fn *fns;
/* Named buffer that should be freed when this display list is deleted. */
GLuint buffer;
} list;
typedef struct { /* All display lists */
list *lists;
int count, size;
} list_set;
#define ISENABLED_TEXTURE_2D (1<<0)
#define ISENABLED_TEXTURE_GEN_S (1<<1)
#define ISENABLED_TEXTURE_GEN_T (1<<2)
#define ISENABLED_LIGHTING (1<<3)
#define ISENABLED_BLEND (1<<4)
#define ISENABLED_DEPTH_TEST (1<<5)
#define ISENABLED_CULL_FACE (1<<6)
#define ISENABLED_NORMALIZE (1<<7)
#define ISENABLED_FOG (1<<8)
#define ISENABLED_COLMAT (1<<9)
#define ISENABLED_VERT_ARRAY (1<<10)
#define ISENABLED_NORM_ARRAY (1<<11)
#define ISENABLED_TEX_ARRAY (1<<12)
#define ISENABLED_COLOR_ARRAY (1<<13)
typedef struct { /* global state */
vert_set set; /* set being built */
int compiling_list; /* list id if inside glNewList; 0 means immediate */
int replaying_list; /* depth of call stack to glCallList */
int compiling_verts; /* inside glBegin */
list_set lists; /* saved lists */
unsigned long enabled;
} jwzgles_state;
static jwzgles_state global_state = { { 0, }, 0, 0, 0, { 0, }, 0, };
static jwzgles_state *state = &global_state;
#ifdef DEBUG
# define LOG(A) fprintf(stderr,"jwzgles: " A "\n")
# define LOG1(A,B) fprintf(stderr,"jwzgles: " A "\n",B)
# define LOG2(A,B,C) fprintf(stderr,"jwzgles: " A "\n",B,C)
# define LOG3(A,B,C,D) fprintf(stderr,"jwzgles: " A "\n",B,C,D)
# define LOG4(A,B,C,D,E) fprintf(stderr,"jwzgles: " A "\n",B,C,D,E)
# define LOG5(A,B,C,D,E,F) fprintf(stderr,"jwzgles: " A "\n",B,C,D,E,F)
# define LOG6(A,B,C,D,E,F,G) fprintf(stderr,"jwzgles: " A "\n",B,C,D,E,F,G)
# define LOG7(A,B,C,D,E,F,G,H) fprintf(stderr,"jwzgles: " A "\n",B,C,D,E,F,G,H)
# define LOG8(A,B,C,D,E,F,G,H,I)\
fprintf(stderr,"jwzgles: "A "\n",B,C,D,E,F,G,H,I)
# define LOG9(A,B,C,D,E,F,G,H,I,J)\
fprintf(stderr,"jwzgles: "A "\n",B,C,D,E,F,G,H,I,J)
# define LOG10(A,B,C,D,E,F,G,H,I,J,K)\
fprintf(stderr,"jwzgles: "A "\n",B,C,D,E,F,G,H,I,J,K)
# define LOG17(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R)\
fprintf(stderr,"jwzgles: "A "\n",B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R)
# define CHECK(S) check_gl_error(S)
#else
# define LOG(A) /* */
# define LOG1(A,B) /* */
# define LOG2(A,B,C) /* */
# define LOG3(A,B,C,D) /* */
# define LOG4(A,B,C,D,E) /* */
# define LOG5(A,B,C,D,E,F) /* */
# define LOG6(A,B,C,D,E,F,G) /* */
# define LOG7(A,B,C,D,E,F,G,H) /* */
# define LOG8(A,B,C,D,E,F,G,H,I) /* */
# define LOG9(A,B,C,D,E,F,G,H,I,J) /* */
# define LOG10(A,B,C,D,E,F,G,H,I,J,K) /* */
# define LOG17(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R) /* */
# define CHECK(S) /* */
#endif
#ifdef DEBUG
static const char *
mode_desc (int mode) /* for debugging messages */
{
switch (mode) {
# define SS(X) case GL_##X: return STRINGIFY(X);
SS(ALPHA)
SS(ALPHA_TEST)
SS(AMBIENT)
SS(AMBIENT_AND_DIFFUSE)
SS(ARRAY_BUFFER)
SS(AUTO_NORMAL)
SS(BACK)
SS(BLEND)
SS(BLEND_DST)
SS(BLEND_SRC)
SS(BLEND_SRC_ALPHA)
SS(BYTE)
SS(C3F_V3F)
SS(C4F_N3F_V3F)
SS(C4UB_V2F)
SS(C4UB_V3F)
SS(CCW)
SS(CLAMP)
SS(COLOR_ARRAY)
SS(COLOR_ARRAY_BUFFER_BINDING);
SS(COLOR_MATERIAL)
SS(COLOR_MATERIAL_FACE)
SS(COLOR_MATERIAL_PARAMETER)
SS(COMPILE)
SS(CULL_FACE)
SS(CW)
SS(DECAL)
SS(DEPTH_BUFFER_BIT)
SS(DEPTH_TEST)
SS(DIFFUSE)
SS(DOUBLEBUFFER)
SS(DST_ALPHA)
SS(DST_COLOR)
SS(DYNAMIC_DRAW)
SS(ELEMENT_ARRAY_BUFFER)
SS(EYE_LINEAR)
SS(EYE_PLANE)
SS(FEEDBACK)
SS(FILL)
SS(FLAT)
SS(FLOAT)
SS(FOG)
SS(FRONT)
SS(FRONT_AND_BACK)
SS(GREATER)
SS(INTENSITY)
SS(INVALID_ENUM)
SS(INVALID_OPERATION)
SS(INVALID_VALUE)
SS(LESS)
SS(LIGHT0)
SS(LIGHT1)
SS(LIGHT2)
SS(LIGHT3)
SS(LIGHTING)
SS(LIGHT_MODEL_AMBIENT)
SS(LIGHT_MODEL_COLOR_CONTROL)
SS(LIGHT_MODEL_LOCAL_VIEWER)
SS(LIGHT_MODEL_TWO_SIDE)
SS(LINE)
SS(LINEAR)
SS(LINEAR_MIPMAP_LINEAR)
SS(LINEAR_MIPMAP_NEAREST)
SS(LINES)
SS(LINE_LOOP)
SS(LINE_STRIP)
SS(LUMINANCE)
SS(LUMINANCE_ALPHA)
SS(MATRIX_MODE)
SS(MODELVIEW)
SS(MODULATE)
SS(N3F_V3F)
SS(NEAREST)
SS(NEAREST_MIPMAP_LINEAR)
SS(NEAREST_MIPMAP_NEAREST)
SS(NORMALIZE)
SS(NORMAL_ARRAY)
SS(NORMAL_ARRAY_BUFFER_BINDING);
SS(OBJECT_LINEAR)
SS(OBJECT_PLANE)
SS(ONE_MINUS_DST_ALPHA)
SS(ONE_MINUS_DST_COLOR)
SS(ONE_MINUS_SRC_ALPHA)
SS(ONE_MINUS_SRC_COLOR)
SS(OUT_OF_MEMORY)
SS(PACK_ALIGNMENT)
SS(POINTS)
SS(POLYGON)
SS(POLYGON_OFFSET_FILL)
SS(POLYGON_SMOOTH)
SS(POLYGON_STIPPLE)
SS(POSITION)
SS(PROJECTION)
SS(Q)
SS(QUADS)
SS(QUAD_STRIP)
SS(R)
SS(RENDER)
SS(REPEAT)
SS(RGB)
SS(RGBA)
SS(RGBA_MODE)
SS(S)
SS(SELECT)
SS(SEPARATE_SPECULAR_COLOR)
SS(SHADE_MODEL)
SS(SHININESS)
SS(SHORT)
SS(SINGLE_COLOR)
SS(SMOOTH)
SS(SPECULAR)
SS(SPHERE_MAP)
SS(SRC_ALPHA)
SS(SRC_ALPHA_SATURATE)
SS(SRC_COLOR)
SS(STACK_OVERFLOW)
SS(STACK_UNDERFLOW)
SS(STATIC_DRAW)
SS(STENCIL_BUFFER_BIT)
SS(T)
SS(T2F_C3F_V3F)
SS(T2F_C4F_N3F_V3F)
SS(T2F_C4UB_V3F)
SS(T2F_N3F_V3F)
SS(T2F_V3F)
SS(T4F_C4F_N3F_V4F)
SS(T4F_V4F)
SS(TEXTURE)
SS(TEXTURE_1D)
SS(TEXTURE_2D)
SS(TEXTURE_ALPHA_SIZE)
SS(TEXTURE_BINDING_2D)
SS(TEXTURE_BLUE_SIZE)
SS(TEXTURE_BORDER)
SS(TEXTURE_BORDER_COLOR)
SS(TEXTURE_COMPONENTS)
SS(TEXTURE_COORD_ARRAY)
SS(TEXTURE_COORD_ARRAY_BUFFER_BINDING);
SS(TEXTURE_ENV)
SS(TEXTURE_ENV_COLOR)
SS(TEXTURE_ENV_MODE)
SS(TEXTURE_GEN_MODE)
SS(TEXTURE_GEN_Q)
SS(TEXTURE_GEN_R)
SS(TEXTURE_GEN_S)
SS(TEXTURE_GEN_T)
SS(TEXTURE_GREEN_SIZE)
SS(TEXTURE_HEIGHT)
SS(TEXTURE_INTENSITY_SIZE)
SS(TEXTURE_LUMINANCE_SIZE)
SS(TEXTURE_MAG_FILTER)
SS(TEXTURE_MIN_FILTER)
SS(TEXTURE_RED_SIZE)
SS(TEXTURE_WRAP_S)
SS(TEXTURE_WRAP_T)
SS(TRIANGLES)
SS(TRIANGLE_FAN)
SS(TRIANGLE_STRIP)
SS(UNPACK_ALIGNMENT)
SS(UNPACK_ROW_LENGTH)
SS(UNSIGNED_BYTE)
SS(UNSIGNED_INT_8_8_8_8_REV)
SS(UNSIGNED_SHORT)
SS(V2F)
SS(V3F)
SS(VERTEX_ARRAY)
SS(VERTEX_ARRAY_BUFFER_BINDING);
/*SS(COLOR_BUFFER_BIT) -- same value as GL_LIGHT0 */
# undef SS
case (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT):
return "DEPTH_BUFFER_BIT | COLOR_BUFFER_BIT";
/* Oops, same as INVALID_ENUM.
case (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT):
return "DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT";
*/
case (GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT):
return "COLOR_BUFFER_BIT | STENCIL_BUFFER_BIT";
case (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT):
return "DEPTH_BUFFER_BIT | COLOR_BUFFER_BIT | STENCIL_BUFFER_BIT";
default:
{
static char buf[255];
sprintf (buf, "0x%04X", mode);
return buf;
}
}
}
static void
check_gl_error (const char *s)
{
GLenum i = glGetError();
if (i == GL_NO_ERROR) return;
fprintf (stderr, "jwzgles: GL ERROR: %s: %s\n", s, mode_desc(i));
}
#endif /* DEBUG */
static void
make_room (const char *name, void **array, int span, int *count, int *size)
{
if (*count + 1 >= *size)
{
int new_size = (*count + 20) * 1.2; /* mildly exponential */
*array = realloc (*array, new_size * span);
Assert (*array, "out of memory");
/* LOG3("%s: grew %d -> %d", name, *size, new_size); */
*size = new_size;
}
}
int
jwzgles_glGenLists (int n)
{
int i;
int ret = 0;
Assert (!state->compiling_verts, "glGenLists not allowed inside glBegin");
/* Ensure space in state->lists, clear the one at the end, and tick counter
Note that lists are never really deleted, and we can never re-use elements
of this array. glDeleteLists zeroes out the contents of the list, but
the list ID is still valid for use with glNewList forever.
#### So maybe this should be a linked list instead of an array.
*/
for (i = 0; i < n; i++)
{
list *L;
int id = 0;
make_room ("glGenLists",
(void **) &state->lists.lists,
sizeof (*state->lists.lists),
&state->lists.count, &state->lists.size);
state->lists.count++;
id = state->lists.count;
L = &state->lists.lists[id-1];
memset (L, 0, sizeof (*L));
L->id = id;
if (ret == 0) ret = id;
LOG1("glGenLists -> %d", L->id);
}
/* Return the ID of the first list allocated */
return ret;
}
void
jwzgles_glNewList (int id, int mode)
{
list *L;
Assert (id > 0 && id <= state->lists.count, "glNewList: bogus ID");
Assert (mode == GL_COMPILE, "glNewList: bad mode");
Assert (!state->compiling_verts, "glNewList not allowed inside glBegin");
Assert (!state->compiling_list, "nested glNewList");
Assert (state->set.count == 0, "missing glEnd");
L = &state->lists.lists[id-1];
Assert (L->id == id, "glNewList corrupted");
if (L->count != 0) jwzgles_glDeleteLists (L->id, 1); /* Overwriting */
Assert (L->count == 0, "glNewList corrupted");
state->compiling_list = id;
LOG1("glNewList -> %d", id);
}
static void save_arrays (list_fn *, int);
static void restore_arrays (list_fn *, int);
static void copy_array_data (draw_array *, int, const char *);
static void optimize_arrays (void);
void
jwzgles_glEndList (void)
{
Assert (state->compiling_list, "extra glEndList");
Assert (state->set.count == 0, "missing glEnd");
Assert (!state->compiling_verts, "glEndList not allowed inside glBegin");
LOG1("glEndList %d", state->compiling_list);
optimize_arrays();
state->compiling_list = 0;
}
static void
list_push (const char * const name,
list_fn_cb fn, fn_proto proto, void_int *av)
{
list *L;
list_fn *F;
int i;
Assert (state->compiling_list > 0, "not inside glNewList");
Assert (state->compiling_list <= state->lists.count, "glNewList corrupted");
L = &state->lists.lists[state->compiling_list-1];
Assert (L, "glNewList: no list");
make_room ("glNewLists",
(void **) &L->fns, sizeof (*L->fns),
&L->count, &L->size);
memset (&L->fns[L->count], 0, sizeof (*L->fns));
F = L->fns + L->count;
F->name = name;
F->fn = fn;
F->proto = proto;
if (proto != PROTO_VOID)
for (i = 0; i < countof(F->argv); i++)
F->argv[i] = av[i];
# ifdef DEBUG
switch (proto) {
case PROTO_VOID:
LOG1 (" push %-12s", name);
break;
case PROTO_I:
if (fn == (list_fn_cb) &jwzgles_glBegin ||
fn == (list_fn_cb) &jwzgles_glFrontFace ||
fn == (list_fn_cb) &jwzgles_glEnable ||
fn == (list_fn_cb) &jwzgles_glDisable ||
fn == (list_fn_cb) &jwzgles_glEnableClientState ||
fn == (list_fn_cb) &jwzgles_glDisableClientState ||
fn == (list_fn_cb) &jwzgles_glShadeModel ||
fn == (list_fn_cb) &jwzgles_glMatrixMode)
LOG2 (" push %-12s %s", name, mode_desc (av[0].i));
else
LOG2 (" push %-12s %d", name, av[0].i);
break;
case PROTO_F:
LOG2 (" push %-12s %7.3f", name, av[0].f);
break;
case PROTO_II:
if (fn == (list_fn_cb) &jwzgles_glBindTexture ||
fn == (list_fn_cb) &jwzgles_glBindBuffer)
LOG3 (" push %-12s %s %d", name, mode_desc (av[0].i), av[1].i);
else
LOG3 (" push %-12s %d %d", name, av[0].i, av[1].i);
break;
case PROTO_FF:
LOG3 (" push %-12s %7.3f %7.3f", name, av[0].f, av[1].f);
break;
case PROTO_IF:
LOG3 (" push %-12s %s %7.3f", name, mode_desc (av[0].i), av[1].f);
break;
case PROTO_III:
case PROTO_ARRAYS:
if (fn == (list_fn_cb) &jwzgles_glDrawArrays ||
fn == (list_fn_cb) &jwzgles_glTexParameteri)
LOG4 (" push %-12s %s %d %d", name, mode_desc (av[0].i),
av[1].i, av[2].i);
else
LOG4 (" push %-12s %d %d %d", name, av[0].i, av[1].i, av[2].i);
break;
case PROTO_FFF:
LOG4 (" push %-12s %7.3f %7.3f %7.3f", name, av[0].f, av[1].f, av[2].f);
break;
case PROTO_IIF:
LOG4 (" push %-12s %s %s %7.3f", name,
mode_desc(av[0].i), mode_desc(av[1].i), av[2].f);
break;
case PROTO_IIII:
LOG5 (" push %-12s %d %d %d %d", name,
av[0].i, av[1].i, av[2].i, av[3].i);
break;
case PROTO_FFFF:
LOG5 (" push %-12s %7.3f %7.3f %7.3f %7.3f", name,
av[0].f, av[1].f, av[2].f, av[3].f);
break;
case PROTO_IFV:
LOG6 (" push %-12s %s %3.1f %3.1f %3.1f %3.1f", name, mode_desc (av[0].i),
av[1].f, av[2].f, av[3].f, av[4].f);
break;
case PROTO_IIV:
LOG6 (" push %-12s %s %d %d %d %d", name, mode_desc (av[0].i),
av[1].i, av[2].i, av[3].i, av[4].i);
break;
case PROTO_IIFV:
LOG7 (" push %-12s %s %-8s %3.1f %3.1f %3.1f %3.1f", name,
mode_desc (av[0].i), mode_desc (av[1].i),
av[2].f, av[3].f, av[4].f, av[5].f);
break;
case PROTO_IIIV:
LOG7 (" push %-12s %s %-8s %3d %3d %3d %3d", name,
mode_desc (av[0].i), mode_desc (av[1].i),
av[2].i, av[3].i, av[4].i, av[5].i);
break;
case PROTO_FV16:
LOG17 (" push %-12s ["
"%8.3f %8.3f %8.3f %8.3f " "\n\t\t\t "
"%8.3f %8.3f %8.3f %8.3f " "\n\t\t\t "
"%8.3f %8.3f %8.3f %8.3f " "\n\t\t\t "
"%8.3f %8.3f %8.3f %8.3f ]",
name,
av[0].f, av[1].f, av[2].f, av[3].f,
av[4].f, av[5].f, av[6].f, av[7].f,
av[8].f, av[9].f, av[10].f, av[11].f,
av[12].f, av[13].f, av[14].f, av[15].f);
break;
default:
Assert (0, "bogus prototype");
break;
}
# endif /* DEBUG */
if (proto == PROTO_ARRAYS) /* glDrawArrays */
save_arrays (F, av[1].i + av[2].i);
L->count++;
}
void
jwzgles_glBegin (int mode)
{
Assert (!state->compiling_verts, "nested glBegin");
state->compiling_verts++;
/* Only these commands are allowed inside glBegin:
glVertex -- not allowed outside
glColor
glSecondaryColor
glIndex
glNormal
glFogCoord
glTexCoord
glMultiTexCoord
glVertexAttrib
glEvalCoord
glEvalPoint
glArrayElement -- not allowed outside
glMaterial
glEdgeFlag
glCallList
glCallLists
*/
if (!state->replaying_list)
LOG2 ("%sglBegin %s",
(state->compiling_list || state->replaying_list ? " " : ""),
mode_desc (mode));
Assert (state->set.count == 0, "glBegin corrupted");
state->set.mode = mode;
state->set.count = 0;
state->set.ncount = 0;
state->set.tcount = 0;
state->set.ccount = 0;
}
void
jwzgles_glDeleteLists (int id0, int range)
{
Assert (!state->compiling_verts, "glDeleteLists not allowed inside glBegin");
if (state->compiling_list)
{
void_int vv[2];
vv[0].i = id0;
vv[1].i = range;
list_push ("glDeleteLists", (list_fn_cb) &jwzgles_glDeleteLists,
PROTO_II, vv);
}
else
{
int id;
if (!state->replaying_list)
LOG2 ("glDeleteLists %d %d", id0, range);
for (id = id0 + range - 1; id >= id0; id--)
{
int i;
list *L;
if (id == 0) continue; /* People do this stupid thing */
if (id > state->lists.count) break; /* this too */
Assert (id > 0 && id <= state->lists.count,
"glDeleteLists: bogus ID");
L = &state->lists.lists[id-1];
Assert (L->id == id, "glDeleteLists corrupted");
for (i = 0; i < L->count; i++)
{
list_fn *lf = &L->fns[i];
if (lf->arrays)
{
int j;
for (j = 0; j < 4; j++)
/* If there's a binding, 'data' is an index, not a ptr. */
if (!lf->arrays[j].binding &&
lf->arrays[j].data)
free (lf->arrays[j].data);
free (lf->arrays);
}
}
if (L->fns)
free (L->fns);
if (L->buffer)
glDeleteBuffers (1, &L->buffer);
memset (L, 0, sizeof (*L));
L->id = id;
}
}
}
extern GLboolean
jwzgles_glIsList (GLuint id)
{
return (id > 0 && id < state->lists.count);
}
void
jwzgles_glNormal3fv (const GLfloat *v)
{
if (state->compiling_list && !state->compiling_verts)
{
void_int vv[3];
vv[0].f = v[0];
vv[1].f = v[1];
vv[2].f = v[2];
list_push ("glNormal3f", (list_fn_cb) &jwzgles_glNormal3f,
PROTO_FFF, vv);
}
else
{
if (!state->replaying_list)
LOG5 ("%s%sglNormal3f %7.3f %7.3f %7.3f",
(state->compiling_list || state->replaying_list ? " " : ""),
(state->compiling_verts ? " rec " : ""),
v[0], v[1], v[2]);
if (state->compiling_verts) /* inside glBegin */
{
state->set.cnorm.x = v[0];
state->set.cnorm.y = v[1];
state->set.cnorm.z = v[2];
state->set.ncount++;
if (state->set.count > 0 && state->set.ncount == 1) /* not first! */
state->set.ncount++;
}
else /* outside glBegin */
{
glNormal3f (v[0], v[1], v[2]);
CHECK("glNormal3f");
}
}
}
void
jwzgles_glNormal3f (GLfloat x, GLfloat y, GLfloat z)
{
GLfloat v[3];
v[0] = x;
v[1] = y;
v[2] = z;
jwzgles_glNormal3fv (v);
}
void
jwzgles_glTexCoord4fv (const GLfloat *v)
{
if (state->compiling_list && !state->compiling_verts)
{
void_int vv[4];
vv[0].f = v[0];
vv[1].f = v[1];
vv[2].f = v[2];
vv[3].f = v[3];
list_push ("glTexCoord4f", (list_fn_cb) &jwzgles_glTexCoord4f,
PROTO_FFFF, vv);
}
else
{
if (!state->replaying_list)
LOG6 ("%s%sglTexCoord4f %7.3f %7.3f %7.3f %7.3f",
(state->compiling_list || state->replaying_list ? " " : ""),
(state->compiling_verts ? " rec " : ""),
v[0], v[1], v[2], v[3]);
Assert (state->compiling_verts, "glTexCoord4fv outside glBegin");
if (state->compiling_verts) /* inside glBegin */
{
state->set.ctex.s = v[0];
state->set.ctex.t = v[1];
state->set.ctex.r = v[2];
state->set.ctex.q = v[3];
state->set.tcount++;
if (state->set.count > 0 && state->set.tcount == 1) /* not first! */
state->set.tcount++;
}
}
}
void
jwzgles_glTexCoord4f (GLfloat s, GLfloat t, GLfloat r, GLfloat q)
{
GLfloat v[4];
v[0] = s;
v[1] = t;
v[2] = r;
v[3] = q;
jwzgles_glTexCoord4fv (v);
}
void
jwzgles_glTexCoord3fv (const GLfloat *v)
{
GLfloat vv[4];
vv[0] = v[0];
vv[1] = v[1];
vv[2] = v[2];
vv[3] = 1;
jwzgles_glTexCoord4fv (vv);
}
void
jwzgles_glTexCoord2fv (const GLfloat *v)
{
GLfloat vv[4];
vv[0] = v[0];
vv[1] = v[1];
vv[2] = 0;
vv[3] = 1;
jwzgles_glTexCoord4fv (vv);
}
void
jwzgles_glTexCoord3f (GLfloat s, GLfloat t, GLfloat r)
{
jwzgles_glTexCoord4f (s, t, r, 1);
}
void
jwzgles_glTexCoord2f (GLfloat s, GLfloat t)
{
jwzgles_glTexCoord4f (s, t, 0, 1);
}
void
jwzgles_glTexCoord1f (GLfloat s)