0023022: This is desirable to access OpenGl extensions and core API (1.2+) in one...
[occt.git] / src / OpenGl / OpenGl_Context.hxx
1 // File:      OpenGl_Context.hxx
2 // Created:   26 January 2012
3 // Author:    Kirill GAVRILOV
4 // Copyright: OPEN CASCADE 2012
5
6 #ifndef _OpenGl_Context_H__
7 #define _OpenGl_Context_H__
8
9 #include <Standard_Transient.hxx>
10 #include <Handle_OpenGl_Context.hxx>
11
12 //! Forward declarations
13 struct OpenGl_GlCore12;
14 struct OpenGl_GlCore13;
15 struct OpenGl_GlCore14;
16 struct OpenGl_GlCore15;
17 struct OpenGl_GlCore20;
18 struct OpenGl_ArbVBO;
19 struct OpenGl_ExtFBO;
20
21 //! This class generalize access to the GL context and available extensions.
22 //!
23 //! Functions are grouped into structures and accessed as fields.
24 //! You should check the group for NULL before usage (if group is not NULL
25 //! then all functions are available):
26 //! @code
27 //!   if (myContext->core20 != NULL)
28 //!   {
29 //!     myGlProgram = myContext->core20->glCreateProgram();
30 //!     .. do more stuff ..
31 //!   }
32 //!   else
33 //!   {
34 //!     .. compatibility with outdated configurations ..
35 //!   }
36 //! @endcode
37 //!
38 //! Current implementation provide access to OpenGL core functionality up to 2.0 version
39 //! (core12, core13, core14, core15, fields core20).
40 //! within several extensions (arbVBO, extFBO, etc.).
41 //!
42 //! Simplified extensions classification:
43 //!  - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
44 //!  - prefixed with EXT are accepted by 2+ vendors;
45 //!  - prefixed with ARB are accepted by Architecture Review Board and are candidates
46 //!    for inclusion into GL core functionality.
47 //! Some functionality can be represented in several extensions simultaneously.
48 //! In this case developer should be careful because different specification may differ
49 //! in aspects (like enumeration values and error-handling).
50 //!
51 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
52 //! with different capabilities. Thats why OpenGl_Context should be initialized and used
53 //! for each GL context individually.
54 class OpenGl_Context : public Standard_Transient
55 {
56 public:
57
58   //! Empty constructor. You should call Init() to perform initialization with bound GL context.
59   Standard_EXPORT OpenGl_Context();
60
61   //! Destructor.
62   Standard_EXPORT virtual ~OpenGl_Context();
63
64   //! Initialize available extensions.
65   //! GL context should be active!
66   Standard_EXPORT void Init();
67
68   //! Check if theExtName extension is supported by active GL context.
69   Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtName);
70
71   //! Auxiliary template to retrieve GL function pointer.
72   //! Pointer to function retrieved from library is statically casted
73   //! to requested type - there no way to check real signature of exported function.
74   //! The context should be bound before call.
75   template <typename FuncType_t>
76   Standard_Boolean FindProc (const char* theFuncName,
77                              FuncType_t& theFuncPtr)
78   {
79     theFuncPtr = (FuncType_t )findProc (theFuncName);
80     return (theFuncPtr != NULL);
81   }
82
83   //! @return true if detected GL version is higher or equal to requested one.
84   inline Standard_Boolean IsGlUpperEqual (const Standard_Integer theVerMajor,
85                                           const Standard_Integer theVerMinor)
86   {
87     return (myGlVerMajor >  theVerMajor)
88         || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
89   }
90
91   //! Clean up errors stack for this GL context (glGetError() in loop).
92   Standard_EXPORT void ResetErrors();
93
94 private:
95
96   //! Wrapper to system function to retrieve GL function pointer by name.
97   Standard_EXPORT void* findProc (const char* theFuncName);
98
99   //! Read OpenGL version information from active context.
100   Standard_EXPORT void readGlVersion();
101
102   //! Private initialization function that should be called only once.
103   Standard_EXPORT void init();
104
105 public: // core profiles
106
107   OpenGl_GlCore12* core12;
108   OpenGl_GlCore13* core13;
109   OpenGl_GlCore14* core14;
110   OpenGl_GlCore15* core15;
111   OpenGl_GlCore20* core20;
112
113 public: // extensions
114
115   OpenGl_ArbVBO*   arbVBO;
116   OpenGl_ExtFBO*   extFBO;
117
118 private:
119
120   void*            myGlLibHandle;   //!< optional handle to GL library
121   OpenGl_GlCore20* myGlCore20;      //!< common structure for GL core functions upto 2.0
122   Standard_Integer myGlVerMajor;    //!< cached GL version major number
123   Standard_Integer myGlVerMinor;    //!< cached GL version minor number
124   Standard_Boolean myIsInitialized; //!< flag to indicate initialization state
125
126 public:
127
128   DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
129
130 };
131
132 #endif // _OpenGl_Context_H__