1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <OpenGl_GraphicDriver.hxx>
22 #include <OpenGl_CView.hxx>
23 #include <OpenGl_Trihedron.hxx>
24 #include <OpenGl_GraduatedTrihedron.hxx>
25 #include <OpenGl_TextureBox.hxx>
26 #include <OpenGl_tgl_funcs.hxx>
28 #include <Quantity_NameOfColor.hxx>
29 #include <TColStd_HArray1OfReal.hxx>
30 #include <AlienImage_AlienImage.hxx>
31 #include <Image_Image.hxx>
33 void OpenGl_GraphicDriver::Environment(const Graphic3d_CView& ACView)
35 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
38 aCView->View->SetTextureEnv(ACView.Context.TexEnvId);
39 aCView->View->SetSurfaceDetail((Visual3d_TypeOfSurfaceDetail)ACView.Context.SurfaceDetail);
44 // Triedron methods : the Triedron is a non-zoomable object.
47 void OpenGl_GraphicDriver::ZBufferTriedronSetup (
48 const Quantity_NameOfColor XColor,
49 const Quantity_NameOfColor YColor,
50 const Quantity_NameOfColor ZColor,
51 const Standard_Real SizeRatio,
52 const Standard_Real AxisDiametr,
53 const Standard_Integer NbFacettes)
55 OpenGl_Trihedron::Setup(XColor,YColor,ZColor,SizeRatio,AxisDiametr,NbFacettes);
58 void OpenGl_GraphicDriver::TriedronDisplay (
59 const Graphic3d_CView& ACView,
60 const Aspect_TypeOfTriedronPosition APosition,
61 const Quantity_NameOfColor AColor,
62 const Standard_Real AScale,
63 const Standard_Boolean AsWireframe )
65 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
68 aCView->View->TriedronDisplay(APosition, AColor, AScale, AsWireframe);
72 void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& ACView)
74 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
77 aCView->View->TriedronErase();
81 void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ACView,const Aspect_TypeOfTriedronEcho AType )
86 void OpenGl_GraphicDriver::BackgroundImage( const Standard_CString FileName,
87 const Graphic3d_CView& ACView,
88 const Aspect_FillMethod FillStyle )
90 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
93 aCView->View->CreateBackgroundTexture( FileName, FillStyle );
97 void OpenGl_GraphicDriver::SetBgImageStyle( const Graphic3d_CView& ACView,
98 const Aspect_FillMethod FillStyle )
100 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
103 aCView->View->SetBackgroundTextureStyle( FillStyle );
107 void OpenGl_GraphicDriver::SetBgGradientStyle(const Graphic3d_CView& ACView,const Aspect_GradientFillMethod FillType)
109 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
112 aCView->View->SetBackgroundGradientType(FillType);
116 void OpenGl_GraphicDriver::GraduatedTrihedronDisplay(const Graphic3d_CView& ACView, const Graphic3d_CGraduatedTrihedron& cubic)
118 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
121 aCView->View->GraduatedTrihedronDisplay(cubic);
125 void OpenGl_GraphicDriver::GraduatedTrihedronErase(const Graphic3d_CView& ACView)
127 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
130 aCView->View->GraduatedTrihedronErase();
134 void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues(const Standard_ShortReal xmin,
135 const Standard_ShortReal ymin,
136 const Standard_ShortReal zmin,
137 const Standard_ShortReal xmax,
138 const Standard_ShortReal ymax,
139 const Standard_ShortReal zmax)
141 OpenGl_GraduatedTrihedron::SetMinMax(xmin, ymin, zmin, xmax, ymax, zmax);
144 // Helper function, returns the nearest power of two greater than the argument value
145 inline Standard_Integer GetNearestPow2(Standard_Integer theValue)
147 // Precaution against overflow
148 Standard_Integer aHalfMax = IntegerLast() >> 1, aRes = 1;
149 if ( theValue > aHalfMax ) theValue = aHalfMax;
150 while ( aRes < theValue ) aRes <<= 1;
154 Standard_Integer OpenGl_GraphicDriver::CreateTexture (const Graphic3d_TypeOfTexture Type,
155 const Handle_AlienImage_AlienImage &Image,
156 const Standard_CString FileName,
157 const Handle(TColStd_HArray1OfReal)& TexUpperBounds) const
159 Handle(Image_Image) MyPic = Image->ToImage();
161 Standard_Integer aGlWidth = (Type == Graphic3d_TOT_2D_MIPMAP) ? MyPic->Width() : GetNearestPow2(MyPic->Width());
162 Standard_Integer aGlHeight = (Type == Graphic3d_TOT_2D_MIPMAP) ? MyPic->Height() : GetNearestPow2(MyPic->Height());
164 TexUpperBounds->SetValue(1, ((Standard_Real) (MyPic->Width())/((Standard_Real) aGlWidth)));
165 TexUpperBounds->SetValue(2, ((Standard_Real) (MyPic->Height())/((Standard_Real) aGlHeight)));
167 unsigned char *MyImageData = new unsigned char[aGlWidth*aGlHeight*4];
168 unsigned char *MyData = MyImageData;
171 Quantity_Color MyColor;
173 if (MyImageData == NULL)
176 static Standard_Integer textureRank=0;
177 char textureName[16];
178 Standard_PCharacter fileName = textureName;
179 sprintf(fileName,"Tex%d",++textureRank);
181 for (j = MyPic->Height() - 1; j >= 0; j--)
182 for (i = 0; i < aGlWidth; i++){
183 if (i < MyPic->Width()){
184 MyColor = MyPic->PixelColor(i, j);
185 *MyData++ = (int)(255 * MyColor.Red());
186 *MyData++ = (int)(255 * MyColor.Green());
187 *MyData++ = (int)(255 * MyColor.Blue());
190 *MyData++ = (int)(0);
191 *MyData++ = (int)(0);
192 *MyData++ = (int)(0);
197 // Padding the lower part of the texture with black
198 for (j = aGlHeight - 1; j >= MyPic->Height(); j--)
199 for (i = 0; i < aGlWidth; i++){
200 *MyData++ = (int)(0);
201 *MyData++ = (int)(0);
202 *MyData++ = (int)(0);
208 case Graphic3d_TOT_1D:
209 TexId = GetTextureData1D (fileName, aGlWidth, aGlHeight, MyImageData);
212 case Graphic3d_TOT_2D:
213 TexId = GetTextureData2D (fileName, aGlWidth, aGlHeight, MyImageData);
216 case Graphic3d_TOT_2D_MIPMAP:
217 TexId = GetTextureData2DMipMap (fileName, aGlWidth, aGlHeight, MyImageData);
224 delete [] MyImageData;
229 void OpenGl_GraphicDriver::DestroyTexture (const Standard_Integer theTexId) const
231 FreeTexture (GetSharedContext(), theTexId);
234 void OpenGl_GraphicDriver::ModifyTexture (const Standard_Integer theTexId,
235 const Graphic3d_CInitTexture& theInfo) const
237 if (theInfo.doModulate)
238 SetTextureModulate (theTexId);
240 SetTextureDecal (theTexId);
242 if (theInfo.doRepeat)
243 SetTextureRepeat (theTexId);
245 SetTextureClamp (theTexId);
247 switch (theInfo.Mode)
250 SetModeObject (theTexId, theInfo.sparams, theInfo.tparams);
254 SetModeSphere (theTexId);
258 SetModeEye (theTexId, theInfo.sparams, theInfo.tparams);
262 SetModeManual (theTexId);
266 if (theInfo.doLinear)
267 SetRenderLinear (theTexId);
269 SetRenderNearest (theTexId);
271 SetTexturePosition (theTexId,
272 theInfo.sx, theInfo.sy,
273 theInfo.tx, theInfo.ty,