-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgldl.py
2478 lines (1932 loc) · 75.6 KB
/
gldl.py
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
#!/usr/bin/env python
import re
import urllib2
import os
print "GLDL gen script 1.0"
# Make folder hierarchy
# GL/ -- GL3/ -- gl3.h
# gldl.py gldl.h
# gldl.c
if not os.path.exists( "GL/GL3" ) :
os.makedirs( "GL/GL3" )
# Download header from opengl.org
if not os.path.exists( "GL/GL3/gl3.h" ) :
print "Downloading gl3.h from opengl.org to GL/GL3..."
gl3_url = urllib2.urlopen( "http://www.opengl.org/registry/api/gl3.h" )
gl3_src = open( "GL/GL3/gl3.h", "wb" )
gl3_src.writelines( gl3_url.readlines() )
gl3_src.close()
else :
print "OpenGL header gl3.h exists in GL/GL3. Reusing..."
print "Parsing OpenGL header..."
# Open GL3 header
gl3_src = open( "GL/GL3/gl3.h", "r" )
# Get all GL procedures
return_types = []
names = []
parameters = []
parameters_n = []
max_name_len = 0
procs_re = re.compile( r"^GLAPI.+APIENTRY gl.+(.*);$" )
cstre = re.compile( r"^#define GL_[A-Z_]+ +[0-9A-Fx]+$" )
csts = 0
for line in gl3_src :
match_line = procs_re.match( line )
if( match_line ) :
line = match_line.group(0)[6:]
line = line.replace( " APIENTRY ", " " )
name_pos = line.find( "gl" )
return_types.append( line[:name_pos-1] )
name, sig = line[name_pos:].split( None, 1 )
names.append( name );
if( len(name) > max_name_len ) :
max_name_len = len(name)
# Check if void parameter
if( sig[1:-2] == "void" ) :
parameters.append( "" )
parameters_n.append( 0 )
else :
parameters.append( sig[1:-2] )
parameters_n.append( len(sig[1:-2].split( "," ) ) )
elif( cstre.match( line) ) :
csts = csts + 1
# Get alphabetically sorted name array (for faster search in C program)
sorted_names = sorted( names )
# constants :
print "fonctions : " + str(len(names))
print "constants : " + str(csts)
print "Generating GL/gldl.h header file..."
# Write GLDL header
gldl_h = open( "GL/gldl.h", "wb" )
gldl_h.write( r'''
#ifndef __GLDL_H
#define __GLDL_H
// GL Core Profile Header
#include "GL3/gl3.h"
// Prevents from loading basic gl.h
#ifndef __gl_h_
#define __gl_h_
#endif
// C Library
#ifdef __cplusplus
extern "C" {
#endif
// GLDL API functions
int gldlInit();
int gldlIsSupported( unsigned int major, unsigned int minor );
void gldlBeginTrace( unsigned int trace_n );
void gldlEndTrace( unsigned int trace_n );
void gldlBreak_impl( const char *file, int line );
#define gldlBreak() gldlBreak_impl( __FILE__, __LINE__ )
// Debug version of GL functions
// Parameters :
// - GL original parameters
// - String representing their names (equal number)
// - String representing the file where the call has been recorded
// - Int representing the line where the call has been recorded
''')
# Write GLDL modified(for debug) GL funcs
for i in range( len(names) ) :
if( parameters_n[i] == 0 ) :
gldl_h.write( return_types[i] + " gldl" + names[i][2:] + " ( const char *, int );\n" )
else :
gldl_h.write( return_types[i] + " gldl" + names[i][2:] + " ( " + parameters[i] + ", " )
for j in range( parameters_n[i] - 1 ) :
gldl_h.write( "const char *, " )
gldl_h.write( "const char *, const char *, int );\n" )
gldl_h.write( "\n\n" )
# Write GL macro functions that the user will call
for i in range( len(names) ) :
if( parameters_n[i] == 0 ) :
gldl_h.write( "#define " + names[i] + "( ) gldl" + names[i][2:] + "( __FILE__, __LINE__ )\n" )
else :
gldl_h.write( "#define " + names[i] + "( " )
for j in range( parameters_n[i] - 1 ) :
gldl_h.write( "arg" + str(j) + ", " )
gldl_h.write( "arg" + str(parameters_n[i]-1) + " ) gldl" + names[i][2:] + "( " )
for j in range( parameters_n[i] - 1 ) :
gldl_h.write( "(arg" + str(j) + "), " )
gldl_h.write( "(arg" + str(parameters_n[i]-1) + "), " )
for j in range( parameters_n[i] - 1 ) :
gldl_h.write( "#arg" + str(j) + ", " )
gldl_h.write( "#arg" + str(parameters_n[i]-1) + ", __FILE__, __LINE__ )\n" )
gldl_h.write( r'''
#ifdef __cplusplus
}
#endif
#endif //__GLDL_H
''')
print "Generating GL/gldl.c source file..."
# Write GLDL src
gldl_c = open( "GL/gldl.c", "wb" )
gldl_c.write( r'''
#include "gldl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//////////////////////////////////////////
// Jump to line 1145 for GLDL API functions
//////////////////////////////////////////
// GL FUNCTION POINTERS (GL implementation)
''')
# Write all GL function pointers
for i in range( len(names) ) :
gldl_c.write( "PFN" + names[i].upper() + "PROC gldl" + names[i][2:] + "_impl;\n" )
gldl_c.write( r'''
// GL function number
#define GLDL_FUNC_N ''' + str(len(names)) + '''
// String array of all GL function names
char *gl_functions[GLDL_FUNC_N] = {
''')
for name in sorted_names :
gldl_c.write( "\t\"" + name + "\",\n" )
gldl_c.write( r'''};
// WINDOWS
#if defined(_WIN32) || defined(WIN32)
# define WIN32_LEAN_AND_MEAN 1
# include <windows.h>
// OpenGL shared library handle
static HMODULE libgl;
/// Load dynamic library opengl32.dll and store its handle
static void OpenLib() {
libgl = LoadLibraryA( "opengl32.dll" );
}
/// Close the libgl handle
static void CloseLib() {
FreeLibrary( libgl );
}
/// Returns the GL Procedure corresponding to the given procedure name
static void *GetProc( const char *pProcName ) {
void *proc = NULL;
proc = wglGetProcAddress( pProcName );
if( !proc )
proc = GetProcAddress( libgl, pProcName );
return proc;
}
// Win32API Texture window stuff
static WNDCLASSEX gldl_wc;
static HWND gldl_win;
// Client Window stuff
static HGLRC cl_ctx;
static HDC cl_win;
void SwapTextureWindow() {
SwapBuffers( GetDC( gldl_win ) );
}
LRESULT CALLBACK windowProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) {
// get current hwnd
gldl_win = hwnd;
switch( msg ) {
case WM_CREATE :
// init GL
GLuint PixelFormat;
HDC hdc = GetDC( hwnd );
PIXELFORMATDESCRIPTOR pfd;
memset( &pfd, 0, sizeof(PIXELFORMATDESCRIPTOR) );
pfd.dwFlags |= PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.cColorBits = 32;
if( !( PixelFormat = ChoosePixelFormat( hdc, &pfd ) ) ) {
printf( "Can't select pixel format\n" );
return 0;
}
if( SetPixelFormat( hdc, PixelFormat, &pfd ) == FALSE ) {
printf( "Can't set pixel format\n" );
return 0;
}
wglMakeCurrent( hdc, cl_ctx );
ShowWindow( hwnd, SW_SHOWDEFAULT );
UpdateWindow( hwnd );
break;
case WM_SIZE :
glViewport( 0, 0, LOWORD(lParam), HIWORD(wParam) );
break;
case WM_PAINT :
SwapTextureWindow();
break;
default :
return DefWindowProc( hwnd, msg, wparam, lparam );
}
return 0;
}
int InitTextureWindow( int width, int height ) {
cl_ctx = wglGetCurrentContext();
cl_win = wglGetCurrentDC();
memset( &wc, 0, sizeof(wc) );
wc.lpszClassName = L"gl32wc";
wc.cbSize = sizeof(WNDCLASSEX);
wc.style ) CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = windowProc;
wc.hInstance = HINST;
wc.hCursor = LoadCursor( 0, IDC_ARROW );
if( !RegisterClassEx( &wc ) ) {
printf( "Can't register window class\n" );
return 0;
}
if( !CreateWindowEx( 0, L"gl32wc",
L"GLDL",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, width, height, 0, 0, HINST, 0) ) {
printf( "Cannot open window\n" );
return 0;
}
return 1;
}
int TextureWindowEvents() {
static MSG msg;
PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
if( msg.message == WM_KEYDOWN )
return 0;
else if( msg.message = WM_CLOSE )
return 0;
else {
DispatchMessage( &msg );
return 1;
}
}
void DestroyTextureWindow() {
HDC hdc = GetDC( gldl_win );
wglMakeCurrent( cl_win, cl_ctx );
if( hdc ) {
ReleaseDC( gldl_win, hdc );
hdc = 0;
}
DestroyWindow( gldl_win );
PostQuitMessage(0);
UnregisterClass( L"gl32wc", HINST );
}
// UNIX
#else
#include <dlfcn.h>
#include <GL/glx.h>
#include <signal.h>
// OpenGL shared library handle
static void *libgl;
/// Load dynamic library libGL.so and store its handle
static void OpenLib() {
libgl = dlopen( "libGL.so.1", RTLD_GLOBAL | RTLD_LAZY );
}
/// Close the libGL handle
static void CloseLib() {
dlclose( libgl );
}
/// Returns the GL Procedure corresponding to the given procedure name
static void *GetProc( const char *pProcName ) {
void *proc = NULL;
proc = glXGetProcAddress( (const GLubyte*)pProcName );
if( !proc )
proc = dlsym( libgl, pProcName );
return proc;
}
// X11 Texture window stuff
static Display *gldl_dpy = NULL;
static Window gldl_win;
static Atom gldl_closeEvent;
// Client Window stuff
static Display *cl_dpy;
static GLXDrawable cl_win;
static GLXContext cl_ctx;
int InitTextureWindow( int width, int height ) {
Window root;
GLint attr[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo *vi;
XSetWindowAttributes swa;
XSizeHints *sh;
cl_dpy = glXGetCurrentDisplay();
cl_ctx = glXGetCurrentContext();
cl_win = glXGetCurrentDrawable();
gldl_dpy = XOpenDisplay( NULL );
if( !gldl_dpy ) {
printf( "Cannot connect to X Server\n" );
return 0;
}
root = DefaultRootWindow( gldl_dpy );
vi = glXChooseVisual( gldl_dpy, 0, attr );
if( !vi ) {
printf( "No matching visual for RGBA:24 with double buffer.\n" );
XCloseDisplay( gldl_dpy );
return 0;
}
swa.colormap = XCreateColormap( gldl_dpy, root, vi->visual, AllocNone );
swa.event_mask = ExposureMask | KeyReleaseMask;
gldl_win = XCreateWindow( gldl_dpy, root, 0, 0, width, height, 0, vi->depth,
InputOutput, vi->visual, CWColormap | CWEventMask, &swa );
XFree( vi );
// fixed aspect ratio (texture aspect ratio)
sh = XAllocSizeHints();
sh->flags = PAspect;
sh->min_aspect.x = width;
sh->max_aspect.x = sh->min_aspect.x;
sh->min_aspect.y = height;
sh->max_aspect.y = sh->min_aspect.y;
XSetWMNormalHints( gldl_dpy, gldl_win, sh );
XFree( sh );
// handle closing signals
gldl_closeEvent = XInternAtom( gldl_dpy, "WM_DELETE_WINDOW", False );
XSetWMProtocols( gldl_dpy, gldl_win, &gldl_closeEvent, 1 );
XStoreName( gldl_dpy, gldl_win, "GLDL" );
// Open created window
XMapWindow( gldl_dpy, gldl_win );
return glXMakeCurrent( gldl_dpy, gldl_win, cl_ctx );
}
int TextureWindowEvents() {
static XEvent xev;
static KeySym key;
XNextEvent( gldl_dpy, &xev );
// check for keypress ( ESC key kills window )
if( xev.type == KeyRelease ) {
key = XKeycodeToKeysym( gldl_dpy, xev.xkey.keycode, 0 );
// only check for escape key
if( key == XK_Escape )
return 0;
}
// check for Closing signal
else if( xev.type == ClientMessage ) {
if( (Atom) xev.xclient.data.l[0] == gldl_closeEvent ) {
return 0;
}
}
// check for resizing
else if( xev.type == Expose ) {
// get new window size
XWindowAttributes wa;
XGetWindowAttributes( gldl_dpy, gldl_win, &wa );
glViewport( 0, 0, wa.width, wa.height );
}
return 1;
}
void SwapTextureWindow() {
glXSwapBuffers( gldl_dpy, gldl_win );
}
void DestroyTextureWindow() {
glXMakeCurrent( cl_dpy, cl_win, cl_ctx );
XUnmapWindow( gldl_dpy, gldl_win );
XDestroyWindow( gldl_dpy, gldl_win );
XCloseDisplay( gldl_dpy );
}
#endif
// ###################################################################
// API FUNCTIONS
static int gldl_init = 0; // Assure that gldl has been init
static int break_functions[GLDL_FUNC_N]; // Breakpoints storing array
static int break_next = 0; // Break on next function ?
static char debug_break[10]; // Used to display break cause (breakpoint nb or a 'n' break)
static int gldlBreak_active = 1; // Is the gldlBreak() function active?
// GL function call traces
// First trace is the init trace (trace until first glClear())
// Five traces after that are custom traces (activated by gldlBeginTrace(n))
#define TRACE_N 6
static struct s_ct {
FILE *f;
int started;
} gldl_traces[TRACE_N];
// Buffers (VBO) managment
struct gldl_buffer {
GLint id;
GLuint size;
GLfloat *data;
int next_free;
};
static struct {
struct gldl_buffer *arr;
unsigned int size;
unsigned int count;
unsigned int first_free;
int bound_array_b;
int bound_elem_array_b;
} gldl_buffers;
// Shaders and Shader Programs managment
struct gldl_shader {
GLuint id; // Shader GL id
GLenum type; // GL_{VERTEX,FRAGMENT}_SHADER
char *src; // Shader source
GLuint size; // Shader source size
int next_free;
};
struct gldl_program {
GLuint id; // Program GL id
GLuint vs, fs; // Program linked vertex and fragment shaders ids
int next_free;
};
static struct {
struct gldl_shader *arr;
unsigned int size;
unsigned int count;
unsigned int first_free;
} gldl_shaders;
static struct {
struct gldl_program *arr;
unsigned int size;
unsigned int count;
unsigned int first_free;
unsigned int bound_program;
} gldl_programs;
// Texture managment
struct gldl_texture {
GLuint id;
GLuint width, height;
int next_free;
};
static struct {
struct gldl_texture *arr;
unsigned int size;
unsigned int count;
unsigned int first_free;
unsigned int bound_textures[8];
unsigned int current_unit;
} gldl_textures;
// States managment
static struct {
int depth_test,
blend,
blendfunc_src,
blendfunc_dest,
face_culling,
culled_face,
bound_vao;
int mag_filter,
min_filter,
wrap_s,
wrap_t;
} gldl_states;
// OpenGL max available version
static struct {
GLint major,
minor;
} gl_version;
// GLDL Buffer functions
static void InitBufferArray();
static void DeleteBufferArray();
static void AddBuffers( GLsizei n, GLuint *ids );
static void DeleteBuffers( GLsizei n, const GLuint *ids );
static void BindBuffer( GLuint id, int elem_array );
static void FillBuffer( const void* data, GLsizei size, unsigned int offset, int elem_array );
static void ListBuffers();
static void PrintBuffer( int id, unsigned int type_size, unsigned int elem_size );
// GLDL Program/Shader functions
static void InitShaderArray();
static void DeleteShaderArray();
static void AddShader( GLuint id, GLenum type );
static void SetShaderSource( GLuint id, const char *src );
static void AddProgram( GLuint id );
static void DeleteProgram( GLuint id );
static void AttachShader( GLuint prog_id, GLuint shader_id );
static void DetachShader( GLuint prog_id, GLuint shader_id );
static void BindProgram( GLuint id );
static void ListPrograms();
static void PrintProgram( int id );
static void ListShaders();
static void PrintShader( int id );
// GLDL Texture functions
static void InitTextureArray();
static void DeleteTextureArray();
static void AddTextures( GLsizei n, GLuint *ids );
static void DeleteTextures( GLsizei n, const GLuint *ids );
static void SetTextureSize( GLuint id, GLuint width, GLuint height );
static void BindTexture( GLuint id );
static void SetTextureUnit( GLuint unit );
static void ShowTexture( GLuint id, int reverse );
static void ListTextures();
static void InitGLStates();
static int GetGLVersion();
static void DebugTest( int func_index );
static void DebugFunction();
static void LoadProcs();
// Public functions
int gldlInit() {
OpenLib();
LoadProcs();
CloseLib();
gldl_init = GetGLVersion();
if( gldl_init ) {
memset( gldl_traces, 0, TRACE_N * sizeof(struct s_ct) );
gldl_traces[0].f = fopen( "trace_init.log", "w" );
gldl_traces[0].started = 1;
gldl_buffers.size = 0;
gldl_programs.size = 0;
gldl_shaders.size = 0;
InitGLStates();
memset( break_functions, -1, GLDL_FUNC_N * sizeof(int) );
printf( "GLDL 1.0\n" );
DebugFunction();
}
return gldl_init;
}
int gldlIsSupported( unsigned int major, unsigned int minor ) {
if( major < 3 ) return 0;
if( major < gl_version.major ) return 1;
return ( major <= gl_version.major && minor <= gl_version.minor );
}
void gldlBeginTrace( unsigned int trace_n ) {
if( !trace_n || trace_n >= TRACE_N ) return;
if( !gldl_traces[trace_n].f ) {
char filename[16];
sprintf( filename, "trace%d.log", trace_n );
gldl_traces[trace_n].f = fopen( filename, "w" );
}
gldl_traces[trace_n].started = 1;
}
void gldlEndTrace( unsigned int trace_n ) {
if( !trace_n || trace_n >= TRACE_N ) return;
gldl_traces[trace_n].started = 0;
}
void gldlBreak_impl( const char *file, int line ) {
if( gldlBreak_active ) {
printf( "Breapoint on gldlBreak() at %s:%d\nEnter the 'tb' command to toggle the gldlBreak function.\n", file, line );
DebugFunction();
}
}
// Private functions
static void InitTextureArray() {
// initialize with 20 elem at first
gldl_textures.size = 20;
gldl_textures.count = 0;
gldl_textures.first_free = 0;
memset( gldl_textures.bound_textures, 0, 8 * sizeof(int) );
gldl_textures.current_unit = 0;
gldl_textures.arr = calloc( gldl_textures.size, sizeof(struct gldl_texture) );
int i;
for( i = 0; i < gldl_textures.size; ++i ) {
gldl_textures.arr[i].id = -1;
gldl_textures.arr[i].width = 0;
gldl_textures.arr[i].height = 0;
gldl_textures.arr[i].next_free = i+1;
}
gldl_textures.arr[gldl_textures.size-1].next_free = -1;
}
static void DeleteTextureArray() {
free( gldl_textures.arr );
gldl_textures.size = 0;
}
static void AddTextures( GLsizei n, GLuint *ids ) {
int i, new_index;
int index = gldl_textures.first_free;
int need_realloc = 0;
// init arrays if not yet done
if( !gldl_textures.size )
InitTextureArray();
// realloc array if full (with 1.7x policy)
for( new_index = n-1; new_index >= 0; --new_index ) {
if( -1 == index ) {
need_realloc = 1;
break;
}
index = gldl_textures.arr[index].next_free;
}
index = gldl_textures.first_free;
if( need_realloc ) {
gldl_textures.arr[gldl_textures.size-1].next_free = gldl_textures.size;
gldl_textures.size *= 1.7;
gldl_textures.arr = realloc( gldl_textures.arr, sizeof(struct gldl_texture) * gldl_textures.size );
for( i = gldl_textures.count; i < gldl_textures.size; ++i ) {
gldl_textures.arr[i].id = -1;
gldl_textures.arr[i].width = 0;
gldl_textures.arr[i].height = 0;
gldl_textures.arr[i].next_free = i+1;
}
gldl_textures.arr[gldl_textures.size-1].next_free = -1;
// change first_free only if new_index is far
if( new_index == 0 )
gldl_textures.first_free = index = gldl_textures.count;
}
// add new textures id
for( i = 0; i < n; ++i ) {
gldl_textures.arr[index].id = ids[i];
gldl_textures.count++;
gldl_textures.first_free = index = gldl_textures.arr[index].next_free;
}
}
static void DeleteTextures( GLsizei n, const GLuint *ids ) {
int cpt;
int i, j;
int tmp;
for( cpt = 0; cpt < n; ++cpt ) {
for( i = 0; i < gldl_textures.size; ++i ) {
// if nothing more to see, return
if( gldl_textures.arr[i].id == -1 )
return;
if( gldl_textures.arr[i].id == ids[cpt] ) {
// delete textures memory
gldl_textures.arr[i].id = 0;
// reorganise array linking
tmp = gldl_textures.first_free;
gldl_textures.first_free = gldl_textures.arr[i].next_free;
gldl_textures.arr[i].next_free = tmp;
// unbind this texture if bound
for( j = 0; j < 8; ++j )
if( gldl_textures.arr[i].id == gldl_textures.bound_textures[j] )
gldl_textures.bound_textures[j] = 0;
gldl_textures.count--;
break;
}
}
}
// If no more textures, destroy array
if( !gldl_textures.count )
DeleteTextureArray();
}
static void SetTextureSize( GLuint id, GLuint width, GLuint height ) {
int i;
for( i = 0; i < gldl_textures.size; ++i ) {
if( gldl_textures.arr[i].id == -1 )
return;
if( gldl_textures.arr[i].id == id ) {
gldl_textures.arr[i].width = width;
gldl_textures.arr[i].height = height;
break;
}
}
}
static void BindTexture( GLuint id ) {
int i;
for( i = 0; i < gldl_textures.size; ++i ) {
if( gldl_textures.arr[i].id == -1 )
return;
if( gldl_textures.arr[i].id == id ) {
gldl_textures.bound_textures[gldl_textures.current_unit] = id;
break;
}
}
}
static void SetTextureUnit( GLuint unit ) {
GLuint real_unit = unit - GL_TEXTURE0;
if( real_unit < 8 )
gldl_textures.current_unit = real_unit;
}
static void ListTextures() {
int tex_cpt = 0;
int i;
for( i = 0; i < gldl_textures.size; ++i ) {
if( gldl_textures.arr[i].id == -1 )
break;
if( gldl_textures.arr[i].id ) {
if( !tex_cpt )
printf( "List of existing textures :\n" );
printf( "%d\n", gldl_textures.arr[i].id );
tex_cpt++;
}
}
if( !tex_cpt )
printf( "No GL textures.\n" );
}
void ShowTexture( GLuint id, int reverse ) {
int tex_index = -1;
int i;
int tex_w, tex_h;
// check if given id is
for( i = 0; i < gldl_textures.size; ++i ) {
if( gldl_textures.arr[i].id == -1 )
break;
if( gldl_textures.arr[i].id == id ) {
if( gldl_textures.arr[i].width != 0 && gldl_textures.arr[i].height != 0 )
tex_index = i;
break;
}
}
if( tex_index < 0 ) {
printf( "This texture does not exist.\n" );
return;
}
tex_w = gldl_textures.arr[tex_index].width;
tex_h = gldl_textures.arr[tex_index].height;
printf( "Displaying texture %d ( w = %d, h = %d )...\n", id, tex_w, tex_h );
// limit popup window size to be less than 800x600
// the user will be able to rescale it after pop
while( tex_w > 800 )
tex_w /= 2;
while( tex_h > 600 )
tex_h /= 2;
// Init platform window to draw on
if( !InitTextureWindow( tex_w, tex_h ) )
return;
// shader for rendering
static const GLchar *vs_src = "\
#version 150 \n\
in vec2 inPosition; \n\
out vec2 fsTexcoord; \n\
uniform int reverse; \n\
\n\
void main() { \n\
fsTexcoord = inPosition * vec2(0.5) + vec2(0.5); \n\
if( reverse > 0 ) fsTexcoord.y = -fsTexcoord.y; \n\
gl_Position = vec4( inPosition, 0, 1 ); \n\
} ";
static const GLchar *fs_src = "\
#version 150 \n\
in vec2 fsTexcoord; \n\
out vec4 outColor; \n\
uniform sampler2D tex; \n\
\n\
void main() { \n\
outColor = texture( tex, fsTexcoord ); \n\
} ";
int shader_ok = 0;
int prog = gldlCreateProgram_impl();
int vs = gldlCreateShader_impl( GL_VERTEX_SHADER );
int fs = gldlCreateShader_impl( GL_FRAGMENT_SHADER );
gldlShaderSource_impl( vs, 1, &vs_src, NULL );
gldlCompileShader_impl( vs );
gldlGetShaderiv_impl( vs, GL_COMPILE_STATUS, &shader_ok );
if( !shader_ok ) {
printf( "Vertex Shader compilation error\n" );
GLint log_length;
char *log;