| 1 | // File: OpenGl_Context.cxx |
| 2 | // Created: 26 January 2012 |
| 3 | // Author: Kirill GAVRILOV |
| 4 | // Copyright: OPEN CASCADE 2012 |
| 5 | |
| 6 | #include <OpenGl_Context.hxx> |
| 7 | |
| 8 | IMPLEMENT_STANDARD_HANDLE (OpenGl_Context, Standard_Transient) |
| 9 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context, Standard_Transient) |
| 10 | |
| 11 | #if (!defined(_WIN32) && !defined(__WIN32__)) |
| 12 | #include <GL/glx.h> |
| 13 | #endif |
| 14 | |
| 15 | // ======================================================================= |
| 16 | // function : OpenGl_Context |
| 17 | // purpose : |
| 18 | // ======================================================================= |
| 19 | OpenGl_Context::OpenGl_Context() |
| 20 | : arbVBO (NULL), |
| 21 | extFBO (NULL) |
| 22 | { |
| 23 | // |
| 24 | } |
| 25 | |
| 26 | // ======================================================================= |
| 27 | // function : ~OpenGl_Context |
| 28 | // purpose : |
| 29 | // ======================================================================= |
| 30 | OpenGl_Context::~OpenGl_Context() |
| 31 | { |
| 32 | // |
| 33 | } |
| 34 | |
| 35 | // ======================================================================= |
| 36 | // function : CheckExtension |
| 37 | // purpose : |
| 38 | // ======================================================================= |
| 39 | Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName, |
| 40 | const char* theExtString) |
| 41 | { |
| 42 | if (theExtName == NULL |
| 43 | || theExtString == NULL) { |
| 44 | std::cerr << "CheckExtension called with NULL string! No GL context?\n"; |
| 45 | return Standard_False; |
| 46 | } |
| 47 | |
| 48 | // Search for theExtName in the extensions string. |
| 49 | // Use of strstr() is not sufficient because extension names can be prefixes of other extension names. |
| 50 | char* aPtrIter = (char* )theExtString; |
| 51 | int anExtNameLen = strlen(theExtName); |
| 52 | const char* aPtrEnd = aPtrIter + strlen(theExtString); |
| 53 | |
| 54 | while (aPtrIter < aPtrEnd) |
| 55 | { |
| 56 | int n = strcspn (aPtrIter, " "); |
| 57 | if ((n == anExtNameLen) && (strncmp (theExtName, aPtrIter, n) == 0)) |
| 58 | return Standard_True; |
| 59 | |
| 60 | aPtrIter += (n + 1); |
| 61 | } |
| 62 | return Standard_False; |
| 63 | } |
| 64 | |
| 65 | // ======================================================================= |
| 66 | // function : Init |
| 67 | // purpose : |
| 68 | // ======================================================================= |
| 69 | void OpenGl_Context::Init() |
| 70 | { |
| 71 | const char* anExtString = (const char* )glGetString (GL_EXTENSIONS); |
| 72 | if (CheckExtension ("GL_ARB_vertex_buffer_object", anExtString)) |
| 73 | { |
| 74 | arbVBO = new OpenGl_ArbVBO(); |
| 75 | memset(arbVBO, 0, sizeof(OpenGl_ArbVBO)); // nullify whole structure for safety |
| 76 | if (!FindProc ("glGenBuffersARB", arbVBO->glGenBuffersARB) |
| 77 | || !FindProc ("glBindBufferARB", arbVBO->glBindBufferARB) |
| 78 | || !FindProc ("glBufferDataARB", arbVBO->glBufferDataARB) |
| 79 | || !FindProc ("glDeleteBuffersARB", arbVBO->glDeleteBuffersARB)) |
| 80 | { |
| 81 | delete arbVBO; |
| 82 | arbVBO = NULL; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (CheckExtension ("GL_EXT_framebuffer_object", anExtString)) |
| 87 | { |
| 88 | extFBO = new OpenGl_ExtFBO(); |
| 89 | memset(extFBO, 0, sizeof(OpenGl_ExtFBO)); // nullify whole structure for safety |
| 90 | if (!FindProc ("glGenFramebuffersEXT", extFBO->glGenFramebuffersEXT) |
| 91 | || !FindProc ("glDeleteFramebuffersEXT", extFBO->glDeleteFramebuffersEXT) |
| 92 | || !FindProc ("glBindFramebufferEXT", extFBO->glBindFramebufferEXT) |
| 93 | || !FindProc ("glFramebufferTexture2DEXT", extFBO->glFramebufferTexture2DEXT) |
| 94 | || !FindProc ("glCheckFramebufferStatusEXT", extFBO->glCheckFramebufferStatusEXT) |
| 95 | || !FindProc ("glGenRenderbuffersEXT", extFBO->glGenRenderbuffersEXT) |
| 96 | || !FindProc ("glDeleteRenderbuffersEXT", extFBO->glDeleteRenderbuffersEXT) |
| 97 | || !FindProc ("glBindRenderbufferEXT", extFBO->glBindRenderbufferEXT) |
| 98 | || !FindProc ("glRenderbufferStorageEXT", extFBO->glRenderbufferStorageEXT) |
| 99 | || !FindProc ("glFramebufferRenderbufferEXT", extFBO->glFramebufferRenderbufferEXT)) |
| 100 | { |
| 101 | delete extFBO; |
| 102 | extFBO = NULL; |
| 103 | } |
| 104 | } |
| 105 | } |