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& theCStructure,
72 const Standard_Boolean toCreate)
74 OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
75 if (aStructure == NULL)
79 aStructure->SetHighlightBox (GetSharedContext(), theCStructure.BoundBox);
81 aStructure->ClearHighlightBox (GetSharedContext());
84 void OpenGl_GraphicDriver::HighlightColor (const Graphic3d_CStructure& theCStructure,
85 const Standard_ShortReal R,
86 const Standard_ShortReal G,
87 const Standard_ShortReal B,
88 const Standard_Boolean toCreate)
90 OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
91 if (aStructure == NULL)
95 aStructure->SetHighlightColor (GetSharedContext(), R, G, B);
97 aStructure->ClearHighlightColor (GetSharedContext());
100 void OpenGl_GraphicDriver::NameSetStructure (const Graphic3d_CStructure& ACStructure)
102 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
105 Standard_Integer aStatus = 0;
106 if (ACStructure.highlight) aStatus |= OPENGL_NS_HIGHLIGHT;
107 if (!ACStructure.visible) aStatus |= OPENGL_NS_HIDE;
108 astructure->SetNamedStatus( aStatus );
112 void OpenGl_GraphicDriver::ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
114 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
117 aCView->View->SetClipLimit(ACView);
120 aCView->WS->Resize(ACView.DefWindow);
125 void OpenGl_GraphicDriver::DeactivateView (const Graphic3d_CView& ACView)
127 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
130 const Handle(OpenGl_View) aDummyView;
131 aCView->WS->SetActiveView(aDummyView);
135 void OpenGl_GraphicDriver::DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
137 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
139 aCView->View->SetFog(ACView, AFlag);
142 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)
144 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
146 return Standard_False;
148 Standard_Integer aWidth = aCView->WS->Width();
149 Standard_Integer aHeight = aCView->WS->Height();
150 Standard_ShortReal xr, yr;
151 if (aCView->View->ProjectObjectToRaster(aWidth, aHeight, AX, AY, AZ, xr, yr))
153 AU = (Standard_Integer) xr;
154 AV = aHeight - (Standard_Integer) yr;
155 return Standard_True;
158 return Standard_False;
161 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)
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();
171 Patched by P.Dolbey: the window pixel height decreased by one
172 in order for yr to remain within valid coordinate range [0; Ym -1]
173 where Ym means window pixel height.
175 return aCView->View->ProjectRasterToObject( aWidth, aHeight, AU, (AYM-1)-Aym-AV, Ax, Ay, Az );
178 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)
180 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
182 return Standard_False;
184 const Standard_Integer aWidth = aCView->WS->Width();
185 const Standard_Integer aHeight = aCView->WS->Height();
187 return aCView->View->ProjectRasterToObjectWithRay( aWidth, aHeight, AU, AYM-Aym-AV, Ax, Ay, Az, Dx, Dy, Dz );
190 void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& theCView)
192 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
194 aCView->WS->Resize (theCView.DefWindow);
197 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)
199 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
202 /*if( width <= 0 || height <= 0 )
203 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
205 aCView->WS->RedrawArea(ACView, ACUnderLayer, ACOverLayer, x, y, width, height);*/
206 // Always do full redraw
207 aCView->WS->Redraw(ACView, ACUnderLayer, ACOverLayer);
211 Graphic3d_PtrFrameBuffer OpenGl_GraphicDriver::FBOCreate (const Graphic3d_CView& ACView, const Standard_Integer theWidth, const Standard_Integer theHeight)
213 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
215 return aCView->WS->FBOCreate(theWidth, theHeight);
216 return (Graphic3d_PtrFrameBuffer)NULL;
219 Graphic3d_PtrFrameBuffer OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight)
221 // activate OpenGL context
226 OpenGl_FrameBuffer* aFrameBuffer = new OpenGl_FrameBuffer();
227 if (!aFrameBuffer->Init (GetGlContext(), theWidth, theHeight))
232 return (Graphic3d_PtrFrameBuffer )aFrameBuffer;
235 void OpenGl_GraphicDriver::FBORelease (const Graphic3d_CView& ACView, Graphic3d_PtrFrameBuffer& theFBOPtr)
237 if (theFBOPtr == NULL)
239 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
242 aCView->WS->FBORelease(theFBOPtr);
247 void OpenGl_Workspace::FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr)
249 // activate OpenGL context
253 // release the object
254 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer*)theFBOPtr;
255 aFrameBuffer->Release (GetGlContext());
259 void OpenGl_GraphicDriver::FBOGetDimensions (const Graphic3d_CView& ,
260 const Graphic3d_PtrFrameBuffer theFBOPtr,
261 Standard_Integer& theWidth, Standard_Integer& theHeight,
262 Standard_Integer& theWidthMax, Standard_Integer& theHeightMax)
264 if (theFBOPtr == NULL)
268 const OpenGl_FrameBuffer* aFrameBuffer = (const OpenGl_FrameBuffer* )theFBOPtr;
269 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
270 theHeight = aFrameBuffer->GetVPSizeY();
271 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
272 theHeightMax = aFrameBuffer->GetSizeY();
275 void OpenGl_GraphicDriver::FBOChangeViewport (const Graphic3d_CView& ,
276 Graphic3d_PtrFrameBuffer& theFBOPtr,
277 const Standard_Integer theWidth, const Standard_Integer theHeight)
279 if (theFBOPtr == NULL)
283 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
284 aFrameBuffer->ChangeViewport (theWidth, theHeight);
287 inline bool getDataFormat (const Image_PixMap& theData,
288 GLenum& thePixelFormat,
291 thePixelFormat = GL_RGB;
292 theDataType = GL_UNSIGNED_BYTE;
293 switch (theData.Format())
295 case Image_PixMap::ImgGray:
296 thePixelFormat = GL_DEPTH_COMPONENT;
297 theDataType = GL_UNSIGNED_BYTE;
299 case Image_PixMap::ImgRGB:
300 thePixelFormat = GL_RGB;
301 theDataType = GL_UNSIGNED_BYTE;
303 case Image_PixMap::ImgBGR:
304 thePixelFormat = GL_BGR;
305 theDataType = GL_UNSIGNED_BYTE;
307 case Image_PixMap::ImgRGBA:
308 case Image_PixMap::ImgRGB32:
309 thePixelFormat = GL_RGBA;
310 theDataType = GL_UNSIGNED_BYTE;
312 case Image_PixMap::ImgBGRA:
313 case Image_PixMap::ImgBGR32:
314 thePixelFormat = GL_BGRA;
315 theDataType = GL_UNSIGNED_BYTE;
317 case Image_PixMap::ImgGrayF:
318 thePixelFormat = GL_DEPTH_COMPONENT;
319 theDataType = GL_FLOAT;
321 case Image_PixMap::ImgRGBF:
322 thePixelFormat = GL_RGB;
323 theDataType = GL_FLOAT;
325 case Image_PixMap::ImgBGRF:
326 thePixelFormat = GL_BGR;
327 theDataType = GL_FLOAT;
329 case Image_PixMap::ImgRGBAF:
330 thePixelFormat = GL_RGBA;
331 theDataType = GL_FLOAT;
333 case Image_PixMap::ImgBGRAF:
334 thePixelFormat = GL_BGRA;
335 theDataType = GL_FLOAT;
342 Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& theCView,
343 Image_PixMap& theImage,
344 const Graphic3d_BufferType& theBufferType)
346 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
347 return (aCView != NULL) && aCView->WS->BufferDump ((OpenGl_FrameBuffer* )theCView.ptrFBO, theImage, theBufferType);
348 return Standard_False;
351 Standard_Boolean OpenGl_Workspace::BufferDump (OpenGl_FrameBuffer* theFBOPtr,
352 Image_PixMap& theImage,
353 const Graphic3d_BufferType& theBufferType)
355 GLenum aFormat, aType;
356 if (theImage.IsEmpty()
357 || !getDataFormat (theImage, aFormat, aType)
358 || ((theBufferType == Graphic3d_BT_Depth) && (aFormat != GL_DEPTH_COMPONENT))
361 return Standard_False;
365 GLint aReadBufferPrev = GL_BACK;
366 if (theFBOPtr != NULL && theFBOPtr->IsValid())
368 theFBOPtr->BindBuffer (GetGlContext());
372 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
373 GLint aDrawBufferPrev = GL_BACK;
374 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
375 glReadBuffer (aDrawBufferPrev);
378 GLint anAlignBack = 1;
379 glGetIntegerv (GL_PACK_ALIGNMENT, &anAlignBack);
380 GLint anExtraBytes = (GLint )theImage.RowExtraBytes();
381 GLint anAligment = Min (GLint(theImage.MaxRowAligmentBytes()), 8); // limit to 8 bytes for OpenGL
382 glPixelStorei (GL_PACK_ALIGNMENT, anAligment);
384 if (anExtraBytes >= anAligment)
387 for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
389 glReadPixels (0, GLint(aRow), GLsizei (theImage.SizeX()), 1, aFormat, aType, theImage.ChangeRow (aRow));
395 glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData());
396 theImage.SetTopDown (false); // image bottom-up in OpenGL
399 glPixelStorei (GL_PACK_ALIGNMENT, anAlignBack);
401 if (theFBOPtr != NULL && theFBOPtr->IsValid())
403 theFBOPtr->UnbindBuffer (GetGlContext());
407 glReadBuffer (aReadBufferPrev);
409 return Standard_True;
412 void OpenGl_GraphicDriver::RemoveView (const Graphic3d_CView& ACView)
414 if (myMapOfView.IsBound (ACView.ViewId))
415 myMapOfView.UnBind (ACView.ViewId);
417 if (myMapOfWS.IsBound (ACView.WsId))
418 myMapOfWS.UnBind (ACView.WsId);
420 OpenGl_CView *aCView = (OpenGl_CView *)ACView.ptrView;
422 ((Graphic3d_CView *)&ACView)->ptrView = NULL;
425 void OpenGl_GraphicDriver::SetLight (const Graphic3d_CView& ACView)
427 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
429 aCView->View->SetLights(ACView.Context);
432 void OpenGl_GraphicDriver::SetPlane (const Graphic3d_CView& ACView)
434 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
436 aCView->View->SetClippingPlanes(ACView.Context);
439 void OpenGl_GraphicDriver::SetVisualisation (const Graphic3d_CView& ACView)
441 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
444 aCView->View->SetVisualisation(ACView.Context);
445 aCView->WS->UseZBuffer() = ( ACView.Context.Visualization == 0? (ACView.Context.ZBufferActivity == 1) : (ACView.Context.ZBufferActivity != 0) );
449 void OpenGl_GraphicDriver::TransformStructure (const Graphic3d_CStructure& ACStructure)
451 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
453 astructure->SetTransformation(&(ACStructure.Transformation[0][0]));
456 void OpenGl_GraphicDriver::DegenerateStructure (const Graphic3d_CStructure& ACStructure)
458 OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
460 astructure->SetDegenerateModel( ACStructure.ContextFillArea.DegenerationMode, ACStructure.ContextFillArea.SkipRatio );
463 void OpenGl_GraphicDriver::Transparency (const Graphic3d_CView& ACView, const Standard_Boolean AFlag)
465 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
467 aCView->WS->UseTransparency(AFlag);
470 void OpenGl_GraphicDriver::Update (const Graphic3d_CView& ACView, const Aspect_CLayer2d& ACUnderLayer, const Aspect_CLayer2d& ACOverLayer)
472 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
474 aCView->WS->Update(ACView,ACUnderLayer,ACOverLayer);
477 Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& theCView)
479 if (openglDisplay.IsNull())
480 return Standard_False;
482 if (myMapOfView.IsBound (theCView.ViewId))
483 myMapOfView.UnBind (theCView.ViewId);
485 if (myMapOfWS.IsBound (theCView.WsId))
486 myMapOfWS.UnBind (theCView.WsId);
488 Handle(OpenGl_Workspace) aWS = Handle(OpenGl_Workspace)::DownCast(openglDisplay->GetWindow (theCView.DefWindow.XWindow));
491 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
492 aWS = new OpenGl_Workspace (openglDisplay, theCView.DefWindow, theCView.GContext, aShareCtx);
493 openglDisplay->SetWindow (theCView.DefWindow.XWindow, aWS);
496 myMapOfWS.Bind (theCView.WsId, aWS);
498 Handle(OpenGl_View) aView = new OpenGl_View (theCView.Context);
499 myMapOfView.Bind (theCView.ViewId, aView);
501 OpenGl_CView* aCView = new OpenGl_CView();
502 aCView->View = aView;
504 theCView.ptrView = aCView;
506 return Standard_True;
509 void OpenGl_GraphicDriver::ViewMapping (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
511 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
514 aCView->View->SetMapping(ACView);
517 aCView->WS->Resize(ACView.DefWindow);
522 void OpenGl_GraphicDriver::ViewOrientation (const Graphic3d_CView& ACView, const Standard_Boolean AWait)
524 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
527 aCView->View->SetOrientation(ACView);
530 aCView->WS->Resize(ACView.DefWindow);
535 void OpenGl_GraphicDriver::SetBackFacingModel (const Graphic3d_CView& ACView)
537 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
539 aCView->View->SetBackfacing(ACView.Backfacing);
542 //=======================================================================
543 //function : AddZLayer
545 //=======================================================================
547 void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_CView& theCView,
548 const Standard_Integer theLayerId)
550 const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
552 aCView->View->AddZLayer (theLayerId);
555 //=======================================================================
556 //function : RemoveZLayer
558 //=======================================================================
560 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_CView& theCView,
561 const Standard_Integer theLayerId)
563 const OpenGl_CView* aCView = (const OpenGl_CView *)theCView.ptrView;
565 aCView->View->RemoveZLayer (theLayerId);