0023069: Cache GL_FEEDBACK mode per frame in TKOpenGl calls
[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 <Aspect_Handle.hxx>
25 #include <Aspect_Drawable.hxx>
26 #include <Aspect_Display.hxx>
27 #include <Aspect_RenderingContext.hxx>
28 #include <Standard_Transient.hxx>
29 #include <Handle_OpenGl_Context.hxx>
30
31 //! Forward declarations
32 struct OpenGl_GlCore12;
33 struct OpenGl_GlCore13;
34 struct OpenGl_GlCore14;
35 struct OpenGl_GlCore15;
36 struct OpenGl_GlCore20;
37 struct OpenGl_ArbVBO;
38 struct OpenGl_ExtFBO;
39
40 //! This class generalize access to the GL context and available extensions.
41 //!
42 //! Functions are grouped into structures and accessed as fields.
43 //! You should check the group for NULL before usage (if group is not NULL
44 //! then all functions are available):
45 //! @code
46 //!   if (myContext->core20 != NULL)
47 //!   {
48 //!     myGlProgram = myContext->core20->glCreateProgram();
49 //!     .. do more stuff ..
50 //!   }
51 //!   else
52 //!   {
53 //!     .. compatibility with outdated configurations ..
54 //!   }
55 //! @endcode
56 //!
57 //! Current implementation provide access to OpenGL core functionality up to 2.0 version
58 //! (core12, core13, core14, core15, fields core20).
59 //! within several extensions (arbVBO, extFBO, etc.).
60 //!
61 //! Simplified extensions classification:
62 //!  - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
63 //!  - prefixed with EXT are accepted by 2+ vendors;
64 //!  - prefixed with ARB are accepted by Architecture Review Board and are candidates
65 //!    for inclusion into GL core functionality.
66 //! Some functionality can be represented in several extensions simultaneously.
67 //! In this case developer should be careful because different specification may differ
68 //! in aspects (like enumeration values and error-handling).
69 //!
70 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
71 //! with different capabilities. Thats why OpenGl_Context should be initialized and used
72 //! for each GL context individually.
73 class OpenGl_Context : public Standard_Transient
74 {
75 public:
76
77   //! Empty constructor. You should call Init() to perform initialization with bound GL context.
78   Standard_EXPORT OpenGl_Context();
79
80   //! Destructor.
81   Standard_EXPORT virtual ~OpenGl_Context();
82
83   //! Initialize available extensions.
84   //! GL context should be active!
85   Standard_EXPORT void Init();
86
87 #if (defined(_WIN32) || defined(__WIN32__))
88   Standard_EXPORT void Init (const Aspect_Handle           theWindow,
89                              const Aspect_Handle           theWindowDC,
90                              const Aspect_RenderingContext theGContext);
91 #else
92   Standard_EXPORT void Init (const Aspect_Drawable         theWindow,
93                              const Aspect_Display          theDisplay,
94                              const Aspect_RenderingContext theGContext);
95 #endif
96
97   //! Check if theExtName extension is supported by active GL context.
98   Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
99
100   //! Auxiliary template to retrieve GL function pointer.
101   //! Pointer to function retrieved from library is statically casted
102   //! to requested type - there no way to check real signature of exported function.
103   //! The context should be bound before call.
104   template <typename FuncType_t>
105   Standard_Boolean FindProc (const char* theFuncName,
106                              FuncType_t& theFuncPtr)
107   {
108     theFuncPtr = (FuncType_t )findProc (theFuncName);
109     return (theFuncPtr != NULL);
110   }
111
112   //! @return true if detected GL version is greater or equal to requested one.
113   inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
114                                             const Standard_Integer theVerMinor)
115   {
116     return (myGlVerMajor >  theVerMajor)
117         || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
118   }
119
120   //! Clean up errors stack for this GL context (glGetError() in loop).
121   Standard_EXPORT void ResetErrors();
122
123   //! Activates current context.
124   //! Class should be initialized with appropriate info.
125   Standard_EXPORT Standard_Boolean MakeCurrent();
126
127   //! Return true if active mode is GL_FEEDBACK (cached state)
128   Standard_EXPORT Standard_Boolean IsFeedback() const;
129
130   //! Setup feedback mode cached state
131   Standard_EXPORT void SetFeedback (const Standard_Boolean theFeedbackOn);
132
133 private:
134
135   //! Wrapper to system function to retrieve GL function pointer by name.
136   Standard_EXPORT void* findProc (const char* theFuncName);
137
138   //! Read OpenGL version information from active context.
139   Standard_EXPORT void readGlVersion();
140
141   //! Private initialization function that should be called only once.
142   Standard_EXPORT void init();
143
144 public: // core profiles
145
146   OpenGl_GlCore12* core12;
147   OpenGl_GlCore13* core13;
148   OpenGl_GlCore14* core14;
149   OpenGl_GlCore15* core15;
150   OpenGl_GlCore20* core20;
151
152 public: // extensions
153
154   OpenGl_ArbVBO*   arbVBO;
155   OpenGl_ExtFBO*   extFBO;
156
157 private:
158
159 #if (defined(_WIN32) || defined(__WIN32__))
160   Aspect_Handle           myWindow;   //!< window handle (owner of GL context) : HWND
161   Aspect_Handle           myWindowDC; //!< Device Descriptor handle : HDC
162   Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
163 #else
164   Aspect_Drawable         myWindow;   //!< window handle (owner of GL context) : GLXDrawable
165   Aspect_Display          myDisplay;  //!< connection to the X-server : Display*
166   Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
167 #endif
168
169   void*            myGlLibHandle;   //!< optional handle to GL library
170   OpenGl_GlCore20* myGlCore20;      //!< common structure for GL core functions upto 2.0
171   Standard_Integer myGlVerMajor;    //!< cached GL version major number
172   Standard_Integer myGlVerMinor;    //!< cached GL version minor number
173   Standard_Boolean myIsFeedback;    //!< flag indicates GL_FEEDBACK mode
174   Standard_Boolean myIsInitialized; //!< flag to indicate initialization state
175
176 public:
177
178   DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
179
180 };
181
182 #endif // _OpenGl_Context_H__