0022792: Globally defined symbol PI conflicts with VTK definition (Intel compiler)
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_9.cxx
CommitLineData
7fd59977 1// File OpenGl_GraphicDriver_9.cxx
2// Created 1/08/97
3// Author PCT
4// Modified
5// 16-09-98 ; BGN : Points d'entree du Triedre (S3819, Phase 1)
6// 22-09-98 ; BGN : S3989 (anciennement S3819)
7// TypeOfTriedron* from Aspect (pas Graphic3d)
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
25#define IMP131100 //GG_Enable to creates more than one texture
26// with the same name.
27
28#define OCC1188 //SAV added methods to set background image
29
30#include <OpenGl_GraphicDriver.jxx>
31
32#include <Aspect_DriverDefinitionError.hxx>
33#include <Quantity_NameOfColor.hxx>
34#include <AlienImage.hxx>
35#include <Standard_Integer.hxx>
36
37#include <OpenGl_tgl_funcs.hxx>
38
39#include <stdlib.h>
40#include <stdio.h>
41
42
43#include <Image_Image.hxx>
44
45
46//-Aliases
47
48//-Global data definitions
49
50//-Methods, in order
51
52// Helper function, returns the nearest power of two greater than the argument value
53inline Standard_Integer GetNearestPow2(Standard_Integer theValue)
54{
55 // Precaution against overflow
56 Standard_Integer aHalfMax = IntegerLast() >> 1, aRes = 1;
57 if ( theValue > aHalfMax ) theValue = aHalfMax;
58 while ( aRes < theValue ) aRes <<= 1;
59 return aRes;
60}
61
62Standard_Integer OpenGl_GraphicDriver::CreateTexture(const Graphic3d_TypeOfTexture Type,const Handle_AlienImage_AlienImage &Image,const Standard_CString FileName,const Handle(TColStd_HArray1OfReal)& TexUpperBounds) const
63{
64 // transmutation de l'image en un bon tableau RGBA pour la toolkit des textures
65 // lamentable d'un point de vue vitesse...
66 Handle(Image_Image) MyPic = Image->ToImage();
67
68 //TODO: The temporary fix in order to make AIS_TexturedShape work. To be improved in next release.
69 Standard_Integer aGlWidth = (Type == Graphic3d_TOT_2D_MIPMAP) ? MyPic->Width() : GetNearestPow2(MyPic->Width());
70 Standard_Integer aGlHeight = (Type == Graphic3d_TOT_2D_MIPMAP) ? MyPic->Height() : GetNearestPow2(MyPic->Height());
71
72 TexUpperBounds->SetValue(1, ((Standard_Real) (MyPic->Width())/((Standard_Real) aGlWidth)));
73 TexUpperBounds->SetValue(2, ((Standard_Real) (MyPic->Height())/((Standard_Real) aGlHeight)));
74
75 unsigned char *MyImageData = (unsigned char *)malloc(aGlWidth*aGlHeight*4);
76 unsigned char *MyData = MyImageData;
77 int TexId;
78 int i,j;
79 Quantity_Color MyColor;
80
81 if (MyImageData == NULL)
82 return -1;
83
84#ifdef IMP131100
85
86 static Standard_Integer textureRank=0;
87 char textureName[16];
88 Standard_PCharacter fileName = textureName;
89 sprintf(fileName,"Tex%d",++textureRank);
90#else
91 Standard_CString fileName = FileName
92#endif
93
94 for (j = MyPic->Height() - 1; j >= 0; j--)
95 for (i = 0; i < aGlWidth; i++){
96 if (i < MyPic->Width()){
97 MyColor = MyPic->PixelColor(i, j);
98 *MyData++ = (int)(255 * MyColor.Red());
99 *MyData++ = (int)(255 * MyColor.Green());
100 *MyData++ = (int)(255 * MyColor.Blue());
101 }
102 else {
103 *MyData++ = (int)(0);
104 *MyData++ = (int)(0);
105 *MyData++ = (int)(0);
106 }
107 *MyData++ = 0xFF;
108 }
109
110 // Padding the lower part of the texture with black
111 for (j = aGlHeight - 1; j >= MyPic->Height(); j--)
112 for (i = 0; i < aGlWidth; i++){
113 *MyData++ = (int)(0);
114 *MyData++ = (int)(0);
115 *MyData++ = (int)(0);
116 *MyData++ = 0xFF;
117 }
118
119
120 switch (Type)
121 {
122 case Graphic3d_TOT_1D:
123 TexId = call_togl_create_texture(0, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
124 break;
125
126 case Graphic3d_TOT_2D:
127 TexId = call_togl_create_texture(1, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
128 break;
129
130 case Graphic3d_TOT_2D_MIPMAP:
131 TexId = call_togl_create_texture(2, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
132 break;
133
134 default:
135 TexId = -1;
136 }
137
138 free(MyImageData);
139 return TexId;
140
141}
142
143void OpenGl_GraphicDriver::DestroyTexture(const Standard_Integer TexId) const
144{
145 call_togl_destroy_texture(TexId);
146}
147
148
149void OpenGl_GraphicDriver::ModifyTexture(const Standard_Integer TexId,const Graphic3d_CInitTexture& AValue) const
150{
151 call_togl_modify_texture(TexId, (CALL_DEF_INIT_TEXTURE *)&AValue);
152}
153
154
155
156
157void OpenGl_GraphicDriver::Environment(const Graphic3d_CView& ACView)
158{
159 call_togl_environment((CALL_DEF_VIEW *)&ACView);
160}
161
162
163//
164// Triedron methods : the Triedron is a non-zoomable object.
165//
166
167void OpenGl_GraphicDriver::ZBufferTriedronSetup (
168 const Quantity_NameOfColor XColor,
169 const Quantity_NameOfColor YColor,
170 const Quantity_NameOfColor ZColor,
171 const Standard_Real SizeRatio,
172 const Standard_Real AxisDiametr,
173 const Standard_Integer NbFacettes)
174{
175 float Xrgb[3];
176 float Yrgb[3];
177 float Zrgb[3];
178 Standard_Real R,G,B;
179 Quantity_Color(XColor).Values(R, G, B, Quantity_TOC_RGB);
180 Xrgb[0] = float (R);
181 Xrgb[1] = float (G);
182 Xrgb[2] = float (B);
183 Quantity_Color(YColor).Values(R, G, B, Quantity_TOC_RGB);
184 Yrgb[0] = float (R);
185 Yrgb[1] = float (G);
186 Yrgb[2] = float (B);
187 Quantity_Color(ZColor).Values(R, G, B, Quantity_TOC_RGB);
188 Zrgb[0] = float (R);
189 Zrgb[1] = float (G);
190 Zrgb[2] = float (B);
191
192 call_togl_ztriedron_setup(Xrgb, Yrgb, Zrgb,
193 (Standard_ShortReal)(SizeRatio), (Standard_ShortReal)(AxisDiametr), NbFacettes);
194}
195
196void OpenGl_GraphicDriver::TriedronDisplay (
197 const Graphic3d_CView& ACView,
198 const Aspect_TypeOfTriedronPosition APosition,
199 const Quantity_NameOfColor AColor,
200 const Standard_Real AScale,
201 const Standard_Boolean AsWireframe )
202{
203 Standard_Real R,G,B;
204 float r,g,b;
205 Quantity_Color Color(AColor);
206 Color.Values(R,G,B,Quantity_TOC_RGB);
207 r = float (R);
208 g = float (G);
209 b = float (B);
210
211 call_togl_triedron_display((CALL_DEF_VIEW *)&ACView,(int)APosition, r,g,b, (float)AScale, AsWireframe);
212}
213
214void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& ACView)
215{
216 call_togl_triedron_erase((CALL_DEF_VIEW *)&ACView);
217}
218
219void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ACView,const Aspect_TypeOfTriedronEcho AType )
220{
221 call_togl_triedron_echo( (CALL_DEF_VIEW *)&ACView,AType );
222}
223
224void OpenGl_GraphicDriver::BackgroundImage( const Standard_CString FileName,
225 const Graphic3d_CView& ACView,
226 const Aspect_FillMethod FillStyle )
227{
228#ifdef OCC1188
229 Graphic3d_CView MyCView = ACView;
230 Standard_Integer width, height;
231 Handle(Image_Image) image;
232
233 Standard_Boolean result = AlienImage::LoadImageFile( FileName, image, width, height );
234
235 if ( result ) {
236 unsigned char *data = (unsigned char*)malloc( width * height * 3 );
237 unsigned char *pdata = data;
238 Standard_Integer i,j;
239 Quantity_Color color;
240
241 if ( data != NULL ) {
242 for ( j = height - 1; j >= 0; j-- )
243 for ( i = 0; i < width; i++ ) {
244 color = image->PixelColor( i, j );
245 *pdata++ = (int)( 255 * color.Red() );
246 *pdata++ = (int)( 255 * color.Green() );
247 *pdata++ = (int)( 255 * color.Blue() );
248 }
249 call_togl_create_bg_texture( (CALL_DEF_VIEW*)&MyCView, width, height, data, (int)FillStyle );
250 // delete data here
251 free( data );
252 }
253 }
254#endif // OCC1188
255}
256
257void OpenGl_GraphicDriver::SetBgImageStyle( const Graphic3d_CView& ACView,
258 const Aspect_FillMethod FillStyle )
259{
260#ifdef OCC1188
261 Graphic3d_CView MyCView = ACView;
262 call_togl_set_bg_texture_style( (CALL_DEF_VIEW*)&MyCView, (int)FillStyle );
263#endif //OCC1188
264}
265
266void OpenGl_GraphicDriver::SetBgGradientStyle(const Graphic3d_CView& ACView,const Aspect_GradientFillMethod FillType)
267{
268 call_togl_set_gradient_type( ACView.WsId, FillType);
269}
270
7fd59977 271void OpenGl_GraphicDriver::GraduatedTrihedronDisplay(const Graphic3d_CView& view,
13a22457 272 const Graphic3d_CGraduatedTrihedron& cubic)
7fd59977 273{
13a22457 274 call_togl_graduatedtrihedron_display((CALL_DEF_VIEW*)&view, cubic);
7fd59977 275}
276
277void OpenGl_GraphicDriver::GraduatedTrihedronErase(const Graphic3d_CView& view)
278{
279 call_togl_graduatedtrihedron_erase((CALL_DEF_VIEW*)&view);
280}
281
282void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues(const Standard_ShortReal xmin,
283 const Standard_ShortReal ymin,
284 const Standard_ShortReal zmin,
285 const Standard_ShortReal xmax,
286 const Standard_ShortReal ymax,
287 const Standard_ShortReal zmax)
288{
289 call_togl_graduatedtrihedron_minmaxvalues(xmin, ymin, zmin, xmax, ymax, zmax);
290}