0024819: TKOpenGl - extend the ray-tracing core by visualization of lines, text and...
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_7.cxx
CommitLineData
b311480e 1// Created on: 2011-10-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
2166f0fa 16#include <OpenGl_GraphicDriver.hxx>
7fd59977 17
7fd59977 18#include <OpenGl_FrameBuffer.hxx>
19
2166f0fa
SK
20#include <OpenGl_Structure.hxx>
21#include <OpenGl_CView.hxx>
eb4320f2 22#include <OpenGl_Text.hxx>
7fd59977 23
2166f0fa 24/*----------------------------------------------------------------------*/
7fd59977 25
2166f0fa
SK
26void OpenGl_GraphicDriver::ActivateView (const Graphic3d_CView& ACView)
27{
28 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
29 if (aCView)
30 aCView->WS->SetActiveView(aCView->View);
7fd59977 31}
32
2166f0fa
SK
33void OpenGl_GraphicDriver::AntiAliasing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
34{
35 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
36 if (aCView)
37 aCView->View->SetAntiAliasing(AFlag);
7fd59977 38}
39
2166f0fa
SK
40void OpenGl_GraphicDriver::Background (const Graphic3d_CView& ACView)
41{
42 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
43 if (aCView)
44 {
45 aCView->WS->SetBackgroundColor(ACView.DefWindow.Background.r,ACView.DefWindow.Background.g,ACView.DefWindow.Background.b);
7fd59977 46 }
7fd59977 47}
48
2166f0fa 49void OpenGl_GraphicDriver::GradientBackground (const Graphic3d_CView& ACView,
7fd59977 50 const Quantity_Color& AColor1,
51 const Quantity_Color& AColor2,
2166f0fa
SK
52 const Aspect_GradientFillMethod AType)
53{
54 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
55 if (aCView)
56 {
57 aCView->View->SetBackgroundGradient(AColor1,AColor2,AType);
7fd59977 58 }
7fd59977 59}
60
2166f0fa
SK
61void OpenGl_GraphicDriver::ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
62{
63 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
64 if (aCView)
65 {
66 aCView->View->SetClipLimit(ACView);
529afc1a 67 if (!AWait)
2166f0fa
SK
68 {
69 aCView->WS->Resize(ACView.DefWindow);
2166f0fa 70 }
7fd59977 71 }
7fd59977 72}
73
2166f0fa
SK
74void OpenGl_GraphicDriver::DeactivateView (const Graphic3d_CView& ACView)
75{
76 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
77 if (aCView)
78 {
79 const Handle(OpenGl_View) aDummyView;
80 aCView->WS->SetActiveView(aDummyView);
7fd59977 81 }
7fd59977 82}
83
2166f0fa
SK
84void OpenGl_GraphicDriver::DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
85{
86 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
87 if (aCView)
88 aCView->View->SetFog(ACView, AFlag);
7fd59977 89}
90
529afc1a 91void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& theCView)
2166f0fa 92{
529afc1a
K
93 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
94 if (aCView != NULL)
95 aCView->WS->Resize (theCView.DefWindow);
2166f0fa 96}
7fd59977 97
35e08fe8 98void OpenGl_GraphicDriver::Redraw (const Graphic3d_CView& ACView,
99 const Aspect_CLayer2d& ACUnderLayer,
100 const Aspect_CLayer2d& ACOverLayer,
101 const Standard_Integer /*x*/,
102 const Standard_Integer /*y*/,
103 const Standard_Integer /*width*/,
104 const Standard_Integer /*height*/)
2166f0fa 105{
a89742cf 106 if (ACView.RenderParams.Method == Graphic3d_RM_RAYTRACING
107 && !myCaps->vboDisable
108 && !myCaps->keepArrayData)
e276548b 109 {
110 if (ACView.WasRedrawnGL)
111 {
112 myDeviceLostFlag = Standard_True;
113 }
114
115 myCaps->keepArrayData = Standard_True;
116 }
117
2166f0fa
SK
118 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
119 if (aCView)
120 {
121 /*if( width <= 0 || height <= 0 )
122 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
123 else
124 aCView->WS->RedrawArea(ACView, ACUnderLayer, ACOverLayer, x, y, width, height);*/
125 // Always do full redraw
126 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
7fd59977 127 }
7fd59977 128}
129
679ecdee 130void OpenGl_GraphicDriver::RedrawImmediate (const Graphic3d_CView& theCView,
131 const Aspect_CLayer2d& theCUnderLayer,
132 const Aspect_CLayer2d& theCOverLayer)
133{
134 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
135 if (aCView != NULL)
136 {
137 aCView->WS->RedrawImmediate (theCView, theCUnderLayer, theCOverLayer);
138 }
139}
140
141void OpenGl_GraphicDriver::Invalidate (const Graphic3d_CView& theCView)
142{
143 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
144 if (aCView != NULL)
145 {
146 aCView->WS->Invalidate (theCView);
147 }
148}
149
2166f0fa
SK
150Graphic3d_PtrFrameBuffer OpenGl_GraphicDriver::FBOCreate (const Graphic3d_CView& ACView, const Standard_Integer theWidth, const Standard_Integer theHeight)
151{
152 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
153 if (aCView)
154 return aCView->WS->FBOCreate(theWidth, theHeight);
155 return (Graphic3d_PtrFrameBuffer)NULL;
7fd59977 156}
157
fd4a6963 158Graphic3d_PtrFrameBuffer OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
159 const Standard_Integer theHeight)
2166f0fa 160{
7fd59977 161 // activate OpenGL context
2166f0fa 162 if (!Activate())
7fd59977 163 return NULL;
2166f0fa 164
7fd59977 165 // create the FBO
fd4a6963 166 const Handle(OpenGl_Context)& aCtx = GetGlContext();
7fd59977 167 OpenGl_FrameBuffer* aFrameBuffer = new OpenGl_FrameBuffer();
fd4a6963 168 if (!aFrameBuffer->Init (aCtx, theWidth, theHeight))
7fd59977 169 {
fd4a6963 170 aFrameBuffer->Release (aCtx.operator->());
7fd59977 171 delete aFrameBuffer;
172 return NULL;
173 }
174 return (Graphic3d_PtrFrameBuffer )aFrameBuffer;
175}
176
2166f0fa 177void OpenGl_GraphicDriver::FBORelease (const Graphic3d_CView& ACView, Graphic3d_PtrFrameBuffer& theFBOPtr)
7fd59977 178{
179 if (theFBOPtr == NULL)
7fd59977 180 return;
2166f0fa
SK
181 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
182 if (aCView)
183 {
184 aCView->WS->FBORelease(theFBOPtr);
185 theFBOPtr = NULL;
7fd59977 186 }
2166f0fa
SK
187}
188
189void OpenGl_Workspace::FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr)
190{
7fd59977 191 // activate OpenGL context
fd4a6963 192 if (!Activate()
193 || theFBOPtr == NULL)
194 {
7fd59977 195 return;
fd4a6963 196 }
2166f0fa 197
7fd59977 198 // release the object
2166f0fa 199 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer*)theFBOPtr;
fd4a6963 200 if (aFrameBuffer != NULL)
201 {
202 aFrameBuffer->Release (GetGlContext().operator->());
203 }
7fd59977 204 delete aFrameBuffer;
7fd59977 205}
206
207void OpenGl_GraphicDriver::FBOGetDimensions (const Graphic3d_CView& ,
208 const Graphic3d_PtrFrameBuffer theFBOPtr,
209 Standard_Integer& theWidth, Standard_Integer& theHeight,
210 Standard_Integer& theWidthMax, Standard_Integer& theHeightMax)
211{
212 if (theFBOPtr == NULL)
213 {
214 return;
215 }
216 const OpenGl_FrameBuffer* aFrameBuffer = (const OpenGl_FrameBuffer* )theFBOPtr;
217 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
218 theHeight = aFrameBuffer->GetVPSizeY();
219 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
220 theHeightMax = aFrameBuffer->GetSizeY();
221}
222
223void OpenGl_GraphicDriver::FBOChangeViewport (const Graphic3d_CView& ,
224 Graphic3d_PtrFrameBuffer& theFBOPtr,
225 const Standard_Integer theWidth, const Standard_Integer theHeight)
226{
227 if (theFBOPtr == NULL)
228 {
229 return;
230 }
231 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
232 aFrameBuffer->ChangeViewport (theWidth, theHeight);
233}
234
692613e5 235inline bool getDataFormat (const Image_PixMap& theData,
236 GLenum& thePixelFormat,
237 GLenum& theDataType)
7fd59977 238{
692613e5 239 thePixelFormat = GL_RGB;
240 theDataType = GL_UNSIGNED_BYTE;
241 switch (theData.Format())
7fd59977 242 {
692613e5 243 case Image_PixMap::ImgGray:
244 thePixelFormat = GL_DEPTH_COMPONENT;
245 theDataType = GL_UNSIGNED_BYTE;
246 return true;
247 case Image_PixMap::ImgRGB:
248 thePixelFormat = GL_RGB;
249 theDataType = GL_UNSIGNED_BYTE;
250 return true;
251 case Image_PixMap::ImgBGR:
252 thePixelFormat = GL_BGR;
253 theDataType = GL_UNSIGNED_BYTE;
254 return true;
255 case Image_PixMap::ImgRGBA:
256 case Image_PixMap::ImgRGB32:
257 thePixelFormat = GL_RGBA;
258 theDataType = GL_UNSIGNED_BYTE;
259 return true;
260 case Image_PixMap::ImgBGRA:
261 case Image_PixMap::ImgBGR32:
262 thePixelFormat = GL_BGRA;
263 theDataType = GL_UNSIGNED_BYTE;
264 return true;
265 case Image_PixMap::ImgGrayF:
266 thePixelFormat = GL_DEPTH_COMPONENT;
267 theDataType = GL_FLOAT;
268 return true;
269 case Image_PixMap::ImgRGBF:
270 thePixelFormat = GL_RGB;
271 theDataType = GL_FLOAT;
272 return true;
273 case Image_PixMap::ImgBGRF:
274 thePixelFormat = GL_BGR;
275 theDataType = GL_FLOAT;
276 return true;
277 case Image_PixMap::ImgRGBAF:
278 thePixelFormat = GL_RGBA;
279 theDataType = GL_FLOAT;
280 return true;
281 case Image_PixMap::ImgBGRAF:
282 thePixelFormat = GL_BGRA;
283 theDataType = GL_FLOAT;
284 return true;
285 default:
286 return false;
7fd59977 287 }
288}
289
692613e5 290Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& theCView,
291 Image_PixMap& theImage,
292 const Graphic3d_BufferType& theBufferType)
7fd59977 293{
692613e5 294 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
295 return (aCView != NULL) && aCView->WS->BufferDump ((OpenGl_FrameBuffer* )theCView.ptrFBO, theImage, theBufferType);
2166f0fa
SK
296}
297
692613e5 298Standard_Boolean OpenGl_Workspace::BufferDump (OpenGl_FrameBuffer* theFBOPtr,
299 Image_PixMap& theImage,
300 const Graphic3d_BufferType& theBufferType)
7fd59977 301{
692613e5 302 GLenum aFormat, aType;
303 if (theImage.IsEmpty()
304 || !getDataFormat (theImage, aFormat, aType)
305 || ((theBufferType == Graphic3d_BT_Depth) && (aFormat != GL_DEPTH_COMPONENT))
306 || !Activate())
7fd59977 307 {
308 return Standard_False;
309 }
310
7fd59977 311 // bind FBO if used
7fd59977 312 GLint aReadBufferPrev = GL_BACK;
692613e5 313 if (theFBOPtr != NULL && theFBOPtr->IsValid())
7fd59977 314 {
692613e5 315 theFBOPtr->BindBuffer (GetGlContext());
7fd59977 316 }
317 else
318 {
319 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
320 GLint aDrawBufferPrev = GL_BACK;
321 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
322 glReadBuffer (aDrawBufferPrev);
323 }
324
74706083 325 // setup alignment
326 const GLint anExtraBytes = (GLint )theImage.RowExtraBytes();
327 const GLint anAligment = Min (GLint(theImage.MaxRowAligmentBytes()), 8); // limit to 8 bytes for OpenGL
692613e5 328 glPixelStorei (GL_PACK_ALIGNMENT, anAligment);
329
74706083 330 const GLint aPixelsWidth = GLint(theImage.SizeRowBytes() / theImage.SizePixelBytes());
331 glPixelStorei (GL_PACK_ROW_LENGTH, (anExtraBytes >= anAligment) ? aPixelsWidth : 0);
332
333 if (theImage.IsTopDown())
692613e5 334 {
335 // copy row by row
336 for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
337 {
74706083 338 // Image_PixMap rows indexation always starts from the upper corner
339 // while order in memory depends on the flag and processed by ChangeRow() method
340 glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, theImage.ChangeRow (aRow));
692613e5 341 }
342 }
343 else
7fd59977 344 {
692613e5 345 glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData());
7fd59977 346 }
7fd59977 347
74706083 348 glPixelStorei (GL_PACK_ALIGNMENT, 1);
349 glPixelStorei (GL_PACK_ROW_LENGTH, 0);
7fd59977 350
692613e5 351 if (theFBOPtr != NULL && theFBOPtr->IsValid())
7fd59977 352 {
692613e5 353 theFBOPtr->UnbindBuffer (GetGlContext());
7fd59977 354 }
355 else
356 {
357 glReadBuffer (aReadBufferPrev);
358 }
359 return Standard_True;
360}
361
dd8a4ce9 362void OpenGl_GraphicDriver::RemoveView (const Graphic3d_CView& theCView)
2166f0fa 363{
fd4a6963 364 Handle(OpenGl_Context) aCtx = GetSharedContext();
365 Handle(OpenGl_View) aView;
366 Handle(OpenGl_Workspace) aWindow;
367 if (myMapOfWS.Find (theCView.WsId, aWindow))
368 {
dd8a4ce9 369 myMapOfWS.UnBind (theCView.WsId);
fd4a6963 370 }
371 if (!aWindow.IsNull())
372 {
373 if (aWindow->GetGlContext()->MakeCurrent())
374 {
375 aCtx = aWindow->GetGlContext();
376 }
377 else
378 {
379 // try to hijack another context if any
380 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
381 if (!anOtherCtx.IsNull()
382 && anOtherCtx != aWindow->GetGlContext())
383 {
384 aCtx = anOtherCtx;
385 aCtx->MakeCurrent();
386 }
387 }
388 }
389 if (myMapOfView.Find (theCView.ViewId, aView))
390 {
391 aView->ReleaseGlResources (aCtx);
392 myMapOfView.UnBind (theCView.ViewId);
393 }
7fd59977 394
fd4a6963 395 if (myMapOfWS.IsEmpty())
dd8a4ce9 396 {
397 // The last view removed but some objects still present.
398 // Release GL resources now without object destruction.
399 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
400 aStructIt.More (); aStructIt.Next())
401 {
402 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
fd4a6963 403 aStruct->ReleaseGlResources (aCtx);
dd8a4ce9 404 }
fd4a6963 405 myTempText->Release (aCtx);
fd4a6963 406 myDeviceLostFlag = !myMapOfStructure.IsEmpty();
dd8a4ce9 407 }
408
bf75be98 409 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
2166f0fa 410 delete aCView;
dd8a4ce9 411 ((Graphic3d_CView *)&theCView)->ptrView = NULL;
fd4a6963 412
413 aCtx.Nullify();
414 aView.Nullify();
415 aWindow.Nullify();
7fd59977 416}
417
2166f0fa
SK
418void OpenGl_GraphicDriver::SetLight (const Graphic3d_CView& ACView)
419{
420 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
421 if (aCView)
422 aCView->View->SetLights(ACView.Context);
7fd59977 423}
424
4269bd1b 425void OpenGl_GraphicDriver::SetClipPlanes (const Graphic3d_CView& theCView)
2166f0fa 426{
4269bd1b 427 const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
2166f0fa 428 if (aCView)
4269bd1b 429 {
430 aCView->View->SetClipPlanes (theCView.Context.ClipPlanes);
431 }
432}
433
b5ac8292 434//=======================================================================
435//function : SetCamera
436//purpose :
437//=======================================================================
438void OpenGl_GraphicDriver::SetCamera (const Graphic3d_CView& theCView)
439{
440 const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
441 if (aCView)
442 {
443 aCView->View->SetCamera (theCView.Context.Camera);
444 }
445}
446
2166f0fa
SK
447void OpenGl_GraphicDriver::SetVisualisation (const Graphic3d_CView& ACView)
448{
449 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
450 if (aCView)
451 {
452 aCView->View->SetVisualisation(ACView.Context);
453 aCView->WS->UseZBuffer() = ( ACView.Context.Visualization == 0? (ACView.Context.ZBufferActivity == 1) : (ACView.Context.ZBufferActivity != 0) );
7fd59977 454 }
7fd59977 455}
456
2166f0fa
SK
457void OpenGl_GraphicDriver::Transparency (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
458{
459 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
460 if (aCView)
461 aCView->WS->UseTransparency(AFlag);
462}
7fd59977 463
b7cd4ba7 464// =======================================================================
465// function : InvalidateBVHData
466// purpose :
467// =======================================================================
468void OpenGl_GraphicDriver::InvalidateBVHData (Graphic3d_CView& theCView, const Standard_Integer theLayerId)
469{
470 OpenGl_CView *aCView = (OpenGl_CView *)theCView.ptrView;
471 if(aCView)
472 {
473 aCView->View->InvalidateBVHData (theLayerId);
474 }
475}
476
5e27df78 477Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& theCView)
2166f0fa 478{
73192b37 479 if (myMapOfView.IsBound (theCView.ViewId)
fd4a6963 480 || myMapOfWS .IsBound (theCView.WsId))
2166f0fa 481 {
fd4a6963 482 return Standard_False;
7fd59977 483 }
484
fd4a6963 485 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
73192b37 486 Handle(OpenGl_Workspace) aWS = new OpenGl_Workspace (myDisplayConnection, theCView.DefWindow, theCView.GContext, myCaps, aShareCtx);
392ac980 487 Handle(OpenGl_View) aView = new OpenGl_View (theCView.Context, &myStateCounter);
fd4a6963 488 myMapOfWS .Bind (theCView.WsId, aWS);
5e27df78 489 myMapOfView.Bind (theCView.ViewId, aView);
7fd59977 490
5e27df78 491 OpenGl_CView* aCView = new OpenGl_CView();
2166f0fa 492 aCView->View = aView;
fd4a6963 493 aCView->WS = aWS;
5e27df78 494 theCView.ptrView = aCView;
7fd59977 495
2166f0fa
SK
496 return Standard_True;
497}
7fd59977 498
2166f0fa
SK
499void OpenGl_GraphicDriver::SetBackFacingModel (const Graphic3d_CView& ACView)
500{
501 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
502 if (aCView)
503 aCView->View->SetBackfacing(ACView.Backfacing);
504}
59f45b7c 505
506//=======================================================================
507//function : AddZLayer
bf75be98 508//purpose :
59f45b7c 509//=======================================================================
510
511void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_CView& theCView,
512 const Standard_Integer theLayerId)
513{
514 const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
515 if (aCView)
516 aCView->View->AddZLayer (theLayerId);
517}
518
519//=======================================================================
520//function : RemoveZLayer
521//purpose :
522//=======================================================================
59f45b7c 523void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_CView& theCView,
524 const Standard_Integer theLayerId)
525{
526 const OpenGl_CView* aCView = (const OpenGl_CView *)theCView.ptrView;
527 if (aCView)
528 aCView->View->RemoveZLayer (theLayerId);
529}
c5751993 530
531//=======================================================================
532//function : SetZLayerSettings
533//purpose :
534//=======================================================================
535Standard_EXPORT void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_CView& theCView,
536 const Standard_Integer theLayerId,
537 const Graphic3d_ZLayerSettings& theSettings)
538{
539 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
540 if (aCView)
541 aCView->View->SetZLayerSettings (theLayerId, theSettings);
542}