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