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
|
/**
* gltool.c
*
* Copyright (C) 2001 Sven Goethel
*
* GNU Library General Public License
* as published by the Free Software Foundation
*
* http://www.gnu.org/copyleft/lgpl.html
* General dynamical loading OpenGL (GL/GLU) support for:
*
*
* <OS - System> <#define> commentary
* -----------------------------------------------
* GNU/Linux, Unices/X11 _X11_ (loads glx also)
* Macinstosh OS9 _MAC_OS9_
* Macinstosh OSX _MAC_OSX_
* Win32 _WIN32_
*
*/
#include "gltool.h"
#include "gl-disp-var.hc"
#include "glu-disp-var.hc"
#ifdef _X11_
#include "glxtool.h"
#endif
#ifdef _WIN32_
#include "wgltool.h"
#endif
static int _glLibsLoaded = 0;
#ifdef _WIN32_
static HMODULE hDLL_OPENGL32 = 0;
static HMODULE hDLL_OPENGLU32 = 0;
#endif
#ifdef _X11_
static void *libHandleGLX=0;
static void *libHandleGL=0;
static void *libHandleGLU=0;
#endif
#ifdef _MAC_OS9_
Ptr glLibMainAddr = 0;
CFragConnectionID glLibConnectId = 0;
#endif
static int gl_begin_ctr;
const char * GLTOOL_USE_GLLIB = "GLTOOL_USE_GLLIB";
const char * GLTOOL_USE_GLULIB = "GLTOOL_USE_GLULIB";
void LIBAPIENTRY print_gl_error (const char *msg, const char *file, int line, GLenum errorcode)
{
if (errorcode != GL_NO_ERROR)
{
const char *errstr = (const char *) disp__gluErrorString (errorcode);
if (errstr != 0)
fprintf (stderr, "\n\n****\n**%s %s:%d>0x%X %s\n****\n", msg, file, line, errorcode, errstr);
else
fprintf (stderr, "\n\n****\n**%s %s:%d>0x%X <unknown>\n****\n", msg, file, line, errorcode);
}
}
#ifdef _WIN32_
void LIBAPIENTRY check_wgl_error (HWND wnd, const char *file, int line)
{
LPVOID lpMsgBuf;
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
0, GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) & lpMsgBuf, 0, 0); // Display the string.
fprintf (stderr, "\n\n****\n** %s:%d>%s\n****\n", file, line, lpMsgBuf);
// Free the buffer.
LocalFree (lpMsgBuf);
}
#endif
void LIBAPIENTRY check_gl_error (const char *file, int line)
{
print_gl_error("GLCHECK", file, line, disp__glGetError());
}
void LIBAPIENTRY __sglBegin(const char * file, int line, GLenum mode)
{
print_gl_error("GL-PreBEGIN-CHECK", file, line, disp__glGetError());
if(gl_begin_ctr!=0)
{
printf("\n\n****\n** GL-BEGIN-ERROR %s:%d> glBegin was called %d times (reset now)\n****\n",
file, line, gl_begin_ctr);
gl_begin_ctr=0;
} else
gl_begin_ctr++;
disp__glBegin(mode);
}
void LIBAPIENTRY __sglEnd(const char * file, int line)
{
if(gl_begin_ctr!=1)
{
printf("\n\n****\n** GL-END-ERROR %s:%d> glBegin was called %d times (reset now)\n****\n",
file, line, gl_begin_ctr);
gl_begin_ctr=1;
} else
gl_begin_ctr--;
disp__glEnd();
print_gl_error("GL-PostEND-CHECK", file, line, disp__glGetError());
}
/**
* call this function only if you
* are not within a glBegin/glEnd block,
* or you think your are not ;-)
*/
void LIBAPIENTRY checkGlBeginEndBalance(const char *file, int line)
{
if(gl_begin_ctr!=0)
{
printf("\n****\n** GL-BeginEnd-ERROR %s:%d> glBegin was called %d times\n****\n",
file, line, gl_begin_ctr);
}
print_gl_error("GL-BeginEnd-CHECK", file, line, disp__glGetError());
}
void LIBAPIENTRY showGlBeginEndBalance(const char *file, int line)
{
printf("\n****\n** GL-BeginEnd %s:%d> glBegin was called %d times\n****\n",
file, line, gl_begin_ctr);
}
int LIBAPIENTRY unloadGLLibrary ()
{
#ifdef _WIN32_
if(hDLL_OPENGL32!=NULL)
{
FreeLibrary(hDLL_OPENGL32);
hDLL_OPENGL32=NULL;
}
if(hDLL_OPENGLU32!=NULL)
{
FreeLibrary(hDLL_OPENGLU32);
hDLL_OPENGLU32=NULL;
}
#endif
#ifdef _X11_
if(libHandleGL!=NULL)
{
dlclose (libHandleGL);
libHandleGL = NULL;
}
if(libHandleGLU!=NULL)
{
dlclose (libHandleGLU);
libHandleGLU = NULL;
}
if(libHandleGLX!=NULL)
{
dlclose (libHandleGLX);
libHandleGLX = NULL;
}
#endif
#ifdef _MAC_OS9_
if (glLibConnectId!=NULL)
{
(void) CloseConnection(&glLibConnectId);
glLibConnectId=0;
}
glLibMainAddr = 0;
#endif
_glLibsLoaded = 0;
return 1;
}
int LIBAPIENTRY loadGLLibrary (const char * libGLName, const char * libGLUName,
int verbose)
{
const char *envGLName = NULL;
const char *envGLUName = NULL;
#ifdef _MAC_OS9_
Str255 errName;
OSErr returnError=fragNoErr;
#endif
if(_glLibsLoaded) return 1;
envGLName = getenv(GLTOOL_USE_GLLIB);
envGLUName = getenv(GLTOOL_USE_GLULIB);
if(envGLName!=NULL)
{
libGLName = envGLName;
if(verbose)
printf("GLTOOL: using env's GLTOOL_USE_GLLIB = %s\n", libGLName);
}
if(envGLUName!=NULL)
{
libGLUName = envGLUName;
if(verbose)
printf("GLTOOL: using env's GLTOOL_USE_GLULIB = %s\n", libGLUName);
}
#ifdef _WIN32_
if(hDLL_OPENGL32!=NULL) return 1;
hDLL_OPENGL32 = LoadLibrary (libGLName);
hDLL_OPENGLU32 = LoadLibrary (libGLUName);
if (hDLL_OPENGL32 == NULL)
{
printf ("GLERROR: cannot access OpenGL library %s\n", libGLName);
fflush (NULL);
return 0;
}
if (hDLL_OPENGLU32 == NULL)
{
printf ("GLERROR: cannot access GLU library %s\n", libGLUName);
fflush (NULL);
return 0;
}
#endif
#ifdef _X11_
if(libHandleGL!=NULL) return 1;
libHandleGL = dlopen (libGLName, RTLD_LAZY | RTLD_GLOBAL);
if (libHandleGL == NULL)
{
printf ("GLERROR: cannot access OpenGL library %s\n", libGLName);
fflush (NULL);
return 0;
}
libHandleGLU = dlopen (libGLUName, RTLD_LAZY | RTLD_GLOBAL);
if (libHandleGLU == NULL)
{
printf ("GLERROR: cannot access GLU library %s\n", libGLUName);
fflush (NULL);
return 0;
}
libHandleGLX = dlopen ("libglx.so", RTLD_LAZY | RTLD_GLOBAL);
if (libHandleGLX == NULL)
{
if(verbose)
printf ("GLINFO: cannot access GLX library libglx.so directly ...\n");
fflush (NULL);
}
#endif
#ifdef _MAC_OS9_
returnError =
GetSharedLibrary("\pOpenGLLibrary",
kPowerPCCFragArch,
kReferenceCFrag,
&glLibConnectId,
&glLibMainAddr,
errName);
if (returnError != fragNoErr)
{
printf ("GetSharedLibrary Err(%d): Ahhh! Didn't find LIBRARY !\n",
returnError);
return 0;
}
#endif
if(verbose)
{
#ifdef _MAC_OSX_
printf ("GLINFO: loadGLLibrary - no special code implemented !\n");
#else
printf ("GLINFO: loaded OpenGL library %s!\n", libGLName);
printf ("GLINFO: loaded GLU library %s!\n", libGLUName);
#endif
fflush (NULL);
}
_glLibsLoaded = 1;
return 1;
}
void * LIBAPIENTRY getGLProcAddressHelper
(const char * libGLName, const char * libGLUName,
const char *func, int *method, int verbose)
{
void * funcPtr = NULL;
int lmethod;
#ifdef _WIN32_
static int __firstAccess = 1;
if(!loadGLLibrary (libGLName, libGLUName, verbose))
return NULL;
if (disp__wglGetProcAddress == NULL && __firstAccess)
{
disp__wglGetProcAddress = ( PROC (CALLBACK *)(LPCSTR) )
GetProcAddress (hDLL_OPENGL32, "wglGetProcAddress");
if (disp__wglGetProcAddress != NULL && verbose )
{
printf ("GLINFO: found wglGetProcAddress in %s\n", libGLName);
fflush (NULL);
}
if (disp__wglGetProcAddress == NULL)
{
printf ("GLINFO: can't find wglGetProcAddress in %s\n", libGLName);
}
}
__firstAccess = 0;
if (disp__wglGetProcAddress != NULL)
funcPtr = disp__wglGetProcAddress (func);
if (funcPtr == NULL)
{
lmethod = 2;
funcPtr = GetProcAddress (hDLL_OPENGL32, func);
}
else
lmethod = 1;
if (funcPtr == NULL)
{
lmethod = 3;
funcPtr = GetProcAddress (hDLL_OPENGLU32, func);
}
#endif
#ifdef _X11_
/*
* void (*glXGetProcAddressARB(const GLubyte *procName))
*/
static int __firstAccess = 1;
if(!loadGLLibrary (libGLName, libGLUName, verbose))
return NULL;
if (disp__glXGetProcAddress == NULL && __firstAccess)
{
disp__glXGetProcAddress = dlsym (libHandleGL, "glXGetProcAddressARB");
if (disp__glXGetProcAddress != NULL && verbose)
{
printf ("GLINFO: found glXGetProcAddressARB in %s\n", libGLName);
fflush (NULL);
}
if (disp__glXGetProcAddress == NULL)
{
disp__glXGetProcAddress = dlsym (libHandleGL, "glXGetProcAddressEXT");
if (disp__glXGetProcAddress != NULL && verbose)
{
printf ("GLINFO: found glXGetProcAddressEXT in %s\n", libGLName);
fflush (NULL);
}
}
if (disp__glXGetProcAddress == NULL)
{
disp__glXGetProcAddress = dlsym (libHandleGL, "glXGetProcAddress");
if (disp__glXGetProcAddress != NULL && verbose)
{
printf ("GLINFO: found glXGetProcAddress in %s\n", libGLName);
fflush (NULL);
}
}
if (disp__glXGetProcAddress == NULL)
{
printf
("GLINFO: cannot find glXGetProcAddress* in OpenGL library %s\n", libGLName);
fflush (NULL);
if (libHandleGLX != NULL)
{
disp__glXGetProcAddress = dlsym (libHandleGLX, "glXGetProcAddressARB");
if (disp__glXGetProcAddress != NULL && verbose)
{
printf ("GLINFO: found glXGetProcAddressARB in libglx.so\n");
fflush (NULL);
}
if (disp__glXGetProcAddress == NULL)
{
disp__glXGetProcAddress = dlsym (libHandleGLX, "glXGetProcAddressEXT");
if (disp__glXGetProcAddress != NULL && verbose)
{
printf ("GLINFO: found glXGetProcAddressEXT in libglx.so\n");
fflush (NULL);
}
}
if (disp__glXGetProcAddress == NULL)
{
disp__glXGetProcAddress = dlsym (libHandleGLX, "glXGetProcAddress");
if (disp__glXGetProcAddress != NULL && verbose)
{
printf ("GLINFO: found glXGetProcAddress in libglx.so\n");
fflush (NULL);
}
}
if (disp__glXGetProcAddress == NULL)
{
printf ("GLINFO: cannot find glXGetProcAddress* in GLX library libglx.so\n");
fflush (NULL);
}
}
}
}
__firstAccess = 0;
if (disp__glXGetProcAddress != NULL)
funcPtr = disp__glXGetProcAddress (func);
if (funcPtr == NULL)
{
lmethod = 2;
funcPtr = dlsym (libHandleGL, func);
}
else
lmethod = 1;
if (funcPtr == NULL)
{
lmethod = 3;
funcPtr = dlsym (libHandleGLU, func);
}
#endif
#ifdef _MAC_OS9_
Str255 errName;
Str255 funcName;
CFragSymbolClass glLibSymClass = 0;
OSErr returnError=fragNoErr;
#ifndef NDEBUG
static int firstTime = 1;
#endif
if(!loadGLLibrary (libGLName, libGLUName, verbose))
return NULL;
c2pstrcpy ( funcName, func );
#ifndef NDEBUG
if(firstTime)
{
PrintSymbolNamesByConnection (glLibConnectId);
firstTime=0;
}
funcPtr = (void *)
SeekSymbolNamesByConnection (glLibConnectId, funcName);
#endif
if(funcPtr==NULL)
{
returnError =
FindSymbol (glLibConnectId, funcName,
&funcPtr, & glLibSymClass );
lmethod=2;
}
#ifndef NDEBUG
else lmethod=3;
#endif
if (returnError != fragNoErr)
{
printf ("GetSharedLibrary Err(%d): Ahhh! Didn't find SYMBOL: %s !\n",
returnError, func);
}
#endif
#ifdef _MAC_OSX_
char underscoreName[256];
strcpy(underscoreName, "_");
strcat(underscoreName, func);
if ( NSIsSymbolNameDefined(underscoreName) ) {
NSSymbol sym = NSLookupAndBindSymbol(underscoreName);
funcPtr = (void *)NSAddressOfSymbol(sym);
}
#endif
if (funcPtr == NULL)
{
if (verbose)
{
printf ("GLINFO: %s (%d): not implemented !\n", func, lmethod);
fflush (NULL);
}
}
else if (verbose)
{
printf ("GLINFO: %s (%d): loaded !\n", func, lmethod);
fflush (NULL);
}
if (method != NULL)
*method = lmethod;
return funcPtr;
}
void LIBAPIENTRY fetch_GL_FUNCS (const char * libGLName,
const char * libGLUName, int force, int reload,
int verbose)
{
static int _firstRun = 1;
if (!reload) {
if(force) {
unloadGLLibrary();
_firstRun = 1;
}
if(!_firstRun)
return;
if(!loadGLLibrary (libGLName, libGLUName, verbose))
return;
}
#define GET_GL_PROCADDRESS(a) getGLProcAddressHelper (libGLName, libGLUName, (a), NULL, verbose);
#include "gl-disp-fetch.hc"
#include "glu-disp-fetch.hc"
_firstRun=0;
#ifdef _X11_
fetch_GLX_FUNCS (libGLName, libGLUName, force, reload, verbose);
#endif
#ifdef _WIN32_
fetch_WGL_FUNCS (libGLName, libGLUName, force, reload, verbose);
#endif
}
#ifdef _MAC_OS9_
#ifndef NDEBUG
static void PrintSymbolNamesByConnection (CFragConnectionID myConnID)
{
long myIndex;
long myCount; /*number of exported symbols in fragment*/
OSErr myErr;
Str255 myName; /*symbol name*/
Ptr myAddr; /*symbol address*/
CFragSymbolClass myClass; /*symbol class*/
static char buffer[256];
myErr = CountSymbols(myConnID, &myCount);
if (!myErr)
for (myIndex = 1; myIndex <= myCount; myIndex++)
{
myErr = GetIndSymbol(myConnID, myIndex, myName,
&myAddr, &myClass);
if (!myErr)
{
p2cstrcpy (buffer, myName);
printf("%d/%d: class %d - name %s\n",
myIndex, myCount, myClass, buffer);
}
}
}
static Ptr SeekSymbolNamesByConnection (CFragConnectionID myConnID, Str255 name)
{
long myIndex;
long myCount; /*number of exported symbols in fragment*/
OSErr myErr;
Str255 myName; /*symbol name*/
Ptr myAddr; /*symbol address*/
CFragSymbolClass myClass; /*symbol class*/
myErr = CountSymbols(myConnID, &myCount);
if (!myErr)
for (myIndex = 1; myIndex <= myCount; myIndex++)
{
myErr = GetIndSymbol(myConnID, myIndex, myName,
&myAddr, &myClass);
if (!myErr && EqualString (myName, name, true, true) == true )
return myAddr;
}
}
#endif /* ifndef NDEBUG */
#endif /* ifdef _MAC_OS9_ */
|