0022962: Invalid realization of reading and writing material in STEP.
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_7.cxx
CommitLineData
7fd59977 1
2// File OpenGl_GraphicDriver_7.cxx
3// Created Mardi 28 janvier 1997
4// Author CAL
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.
8
9//-Copyright MatraDatavision 1997
10
11//-Version
12
13//-Design Declaration des variables specifiques aux Drivers
14
15//-Warning Un driver encapsule les Pex et OpenGl drivers
16
17//-References
18
19//-Language C++ 2.0
20
21//-Declarations
22
23// for the class
24#include <OpenGl_GraphicDriver.jxx>
25
26#include <Aspect_DriverDefinitionError.hxx>
27#include <InterfaceGraphic_RawBufferData.hxx>
28
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>
34
35//-Aliases
36
37//-Global data definitions
38
39//-Methods, in order
40
41void OpenGl_GraphicDriver::ActivateView (const Graphic3d_CView& ACView) {
42
43 Graphic3d_CView MyCView = ACView;
44
45 if (MyTraceLevel) {
46 PrintFunction ("call_togl_activateview");
47 PrintCView (MyCView, 1);
48 }
49 call_togl_activateview (&MyCView);
50
51}
52
53void OpenGl_GraphicDriver::AntiAliasing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag) {
54
55 Graphic3d_CView MyCView = ACView;
56
57 if (MyTraceLevel) {
58 PrintFunction ("call_togl_antialiasing");
59 PrintCView (MyCView, 1);
60 PrintBoolean ("AFlag", AFlag);
61 }
62 call_togl_antialiasing (&MyCView, (AFlag ? 1 : 0));
63
64}
65
66void OpenGl_GraphicDriver::Background (const Graphic3d_CView& ACView) {
67
68 Graphic3d_CView MyCView = ACView;
69
70 if (MyTraceLevel) {
71 PrintFunction ("call_togl_background");
72 PrintCView (MyCView, 1);
73 }
74 call_togl_background (&MyCView);
75
76}
77
78void OpenGl_GraphicDriver::GradientBackground(const Graphic3d_CView& ACView,
79 const Quantity_Color& AColor1,
80 const Quantity_Color& AColor2,
81 const Aspect_GradientFillMethod AType){
82
83 Graphic3d_CView MyCView = ACView;
84
85 if (MyTraceLevel) {
86 PrintFunction ("call_togl_gradient_background");
87 PrintCView (MyCView, 1);
88 }
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;
93 tcolor1.rgb[0] = R1;
94 tcolor1.rgb[1] = G1;
95 tcolor1.rgb[2] = B1;
96 tcolor1.rgb[3] = 0;
97 tcolor2.rgb[0] = R2;
98 tcolor2.rgb[1] = G2;
99 tcolor2.rgb[2] = B2;
100 tcolor2.rgb[3] = 0;
101
102 call_togl_gradient_background (MyCView.WsId, AType, &tcolor1, &tcolor2);
103
104}
105
106
107void OpenGl_GraphicDriver::Blink (const Graphic3d_CStructure& ACStructure, const Standard_Boolean Create) {
108
109 Graphic3d_CStructure MyCStructure = ACStructure;
110
111 if (MyTraceLevel) {
112 PrintFunction ("call_togl_blink");
113 PrintCStructure (MyCStructure, 1);
114 PrintBoolean ("Create", Create);
115 }
116 call_togl_blink (&MyCStructure, (Create ? 1 : 0));
117
118}
119
120void OpenGl_GraphicDriver::BoundaryBox (const Graphic3d_CStructure& ACStructure, const Standard_Boolean Create) {
121
122 Graphic3d_CStructure MyCStructure = ACStructure;
123
124 if (MyTraceLevel) {
125 PrintFunction ("call_togl_boundarybox");
126 PrintCStructure (MyCStructure, 1);
127 PrintBoolean ("Create", Create);
128 }
129 call_togl_boundarybox (&MyCStructure, (Create ? 1 : 0));
130
131}
132
133void OpenGl_GraphicDriver::HighlightColor (const Graphic3d_CStructure& ACStructure, const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B, const Standard_Boolean Create) {
134
135 Graphic3d_CStructure MyCStructure = ACStructure;
136
137 if (MyTraceLevel) {
138 PrintFunction ("call_togl_highlightcolor");
139 PrintCStructure (MyCStructure, 1);
140 PrintShortReal ("R", R);
141 PrintShortReal ("G", G);
142 PrintShortReal ("B", B);
143 }
144 CALL_DEF_COLOR acolor;
145 acolor.r = R;
146 acolor.g = G;
147 acolor.b = B;
148 call_togl_highlightcolor (&MyCStructure, &acolor, (Create ? 1 : 0));
149
150}
151
152void OpenGl_GraphicDriver::NameSetStructure (const Graphic3d_CStructure& ACStructure) {
153
154 Graphic3d_CStructure MyCStructure = ACStructure;
155
156 if (MyTraceLevel) {
157 PrintFunction ("call_togl_namesetstructure");
158 PrintCStructure (MyCStructure, 1);
159 }
160 call_togl_namesetstructure (&MyCStructure);
161
162}
163
164void OpenGl_GraphicDriver::ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait) {
165
166 Graphic3d_CView MyCView = ACView;
167
168 if (MyTraceLevel) {
169 PrintFunction ("call_togl_cliplimit");
170 PrintCView (MyCView, 1);
171 PrintBoolean ("AWait", AWait);
172 }
173 call_togl_cliplimit (&MyCView, (AWait ? 1 : 0));
174
175}
176
177void OpenGl_GraphicDriver::DeactivateView (const Graphic3d_CView& ACView) {
178 Graphic3d_CView MyCView = ACView;
179
180 if (MyTraceLevel) {
181 PrintFunction ("call_togl_deactivateview");
182 PrintCView (MyCView, 1);
183 }
184 call_togl_deactivateview (&MyCView);
185
186}
187
188void OpenGl_GraphicDriver::DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag) {
189
190 Graphic3d_CView MyCView = ACView;
191
192 if (MyTraceLevel) {
193 PrintFunction ("call_togl_depthcueing");
194 PrintCView (MyCView, 1);
195 PrintBoolean ("AFlag", AFlag);
196 }
197 call_togl_depthcueing (&MyCView, (AFlag ? 1 : 0));
198
199}
200
201Standard_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) {
202
203 Graphic3d_CView MyCView = ACView;
204
205 Standard_Integer Result;
206
207 if (MyTraceLevel) {
208 PrintFunction ("call_togl_project_raster");
209 PrintCView (MyCView, 1);
210 }
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 );
216 if (MyTraceLevel) {
217 PrintIResult ("call_togl_adopt_to_rect", Result);
218 }
219 if (Result)
220 return Standard_False;
221 }
222
223 Result = call_togl_project_raster (ACView.ViewId, AX, AY, AZ, &AU, &AV);
224 if (MyTraceLevel) {
225 PrintIResult ("call_togl_project_raster", Result);
226 }
227
228 if ( ACView.DefBitmap.bitmap ) {
229 Result = call_togl_adopt_to_rect( ACView.ViewId,
230 ACView.DefWindow.dx,
231 ACView.DefWindow.dy );
232 if (MyTraceLevel) {
233 PrintIResult ("call_togl_adopt_to_rect", Result);
234 }
235 if (Result)
236 return Standard_False;
237 }
238
239 return (Result == 0 ? Standard_True : Standard_False);
240
241}
242
243Standard_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) {
244
245 Graphic3d_CView MyCView = ACView;
246
247 Standard_Integer Result;
248
249 if (MyTraceLevel) {
250 PrintFunction ("call_togl_unproject_raster");
251 PrintCView (MyCView, 1);
252 }
253 Result = call_togl_unproject_raster
254 (ACView.ViewId, Axm, Aym, AXM, AYM, AU, AV, &Ax, &Ay, &Az);
255 if (MyTraceLevel) {
256 PrintIResult ("call_togl_unproject_raster", Result);
257 }
258 return (Result == 0 ? Standard_True : Standard_False);
259
260}
261
262Standard_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) {
263
264 Graphic3d_CView MyCView = ACView;
265
266 Standard_Integer Result;
267
268 if (MyTraceLevel) {
269 PrintFunction ("call_togl_unproject_raster_with_ray");
270 PrintCView (MyCView, 1);
271 }
272 Result = call_togl_unproject_raster_with_ray
273 (ACView.ViewId, Axm, Aym, AXM, AYM, AU, AV, &Ax, &Ay, &Az, &Dx, &Dy, &Dz);
274 if (MyTraceLevel) {
275 PrintIResult ("call_togl_unproject_raster_with_ray", Result);
276 }
277 return (Result == 0 ? Standard_True : Standard_False);
278
279}
280
281void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& ACView) {
282
283 Graphic3d_CView MyCView = ACView;
284
285 if (MyTraceLevel) {
286 PrintFunction ("call_togl_ratio_window");
287 PrintCView (MyCView, 1);
288 }
289 call_togl_ratio_window (&MyCView);
290
291}
292
293void 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) {
294
295 Graphic3d_CView MyCView = ACView;
296 Aspect_CLayer2d MyCUnderLayer = ACUnderLayer;
297 Aspect_CLayer2d MyCOverLayer = ACOverLayer;
298
299 if (MyTraceLevel) {
300 PrintFunction ("call_togl_redraw");
301 PrintCView (MyCView, 1);
302 }
303 //PRO19603
304 if( width <= 0 || height <= 0 )
305 call_togl_redraw (&MyCView, &MyCUnderLayer, &MyCOverLayer);
306 else {
307 call_togl_redraw_area (&MyCView, &MyCUnderLayer, &MyCOverLayer,
308 x, y, width, height);
309 }
310 //PRO19603
311}
312
313Graphic3d_PtrFrameBuffer OpenGl_GraphicDriver::FBOCreate (const Graphic3d_CView& theCView,
314 const Standard_Integer theWidth,
315 const Standard_Integer theHeight) {
316 // activate OpenGL context
317 CMN_KEY_DATA aData;
318 if ((TsmGetWSAttri (theCView.WsId, WSWindow, &aData) != TSuccess) ||
319 (TxglWinset (call_thedisplay, (WINDOW) aData.ldata) != TSuccess))
320 {
321 return NULL;
322 }
323 // create the FBO
324 OpenGl_FrameBuffer* aFrameBuffer = new OpenGl_FrameBuffer();
325 if (!aFrameBuffer->Init (theWidth, theHeight))
326 {
327 delete aFrameBuffer;
328 return NULL;
329 }
330 return (Graphic3d_PtrFrameBuffer )aFrameBuffer;
331}
332
333void OpenGl_GraphicDriver::FBORelease (const Graphic3d_CView& theCView,
334 Graphic3d_PtrFrameBuffer& theFBOPtr)
335{
336 if (theFBOPtr == NULL)
337 {
338 return;
339 }
340 // activate OpenGL context
341 CMN_KEY_DATA aData;
342 if ((TsmGetWSAttri (theCView.WsId, WSWindow, &aData) != TSuccess) ||
343 (TxglWinset (call_thedisplay, (WINDOW) aData.ldata) != TSuccess))
344 {
345 return;
346 }
347 // release the object
348 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
349 delete aFrameBuffer;
350 theFBOPtr = NULL;
351}
352
353void 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)
357{
358 if (theFBOPtr == NULL)
359 {
360 return;
361 }
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();
367}
368
369void OpenGl_GraphicDriver::FBOChangeViewport (const Graphic3d_CView& ,
370 Graphic3d_PtrFrameBuffer& theFBOPtr,
371 const Standard_Integer theWidth, const Standard_Integer theHeight)
372{
373 if (theFBOPtr == NULL)
374 {
375 return;
376 }
377 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
378 aFrameBuffer->ChangeViewport (theWidth, theHeight);
379}
380
381// OpenGL 1.2 stuff
382#ifndef GL_BGR
383 #define GL_BGR 0x80E0
384#endif
385#ifndef GL_BGRA
386 #define GL_BGRA 0x80E1
387#endif
388
389
390static inline GLenum TFormatToGLEnum (const TRawBufferDataFormat theTFormat)
391{
392 switch (theTFormat)
393 {
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;
403 default: return 0;
404 }
405}
406
407static inline GLenum TTypeToGLEnum (const TRawBufferDataType theTType)
408{
409 switch (theTType)
410 {
411 case TUByte: return GL_UNSIGNED_BYTE;
412 case TFloat: return GL_FLOAT;
413 default: return 0;
414 }
415}
416
417Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& theCView,
418 Image_CRawBufferData& theBuffer)
419{
420 GLenum aFormat = TFormatToGLEnum (theBuffer.format);
421 GLenum aType = TTypeToGLEnum (theBuffer.type);
422
423 // safe checks
424 if (aFormat == 0 || aType == 0 ||
425 theBuffer.widthPx == 0 || theBuffer.heightPx == 0 ||
426 theBuffer.dataPtr == NULL)
427 {
428 return Standard_False;
429 }
430
431 // activate OpenGL context
432 CMN_KEY_DATA aData;
433 if ((TsmGetWSAttri (theCView.WsId, WSWindow, &aData) != TSuccess) ||
434 (TxglWinset (call_thedisplay, (WINDOW) aData.ldata) != TSuccess))
435 {
436 return Standard_False;
437 }
438
439 // bind FBO if used
440 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
441 GLint aReadBufferPrev = GL_BACK;
442 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
443 {
444 aFrameBuffer->BindBuffer();
445 }
446 else
447 {
448 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
449 GLint aDrawBufferPrev = GL_BACK;
450 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
451 glReadBuffer (aDrawBufferPrev);
452 }
453
454 GLint anAlignBack = 1;
455 glGetIntegerv (GL_PACK_ALIGNMENT, &anAlignBack);
456 if (theBuffer.rowAligmentBytes == 0)
457 {
458 theBuffer.rowAligmentBytes = 1;
459 }
460 glPixelStorei (GL_PACK_ALIGNMENT, theBuffer.rowAligmentBytes);
461
462 // read pixels
463 glReadPixels (0, 0, theBuffer.widthPx, theBuffer.heightPx, aFormat, aType, (GLvoid* )theBuffer.dataPtr);
464 glPixelStorei (GL_PACK_ALIGNMENT, anAlignBack);
465
466 if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
467 {
468 aFrameBuffer->UnbindBuffer();
469 }
470 else
471 {
472 glReadBuffer (aReadBufferPrev);
473 }
474 return Standard_True;
475}
476
477void OpenGl_GraphicDriver::RemoveView (const Graphic3d_CView& ACView) {
478
479 Graphic3d_CView MyCView = ACView;
480
481 if (MyTraceLevel) {
482 PrintFunction ("call_togl_removeview");
483 PrintCView (MyCView, 1);
484 }
485 call_togl_removeview (&MyCView);
486
487}
488
489void OpenGl_GraphicDriver::SetLight (const Graphic3d_CView& ACView) {
490
491 Graphic3d_CView MyCView = ACView;
492
493 if (MyTraceLevel) {
494 PrintFunction ("call_togl_setlight");
495 PrintCView (MyCView, 1);
496 }
497 call_togl_setlight (&MyCView);
498
499}
500
501void OpenGl_GraphicDriver::SetPlane (const Graphic3d_CView& ACView) {
502
503 Graphic3d_CView MyCView = ACView;
504
505 if (MyTraceLevel) {
506 PrintFunction ("call_togl_setplane");
507 PrintCView (MyCView, 1);
508 }
509 call_togl_setplane (&MyCView);
510
511}
512
513void OpenGl_GraphicDriver::SetVisualisation (const Graphic3d_CView& ACView) {
514
515 Graphic3d_CView MyCView = ACView;
516
517 if (MyTraceLevel) {
518 PrintFunction ("call_togl_setvisualisation");
519 PrintCView (MyCView, 1);
520 }
521 call_togl_setvisualisation (&MyCView);
522
523}
524
525void OpenGl_GraphicDriver::TransformStructure (const Graphic3d_CStructure& ACStructure) {
526
527 Graphic3d_CStructure MyCStructure = ACStructure;
528
529 if (MyTraceLevel) {
530 PrintFunction ("call_togl_transformstructure");
531 PrintCStructure (MyCStructure, 1);
532 }
533 call_togl_transformstructure (&MyCStructure);
534
535}
536
537void OpenGl_GraphicDriver :: DegenerateStructure (
538 const Graphic3d_CStructure& ACStructure
539 ) {
540 Graphic3d_CStructure MyCStructure = ACStructure;
541
542 if ( MyTraceLevel ) {
543
544 PrintFunction ( "call_togl_degeneratestructure" );
545 PrintCStructure ( MyCStructure, 1 );
546
547 } // end if
548
549 call_togl_degeneratestructure ( &MyCStructure );
550 } // end OpenGl_GraphicDriver :: DegenerateStructure
551
552 void OpenGl_GraphicDriver::Transparency (const Graphic3d_CView& ACView, const Standard_Boolean AFlag) {
553
554 Graphic3d_CView MyCView = ACView;
555
556 if (MyTraceLevel) {
557 PrintFunction ("call_togl_transparency");
558 PrintCView (MyCView, 1);
559 PrintBoolean ("AFlag", AFlag);
560 }
561 call_togl_transparency (MyCView.WsId, MyCView.ViewId, (AFlag ? 1 : 0));
562
563 }
564
565 void OpenGl_GraphicDriver::Update (const Graphic3d_CView& ACView, const Aspect_CLayer2d& ACUnderLayer, const Aspect_CLayer2d& ACOverLayer) {
566
567 Graphic3d_CView MyCView = ACView;
568 Aspect_CLayer2d MyCUnderLayer = ACUnderLayer;
569 Aspect_CLayer2d MyCOverLayer = ACOverLayer;
570
571 if (MyTraceLevel) {
572 PrintFunction ("call_togl_update");
573 PrintCView (MyCView, 1);
574 }
575 call_togl_update (&MyCView, &MyCUnderLayer, &MyCOverLayer);
576
577 }
578
579 Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& ACView) {
580
581 Graphic3d_CView MyCView = ACView;
582 Standard_Integer Result;
583
584 if (MyTraceLevel) {
585 PrintFunction ("call_togl_view");
586 PrintCView (MyCView, 1);
587 }
588 Result = call_togl_view (&MyCView);
589 if (MyTraceLevel) {
590 PrintIResult ("call_togl_view", Result);
591 }
592 return (Result == 1 ? Standard_True : Standard_False);
593
594 }
595
596 void OpenGl_GraphicDriver::ViewMapping (const Graphic3d_CView& ACView, const Standard_Boolean AWait) {
597
598 Graphic3d_CView MyCView = ACView;
599 Standard_Integer Result;
600
601 if (MyTraceLevel) {
602 PrintFunction ("call_togl_viewmapping");
603 PrintCView (MyCView, 1);
604 PrintBoolean ("AWait", AWait);
605 }
606 Result = call_togl_viewmapping (&MyCView, (AWait ? 1 : 0));
607 if (MyTraceLevel) {
608 PrintIResult ("call_togl_viewmapping", Result);
609 }
610
611 }
612
613 void OpenGl_GraphicDriver::ViewOrientation (const Graphic3d_CView& ACView, const Standard_Boolean AWait) {
614
615 Graphic3d_CView MyCView = ACView;
616 Standard_Integer Result;
617
618 if (MyTraceLevel) {
619 PrintFunction ("call_togl_vieworientation");
620 PrintCView (MyCView, 1);
621 PrintBoolean ("AWait", AWait);
622 }
623 Result = call_togl_vieworientation (&MyCView, (AWait ? 1 : 0));
624 if (MyTraceLevel) {
625 PrintIResult ("call_togl_viewmapping", Result);
626 }
627
628 }
629
630 void OpenGl_GraphicDriver :: SetBackFacingModel ( const Graphic3d_CView& aView )
631 {
632 Graphic3d_CView myView = aView;
633
634 if ( MyTraceLevel ) {
635
636 PrintFunction ( "call_togl_backfacing" );
637 PrintCView ( myView, 1 );
638
639 } // end if
640
641 call_togl_backfacing ( &myView );
642 } // end Graphic3d_GraphicDriver :: SetBackFacingModel