1 // File: OpenGl_GraphicDriver_7.cxx
2 // Created: 20 October 2011
3 // Author: Sergey ZERCHANINOV
4 // Copyright: OPEN CASCADE 2011
6 #include <OpenGl_GraphicDriver.hxx>
8 #include <OpenGl_FrameBuffer.hxx>
10 #include <OpenGl_Structure.hxx>
11 #include <OpenGl_CView.hxx>
12 #include <OpenGl_Display.hxx>
14 /*----------------------------------------------------------------------*/
16 void OpenGl_GraphicDriver::ActivateView (const Graphic3d_CView& ACView)
18 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
20 aCView->WS->SetActiveView(aCView->View);
23 void OpenGl_GraphicDriver::AntiAliasing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
25 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
27 aCView->View->SetAntiAliasing(AFlag);
30 void OpenGl_GraphicDriver::Background (const Graphic3d_CView& ACView)
32 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
35 aCView->WS->SetBackgroundColor(ACView.DefWindow.Background.r,ACView.DefWindow.Background.g,ACView.DefWindow.Background.b);
36 aCView->WS->Invalidate();
40 void OpenGl_GraphicDriver::GradientBackground (const Graphic3d_CView& ACView,
41 const Quantity_Color& AColor1,
42 const Quantity_Color& AColor2,
43 const Aspect_GradientFillMethod AType)
45 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
48 aCView->View->SetBackgroundGradient(AColor1,AColor2,AType);
49 aCView->WS->Invalidate();
53 void OpenGl_GraphicDriver::Blink (const Graphic3d_CStructure &, const Standard_Boolean)
58 void OpenGl_GraphicDriver::BoundaryBox (const Graphic3d_CStructure& ACStructure, const Standard_Boolean Create)
60 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
65 astructure->SetHighlightBox(ACStructure.BoundBox);
67 astructure->ClearHighlightBox();
70 void OpenGl_GraphicDriver::HighlightColor (const Graphic3d_CStructure& ACStructure, const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B, const Standard_Boolean Create)
72 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
77 astructure->SetHighlightColor(R,G,B);
79 astructure->ClearHighlightColor();
82 void OpenGl_GraphicDriver::NameSetStructure (const Graphic3d_CStructure& ACStructure)
84 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
87 Standard_Integer aStatus = 0;
88 if (ACStructure.highlight) aStatus |= OPENGL_NS_HIGHLIGHT;
89 if (!ACStructure.visible) aStatus |= OPENGL_NS_HIDE;
90 astructure->SetNamedStatus( aStatus );
94 void OpenGl_GraphicDriver::ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
96 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
99 aCView->View->SetClipLimit(ACView);
102 aCView->WS->Resize(ACView.DefWindow);
103 aCView->WS->Invalidate();
108 void OpenGl_GraphicDriver::DeactivateView (const Graphic3d_CView& ACView)
110 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
113 const Handle(OpenGl_View) aDummyView;
114 aCView->WS->SetActiveView(aDummyView);
118 void OpenGl_GraphicDriver::DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
120 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
122 aCView->View->SetFog(ACView, AFlag);
125 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)
127 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
129 return Standard_False;
131 Standard_Integer aWidth = aCView->WS->Width();
132 Standard_Integer aHeight = aCView->WS->Height();
133 Standard_ShortReal xr, yr;
134 if (aCView->View->ProjectObjectToRaster(aWidth, aHeight, AX, AY, AZ, xr, yr))
136 AU = (Standard_Integer) xr;
137 AV = aHeight - (Standard_Integer) yr;
138 return Standard_True;
141 return Standard_False;
144 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)
146 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
148 return Standard_False;
150 const Standard_Integer aWidth = aCView->WS->Width();
151 const Standard_Integer aHeight = aCView->WS->Height();
154 Patched by P.Dolbey: the window pixel height decreased by one
155 in order for yr to remain within valid coordinate range [0; Ym -1]
156 where Ym means window pixel height.
158 return aCView->View->ProjectRasterToObject( aWidth, aHeight, AU, (AYM-1)-Aym-AV, Ax, Ay, Az );
161 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)
163 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
165 return Standard_False;
167 const Standard_Integer aWidth = aCView->WS->Width();
168 const Standard_Integer aHeight = aCView->WS->Height();
170 return aCView->View->ProjectRasterToObjectWithRay( aWidth, aHeight, AU, AYM-Aym-AV, Ax, Ay, Az, Dx, Dy, Dz );
173 void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& theCView)
175 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
177 aCView->WS->Resize (theCView.DefWindow);
180 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)
182 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
185 /*if( width <= 0 || height <= 0 )
186 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
188 aCView->WS->RedrawArea(ACView, ACUnderLayer, ACOverLayer, x, y, width, height);*/
189 // Always do full redraw
190 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
194 Graphic3d_PtrFrameBuffer OpenGl_GraphicDriver::FBOCreate (const Graphic3d_CView& ACView, const Standard_Integer theWidth, const Standard_Integer theHeight)
196 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
198 return aCView->WS->FBOCreate(theWidth, theHeight);
199 return (Graphic3d_PtrFrameBuffer)NULL;
202 Graphic3d_PtrFrameBuffer OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight)
204 // activate OpenGL context
209 OpenGl_FrameBuffer* aFrameBuffer = new OpenGl_FrameBuffer();
210 if (!aFrameBuffer->Init (GetGlContext(), theWidth, theHeight))
215 return (Graphic3d_PtrFrameBuffer )aFrameBuffer;
218 void OpenGl_GraphicDriver::FBORelease (const Graphic3d_CView& ACView, Graphic3d_PtrFrameBuffer& theFBOPtr)
220 if (theFBOPtr == NULL)
222 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
225 aCView->WS->FBORelease(theFBOPtr);
230 void OpenGl_Workspace::FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr)
232 // activate OpenGL context
236 // release the object
237 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer*)theFBOPtr;
238 aFrameBuffer->Release (GetGlContext());
242 void OpenGl_GraphicDriver::FBOGetDimensions (const Graphic3d_CView& ,
243 const Graphic3d_PtrFrameBuffer theFBOPtr,
244 Standard_Integer& theWidth, Standard_Integer& theHeight,
245 Standard_Integer& theWidthMax, Standard_Integer& theHeightMax)
247 if (theFBOPtr == NULL)
251 const OpenGl_FrameBuffer* aFrameBuffer = (const OpenGl_FrameBuffer* )theFBOPtr;
252 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
253 theHeight = aFrameBuffer->GetVPSizeY();
254 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
255 theHeightMax = aFrameBuffer->GetSizeY();
258 void OpenGl_GraphicDriver::FBOChangeViewport (const Graphic3d_CView& ,
259 Graphic3d_PtrFrameBuffer& theFBOPtr,
260 const Standard_Integer theWidth, const Standard_Integer theHeight)
262 if (theFBOPtr == NULL)
266 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
267 aFrameBuffer->ChangeViewport (theWidth, theHeight);
272 #define GL_BGR 0x80E0
275 #define GL_BGRA 0x80E1
278 static inline GLenum TFormatToGLEnum (const TRawBufferDataFormat theTFormat)
282 case TRGB: return GL_RGB;
283 case TBGR: return GL_BGR;
284 case TRGBA: return GL_RGBA;
285 case TBGRA: return GL_BGRA;
286 case TDepthComponent: return GL_DEPTH_COMPONENT;
287 case TRed: return GL_RED;
288 case TGreen: return GL_GREEN;
289 case TBlue: return GL_BLUE;
290 case TAlpha: return GL_ALPHA;
295 static inline GLenum TTypeToGLEnum (const TRawBufferDataType theTType)
299 case TUByte: return GL_UNSIGNED_BYTE;
300 case TFloat: return GL_FLOAT;
305 Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& ACView, Image_CRawBufferData& theBuffer)
307 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
309 return aCView->WS->BufferDump((OpenGl_FrameBuffer *)ACView.ptrFBO,theBuffer);
310 return Standard_False;
313 Standard_Boolean OpenGl_Workspace::BufferDump (OpenGl_FrameBuffer *theFBOPtr, Image_CRawBufferData& theBuffer)
315 GLenum aFormat = TFormatToGLEnum (theBuffer.format);
316 GLenum aType = TTypeToGLEnum (theBuffer.type);
319 if (aFormat == 0 || aType == 0 ||
320 theBuffer.widthPx == 0 || theBuffer.heightPx == 0 ||
321 theBuffer.dataPtr == NULL)
323 return Standard_False;
326 // activate OpenGL context
328 return Standard_False;
331 OpenGl_FrameBuffer* aFrameBuffer = theFBOPtr;
332 GLint aReadBufferPrev = GL_BACK;
333 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
335 aFrameBuffer->BindBuffer (GetGlContext());
339 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
340 GLint aDrawBufferPrev = GL_BACK;
341 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
342 glReadBuffer (aDrawBufferPrev);
345 GLint anAlignBack = 1;
346 glGetIntegerv (GL_PACK_ALIGNMENT, &anAlignBack);
347 if (theBuffer.rowAligmentBytes == 0)
349 theBuffer.rowAligmentBytes = 1;
351 glPixelStorei (GL_PACK_ALIGNMENT, theBuffer.rowAligmentBytes);
354 glReadPixels (0, 0, theBuffer.widthPx, theBuffer.heightPx, aFormat, aType, (GLvoid* )theBuffer.dataPtr);
355 glPixelStorei (GL_PACK_ALIGNMENT, anAlignBack);
357 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
359 aFrameBuffer->UnbindBuffer (GetGlContext());
363 glReadBuffer (aReadBufferPrev);
365 return Standard_True;
368 void OpenGl_GraphicDriver::RemoveView (const Graphic3d_CView& ACView)
370 if (GetMapOfViews().IsBound (ACView.ViewId))
371 GetMapOfViews().UnBind (ACView.ViewId);
373 if (GetMapOfWorkspaces().IsBound (ACView.WsId))
374 GetMapOfWorkspaces().UnBind (ACView.WsId);
376 OpenGl_CView *aCView = (OpenGl_CView *)ACView.ptrView;
378 ((Graphic3d_CView *)&ACView)->ptrView = NULL;
381 void OpenGl_GraphicDriver::SetLight (const Graphic3d_CView& ACView)
383 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
385 aCView->View->SetLights(ACView.Context);
388 void OpenGl_GraphicDriver::SetPlane (const Graphic3d_CView& ACView)
390 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
392 aCView->View->SetClippingPlanes(ACView.Context);
395 void OpenGl_GraphicDriver::SetVisualisation (const Graphic3d_CView& ACView)
397 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
400 aCView->View->SetVisualisation(ACView.Context);
401 aCView->WS->UseZBuffer() = ( ACView.Context.Visualization == 0? (ACView.Context.ZBufferActivity == 1) : (ACView.Context.ZBufferActivity != 0) );
405 void OpenGl_GraphicDriver::TransformStructure (const Graphic3d_CStructure& ACStructure)
407 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
409 astructure->SetTransformation(&(ACStructure.Transformation[0][0]));
412 void OpenGl_GraphicDriver::DegenerateStructure (const Graphic3d_CStructure& ACStructure)
414 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
416 astructure->SetDegenerateModel( ACStructure.ContextFillArea.DegenerationMode, ACStructure.ContextFillArea.SkipRatio );
419 void OpenGl_GraphicDriver::Transparency (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
421 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
423 aCView->WS->UseTransparency(AFlag);
426 void OpenGl_GraphicDriver::Update (const Graphic3d_CView& ACView, const Aspect_CLayer2d& ACUnderLayer, const Aspect_CLayer2d& ACOverLayer)
428 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
430 aCView->WS->Update(ACView,ACUnderLayer,ACOverLayer);
433 Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& ACView)
435 if (openglDisplay.IsNull())
436 return Standard_False;
438 if (GetMapOfViews().IsBound (ACView.ViewId))
439 GetMapOfViews().UnBind (ACView.ViewId);
441 if (GetMapOfWorkspaces().IsBound (ACView.WsId))
442 GetMapOfWorkspaces().UnBind (ACView.WsId);
444 Handle(OpenGl_Workspace) aWS = Handle(OpenGl_Workspace)::DownCast(openglDisplay->GetWindow( ACView.DefWindow.XWindow ));
447 aWS = new OpenGl_Workspace( openglDisplay, ACView.DefWindow, ACView.GContext );
448 openglDisplay->SetWindow( ACView.DefWindow.XWindow, aWS );
451 GetMapOfWorkspaces().Bind (ACView.WsId, aWS);
453 Handle(OpenGl_View) aView = new OpenGl_View( ACView.Context );
454 GetMapOfViews().Bind (ACView.ViewId, aView);
456 OpenGl_CView *aCView = new OpenGl_CView;
457 aCView->View = aView;
459 ACView.ptrView = aCView;
461 return Standard_True;
464 void OpenGl_GraphicDriver::ViewMapping (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
466 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
469 aCView->View->SetMapping(ACView);
472 aCView->WS->Resize(ACView.DefWindow);
473 aCView->WS->Invalidate();
478 void OpenGl_GraphicDriver::ViewOrientation (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
480 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
483 aCView->View->SetOrientation(ACView);
486 aCView->WS->Resize(ACView.DefWindow);
487 aCView->WS->Invalidate();
492 void OpenGl_GraphicDriver::SetBackFacingModel (const Graphic3d_CView& ACView)
494 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
496 aCView->View->SetBackfacing(ACView.Backfacing);
499 //=======================================================================
500 //function : AddZLayer
502 //=======================================================================
504 void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_CView& theCView,
505 const Standard_Integer theLayerId)
507 const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
509 aCView->View->AddZLayer (theLayerId);
512 //=======================================================================
513 //function : RemoveZLayer
515 //=======================================================================
517 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_CView& theCView,
518 const Standard_Integer theLayerId)
520 const OpenGl_CView* aCView = (const OpenGl_CView *)theCView.ptrView;
522 aCView->View->RemoveZLayer (theLayerId);