1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-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 #include <OpenGl_GraphicDriver.hxx>
23 #include <OpenGl_FrameBuffer.hxx>
25 #include <OpenGl_Structure.hxx>
26 #include <OpenGl_CView.hxx>
27 #include <OpenGl_Display.hxx>
29 /*----------------------------------------------------------------------*/
31 void OpenGl_GraphicDriver::ActivateView (const Graphic3d_CView& ACView)
33 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
35 aCView->WS->SetActiveView(aCView->View);
38 void OpenGl_GraphicDriver::AntiAliasing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
40 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
42 aCView->View->SetAntiAliasing(AFlag);
45 void OpenGl_GraphicDriver::Background (const Graphic3d_CView& ACView)
47 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
50 aCView->WS->SetBackgroundColor(ACView.DefWindow.Background.r,ACView.DefWindow.Background.g,ACView.DefWindow.Background.b);
54 void OpenGl_GraphicDriver::GradientBackground (const Graphic3d_CView& ACView,
55 const Quantity_Color& AColor1,
56 const Quantity_Color& AColor2,
57 const Aspect_GradientFillMethod AType)
59 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
62 aCView->View->SetBackgroundGradient(AColor1,AColor2,AType);
66 void OpenGl_GraphicDriver::Blink (const Graphic3d_CStructure &, const Standard_Boolean)
71 void OpenGl_GraphicDriver::BoundaryBox (const Graphic3d_CStructure& ACStructure, const Standard_Boolean Create)
73 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
78 astructure->SetHighlightBox(ACStructure.BoundBox);
80 astructure->ClearHighlightBox();
83 void OpenGl_GraphicDriver::HighlightColor (const Graphic3d_CStructure& ACStructure, const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B, const Standard_Boolean Create)
85 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
90 astructure->SetHighlightColor(R,G,B);
92 astructure->ClearHighlightColor();
95 void OpenGl_GraphicDriver::NameSetStructure (const Graphic3d_CStructure& ACStructure)
97 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
100 Standard_Integer aStatus = 0;
101 if (ACStructure.highlight) aStatus |= OPENGL_NS_HIGHLIGHT;
102 if (!ACStructure.visible) aStatus |= OPENGL_NS_HIDE;
103 astructure->SetNamedStatus( aStatus );
107 void OpenGl_GraphicDriver::ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
109 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
112 aCView->View->SetClipLimit(ACView);
115 aCView->WS->Resize(ACView.DefWindow);
120 void OpenGl_GraphicDriver::DeactivateView (const Graphic3d_CView& ACView)
122 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
125 const Handle(OpenGl_View) aDummyView;
126 aCView->WS->SetActiveView(aDummyView);
130 void OpenGl_GraphicDriver::DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
132 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
134 aCView->View->SetFog(ACView, AFlag);
137 Standard_Boolean OpenGl_GraphicDriver::ProjectRaster (const Graphic3d_CView& ACView, const Standard_ShortReal AX, const Standard_ShortReal AY, const Standard_ShortReal AZ, Standard_Integer& AU, Standard_Integer& AV)
139 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
141 return Standard_False;
143 Standard_Integer aWidth = aCView->WS->Width();
144 Standard_Integer aHeight = aCView->WS->Height();
145 Standard_ShortReal xr, yr;
146 if (aCView->View->ProjectObjectToRaster(aWidth, aHeight, AX, AY, AZ, xr, yr))
148 AU = (Standard_Integer) xr;
149 AV = aHeight - (Standard_Integer) yr;
150 return Standard_True;
153 return Standard_False;
156 Standard_Boolean OpenGl_GraphicDriver::UnProjectRaster (const Graphic3d_CView& ACView, const Standard_Integer Axm, const Standard_Integer Aym, const Standard_Integer AXM, const Standard_Integer AYM, const Standard_Integer AU, const Standard_Integer AV, Standard_ShortReal& Ax, Standard_ShortReal& Ay, Standard_ShortReal& Az)
158 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
160 return Standard_False;
162 const Standard_Integer aWidth = aCView->WS->Width();
163 const Standard_Integer aHeight = aCView->WS->Height();
166 Patched by P.Dolbey: the window pixel height decreased by one
167 in order for yr to remain within valid coordinate range [0; Ym -1]
168 where Ym means window pixel height.
170 return aCView->View->ProjectRasterToObject( aWidth, aHeight, AU, (AYM-1)-Aym-AV, Ax, Ay, Az );
173 Standard_Boolean OpenGl_GraphicDriver::UnProjectRasterWithRay (const Graphic3d_CView& ACView, const Standard_Integer Axm, const Standard_Integer Aym, const Standard_Integer AXM, const Standard_Integer AYM, const Standard_Integer AU, const Standard_Integer AV, Standard_ShortReal& Ax, Standard_ShortReal& Ay, Standard_ShortReal& Az, Standard_ShortReal& Dx, Standard_ShortReal& Dy, Standard_ShortReal& Dz)
175 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
177 return Standard_False;
179 const Standard_Integer aWidth = aCView->WS->Width();
180 const Standard_Integer aHeight = aCView->WS->Height();
182 return aCView->View->ProjectRasterToObjectWithRay( aWidth, aHeight, AU, AYM-Aym-AV, Ax, Ay, Az, Dx, Dy, Dz );
185 void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& theCView)
187 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
189 aCView->WS->Resize (theCView.DefWindow);
192 void OpenGl_GraphicDriver::Redraw (const Graphic3d_CView& ACView, const Aspect_CLayer2d& ACUnderLayer, const Aspect_CLayer2d& ACOverLayer, const Standard_Integer x, const Standard_Integer y, const Standard_Integer width, const Standard_Integer height)
194 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
197 /*if( width <= 0 || height <= 0 )
198 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
200 aCView->WS->RedrawArea(ACView, ACUnderLayer, ACOverLayer, x, y, width, height);*/
201 // Always do full redraw
202 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
206 Graphic3d_PtrFrameBuffer OpenGl_GraphicDriver::FBOCreate (const Graphic3d_CView& ACView, const Standard_Integer theWidth, const Standard_Integer theHeight)
208 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
210 return aCView->WS->FBOCreate(theWidth, theHeight);
211 return (Graphic3d_PtrFrameBuffer)NULL;
214 Graphic3d_PtrFrameBuffer OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight)
216 // activate OpenGL context
221 OpenGl_FrameBuffer* aFrameBuffer = new OpenGl_FrameBuffer();
222 if (!aFrameBuffer->Init (GetGlContext(), theWidth, theHeight))
227 return (Graphic3d_PtrFrameBuffer )aFrameBuffer;
230 void OpenGl_GraphicDriver::FBORelease (const Graphic3d_CView& ACView, Graphic3d_PtrFrameBuffer& theFBOPtr)
232 if (theFBOPtr == NULL)
234 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
237 aCView->WS->FBORelease(theFBOPtr);
242 void OpenGl_Workspace::FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr)
244 // activate OpenGL context
248 // release the object
249 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer*)theFBOPtr;
250 aFrameBuffer->Release (GetGlContext());
254 void OpenGl_GraphicDriver::FBOGetDimensions (const Graphic3d_CView& ,
255 const Graphic3d_PtrFrameBuffer theFBOPtr,
256 Standard_Integer& theWidth, Standard_Integer& theHeight,
257 Standard_Integer& theWidthMax, Standard_Integer& theHeightMax)
259 if (theFBOPtr == NULL)
263 const OpenGl_FrameBuffer* aFrameBuffer = (const OpenGl_FrameBuffer* )theFBOPtr;
264 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
265 theHeight = aFrameBuffer->GetVPSizeY();
266 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
267 theHeightMax = aFrameBuffer->GetSizeY();
270 void OpenGl_GraphicDriver::FBOChangeViewport (const Graphic3d_CView& ,
271 Graphic3d_PtrFrameBuffer& theFBOPtr,
272 const Standard_Integer theWidth, const Standard_Integer theHeight)
274 if (theFBOPtr == NULL)
278 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
279 aFrameBuffer->ChangeViewport (theWidth, theHeight);
284 #define GL_BGR 0x80E0
287 #define GL_BGRA 0x80E1
290 static inline GLenum TFormatToGLEnum (const TRawBufferDataFormat theTFormat)
294 case TRGB: return GL_RGB;
295 case TBGR: return GL_BGR;
296 case TRGBA: return GL_RGBA;
297 case TBGRA: return GL_BGRA;
298 case TDepthComponent: return GL_DEPTH_COMPONENT;
299 case TRed: return GL_RED;
300 case TGreen: return GL_GREEN;
301 case TBlue: return GL_BLUE;
302 case TAlpha: return GL_ALPHA;
307 static inline GLenum TTypeToGLEnum (const TRawBufferDataType theTType)
311 case TUByte: return GL_UNSIGNED_BYTE;
312 case TFloat: return GL_FLOAT;
317 Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& ACView, Image_CRawBufferData& theBuffer)
319 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
321 return aCView->WS->BufferDump((OpenGl_FrameBuffer *)ACView.ptrFBO,theBuffer);
322 return Standard_False;
325 Standard_Boolean OpenGl_Workspace::BufferDump (OpenGl_FrameBuffer *theFBOPtr, Image_CRawBufferData& theBuffer)
327 GLenum aFormat = TFormatToGLEnum (theBuffer.format);
328 GLenum aType = TTypeToGLEnum (theBuffer.type);
331 if (aFormat == 0 || aType == 0 ||
332 theBuffer.widthPx == 0 || theBuffer.heightPx == 0 ||
333 theBuffer.dataPtr == NULL)
335 return Standard_False;
338 // activate OpenGL context
340 return Standard_False;
343 OpenGl_FrameBuffer* aFrameBuffer = theFBOPtr;
344 GLint aReadBufferPrev = GL_BACK;
345 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
347 aFrameBuffer->BindBuffer (GetGlContext());
351 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
352 GLint aDrawBufferPrev = GL_BACK;
353 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
354 glReadBuffer (aDrawBufferPrev);
357 GLint anAlignBack = 1;
358 glGetIntegerv (GL_PACK_ALIGNMENT, &anAlignBack);
359 if (theBuffer.rowAligmentBytes == 0)
361 theBuffer.rowAligmentBytes = 1;
363 glPixelStorei (GL_PACK_ALIGNMENT, theBuffer.rowAligmentBytes);
366 glReadPixels (0, 0, theBuffer.widthPx, theBuffer.heightPx, aFormat, aType, (GLvoid* )theBuffer.dataPtr);
367 glPixelStorei (GL_PACK_ALIGNMENT, anAlignBack);
369 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
371 aFrameBuffer->UnbindBuffer (GetGlContext());
375 glReadBuffer (aReadBufferPrev);
377 return Standard_True;
380 void OpenGl_GraphicDriver::RemoveView (const Graphic3d_CView& ACView)
382 if (GetMapOfViews().IsBound (ACView.ViewId))
383 GetMapOfViews().UnBind (ACView.ViewId);
385 if (GetMapOfWorkspaces().IsBound (ACView.WsId))
386 GetMapOfWorkspaces().UnBind (ACView.WsId);
388 OpenGl_CView *aCView = (OpenGl_CView *)ACView.ptrView;
390 ((Graphic3d_CView *)&ACView)->ptrView = NULL;
393 void OpenGl_GraphicDriver::SetLight (const Graphic3d_CView& ACView)
395 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
397 aCView->View->SetLights(ACView.Context);
400 void OpenGl_GraphicDriver::SetPlane (const Graphic3d_CView& ACView)
402 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
404 aCView->View->SetClippingPlanes(ACView.Context);
407 void OpenGl_GraphicDriver::SetVisualisation (const Graphic3d_CView& ACView)
409 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
412 aCView->View->SetVisualisation(ACView.Context);
413 aCView->WS->UseZBuffer() = ( ACView.Context.Visualization == 0? (ACView.Context.ZBufferActivity == 1) : (ACView.Context.ZBufferActivity != 0) );
417 void OpenGl_GraphicDriver::TransformStructure (const Graphic3d_CStructure& ACStructure)
419 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
421 astructure->SetTransformation(&(ACStructure.Transformation[0][0]));
424 void OpenGl_GraphicDriver::DegenerateStructure (const Graphic3d_CStructure& ACStructure)
426 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
428 astructure->SetDegenerateModel( ACStructure.ContextFillArea.DegenerationMode, ACStructure.ContextFillArea.SkipRatio );
431 void OpenGl_GraphicDriver::Transparency (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
433 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
435 aCView->WS->UseTransparency(AFlag);
438 void OpenGl_GraphicDriver::Update (const Graphic3d_CView& ACView, const Aspect_CLayer2d& ACUnderLayer, const Aspect_CLayer2d& ACOverLayer)
440 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
442 aCView->WS->Update(ACView,ACUnderLayer,ACOverLayer);
445 Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& ACView)
447 if (openglDisplay.IsNull())
448 return Standard_False;
450 if (GetMapOfViews().IsBound (ACView.ViewId))
451 GetMapOfViews().UnBind (ACView.ViewId);
453 if (GetMapOfWorkspaces().IsBound (ACView.WsId))
454 GetMapOfWorkspaces().UnBind (ACView.WsId);
456 Handle(OpenGl_Workspace) aWS = Handle(OpenGl_Workspace)::DownCast(openglDisplay->GetWindow( ACView.DefWindow.XWindow ));
459 aWS = new OpenGl_Workspace( openglDisplay, ACView.DefWindow, ACView.GContext );
460 openglDisplay->SetWindow( ACView.DefWindow.XWindow, aWS );
463 GetMapOfWorkspaces().Bind (ACView.WsId, aWS);
465 Handle(OpenGl_View) aView = new OpenGl_View( ACView.Context );
466 GetMapOfViews().Bind (ACView.ViewId, aView);
468 OpenGl_CView *aCView = new OpenGl_CView;
469 aCView->View = aView;
471 ACView.ptrView = aCView;
473 return Standard_True;
476 void OpenGl_GraphicDriver::ViewMapping (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
478 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
481 aCView->View->SetMapping(ACView);
484 aCView->WS->Resize(ACView.DefWindow);
489 void OpenGl_GraphicDriver::ViewOrientation (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
491 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
494 aCView->View->SetOrientation(ACView);
497 aCView->WS->Resize(ACView.DefWindow);
502 void OpenGl_GraphicDriver::SetBackFacingModel (const Graphic3d_CView& ACView)
504 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
506 aCView->View->SetBackfacing(ACView.Backfacing);
509 //=======================================================================
510 //function : AddZLayer
512 //=======================================================================
514 void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_CView& theCView,
515 const Standard_Integer theLayerId)
517 const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
519 aCView->View->AddZLayer (theLayerId);
522 //=======================================================================
523 //function : RemoveZLayer
525 //=======================================================================
527 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_CView& theCView,
528 const Standard_Integer theLayerId)
530 const OpenGl_CView* aCView = (const OpenGl_CView *)theCView.ptrView;
532 aCView->View->RemoveZLayer (theLayerId);