2 // File OpenGl_GraphicDriver_9.cxx
6 // 16-09-98 ; BGN : Points d'entree du Triedre (S3819, Phase 1)
7 // 22-09-98 ; BGN : S3989 (anciennement S3819)
8 // TypeOfTriedron* from Aspect (pas Graphic3d)
10 //-Copyright MatraDatavision 1997
14 //-Design Declaration des variables specifiques aux Drivers
16 //-Warning Un driver encapsule les Pex et OpenGl drivers
26 #define IMP131100 //GG_Enable to creates more than one texture
27 // with the same name.
29 #define OCC1188 //SAV added methods to set background image
31 #include <OpenGl_GraphicDriver.jxx>
33 #include <Aspect_DriverDefinitionError.hxx>
34 #include <Quantity_NameOfColor.hxx>
35 #include <AlienImage.hxx>
36 #include <Standard_Integer.hxx>
38 #include <OpenGl_tgl_funcs.hxx>
44 #include <Image_Image.hxx>
49 //-Global data definitions
53 // Helper function, returns the nearest power of two greater than the argument value
54 inline Standard_Integer GetNearestPow2(Standard_Integer theValue)
56 // Precaution against overflow
57 Standard_Integer aHalfMax = IntegerLast() >> 1, aRes = 1;
58 if ( theValue > aHalfMax ) theValue = aHalfMax;
59 while ( aRes < theValue ) aRes <<= 1;
63 Standard_Integer OpenGl_GraphicDriver::CreateTexture(const Graphic3d_TypeOfTexture Type,const Handle_AlienImage_AlienImage &Image,const Standard_CString FileName,const Handle(TColStd_HArray1OfReal)& TexUpperBounds) const
65 // transmutation de l'image en un bon tableau RGBA pour la toolkit des textures
66 // lamentable d'un point de vue vitesse...
67 Handle(Image_Image) MyPic = Image->ToImage();
69 //TODO: The temporary fix in order to make AIS_TexturedShape work. To be improved in next release.
70 Standard_Integer aGlWidth = (Type == Graphic3d_TOT_2D_MIPMAP) ? MyPic->Width() : GetNearestPow2(MyPic->Width());
71 Standard_Integer aGlHeight = (Type == Graphic3d_TOT_2D_MIPMAP) ? MyPic->Height() : GetNearestPow2(MyPic->Height());
73 TexUpperBounds->SetValue(1, ((Standard_Real) (MyPic->Width())/((Standard_Real) aGlWidth)));
74 TexUpperBounds->SetValue(2, ((Standard_Real) (MyPic->Height())/((Standard_Real) aGlHeight)));
76 unsigned char *MyImageData = (unsigned char *)malloc(aGlWidth*aGlHeight*4);
77 unsigned char *MyData = MyImageData;
80 Quantity_Color MyColor;
82 if (MyImageData == NULL)
87 static Standard_Integer textureRank=0;
89 Standard_PCharacter fileName = textureName;
90 sprintf(fileName,"Tex%d",++textureRank);
92 Standard_CString fileName = FileName
95 for (j = MyPic->Height() - 1; j >= 0; j--)
96 for (i = 0; i < aGlWidth; i++){
97 if (i < MyPic->Width()){
98 MyColor = MyPic->PixelColor(i, j);
99 *MyData++ = (int)(255 * MyColor.Red());
100 *MyData++ = (int)(255 * MyColor.Green());
101 *MyData++ = (int)(255 * MyColor.Blue());
104 *MyData++ = (int)(0);
105 *MyData++ = (int)(0);
106 *MyData++ = (int)(0);
111 // Padding the lower part of the texture with black
112 for (j = aGlHeight - 1; j >= MyPic->Height(); j--)
113 for (i = 0; i < aGlWidth; i++){
114 *MyData++ = (int)(0);
115 *MyData++ = (int)(0);
116 *MyData++ = (int)(0);
123 case Graphic3d_TOT_1D:
124 TexId = call_togl_create_texture(0, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
127 case Graphic3d_TOT_2D:
128 TexId = call_togl_create_texture(1, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
131 case Graphic3d_TOT_2D_MIPMAP:
132 TexId = call_togl_create_texture(2, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
144 void OpenGl_GraphicDriver::DestroyTexture(const Standard_Integer TexId) const
146 call_togl_destroy_texture(TexId);
150 void OpenGl_GraphicDriver::ModifyTexture(const Standard_Integer TexId,const Graphic3d_CInitTexture& AValue) const
152 call_togl_modify_texture(TexId, (CALL_DEF_INIT_TEXTURE *)&AValue);
158 void OpenGl_GraphicDriver::Environment(const Graphic3d_CView& ACView)
160 call_togl_environment((CALL_DEF_VIEW *)&ACView);
165 // Triedron methods : the Triedron is a non-zoomable object.
168 void OpenGl_GraphicDriver::ZBufferTriedronSetup (
169 const Quantity_NameOfColor XColor,
170 const Quantity_NameOfColor YColor,
171 const Quantity_NameOfColor ZColor,
172 const Standard_Real SizeRatio,
173 const Standard_Real AxisDiametr,
174 const Standard_Integer NbFacettes)
180 Quantity_Color(XColor).Values(R, G, B, Quantity_TOC_RGB);
184 Quantity_Color(YColor).Values(R, G, B, Quantity_TOC_RGB);
188 Quantity_Color(ZColor).Values(R, G, B, Quantity_TOC_RGB);
193 call_togl_ztriedron_setup(Xrgb, Yrgb, Zrgb,
194 (Standard_ShortReal)(SizeRatio), (Standard_ShortReal)(AxisDiametr), NbFacettes);
197 void OpenGl_GraphicDriver::TriedronDisplay (
198 const Graphic3d_CView& ACView,
199 const Aspect_TypeOfTriedronPosition APosition,
200 const Quantity_NameOfColor AColor,
201 const Standard_Real AScale,
202 const Standard_Boolean AsWireframe )
206 Quantity_Color Color(AColor);
207 Color.Values(R,G,B,Quantity_TOC_RGB);
212 call_togl_triedron_display((CALL_DEF_VIEW *)&ACView,(int)APosition, r,g,b, (float)AScale, AsWireframe);
215 void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& ACView)
217 call_togl_triedron_erase((CALL_DEF_VIEW *)&ACView);
220 void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ACView,const Aspect_TypeOfTriedronEcho AType )
222 call_togl_triedron_echo( (CALL_DEF_VIEW *)&ACView,AType );
225 void OpenGl_GraphicDriver::BackgroundImage( const Standard_CString FileName,
226 const Graphic3d_CView& ACView,
227 const Aspect_FillMethod FillStyle )
230 Graphic3d_CView MyCView = ACView;
231 Standard_Integer width, height;
232 Handle(Image_Image) image;
234 Standard_Boolean result = AlienImage::LoadImageFile( FileName, image, width, height );
237 unsigned char *data = (unsigned char*)malloc( width * height * 3 );
238 unsigned char *pdata = data;
239 Standard_Integer i,j;
240 Quantity_Color color;
242 if ( data != NULL ) {
243 for ( j = height - 1; j >= 0; j-- )
244 for ( i = 0; i < width; i++ ) {
245 color = image->PixelColor( i, j );
246 *pdata++ = (int)( 255 * color.Red() );
247 *pdata++ = (int)( 255 * color.Green() );
248 *pdata++ = (int)( 255 * color.Blue() );
250 call_togl_create_bg_texture( (CALL_DEF_VIEW*)&MyCView, width, height, data, (int)FillStyle );
258 void OpenGl_GraphicDriver::SetBgImageStyle( const Graphic3d_CView& ACView,
259 const Aspect_FillMethod FillStyle )
262 Graphic3d_CView MyCView = ACView;
263 call_togl_set_bg_texture_style( (CALL_DEF_VIEW*)&MyCView, (int)FillStyle );
267 void OpenGl_GraphicDriver::SetBgGradientStyle(const Graphic3d_CView& ACView,const Aspect_GradientFillMethod FillType)
269 call_togl_set_gradient_type( ACView.WsId, FillType);
272 void OpenGl_GraphicDriver::GetGraduatedTrihedron(const Graphic3d_CView& view,
274 Standard_CString& xname,
275 Standard_CString& yname,
276 Standard_CString& zname,
278 Standard_Boolean& xdrawname,
279 Standard_Boolean& ydrawname,
280 Standard_Boolean& zdrawname,
282 Standard_Boolean& xdrawvalues,
283 Standard_Boolean& ydrawvalues,
284 Standard_Boolean& zdrawvalues,
286 Standard_Boolean& drawgrid,
288 Standard_Boolean& drawaxes,
289 /* Number of splits along axes */
290 Standard_Integer& nbx,
291 Standard_Integer& nby,
292 Standard_Integer& nbz,
293 /* Offset for drawing values */
294 Standard_Integer& xoffset,
295 Standard_Integer& yoffset,
296 Standard_Integer& zoffset,
297 /* Offset for drawing names of axes */
298 Standard_Integer& xaxisoffset,
299 Standard_Integer& yaxisoffset,
300 Standard_Integer& zaxisoffset,
302 Standard_Boolean& xdrawtickmarks,
303 Standard_Boolean& ydrawtickmarks,
304 Standard_Boolean& zdrawtickmarks,
305 /* Length of tickmarks */
306 Standard_Integer& xtickmarklength,
307 Standard_Integer& ytickmarklength,
308 Standard_Integer& ztickmarklength,
310 Quantity_Color& gridcolor,
312 Quantity_Color& xnamecolor,
314 Quantity_Color& ynamecolor,
316 Quantity_Color& znamecolor,
317 /* X color of axis and values */
318 Quantity_Color& xcolor,
319 /* Y color of axis and values */
320 Quantity_Color& ycolor,
321 /* Z color of axis and values */
322 Quantity_Color& zcolor,
323 /* Name of font for names of axes */
324 Standard_CString& fontOfNames,
325 /* Style of names of axes */
326 OSD_FontAspect& styleOfNames,
327 /* Size of names of axes */
328 Standard_Integer& sizeOfNames,
329 /* Name of font for values */
330 Standard_CString& fontOfValues,
331 /* Style of values */
332 OSD_FontAspect& styleOfValues,
334 Standard_Integer& sizeOfValues) const
336 Graphic3d_CGraduatedTrihedron cubic;
337 call_togl_graduatedtrihedron_get((CALL_DEF_VIEW*)&view, &cubic);
344 xdrawname = cubic.xdrawname;
345 ydrawname = cubic.ydrawname;
346 zdrawname = cubic.zdrawname;
348 xdrawvalues = cubic.xdrawvalues;
349 ydrawvalues = cubic.ydrawvalues;
350 zdrawvalues = cubic.zdrawvalues;
352 drawgrid = cubic.drawgrid;
354 drawaxes = cubic.drawaxes;
355 /* Number of splits along axes */
359 /* Offset for drawing values */
360 xoffset = cubic.xoffset;
361 yoffset = cubic.yoffset;
362 zoffset = cubic.zoffset;
363 /* Offset for drawing names of axes */
364 xaxisoffset = cubic.xaxisoffset;
365 yaxisoffset = cubic.yaxisoffset;
366 zaxisoffset = cubic.zaxisoffset;
368 xdrawtickmarks = cubic.xdrawtickmarks;
369 ydrawtickmarks = cubic.ydrawtickmarks;
370 zdrawtickmarks = cubic.zdrawtickmarks;
371 /* Length of tickmarks */
372 xtickmarklength = cubic.xtickmarklength;
373 ytickmarklength = cubic.ytickmarklength;
374 ztickmarklength = cubic.ztickmarklength;
376 gridcolor.SetValues(cubic.gridcolor[0], cubic.gridcolor[1], cubic.gridcolor[2], Quantity_TOC_RGB);
378 xnamecolor.SetValues(cubic.xnamecolor[0], cubic.xnamecolor[1], cubic.xnamecolor[2], Quantity_TOC_RGB);
380 ynamecolor.SetValues(cubic.ynamecolor[0], cubic.ynamecolor[1], cubic.ynamecolor[2], Quantity_TOC_RGB);
382 znamecolor.SetValues(cubic.znamecolor[0], cubic.znamecolor[1], cubic.znamecolor[2], Quantity_TOC_RGB);
383 /* X color of axis and values */
384 xcolor.SetValues(cubic.xcolor[0], cubic.xcolor[1], cubic.xcolor[2], Quantity_TOC_RGB);
385 /* Y color of axis and values */
386 ycolor.SetValues(cubic.ycolor[0], cubic.ycolor[1], cubic.ycolor[2], Quantity_TOC_RGB);
387 /* Z color of axis and values */
388 zcolor.SetValues(cubic.zcolor[0], cubic.zcolor[1], cubic.zcolor[2], Quantity_TOC_RGB);
389 /* Name of font for names of axes */
390 fontOfNames = cubic.fontOfNames;
391 /* Style of names of axes */
392 styleOfNames = cubic.styleOfNames;
393 /* Size of names of axes */
394 sizeOfNames = cubic.sizeOfNames;
395 /* Name of font for values */
396 fontOfValues = cubic.fontOfValues;
397 /* Style of values */
398 styleOfValues = cubic.styleOfValues;
400 sizeOfValues = cubic.sizeOfValues;
401 /* Name of font for names of axes */
402 fontOfNames = cubic.fontOfNames;
403 /* Style of names of axes */
404 styleOfNames = cubic.styleOfNames;
405 /* Size of names of axes */
406 sizeOfNames = cubic.sizeOfNames;
407 /* Name of font for values */
408 fontOfValues = cubic.fontOfValues;
409 /* Style of values */
410 styleOfValues = cubic.styleOfValues;
412 sizeOfValues = cubic.sizeOfValues;
415 void OpenGl_GraphicDriver::GraduatedTrihedronDisplay(const Graphic3d_CView& view,
416 Graphic3d_CGraduatedTrihedron& cubic,
418 const Standard_CString xname,
419 const Standard_CString yname,
420 const Standard_CString zname,
422 const Standard_Boolean xdrawname,
423 const Standard_Boolean ydrawname,
424 const Standard_Boolean zdrawname,
426 const Standard_Boolean xdrawvalues,
427 const Standard_Boolean ydrawvalues,
428 const Standard_Boolean zdrawvalues,
430 const Standard_Boolean drawgrid,
432 const Standard_Boolean drawaxes,
433 /* Number of splits along axes */
434 const Standard_Integer nbx,
435 const Standard_Integer nby,
436 const Standard_Integer nbz,
437 /* Offset for drawing values */
438 const Standard_Integer xoffset,
439 const Standard_Integer yoffset,
440 const Standard_Integer zoffset,
441 /* Offset for drawing names of axes */
442 const Standard_Integer xaxisoffset,
443 const Standard_Integer yaxisoffset,
444 const Standard_Integer zaxisoffset,
446 const Standard_Boolean xdrawtickmarks,
447 const Standard_Boolean ydrawtickmarks,
448 const Standard_Boolean zdrawtickmarks,
449 /* Length of tickmarks */
450 const Standard_Integer xtickmarklength,
451 const Standard_Integer ytickmarklength,
452 const Standard_Integer ztickmarklength,
454 const Quantity_Color& gridcolor,
456 const Quantity_Color& xnamecolor,
458 const Quantity_Color& ynamecolor,
460 const Quantity_Color& znamecolor,
461 /* X color of axis and values */
462 const Quantity_Color& xcolor,
463 /* Y color of axis and values */
464 const Quantity_Color& ycolor,
465 /* Z color of axis and values */
466 const Quantity_Color& zcolor,
467 /* Name of font for names of axes */
468 const Standard_CString fontOfNames,
469 /* Style of names of axes */
470 const OSD_FontAspect styleOfNames,
471 /* Size of names of axes */
472 const Standard_Integer sizeOfNames,
473 /* Name of font for values */
474 const Standard_CString fontOfValues,
475 /* Style of values */
476 const OSD_FontAspect styleOfValues,
478 const Standard_Integer sizeOfValues)
481 cubic.xname = (char*) xname;
482 cubic.yname = (char*) yname;
483 cubic.zname = (char*) zname;
485 cubic.xdrawname = xdrawname;
486 cubic.ydrawname = ydrawname;
487 cubic.zdrawname = zdrawname;
489 cubic.xdrawvalues = xdrawvalues;
490 cubic.ydrawvalues = ydrawvalues;
491 cubic.zdrawvalues = zdrawvalues;
493 cubic.drawgrid = drawgrid;
495 cubic.drawaxes = drawaxes;
496 /* Number of splits along axes */
500 /* Offset for drawing values */
501 cubic.xoffset = xoffset;
502 cubic.yoffset = yoffset;
503 cubic.zoffset = zoffset;
504 /* Offset for drawing names of axes */
505 cubic.xaxisoffset = xaxisoffset;
506 cubic.yaxisoffset = yaxisoffset;
507 cubic.zaxisoffset = zaxisoffset;
509 cubic.xdrawtickmarks = xdrawtickmarks;
510 cubic.ydrawtickmarks = ydrawtickmarks;
511 cubic.zdrawtickmarks = zdrawtickmarks;
512 /* Length of tickmarks */
513 cubic.xtickmarklength = xtickmarklength;
514 cubic.ytickmarklength = ytickmarklength;
515 cubic.ztickmarklength = ztickmarklength;
517 cubic.gridcolor[0] = (Standard_ShortReal) gridcolor.Red();
518 cubic.gridcolor[1] = (Standard_ShortReal) gridcolor.Green();
519 cubic.gridcolor[2] = (Standard_ShortReal) gridcolor.Blue();
521 cubic.xnamecolor[0] = (Standard_ShortReal) xnamecolor.Red();
522 cubic.xnamecolor[1] = (Standard_ShortReal) xnamecolor.Green();
523 cubic.xnamecolor[2] = (Standard_ShortReal) xnamecolor.Blue();
525 cubic.ynamecolor[0] = (Standard_ShortReal) ynamecolor.Red();
526 cubic.ynamecolor[1] = (Standard_ShortReal) ynamecolor.Green();
527 cubic.ynamecolor[2] = (Standard_ShortReal) ynamecolor.Blue();
529 cubic.znamecolor[0] = (Standard_ShortReal) znamecolor.Red();
530 cubic.znamecolor[1] = (Standard_ShortReal) znamecolor.Green();
531 cubic.znamecolor[2] = (Standard_ShortReal) znamecolor.Blue();
532 /* X color of axis and values */
533 cubic.xcolor[0] = (Standard_ShortReal) xcolor.Red();
534 cubic.xcolor[1] = (Standard_ShortReal) xcolor.Green();
535 cubic.xcolor[2] = (Standard_ShortReal) xcolor.Blue();
536 /* Y color of axis and values */
537 cubic.ycolor[0] = (Standard_ShortReal) ycolor.Red();
538 cubic.ycolor[1] = (Standard_ShortReal) ycolor.Green();
539 cubic.ycolor[2] = (Standard_ShortReal) ycolor.Blue();
540 /* Z color of axis and values */
541 cubic.zcolor[0] = (Standard_ShortReal) zcolor.Red();
542 cubic.zcolor[1] = (Standard_ShortReal) zcolor.Green();
543 cubic.zcolor[2] = (Standard_ShortReal) zcolor.Blue();
544 /* Name of font for names of axes */
545 cubic.fontOfNames = (char*) fontOfNames;
546 /* Style of names of axes */
547 cubic.styleOfNames = styleOfNames;
548 /* Size of names of axes */
549 cubic.sizeOfNames = sizeOfNames;
550 /* Name of font for values */
551 cubic.fontOfValues = (char*) fontOfValues;
552 /* Style of values */
553 cubic.styleOfValues = styleOfValues;
555 cubic.sizeOfValues = sizeOfValues;
556 /* Name of font for names of axes */
557 cubic.fontOfNames = (char*) fontOfNames;
558 /* Style of names of axes */
559 cubic.styleOfNames = styleOfNames;
560 /* Size of names of axes */
561 cubic.sizeOfNames = sizeOfNames;
562 /* Name of font for values */
563 cubic.fontOfValues = (char*) fontOfValues;
564 /* Style of values */
565 cubic.styleOfValues = styleOfValues;
567 cubic.sizeOfValues = sizeOfValues;
569 call_togl_graduatedtrihedron_display((CALL_DEF_VIEW*)&view, &cubic);
572 void OpenGl_GraphicDriver::GraduatedTrihedronErase(const Graphic3d_CView& view)
574 call_togl_graduatedtrihedron_erase((CALL_DEF_VIEW*)&view);
577 void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues(const Standard_ShortReal xmin,
578 const Standard_ShortReal ymin,
579 const Standard_ShortReal zmin,
580 const Standard_ShortReal xmax,
581 const Standard_ShortReal ymax,
582 const Standard_ShortReal zmax)
584 call_togl_graduatedtrihedron_minmaxvalues(xmin, ymin, zmin, xmax, ymax, zmax);