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 if (ACStructure.pick) aStatus |= OPENGL_NS_PICK;
91 astructure->SetNamedStatus( aStatus );
95 void OpenGl_GraphicDriver::ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
97 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
100 aCView->View->SetClipLimit(ACView);
101 if (!AWait && !ACView.DefBitmap.bitmap)
103 aCView->WS->Resize(ACView.DefWindow);
104 aCView->WS->Invalidate();
109 void OpenGl_GraphicDriver::DeactivateView (const Graphic3d_CView& ACView)
111 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
114 const Handle(OpenGl_View) aDummyView;
115 aCView->WS->SetActiveView(aDummyView);
119 void OpenGl_GraphicDriver::DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
121 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
123 aCView->View->SetFog(ACView, AFlag);
126 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)
128 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
130 return Standard_False;
132 Standard_Integer aWidth = aCView->WS->Width();
133 Standard_Integer aHeight = aCView->WS->Height();
135 //if we want project something before to dump it into pixmap
136 //For right copution of projection before dumping to pixmap
137 if ( ACView.DefBitmap.bitmap )
139 aWidth = ACView.DefBitmap.width;
140 aHeight = ACView.DefBitmap.height;
143 Standard_ShortReal xr, yr;
144 if (aCView->View->ProjectObjectToRaster(aWidth, aHeight, AX, AY, AZ, xr, yr))
146 AU = (Standard_Integer) xr;
147 AV = aHeight - (Standard_Integer) yr;
148 return Standard_True;
151 return Standard_False;
154 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)
156 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
158 return Standard_False;
160 const Standard_Integer aWidth = aCView->WS->Width();
161 const Standard_Integer aHeight = aCView->WS->Height();
164 Patched by P.Dolbey: the window pixel height decreased by one
165 in order for yr to remain within valid coordinate range [0; Ym -1]
166 where Ym means window pixel height.
168 return aCView->View->ProjectRasterToObject( aWidth, aHeight, AU, (AYM-1)-Aym-AV, Ax, Ay, Az );
171 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)
173 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
175 return Standard_False;
177 const Standard_Integer aWidth = aCView->WS->Width();
178 const Standard_Integer aHeight = aCView->WS->Height();
180 return aCView->View->ProjectRasterToObjectWithRay( aWidth, aHeight, AU, AYM-Aym-AV, Ax, Ay, Az, Dx, Dy, Dz );
183 void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& ACView)
185 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
187 if( !ACView.DefBitmap.bitmap )
188 aCView->WS->Resize(ACView.DefWindow);
191 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)
193 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
196 /*if( width <= 0 || height <= 0 )
197 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
199 aCView->WS->RedrawArea(ACView, ACUnderLayer, ACOverLayer, x, y, width, height);*/
200 // Always do full redraw
201 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
205 Graphic3d_PtrFrameBuffer OpenGl_GraphicDriver::FBOCreate (const Graphic3d_CView& ACView, const Standard_Integer theWidth, const Standard_Integer theHeight)
207 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
209 return aCView->WS->FBOCreate(theWidth, theHeight);
210 return (Graphic3d_PtrFrameBuffer)NULL;
213 Graphic3d_PtrFrameBuffer OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight)
215 // activate OpenGL context
220 OpenGl_FrameBuffer* aFrameBuffer = new OpenGl_FrameBuffer();
221 if (!aFrameBuffer->Init (GetGlContext(), theWidth, theHeight))
226 return (Graphic3d_PtrFrameBuffer )aFrameBuffer;
229 void OpenGl_GraphicDriver::FBORelease (const Graphic3d_CView& ACView, Graphic3d_PtrFrameBuffer& theFBOPtr)
231 if (theFBOPtr == NULL)
233 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
236 aCView->WS->FBORelease(theFBOPtr);
241 void OpenGl_Workspace::FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr)
243 // activate OpenGL context
247 // release the object
248 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer*)theFBOPtr;
249 aFrameBuffer->Release (GetGlContext());
253 void OpenGl_GraphicDriver::FBOGetDimensions (const Graphic3d_CView& ,
254 const Graphic3d_PtrFrameBuffer theFBOPtr,
255 Standard_Integer& theWidth, Standard_Integer& theHeight,
256 Standard_Integer& theWidthMax, Standard_Integer& theHeightMax)
258 if (theFBOPtr == NULL)
262 const OpenGl_FrameBuffer* aFrameBuffer = (const OpenGl_FrameBuffer* )theFBOPtr;
263 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
264 theHeight = aFrameBuffer->GetVPSizeY();
265 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
266 theHeightMax = aFrameBuffer->GetSizeY();
269 void OpenGl_GraphicDriver::FBOChangeViewport (const Graphic3d_CView& ,
270 Graphic3d_PtrFrameBuffer& theFBOPtr,
271 const Standard_Integer theWidth, const Standard_Integer theHeight)
273 if (theFBOPtr == NULL)
277 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
278 aFrameBuffer->ChangeViewport (theWidth, theHeight);
283 #define GL_BGR 0x80E0
286 #define GL_BGRA 0x80E1
289 static inline GLenum TFormatToGLEnum (const TRawBufferDataFormat theTFormat)
293 case TRGB: return GL_RGB;
294 case TBGR: return GL_BGR;
295 case TRGBA: return GL_RGBA;
296 case TBGRA: return GL_BGRA;
297 case TDepthComponent: return GL_DEPTH_COMPONENT;
298 case TRed: return GL_RED;
299 case TGreen: return GL_GREEN;
300 case TBlue: return GL_BLUE;
301 case TAlpha: return GL_ALPHA;
306 static inline GLenum TTypeToGLEnum (const TRawBufferDataType theTType)
310 case TUByte: return GL_UNSIGNED_BYTE;
311 case TFloat: return GL_FLOAT;
316 Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& ACView, Image_CRawBufferData& theBuffer)
318 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
320 return aCView->WS->BufferDump((OpenGl_FrameBuffer *)ACView.ptrFBO,theBuffer);
321 return Standard_False;
324 Standard_Boolean OpenGl_Workspace::BufferDump (OpenGl_FrameBuffer *theFBOPtr, Image_CRawBufferData& theBuffer)
326 GLenum aFormat = TFormatToGLEnum (theBuffer.format);
327 GLenum aType = TTypeToGLEnum (theBuffer.type);
330 if (aFormat == 0 || aType == 0 ||
331 theBuffer.widthPx == 0 || theBuffer.heightPx == 0 ||
332 theBuffer.dataPtr == NULL)
334 return Standard_False;
337 // activate OpenGL context
339 return Standard_False;
342 OpenGl_FrameBuffer* aFrameBuffer = theFBOPtr;
343 GLint aReadBufferPrev = GL_BACK;
344 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
346 aFrameBuffer->BindBuffer (GetGlContext());
350 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
351 GLint aDrawBufferPrev = GL_BACK;
352 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
353 glReadBuffer (aDrawBufferPrev);
356 GLint anAlignBack = 1;
357 glGetIntegerv (GL_PACK_ALIGNMENT, &anAlignBack);
358 if (theBuffer.rowAligmentBytes == 0)
360 theBuffer.rowAligmentBytes = 1;
362 glPixelStorei (GL_PACK_ALIGNMENT, theBuffer.rowAligmentBytes);
365 glReadPixels (0, 0, theBuffer.widthPx, theBuffer.heightPx, aFormat, aType, (GLvoid* )theBuffer.dataPtr);
366 glPixelStorei (GL_PACK_ALIGNMENT, anAlignBack);
368 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
370 aFrameBuffer->UnbindBuffer (GetGlContext());
374 glReadBuffer (aReadBufferPrev);
376 return Standard_True;
379 void OpenGl_GraphicDriver::RemoveView (const Graphic3d_CView& ACView)
381 if (GetMapOfViews().IsBound (ACView.ViewId))
382 GetMapOfViews().UnBind (ACView.ViewId);
384 if (GetMapOfWorkspaces().IsBound (ACView.WsId))
385 GetMapOfWorkspaces().UnBind (ACView.WsId);
387 OpenGl_CView *aCView = (OpenGl_CView *)ACView.ptrView;
389 ((Graphic3d_CView *)&ACView)->ptrView = NULL;
392 void OpenGl_GraphicDriver::SetLight (const Graphic3d_CView& ACView)
394 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
396 aCView->View->SetLights(ACView.Context);
399 void OpenGl_GraphicDriver::SetPlane (const Graphic3d_CView& ACView)
401 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
403 aCView->View->SetClippingPlanes(ACView.Context);
406 void OpenGl_GraphicDriver::SetVisualisation (const Graphic3d_CView& ACView)
408 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
411 aCView->View->SetVisualisation(ACView.Context);
412 aCView->WS->UseZBuffer() = ( ACView.Context.Visualization == 0? (ACView.Context.ZBufferActivity == 1) : (ACView.Context.ZBufferActivity != 0) );
416 void OpenGl_GraphicDriver::TransformStructure (const Graphic3d_CStructure& ACStructure)
418 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
420 astructure->SetTransformation(&(ACStructure.Transformation[0][0]));
423 void OpenGl_GraphicDriver::DegenerateStructure (const Graphic3d_CStructure& ACStructure)
425 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
427 astructure->SetDegenerateModel( ACStructure.ContextFillArea.DegenerationMode, ACStructure.ContextFillArea.SkipRatio );
430 void OpenGl_GraphicDriver::Transparency (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
432 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
434 aCView->WS->UseTransparency(AFlag);
437 void OpenGl_GraphicDriver::Update (const Graphic3d_CView& ACView, const Aspect_CLayer2d& ACUnderLayer, const Aspect_CLayer2d& ACOverLayer)
439 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
441 aCView->WS->Update(ACView,ACUnderLayer,ACOverLayer);
444 Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& ACView)
446 if (openglDisplay.IsNull())
447 return Standard_False;
449 if (GetMapOfViews().IsBound (ACView.ViewId))
450 GetMapOfViews().UnBind (ACView.ViewId);
452 if (GetMapOfWorkspaces().IsBound (ACView.WsId))
453 GetMapOfWorkspaces().UnBind (ACView.WsId);
455 Handle(OpenGl_Workspace) aWS = Handle(OpenGl_Workspace)::DownCast(openglDisplay->GetWindow( ACView.DefWindow.XWindow ));
458 aWS = new OpenGl_Workspace( openglDisplay, ACView.DefWindow, ACView.GContext );
459 openglDisplay->SetWindow( ACView.DefWindow.XWindow, aWS );
462 GetMapOfWorkspaces().Bind (ACView.WsId, aWS);
464 Handle(OpenGl_View) aView = new OpenGl_View( ACView.Context );
465 GetMapOfViews().Bind (ACView.ViewId, aView);
467 OpenGl_CView *aCView = new OpenGl_CView;
468 aCView->View = aView;
470 ACView.ptrView = aCView;
472 return Standard_True;
475 void OpenGl_GraphicDriver::ViewMapping (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
477 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
480 aCView->View->SetMapping(ACView);
481 if (!AWait && !ACView.DefBitmap.bitmap)
483 aCView->WS->Resize(ACView.DefWindow);
484 aCView->WS->Invalidate();
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);
495 if (!AWait && !ACView.DefBitmap.bitmap)
497 aCView->WS->Resize(ACView.DefWindow);
498 aCView->WS->Invalidate();
503 void OpenGl_GraphicDriver::SetBackFacingModel (const Graphic3d_CView& ACView)
505 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
507 aCView->View->SetBackfacing(ACView.Backfacing);