1 // Created on: 2012-01-26
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2012 OPEN CASCADE SAS
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.
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.
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.
21 #ifndef _OpenGl_Context_H__
22 #define _OpenGl_Context_H__
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>
31 //! Forward declarations
32 struct OpenGl_GlCore12;
33 struct OpenGl_GlCore13;
34 struct OpenGl_GlCore14;
35 struct OpenGl_GlCore15;
36 struct OpenGl_GlCore20;
40 //! This class generalize access to the GL context and available extensions.
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):
46 //! if (myContext->core20 != NULL)
48 //! myGlProgram = myContext->core20->glCreateProgram();
49 //! .. do more stuff ..
53 //! .. compatibility with outdated configurations ..
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.).
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).
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
77 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
78 Standard_EXPORT OpenGl_Context();
81 Standard_EXPORT virtual ~OpenGl_Context();
83 //! Initialize available extensions.
84 //! GL context should be active!
85 Standard_EXPORT void Init();
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);
92 Standard_EXPORT void Init (const Aspect_Drawable theWindow,
93 const Aspect_Display theDisplay,
94 const Aspect_RenderingContext theGContext);
97 //! Check if theExtName extension is supported by active GL context.
98 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
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)
108 theFuncPtr = (FuncType_t )findProc (theFuncName);
109 return (theFuncPtr != NULL);
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)
116 return (myGlVerMajor > theVerMajor)
117 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
120 //! Clean up errors stack for this GL context (glGetError() in loop).
121 Standard_EXPORT void ResetErrors();
123 //! Activates current context.
124 //! Class should be initialized with appropriate info.
125 Standard_EXPORT Standard_Boolean MakeCurrent();
127 //! Return true if active mode is GL_FEEDBACK (cached state)
128 Standard_EXPORT Standard_Boolean IsFeedback() const;
130 //! Setup feedback mode cached state
131 Standard_EXPORT void SetFeedback (const Standard_Boolean theFeedbackOn);
135 //! Wrapper to system function to retrieve GL function pointer by name.
136 Standard_EXPORT void* findProc (const char* theFuncName);
138 //! Read OpenGL version information from active context.
139 Standard_EXPORT void readGlVersion();
141 //! Private initialization function that should be called only once.
142 Standard_EXPORT void init();
144 public: // core profiles
146 OpenGl_GlCore12* core12;
147 OpenGl_GlCore13* core13;
148 OpenGl_GlCore14* core14;
149 OpenGl_GlCore15* core15;
150 OpenGl_GlCore20* core20;
152 public: // extensions
154 OpenGl_ArbVBO* arbVBO;
155 OpenGl_ExtFBO* extFBO;
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
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
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
178 DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
182 #endif // _OpenGl_Context_H__