00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <vrj/Draw/OGL/GlExtensionLoader.h>
00034 #include <vpr/vprConfig.h>
00035 #include <vpr/Util/Debug.h>
00036
00037 #include <GL/gl.h>
00038
00039 #if !defined(WIN32) && !defined(VPR_OS_Darwin)
00040 #include <GL/glx.h>
00041 #endif
00042
00043 #if defined(VPR_OS_Darwin)
00044 #include <mach-o/dyld.h>
00045 #elif !defined(WIN32)
00046 #include <dlfcn.h>
00047 #endif
00048
00049
00050 namespace vrj
00051 {
00052
00053 GlExtensionLoader::VoidExtFunc GlExtensionLoader::getFunctionByName(const char* name)
00054 {
00055 VoidExtFunc found_func(NULL);
00056
00057 #if defined(VPR_OS_Darwin)
00058 if (NSIsSymbolNameDefined(name))
00059 {
00060 NSSymbol symbol = NSLookupAndBindSymbol(name);
00061 if (0 != symbol)
00062 {
00063 found_func = VoidExtFunc(NSAddressOfSymbol(symbol));
00064 }
00065 }
00066 #elif defined(WIN32)
00067
00068 found_func = (void(__cdecl*)(void)) wglGetProcAddress(name);
00069
00070 #elif defined(VPR_OS_IRIX) || defined(VPR_OS_HPUX) || \
00071 defined(VPR_OS_Linux) || defined(VPR_OS_FreeBSD) || defined(VPR_OS_Solaris)
00072
00073
00074 static void (*(*__GetProcAddress)(const GLubyte *))(void) = NULL;
00075
00076 static void* lib_handle(NULL);
00077
00078 if (lib_handle == NULL)
00079 {
00080 lib_handle = dlopen(NULL, RTLD_NOW | RTLD_LOCAL);
00081
00082 if (!lib_handle)
00083 {
00084 vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL) << "Error in dlopen:" << dlerror() << vprDEBUG_FLUSH;
00085 }
00086 }
00087
00088 if (__GetProcAddress == NULL)
00089 {
00090 __GetProcAddress = (void (*(*)(const GLubyte*))()) dlsym(lib_handle, "glXGetProcAddressARB");
00091
00092 if (__GetProcAddress == NULL)
00093 {
00094 __GetProcAddress = (void (*(*)(const GLubyte*))()) dlsym(lib_handle, "glXGetProcAddress");
00095
00096 if (__GetProcAddress == NULL)
00097 {
00098 vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL) << "WARNING: Neither glXGetProcAddress nor "
00099 "glXGetProcAddressARB found! Disabling all "
00100 " extensions lookups.\n" << vprDEBUG_FLUSH;
00101 }
00102 else
00103 {
00104 vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL) << "Using glXGetProcAddress for GL extension handling.\n" << vprDEBUG_FLUSH;
00105 }
00106 }
00107 else
00108 {
00109 vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL) << "Using glXGetProcAddressARB for GL extension handling.\n" << vprDEBUG_FLUSH;
00110 }
00111 }
00112
00113 if (__GetProcAddress != NULL)
00114 {
00115 found_func = reinterpret_cast<VoidExtFunc>(__GetProcAddress((const GLubyte*)name));
00116 }
00117 else
00118 {
00119 found_func = (VoidExtFunc)(dlsym(lib_handle, name));
00120 }
00121
00122 #else
00123 vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
00124 << "Warnining: Missing implementation for extension registration on this platform.\n"
00125 << "OpenGL extensions will not be available to VR Juggler.\n" << vprDEBUG_FLUSH;
00126 #endif
00127
00128 if (found_func == NULL)
00129 {
00130 vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL) << "GlExtensionLoader:: Couldn't find function " << name << std::endl << vprDEBUG_FLUSH;
00131 }
00132 else
00133 {
00134 vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL) << "GlExtensionLoader:: Found function " << name << std::endl << vprDEBUG_FLUSH;
00135 }
00136
00137 return found_func;
00138 }
00139
00140 }
00141