importgl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* San Angeles Observation OpenGL ES version example
  2. * Copyright 2004-2005 Jetro Lauha
  3. * All rights reserved.
  4. * Web: http://iki.fi/jetro/
  5. *
  6. * This source is free software; you can redistribute it and/or
  7. * modify it under the terms of EITHER:
  8. * (1) The GNU Lesser General Public License as published by the Free
  9. * Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version. The text of the GNU Lesser
  11. * General Public License is included with this source in the
  12. * file LICENSE-LGPL.txt.
  13. * (2) The BSD-style license that is included with this source in
  14. * the file LICENSE-BSD.txt.
  15. *
  16. * This source is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  19. * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
  20. *
  21. * $Id: importgl.c,v 1.4 2005/02/08 18:42:55 tonic Exp $
  22. * $Revision: 1.4 $
  23. */
  24. #undef WIN32
  25. #undef LINUX
  26. #ifdef _MSC_VER
  27. // Desktop or mobile Win32 environment:
  28. #define WIN32
  29. #else
  30. // Linux environment:
  31. #define LINUX
  32. #endif
  33. #if defined(WIN32)
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. #include <tchar.h>
  37. static HMODULE sGLESDLL = NULL;
  38. #endif // WIN32
  39. #ifdef LINUX
  40. #include <stdlib.h>
  41. #include <dlfcn.h>
  42. static void *sGLESSO = NULL;
  43. #endif // LINUX
  44. #define IMPORTGL_NO_FNPTR_DEFS
  45. #define IMPORTGL_API
  46. #define IMPORTGL_FNPTRINIT = NULL
  47. #include "importgl.h"
  48. /* Imports function pointers to selected function calls in OpenGL ES Common
  49. * or Common Lite profile DLL or shared object. The function pointers are
  50. * stored as global symbols with equivalent function name but prefixed with
  51. * "funcPtr_". Standard gl/egl calls are redirected to the function pointers
  52. * with preprocessor macros (see importgl.h).
  53. */
  54. int importGLInit()
  55. {
  56. int result = 1;
  57. #undef IMPORT_FUNC
  58. #ifdef WIN32
  59. sGLESDLL = LoadLibrary(_T("libGLES_CM.dll"));
  60. if (sGLESDLL == NULL)
  61. sGLESDLL = LoadLibrary(_T("libGLES_CL.dll"));
  62. if (sGLESDLL == NULL)
  63. return 0; // Cannot find OpenGL ES Common or Common Lite DLL.
  64. /* The following fetches address to each egl & gl function call
  65. * and stores it to the related function pointer. Casting through
  66. * void * results in warnings with VC warning level 4, which
  67. * could be fixed by casting to the true type for each fetch.
  68. */
  69. #define IMPORT_FUNC(funcName) do { \
  70. void *procAddress = (void *)GetProcAddress(sGLESDLL, _T(#funcName)); \
  71. if (procAddress == NULL) result = 0; \
  72. *((void **)&FNPTR(funcName)) = procAddress; } while (0)
  73. #endif // WIN32
  74. #ifdef LINUX
  75. sGLESSO = dlopen("libGLES_CM.so", RTLD_NOW);
  76. if (sGLESSO == NULL)
  77. sGLESSO = dlopen("libGLES_CL.so", RTLD_NOW);
  78. if (sGLESSO == NULL)
  79. return 0; // Cannot find OpenGL ES Common or Common Lite SO.
  80. #define IMPORT_FUNC(funcName) do { \
  81. void *procAddress = (void *)dlsym(sGLESSO, #funcName); \
  82. if (procAddress == NULL) result = 0; \
  83. *((void **)&FNPTR(funcName)) = procAddress; } while (0)
  84. #endif // LINUX
  85. IMPORT_FUNC(eglChooseConfig);
  86. IMPORT_FUNC(eglCreateContext);
  87. IMPORT_FUNC(eglCreateWindowSurface);
  88. IMPORT_FUNC(eglDestroyContext);
  89. IMPORT_FUNC(eglDestroySurface);
  90. IMPORT_FUNC(eglGetConfigAttrib);
  91. IMPORT_FUNC(eglGetConfigs);
  92. IMPORT_FUNC(eglGetDisplay);
  93. IMPORT_FUNC(eglGetError);
  94. IMPORT_FUNC(eglInitialize);
  95. IMPORT_FUNC(eglMakeCurrent);
  96. IMPORT_FUNC(eglSwapBuffers);
  97. IMPORT_FUNC(eglTerminate);
  98. IMPORT_FUNC(glBlendFunc);
  99. IMPORT_FUNC(glClear);
  100. IMPORT_FUNC(glClearColorx);
  101. IMPORT_FUNC(glColor4x);
  102. IMPORT_FUNC(glColorPointer);
  103. IMPORT_FUNC(glDisable);
  104. IMPORT_FUNC(glDisableClientState);
  105. IMPORT_FUNC(glDrawArrays);
  106. IMPORT_FUNC(glEnable);
  107. IMPORT_FUNC(glEnableClientState);
  108. IMPORT_FUNC(glFrustumx);
  109. IMPORT_FUNC(glGetError);
  110. IMPORT_FUNC(glLightxv);
  111. IMPORT_FUNC(glLoadIdentity);
  112. IMPORT_FUNC(glMaterialx);
  113. IMPORT_FUNC(glMaterialxv);
  114. IMPORT_FUNC(glMatrixMode);
  115. IMPORT_FUNC(glMultMatrixx);
  116. IMPORT_FUNC(glNormalPointer);
  117. IMPORT_FUNC(glPopMatrix);
  118. IMPORT_FUNC(glPushMatrix);
  119. IMPORT_FUNC(glRotatex);
  120. IMPORT_FUNC(glScalex);
  121. IMPORT_FUNC(glShadeModel);
  122. IMPORT_FUNC(glTranslatex);
  123. IMPORT_FUNC(glVertexPointer);
  124. IMPORT_FUNC(glViewport);
  125. return result;
  126. }
  127. void importGLDeinit()
  128. {
  129. #ifdef WIN32
  130. FreeLibrary(sGLESDLL);
  131. #endif
  132. #ifdef LINUX
  133. dlclose(sGLESSO);
  134. #endif
  135. }