2 // File OpenGl_GraphicDriver_7.cxx
3 // Created Mardi 28 janvier 1997
5 // Modified GG 10/11/99 PRO19603 Change the Redraw method (add redraw area capabillity)
6 // EUG 07/10/99 G003 Add DegenerateStructure() and
7 // SetBackFacingModel() methods.
9 //-Copyright MatraDatavision 1997
13 //-Design Declaration des variables specifiques aux Drivers
15 //-Warning Un driver encapsule les Pex et OpenGl drivers
24 #include <OpenGl_GraphicDriver.jxx>
26 #include <Aspect_DriverDefinitionError.hxx>
27 #include <InterfaceGraphic_RawBufferData.hxx>
29 #include <OpenGl_tgl_funcs.hxx>
30 #include <OpenGl_tsm_ws.hxx>
31 #include <OpenGl_tgl_tox.hxx>
32 #include <OpenGl_txgl.hxx>
33 #include <OpenGl_FrameBuffer.hxx>
37 //-Global data definitions
41 void OpenGl_GraphicDriver::ActivateView (const Graphic3d_CView& ACView) {
43 Graphic3d_CView MyCView = ACView;
46 PrintFunction ("call_togl_activateview");
47 PrintCView (MyCView, 1);
49 call_togl_activateview (&MyCView);
53 void OpenGl_GraphicDriver::AntiAliasing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag) {
55 Graphic3d_CView MyCView = ACView;
58 PrintFunction ("call_togl_antialiasing");
59 PrintCView (MyCView, 1);
60 PrintBoolean ("AFlag", AFlag);
62 call_togl_antialiasing (&MyCView, (AFlag ? 1 : 0));
66 void OpenGl_GraphicDriver::Background (const Graphic3d_CView& ACView) {
68 Graphic3d_CView MyCView = ACView;
71 PrintFunction ("call_togl_background");
72 PrintCView (MyCView, 1);
74 call_togl_background (&MyCView);
78 void OpenGl_GraphicDriver::GradientBackground(const Graphic3d_CView& ACView,
79 const Quantity_Color& AColor1,
80 const Quantity_Color& AColor2,
81 const Aspect_GradientFillMethod AType){
83 Graphic3d_CView MyCView = ACView;
86 PrintFunction ("call_togl_gradient_background");
87 PrintCView (MyCView, 1);
89 Standard_Real R1,G1,B1,R2,G2,B2;
90 AColor1.Values( R1, G1, B1, Quantity_TOC_RGB );
91 AColor2.Values( R2, G2, B2, Quantity_TOC_RGB );
92 TEL_COLOUR tcolor1, tcolor2;
102 call_togl_gradient_background (MyCView.WsId, AType, &tcolor1, &tcolor2);
107 void OpenGl_GraphicDriver::Blink (const Graphic3d_CStructure& ACStructure, const Standard_Boolean Create) {
109 Graphic3d_CStructure MyCStructure = ACStructure;
112 PrintFunction ("call_togl_blink");
113 PrintCStructure (MyCStructure, 1);
114 PrintBoolean ("Create", Create);
116 call_togl_blink (&MyCStructure, (Create ? 1 : 0));
120 void OpenGl_GraphicDriver::BoundaryBox (const Graphic3d_CStructure& ACStructure, const Standard_Boolean Create) {
122 Graphic3d_CStructure MyCStructure = ACStructure;
125 PrintFunction ("call_togl_boundarybox");
126 PrintCStructure (MyCStructure, 1);
127 PrintBoolean ("Create", Create);
129 call_togl_boundarybox (&MyCStructure, (Create ? 1 : 0));
133 void OpenGl_GraphicDriver::HighlightColor (const Graphic3d_CStructure& ACStructure, const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B, const Standard_Boolean Create) {
135 Graphic3d_CStructure MyCStructure = ACStructure;
138 PrintFunction ("call_togl_highlightcolor");
139 PrintCStructure (MyCStructure, 1);
140 PrintShortReal ("R", R);
141 PrintShortReal ("G", G);
142 PrintShortReal ("B", B);
144 CALL_DEF_COLOR acolor;
148 call_togl_highlightcolor (&MyCStructure, &acolor, (Create ? 1 : 0));
152 void OpenGl_GraphicDriver::NameSetStructure (const Graphic3d_CStructure& ACStructure) {
154 Graphic3d_CStructure MyCStructure = ACStructure;
157 PrintFunction ("call_togl_namesetstructure");
158 PrintCStructure (MyCStructure, 1);
160 call_togl_namesetstructure (&MyCStructure);
164 void OpenGl_GraphicDriver::ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait) {
166 Graphic3d_CView MyCView = ACView;
169 PrintFunction ("call_togl_cliplimit");
170 PrintCView (MyCView, 1);
171 PrintBoolean ("AWait", AWait);
173 call_togl_cliplimit (&MyCView, (AWait ? 1 : 0));
177 void OpenGl_GraphicDriver::DeactivateView (const Graphic3d_CView& ACView) {
178 Graphic3d_CView MyCView = ACView;
181 PrintFunction ("call_togl_deactivateview");
182 PrintCView (MyCView, 1);
184 call_togl_deactivateview (&MyCView);
188 void OpenGl_GraphicDriver::DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag) {
190 Graphic3d_CView MyCView = ACView;
193 PrintFunction ("call_togl_depthcueing");
194 PrintCView (MyCView, 1);
195 PrintBoolean ("AFlag", AFlag);
197 call_togl_depthcueing (&MyCView, (AFlag ? 1 : 0));
201 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) {
203 Graphic3d_CView MyCView = ACView;
205 Standard_Integer Result;
208 PrintFunction ("call_togl_project_raster");
209 PrintCView (MyCView, 1);
211 //if we want project something before to dump it into pixmap
212 if ( ACView.DefBitmap.bitmap ) {
213 Result = call_togl_adopt_to_rect( ACView.ViewId,
214 ACView.DefBitmap.width,
215 ACView.DefBitmap.height );
217 PrintIResult ("call_togl_adopt_to_rect", Result);
220 return Standard_False;
223 Result = call_togl_project_raster (ACView.ViewId, AX, AY, AZ, &AU, &AV);
225 PrintIResult ("call_togl_project_raster", Result);
228 if ( ACView.DefBitmap.bitmap ) {
229 Result = call_togl_adopt_to_rect( ACView.ViewId,
231 ACView.DefWindow.dy );
233 PrintIResult ("call_togl_adopt_to_rect", Result);
236 return Standard_False;
239 return (Result == 0 ? Standard_True : Standard_False);
243 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) {
245 Graphic3d_CView MyCView = ACView;
247 Standard_Integer Result;
250 PrintFunction ("call_togl_unproject_raster");
251 PrintCView (MyCView, 1);
253 Result = call_togl_unproject_raster
254 (ACView.ViewId, Axm, Aym, AXM, AYM, AU, AV, &Ax, &Ay, &Az);
256 PrintIResult ("call_togl_unproject_raster", Result);
258 return (Result == 0 ? Standard_True : Standard_False);
262 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) {
264 Graphic3d_CView MyCView = ACView;
266 Standard_Integer Result;
269 PrintFunction ("call_togl_unproject_raster_with_ray");
270 PrintCView (MyCView, 1);
272 Result = call_togl_unproject_raster_with_ray
273 (ACView.ViewId, Axm, Aym, AXM, AYM, AU, AV, &Ax, &Ay, &Az, &Dx, &Dy, &Dz);
275 PrintIResult ("call_togl_unproject_raster_with_ray", Result);
277 return (Result == 0 ? Standard_True : Standard_False);
281 void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& ACView) {
283 Graphic3d_CView MyCView = ACView;
286 PrintFunction ("call_togl_ratio_window");
287 PrintCView (MyCView, 1);
289 call_togl_ratio_window (&MyCView);
293 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) {
295 Graphic3d_CView MyCView = ACView;
296 Aspect_CLayer2d MyCUnderLayer = ACUnderLayer;
297 Aspect_CLayer2d MyCOverLayer = ACOverLayer;
300 PrintFunction ("call_togl_redraw");
301 PrintCView (MyCView, 1);
304 if( width <= 0 || height <= 0 )
305 call_togl_redraw (&MyCView, &MyCUnderLayer, &MyCOverLayer);
307 call_togl_redraw_area (&MyCView, &MyCUnderLayer, &MyCOverLayer,
308 x, y, width, height);
313 Graphic3d_PtrFrameBuffer OpenGl_GraphicDriver::FBOCreate (const Graphic3d_CView& theCView,
314 const Standard_Integer theWidth,
315 const Standard_Integer theHeight) {
316 // activate OpenGL context
318 if ((TsmGetWSAttri (theCView.WsId, WSWindow, &aData) != TSuccess) ||
319 (TxglWinset (call_thedisplay, (WINDOW) aData.ldata) != TSuccess))
324 OpenGl_FrameBuffer* aFrameBuffer = new OpenGl_FrameBuffer();
325 if (!aFrameBuffer->Init (theWidth, theHeight))
330 return (Graphic3d_PtrFrameBuffer )aFrameBuffer;
333 void OpenGl_GraphicDriver::FBORelease (const Graphic3d_CView& theCView,
334 Graphic3d_PtrFrameBuffer& theFBOPtr)
336 if (theFBOPtr == NULL)
340 // activate OpenGL context
342 if ((TsmGetWSAttri (theCView.WsId, WSWindow, &aData) != TSuccess) ||
343 (TxglWinset (call_thedisplay, (WINDOW) aData.ldata) != TSuccess))
347 // release the object
348 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
353 void OpenGl_GraphicDriver::FBOGetDimensions (const Graphic3d_CView& ,
354 const Graphic3d_PtrFrameBuffer theFBOPtr,
355 Standard_Integer& theWidth, Standard_Integer& theHeight,
356 Standard_Integer& theWidthMax, Standard_Integer& theHeightMax)
358 if (theFBOPtr == NULL)
362 const OpenGl_FrameBuffer* aFrameBuffer = (const OpenGl_FrameBuffer* )theFBOPtr;
363 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
364 theHeight = aFrameBuffer->GetVPSizeY();
365 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
366 theHeightMax = aFrameBuffer->GetSizeY();
369 void OpenGl_GraphicDriver::FBOChangeViewport (const Graphic3d_CView& ,
370 Graphic3d_PtrFrameBuffer& theFBOPtr,
371 const Standard_Integer theWidth, const Standard_Integer theHeight)
373 if (theFBOPtr == NULL)
377 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
378 aFrameBuffer->ChangeViewport (theWidth, theHeight);
383 #define GL_BGR 0x80E0
386 #define GL_BGRA 0x80E1
390 static inline GLenum TFormatToGLEnum (const TRawBufferDataFormat theTFormat)
394 case TRGB: return GL_RGB;
395 case TBGR: return GL_BGR;
396 case TRGBA: return GL_RGBA;
397 case TBGRA: return GL_BGRA;
398 case TDepthComponent: return GL_DEPTH_COMPONENT;
399 case TRed: return GL_RED;
400 case TGreen: return GL_GREEN;
401 case TBlue: return GL_BLUE;
402 case TAlpha: return GL_ALPHA;
407 static inline GLenum TTypeToGLEnum (const TRawBufferDataType theTType)
411 case TUByte: return GL_UNSIGNED_BYTE;
412 case TFloat: return GL_FLOAT;
417 Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& theCView,
418 Image_CRawBufferData& theBuffer)
420 GLenum aFormat = TFormatToGLEnum (theBuffer.format);
421 GLenum aType = TTypeToGLEnum (theBuffer.type);
424 if (aFormat == 0 || aType == 0 ||
425 theBuffer.widthPx == 0 || theBuffer.heightPx == 0 ||
426 theBuffer.dataPtr == NULL)
428 return Standard_False;
431 // activate OpenGL context
433 if ((TsmGetWSAttri (theCView.WsId, WSWindow, &aData) != TSuccess) ||
434 (TxglWinset (call_thedisplay, (WINDOW) aData.ldata) != TSuccess))
436 return Standard_False;
440 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
441 GLint aReadBufferPrev = GL_BACK;
442 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
444 aFrameBuffer->BindBuffer();
448 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
449 GLint aDrawBufferPrev = GL_BACK;
450 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
451 glReadBuffer (aDrawBufferPrev);
454 GLint anAlignBack = 1;
455 glGetIntegerv (GL_PACK_ALIGNMENT, &anAlignBack);
456 if (theBuffer.rowAligmentBytes == 0)
458 theBuffer.rowAligmentBytes = 1;
460 glPixelStorei (GL_PACK_ALIGNMENT, theBuffer.rowAligmentBytes);
463 glReadPixels (0, 0, theBuffer.widthPx, theBuffer.heightPx, aFormat, aType, (GLvoid* )theBuffer.dataPtr);
464 glPixelStorei (GL_PACK_ALIGNMENT, anAlignBack);
466 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
468 aFrameBuffer->UnbindBuffer();
472 glReadBuffer (aReadBufferPrev);
474 return Standard_True;
477 void OpenGl_GraphicDriver::RemoveView (const Graphic3d_CView& ACView) {
479 Graphic3d_CView MyCView = ACView;
482 PrintFunction ("call_togl_removeview");
483 PrintCView (MyCView, 1);
485 call_togl_removeview (&MyCView);
489 void OpenGl_GraphicDriver::SetLight (const Graphic3d_CView& ACView) {
491 Graphic3d_CView MyCView = ACView;
494 PrintFunction ("call_togl_setlight");
495 PrintCView (MyCView, 1);
497 call_togl_setlight (&MyCView);
501 void OpenGl_GraphicDriver::SetPlane (const Graphic3d_CView& ACView) {
503 Graphic3d_CView MyCView = ACView;
506 PrintFunction ("call_togl_setplane");
507 PrintCView (MyCView, 1);
509 call_togl_setplane (&MyCView);
513 void OpenGl_GraphicDriver::SetVisualisation (const Graphic3d_CView& ACView) {
515 Graphic3d_CView MyCView = ACView;
518 PrintFunction ("call_togl_setvisualisation");
519 PrintCView (MyCView, 1);
521 call_togl_setvisualisation (&MyCView);
525 void OpenGl_GraphicDriver::TransformStructure (const Graphic3d_CStructure& ACStructure) {
527 Graphic3d_CStructure MyCStructure = ACStructure;
530 PrintFunction ("call_togl_transformstructure");
531 PrintCStructure (MyCStructure, 1);
533 call_togl_transformstructure (&MyCStructure);
537 void OpenGl_GraphicDriver :: DegenerateStructure (
538 const Graphic3d_CStructure& ACStructure
540 Graphic3d_CStructure MyCStructure = ACStructure;
542 if ( MyTraceLevel ) {
544 PrintFunction ( "call_togl_degeneratestructure" );
545 PrintCStructure ( MyCStructure, 1 );
549 call_togl_degeneratestructure ( &MyCStructure );
550 } // end OpenGl_GraphicDriver :: DegenerateStructure
552 void OpenGl_GraphicDriver::Transparency (const Graphic3d_CView& ACView, const Standard_Boolean AFlag) {
554 Graphic3d_CView MyCView = ACView;
557 PrintFunction ("call_togl_transparency");
558 PrintCView (MyCView, 1);
559 PrintBoolean ("AFlag", AFlag);
561 call_togl_transparency (MyCView.WsId, MyCView.ViewId, (AFlag ? 1 : 0));
565 void OpenGl_GraphicDriver::Update (const Graphic3d_CView& ACView, const Aspect_CLayer2d& ACUnderLayer, const Aspect_CLayer2d& ACOverLayer) {
567 Graphic3d_CView MyCView = ACView;
568 Aspect_CLayer2d MyCUnderLayer = ACUnderLayer;
569 Aspect_CLayer2d MyCOverLayer = ACOverLayer;
572 PrintFunction ("call_togl_update");
573 PrintCView (MyCView, 1);
575 call_togl_update (&MyCView, &MyCUnderLayer, &MyCOverLayer);
579 Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& ACView) {
581 Graphic3d_CView MyCView = ACView;
582 Standard_Integer Result;
585 PrintFunction ("call_togl_view");
586 PrintCView (MyCView, 1);
588 Result = call_togl_view (&MyCView);
590 PrintIResult ("call_togl_view", Result);
592 return (Result == 1 ? Standard_True : Standard_False);
596 void OpenGl_GraphicDriver::ViewMapping (const Graphic3d_CView& ACView, const Standard_Boolean AWait) {
598 Graphic3d_CView MyCView = ACView;
599 Standard_Integer Result;
602 PrintFunction ("call_togl_viewmapping");
603 PrintCView (MyCView, 1);
604 PrintBoolean ("AWait", AWait);
606 Result = call_togl_viewmapping (&MyCView, (AWait ? 1 : 0));
608 PrintIResult ("call_togl_viewmapping", Result);
613 void OpenGl_GraphicDriver::ViewOrientation (const Graphic3d_CView& ACView, const Standard_Boolean AWait) {
615 Graphic3d_CView MyCView = ACView;
616 Standard_Integer Result;
619 PrintFunction ("call_togl_vieworientation");
620 PrintCView (MyCView, 1);
621 PrintBoolean ("AWait", AWait);
623 Result = call_togl_vieworientation (&MyCView, (AWait ? 1 : 0));
625 PrintIResult ("call_togl_viewmapping", Result);
630 void OpenGl_GraphicDriver :: SetBackFacingModel ( const Graphic3d_CView& aView )
632 Graphic3d_CView myView = aView;
634 if ( MyTraceLevel ) {
636 PrintFunction ( "call_togl_backfacing" );
637 PrintCView ( myView, 1 );
641 call_togl_backfacing ( &myView );
642 } // end Graphic3d_GraphicDriver :: SetBackFacingModel