0023525: Disappearing of highlight in screenshot
[occt.git] / src / OpenGl / OpenGl_GraphicDriver.cxx
CommitLineData
b311480e 1// Created on: 2011-10-20
2// Created by: Sergey ZERCHANINOV
1981cb22 3// Copyright (c) 2011-2013 OPEN CASCADE SAS
b311480e 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 20#include <OpenGl_GraphicDriver.hxx>
7fd59977 21
f0430952 22#include <OpenGl_Context.hxx>
1981cb22 23#include <OpenGl_CView.hxx>
2166f0fa
SK
24#include <OpenGl_View.hxx>
25#include <OpenGl_Workspace.hxx>
7fd59977 26
2166f0fa
SK
27IMPLEMENT_STANDARD_HANDLE(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
28IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
7fd59977 29
2166f0fa
SK
30namespace
31{
5e27df78 32 // Global switch - shared by whole TKOpenGl module. To be removed.
2166f0fa 33 static Standard_Boolean TheToUseVbo = Standard_True;
5e27df78 34
35 static const Handle(OpenGl_Context) TheNullGlCtx;
2166f0fa 36};
7fd59977 37
38// Pour eviter de "mangler" MetaGraphicDriverFactory, le nom de la
39// fonction qui cree un Graphic3d_GraphicDriver.
40// En effet, ce nom est recherche par la methode DlSymb de la
41// classe OSD_SharedLibrary dans la methode SetGraphicDriver de la
42// classe Graphic3d_GraphicDevice
43extern "C" {
5e27df78 44#if defined(_MSC_VER) // disable MS VC++ warning on C-style function returning C++ object
2166f0fa
SK
45 #pragma warning(push)
46 #pragma warning(disable:4190)
7fd59977 47#endif
5e27df78 48 Standard_EXPORT Handle(Graphic3d_GraphicDriver) MetaGraphicDriverFactory (const Standard_CString theShrName)
2166f0fa 49 {
5e27df78 50 Handle(OpenGl_GraphicDriver) aDriver = new OpenGl_GraphicDriver (theShrName);
51 return aDriver;
2166f0fa 52 }
5e27df78 53#if defined(_MSC_VER)
2166f0fa 54 #pragma warning(pop)
7fd59977 55#endif
56}
57
f0430952 58// =======================================================================
59// function : OpenGl_GraphicDriver
60// purpose :
61// =======================================================================
2166f0fa 62OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Standard_CString theShrName)
5e27df78 63: Graphic3d_GraphicDriver (theShrName),
64 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
65 myMapOfWS (1, NCollection_BaseAllocator::CommonBaseAllocator()),
66 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator()),
67 myUserDrawCallback (NULL)
7fd59977 68{
2166f0fa 69 //
7fd59977 70}
71
f0430952 72// =======================================================================
5e27df78 73// function : UserDrawCallback
f0430952 74// purpose :
75// =======================================================================
5e27df78 76OpenGl_GraphicDriver::OpenGl_UserDrawCallback_t& OpenGl_GraphicDriver::UserDrawCallback()
2166f0fa 77{
5e27df78 78 return myUserDrawCallback;
2166f0fa
SK
79}
80
f0430952 81// =======================================================================
5e27df78 82// function : DefaultTextHeight
f0430952 83// purpose :
84// =======================================================================
5e27df78 85Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
2166f0fa 86{
5e27df78 87 return 16.;
2166f0fa
SK
88}
89
f0430952 90// =======================================================================
91// function : InvalidateAllWorkspaces
92// purpose : ex-TsmInitUpdateState, deprecated, need to decide what to do with EraseAnimation() call
93// =======================================================================
2166f0fa
SK
94void OpenGl_GraphicDriver::InvalidateAllWorkspaces()
95{
5e27df78 96 for (NCollection_DataMap<Standard_Integer, Handle(OpenGl_Workspace)>::Iterator anIt (myMapOfWS);
2166f0fa
SK
97 anIt.More(); anIt.Next())
98 {
2166f0fa
SK
99 anIt.ChangeValue()->EraseAnimation();
100 }
101}
102
f0430952 103// =======================================================================
104// function : ToUseVBO
105// purpose :
106// =======================================================================
2166f0fa 107Standard_Boolean OpenGl_GraphicDriver::ToUseVBO()
7fd59977 108{
2166f0fa 109 return TheToUseVbo;
7fd59977 110}
111
f0430952 112// =======================================================================
113// function : EnableVBO
114// purpose :
115// =======================================================================
2166f0fa 116void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
7fd59977 117{
2166f0fa 118 TheToUseVbo = theToTurnOn;
7fd59977 119}
f0430952 120
121// =======================================================================
5e27df78 122// function : GetSharedContext
123// purpose :
124// =======================================================================
125const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
126{
127 if (myMapOfWS.IsEmpty())
128 {
129 return TheNullGlCtx;
130 }
131
132 NCollection_DataMap<Standard_Integer, Handle(OpenGl_Workspace)>::Iterator anIter (myMapOfWS);
133 return anIter.Value()->GetGlContext();
134}
135
136// =======================================================================
f0430952 137// function : MemoryInfo
138// purpose :
139// =======================================================================
140Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
141 TCollection_AsciiString& theInfo) const
142{
143 // this is extra work (for OpenGl_Context initialization)...
144 OpenGl_Context aGlCtx;
145 if (!aGlCtx.Init())
146 {
147 return Standard_False;
148 }
149 theFreeBytes = aGlCtx.AvailableMemory();
150 theInfo = aGlCtx.MemoryInfo();
151 return !theInfo.IsEmpty();
152}
1981cb22 153
154// =======================================================================
155// function : SetImmediateModeDrawToFront
156// purpose :
157// =======================================================================
158Standard_Boolean OpenGl_GraphicDriver::SetImmediateModeDrawToFront (const Graphic3d_CView& theCView,
159 const Standard_Boolean theDrawToFrontBuffer)
160{
161 if (theCView.ViewId == -1)
162 {
163 return Standard_False;
164 }
165
166 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
167 if (aCView != NULL)
168 {
169 return aCView->WS->SetImmediateModeDrawToFront (theDrawToFrontBuffer);
170 }
171 return Standard_False;
172}
173
174// =======================================================================
175// function : BeginAddMode
176// purpose :
177// =======================================================================
178Standard_Boolean OpenGl_GraphicDriver::BeginAddMode (const Graphic3d_CView& theCView)
179{
180 if (theCView.ViewId == -1)
181 {
182 return Standard_False;
183 }
184
185 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
186 if (aCView != NULL && aCView->WS->BeginAddMode())
187 {
188 myImmediateWS = aCView->WS;
189 return Standard_True;
190 }
191
192 return Standard_False;
193}
194
195// =======================================================================
196// function : EndAddMode
197// purpose :
198// =======================================================================
199void OpenGl_GraphicDriver::EndAddMode()
200{
201 if (!myImmediateWS.IsNull())
202 {
203 myImmediateWS->EndAddMode();
204 myImmediateWS.Nullify();
205 }
206}
207
208// =======================================================================
209// function : BeginImmediatMode
210// purpose :
211// =======================================================================
212Standard_Boolean OpenGl_GraphicDriver::BeginImmediatMode (const Graphic3d_CView& theCView,
213 const Aspect_CLayer2d& theCUnderLayer,
214 const Aspect_CLayer2d& theCOverLayer,
215 const Standard_Boolean theDoubleBuffer,
216 const Standard_Boolean theRetainMode)
217{
218 if (theCView.ViewId == -1)
219 {
220 return Standard_False;
221 }
222
223 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
224 if (aCView != NULL && aCView->WS->BeginImmediatMode (theCView, theDoubleBuffer, theRetainMode))
225 {
226 myImmediateWS = aCView->WS;
227 return Standard_True;
228 }
229
230 return Standard_False;
231}
232
233// =======================================================================
234// function : ClearImmediatMode
235// purpose :
236// =======================================================================
237void OpenGl_GraphicDriver::ClearImmediatMode (const Graphic3d_CView& theCView,
238 const Standard_Boolean theToFlush)
239{
240 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
241 if (aCView != NULL)
242 {
243 aCView->WS->ClearImmediatMode (theCView, theToFlush);
244 }
245}
246
247// =======================================================================
248// function : DrawStructure
249// purpose :
250// =======================================================================
251void OpenGl_GraphicDriver::DrawStructure (const Graphic3d_CStructure& theCStructure)
252{
253 OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
254 if (aStructure == NULL)
255 {
256 return;
257 }
258
259 if (!myImmediateWS.IsNull())
260 {
261 myImmediateWS->DrawStructure (aStructure);
262 }
263}
264
265// =======================================================================
266// function : EndImmediatMode
267// purpose :
268// =======================================================================
269void OpenGl_GraphicDriver::EndImmediatMode (const Standard_Integer )
270{
271 if (!myImmediateWS.IsNull())
272 {
273 myImmediateWS->EndImmediatMode();
274 myImmediateWS.Nullify();
275 }
276}