0022819: Redesign of OpenGl driver
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_9.cxx
1 // File:      OpenGl_GraphicDriver_9.cxx
2 // Created:   20 October 2011
3 // Author:    Sergey ZERCHANINOV
4 // Copyright: OPEN CASCADE 2011
5
6 #include <OpenGl_GraphicDriver.hxx>
7 #include <OpenGl_CView.hxx>
8 #include <OpenGl_Trihedron.hxx>
9 #include <OpenGl_GraduatedTrihedron.hxx>
10 #include <OpenGl_TextureBox.hxx>
11 #include <OpenGl_tgl_funcs.hxx>
12
13 #include <Quantity_NameOfColor.hxx>
14 #include <TColStd_HArray1OfReal.hxx>
15 #include <AlienImage_AlienImage.hxx>
16 #include <Image_Image.hxx>
17
18 void OpenGl_GraphicDriver::Environment(const Graphic3d_CView& ACView)
19 {
20   const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
21   if (aCView)
22   {
23     aCView->View->SetTextureEnv(ACView.Context.TexEnvId);
24     aCView->View->SetSurfaceDetail((Visual3d_TypeOfSurfaceDetail)ACView.Context.SurfaceDetail);
25   }
26 }
27
28 //
29 // Triedron methods : the Triedron is a non-zoomable object.
30 //
31
32 void OpenGl_GraphicDriver::ZBufferTriedronSetup (
33   const Quantity_NameOfColor XColor,
34   const Quantity_NameOfColor YColor,
35   const Quantity_NameOfColor ZColor,
36   const Standard_Real        SizeRatio,
37   const Standard_Real        AxisDiametr,
38   const Standard_Integer     NbFacettes)
39 {
40   OpenGl_Trihedron::Setup(XColor,YColor,ZColor,SizeRatio,AxisDiametr,NbFacettes);
41 }
42
43 void OpenGl_GraphicDriver::TriedronDisplay (
44   const Graphic3d_CView& ACView,
45   const Aspect_TypeOfTriedronPosition APosition,
46   const Quantity_NameOfColor AColor, 
47   const Standard_Real AScale,
48   const Standard_Boolean AsWireframe )
49 {
50   const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
51   if (aCView)
52   {
53     aCView->View->TriedronDisplay(APosition, AColor, AScale, AsWireframe);
54     aCView->WS->Invalidate();
55   }
56 }
57
58 void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& ACView) 
59 {
60   const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
61   if (aCView)
62   {
63     aCView->View->TriedronErase();
64     aCView->WS->Invalidate();
65   }
66 }
67
68 void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ACView,const Aspect_TypeOfTriedronEcho AType )
69 {
70   // Do nothing
71 }
72
73 void OpenGl_GraphicDriver::BackgroundImage( const Standard_CString FileName, 
74                                            const Graphic3d_CView& ACView,
75                                            const Aspect_FillMethod FillStyle )
76 {
77   const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
78   if (aCView)
79   {
80     aCView->View->CreateBackgroundTexture( FileName, FillStyle );
81     aCView->WS->Invalidate();
82   }
83 }
84
85 void OpenGl_GraphicDriver::SetBgImageStyle( const Graphic3d_CView& ACView,
86                                            const Aspect_FillMethod FillStyle )
87 {
88   const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
89   if (aCView)
90   {
91     aCView->View->SetBackgroundTextureStyle( FillStyle );
92     aCView->WS->Invalidate();
93   }
94 }
95
96 void OpenGl_GraphicDriver::SetBgGradientStyle(const Graphic3d_CView& ACView,const Aspect_GradientFillMethod FillType)
97 {
98   const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
99   if (aCView)
100   {
101     aCView->View->SetBackgroundGradientType(FillType);
102     aCView->WS->Invalidate();
103   }
104 }
105
106 void OpenGl_GraphicDriver::GraduatedTrihedronDisplay(const Graphic3d_CView& ACView, const Graphic3d_CGraduatedTrihedron& cubic)
107 {
108   const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
109   if (aCView)
110   {
111     aCView->View->GraduatedTrihedronDisplay(cubic);
112     aCView->WS->Invalidate();
113   }
114 }
115
116 void OpenGl_GraphicDriver::GraduatedTrihedronErase(const Graphic3d_CView& ACView)
117 {
118   const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
119   if (aCView)
120   {
121     aCView->View->GraduatedTrihedronErase();
122     aCView->WS->Invalidate();
123   }
124 }
125
126 void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues(const Standard_ShortReal xmin,
127                                                          const Standard_ShortReal ymin,
128                                                          const Standard_ShortReal zmin,
129                                                          const Standard_ShortReal xmax,
130                                                          const Standard_ShortReal ymax,
131                                                          const Standard_ShortReal zmax)
132 {
133   OpenGl_GraduatedTrihedron::SetMinMax(xmin, ymin, zmin, xmax, ymax, zmax);
134 }
135
136 // Helper function, returns the nearest power of two greater than the argument value
137 inline Standard_Integer GetNearestPow2(Standard_Integer theValue)
138 {
139   // Precaution against overflow
140   Standard_Integer aHalfMax = IntegerLast() >> 1, aRes = 1;
141   if ( theValue > aHalfMax ) theValue = aHalfMax;
142   while ( aRes < theValue ) aRes <<= 1;
143   return aRes;
144 }
145
146 Standard_Integer OpenGl_GraphicDriver::CreateTexture (const Graphic3d_TypeOfTexture Type,
147                                                       const Handle_AlienImage_AlienImage &Image,
148                                                                                                           const Standard_CString FileName,
149                                                                                                           const Handle(TColStd_HArray1OfReal)& TexUpperBounds) const
150 {
151   Handle(Image_Image) MyPic = Image->ToImage();
152
153   Standard_Integer aGlWidth  = (Type == Graphic3d_TOT_2D_MIPMAP) ? MyPic->Width() : GetNearestPow2(MyPic->Width());
154   Standard_Integer aGlHeight = (Type == Graphic3d_TOT_2D_MIPMAP) ? MyPic->Height() : GetNearestPow2(MyPic->Height());
155
156   TexUpperBounds->SetValue(1, ((Standard_Real) (MyPic->Width())/((Standard_Real) aGlWidth)));
157   TexUpperBounds->SetValue(2, ((Standard_Real) (MyPic->Height())/((Standard_Real) aGlHeight)));
158
159   unsigned char *MyImageData = (unsigned char *)malloc(aGlWidth*aGlHeight*4);
160   unsigned char *MyData = MyImageData;
161   int TexId;
162   int i,j;
163   Quantity_Color MyColor;
164
165   if (MyImageData == NULL)
166     return -1;
167
168   static Standard_Integer textureRank=0;  
169   char textureName[16];
170   Standard_PCharacter fileName = textureName; 
171   sprintf(fileName,"Tex%d",++textureRank);
172
173     for (j = MyPic->Height() - 1; j >= 0; j--)
174       for (i = 0; i < aGlWidth; i++){
175         if (i < MyPic->Width()){
176           MyColor = MyPic->PixelColor(i, j);
177           *MyData++ = (int)(255 * MyColor.Red());
178           *MyData++ = (int)(255 * MyColor.Green());
179           *MyData++ = (int)(255 * MyColor.Blue());
180         }
181         else {
182           *MyData++ = (int)(0);
183           *MyData++ = (int)(0);
184           *MyData++ = (int)(0);
185         }
186         *MyData++ = 0xFF;
187       }
188
189       // Padding the lower part of the texture with black
190       for (j = aGlHeight - 1; j >= MyPic->Height(); j--)
191         for (i = 0; i < aGlWidth; i++){
192           *MyData++ = (int)(0);
193           *MyData++ = (int)(0);
194           *MyData++ = (int)(0);
195           *MyData++ = 0xFF;
196         }  
197
198
199         switch (Type)
200         {
201         case Graphic3d_TOT_1D:
202           TexId = call_togl_create_texture(0, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
203           break;
204
205         case Graphic3d_TOT_2D:
206           TexId = call_togl_create_texture(1, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
207           break;
208
209         case Graphic3d_TOT_2D_MIPMAP:
210           TexId = call_togl_create_texture(2, aGlWidth, aGlHeight, MyImageData, (Standard_PCharacter)fileName);
211           break;
212
213         default:
214           TexId = -1;
215         }
216
217         free(MyImageData);
218         return TexId;
219
220 }
221
222 void OpenGl_GraphicDriver::DestroyTexture(const Standard_Integer TexId) const
223 {
224   call_togl_destroy_texture(TexId);
225 }
226
227
228 void OpenGl_GraphicDriver::ModifyTexture(const Standard_Integer TexId,const Graphic3d_CInitTexture& AValue) const
229 {
230   call_togl_modify_texture(TexId, (CALL_DEF_INIT_TEXTURE *)&AValue);
231 }