0023000: Improve the way the gradient and textured background is managed in 3d viewer
[occt.git] / src / OpenGl / OpenGl_Context.hxx
CommitLineData
b311480e 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
2166f0fa
SK
20
21#ifndef _OpenGl_Context_H__
22#define _OpenGl_Context_H__
23
2bd4c032 24#include <Aspect_Handle.hxx>
25#include <Aspect_Drawable.hxx>
26#include <Aspect_Display.hxx>
27#include <Aspect_RenderingContext.hxx>
2166f0fa
SK
28#include <Standard_Transient.hxx>
29#include <Handle_OpenGl_Context.hxx>
30
5f8b738e 31//! Forward declarations
32struct OpenGl_GlCore12;
33struct OpenGl_GlCore13;
34struct OpenGl_GlCore14;
35struct OpenGl_GlCore15;
36struct OpenGl_GlCore20;
37struct OpenGl_ArbVBO;
38struct OpenGl_ExtFBO;
2166f0fa 39
5f8b738e 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.
2166f0fa
SK
73class OpenGl_Context : public Standard_Transient
74{
75public:
76
5f8b738e 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();
2166f0fa
SK
82
83 //! Initialize available extensions.
84 //! GL context should be active!
5f8b738e 85 Standard_EXPORT void Init();
2166f0fa 86
2bd4c032 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
5f8b738e 97 //! Check if theExtName extension is supported by active GL context.
2bd4c032 98 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
2166f0fa
SK
99
100 //! Auxiliary template to retrieve GL function pointer.
5f8b738e 101 //! Pointer to function retrieved from library is statically casted
102 //! to requested type - there no way to check real signature of exported function.
2166f0fa
SK
103 //! The context should be bound before call.
104 template <typename FuncType_t>
5f8b738e 105 Standard_Boolean FindProc (const char* theFuncName,
106 FuncType_t& theFuncPtr)
2166f0fa 107 {
5f8b738e 108 theFuncPtr = (FuncType_t )findProc (theFuncName);
2166f0fa
SK
109 return (theFuncPtr != NULL);
110 }
111
2bd4c032 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)
5f8b738e 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
2bd4c032 123 //! Activates current context.
124 //! Class should be initialized with appropriate info.
125 Standard_EXPORT Standard_Boolean MakeCurrent();
126
5f8b738e 127private:
128
129 //! Wrapper to system function to retrieve GL function pointer by name.
130 Standard_EXPORT void* findProc (const char* theFuncName);
131
132 //! Read OpenGL version information from active context.
133 Standard_EXPORT void readGlVersion();
134
135 //! Private initialization function that should be called only once.
136 Standard_EXPORT void init();
137
138public: // core profiles
139
140 OpenGl_GlCore12* core12;
141 OpenGl_GlCore13* core13;
142 OpenGl_GlCore14* core14;
143 OpenGl_GlCore15* core15;
144 OpenGl_GlCore20* core20;
145
146public: // extensions
147
148 OpenGl_ArbVBO* arbVBO;
149 OpenGl_ExtFBO* extFBO;
150
151private:
2166f0fa 152
2bd4c032 153#if (defined(_WIN32) || defined(__WIN32__))
154 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
155 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
156 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
157#else
158 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
159 Aspect_Display myDisplay; //!< connection to the X-server : Display*
160 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
161#endif
162
5f8b738e 163 void* myGlLibHandle; //!< optional handle to GL library
164 OpenGl_GlCore20* myGlCore20; //!< common structure for GL core functions upto 2.0
165 Standard_Integer myGlVerMajor; //!< cached GL version major number
166 Standard_Integer myGlVerMinor; //!< cached GL version minor number
167 Standard_Boolean myIsInitialized; //!< flag to indicate initialization state
2166f0fa
SK
168
169public:
170
5f8b738e 171 DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
2166f0fa
SK
172
173};
174
175#endif // _OpenGl_Context_H__