0022779: Pixel format should be chosen to support stencil buffer
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_9.cxx
CommitLineData
b311480e 1// Created on: 2011-10-20
2// Created by: Sergey ZERCHANINOV
3// Copyright (c) 2011-2012 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
2166f0fa
SK
20
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>
7fd59977 27
2166f0fa
SK
28#include <Quantity_NameOfColor.hxx>
29#include <TColStd_HArray1OfReal.hxx>
30#include <AlienImage_AlienImage.hxx>
31#include <Image_Image.hxx>
7fd59977 32
2166f0fa
SK
33void OpenGl_GraphicDriver::Environment(const Graphic3d_CView& ACView)
34{
35 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
36 if (aCView)
37 {
38 aCView->View->SetTextureEnv(ACView.Context.TexEnvId);
39 aCView->View->SetSurfaceDetail((Visual3d_TypeOfSurfaceDetail)ACView.Context.SurfaceDetail);
40 }
41}
7fd59977 42
2166f0fa
SK
43//
44// Triedron methods : the Triedron is a non-zoomable object.
45//
7fd59977 46
2166f0fa
SK
47void 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)
54{
55 OpenGl_Trihedron::Setup(XColor,YColor,ZColor,SizeRatio,AxisDiametr,NbFacettes);
56}
7fd59977 57
2166f0fa
SK
58void 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 )
64{
65 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
66 if (aCView)
67 {
68 aCView->View->TriedronDisplay(APosition, AColor, AScale, AsWireframe);
2166f0fa
SK
69 }
70}
7fd59977 71
2166f0fa
SK
72void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& ACView)
73{
74 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
75 if (aCView)
76 {
77 aCView->View->TriedronErase();
2166f0fa
SK
78 }
79}
7fd59977 80
2166f0fa
SK
81void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ACView,const Aspect_TypeOfTriedronEcho AType )
82{
83 // Do nothing
84}
7fd59977 85
2166f0fa
SK
86void OpenGl_GraphicDriver::BackgroundImage( const Standard_CString FileName,
87 const Graphic3d_CView& ACView,
88 const Aspect_FillMethod FillStyle )
89{
90 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
91 if (aCView)
92 {
93 aCView->View->CreateBackgroundTexture( FileName, FillStyle );
2166f0fa
SK
94 }
95}
7fd59977 96
2166f0fa
SK
97void OpenGl_GraphicDriver::SetBgImageStyle( const Graphic3d_CView& ACView,
98 const Aspect_FillMethod FillStyle )
99{
100 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
101 if (aCView)
102 {
103 aCView->View->SetBackgroundTextureStyle( FillStyle );
2166f0fa
SK
104 }
105}
7fd59977 106
2166f0fa
SK
107void OpenGl_GraphicDriver::SetBgGradientStyle(const Graphic3d_CView& ACView,const Aspect_GradientFillMethod FillType)
108{
109 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
110 if (aCView)
111 {
112 aCView->View->SetBackgroundGradientType(FillType);
2166f0fa
SK
113 }
114}
7fd59977 115
2166f0fa
SK
116void OpenGl_GraphicDriver::GraduatedTrihedronDisplay(const Graphic3d_CView& ACView, const Graphic3d_CGraduatedTrihedron& cubic)
117{
118 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
119 if (aCView)
120 {
121 aCView->View->GraduatedTrihedronDisplay(cubic);
2166f0fa
SK
122 }
123}
7fd59977 124
2166f0fa
SK
125void OpenGl_GraphicDriver::GraduatedTrihedronErase(const Graphic3d_CView& ACView)
126{
127 const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
128 if (aCView)
129 {
130 aCView->View->GraduatedTrihedronErase();
2166f0fa
SK
131 }
132}
7fd59977 133
2166f0fa
SK
134void 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)
140{
141 OpenGl_GraduatedTrihedron::SetMinMax(xmin, ymin, zmin, xmax, ymax, zmax);
142}
7fd59977 143
144// Helper function, returns the nearest power of two greater than the argument value
145inline Standard_Integer GetNearestPow2(Standard_Integer theValue)
146{
147 // Precaution against overflow
148 Standard_Integer aHalfMax = IntegerLast() >> 1, aRes = 1;
149 if ( theValue > aHalfMax ) theValue = aHalfMax;
150 while ( aRes < theValue ) aRes <<= 1;
151 return aRes;
152}
153
2166f0fa
SK
154Standard_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
7fd59977 158{
7fd59977 159 Handle(Image_Image) MyPic = Image->ToImage();
160
7fd59977 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());
163
164 TexUpperBounds->SetValue(1, ((Standard_Real) (MyPic->Width())/((Standard_Real) aGlWidth)));
165 TexUpperBounds->SetValue(2, ((Standard_Real) (MyPic->Height())/((Standard_Real) aGlHeight)));
166
9d35f668 167 unsigned char *MyImageData = new unsigned char[aGlWidth*aGlHeight*4];
7fd59977 168 unsigned char *MyData = MyImageData;
169 int TexId;
170 int i,j;
171 Quantity_Color MyColor;
172
173 if (MyImageData == NULL)
174 return -1;
175
7fd59977 176 static Standard_Integer textureRank=0;
177 char textureName[16];
178 Standard_PCharacter fileName = textureName;
179 sprintf(fileName,"Tex%d",++textureRank);
7fd59977 180
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());
188 }
189 else {
190 *MyData++ = (int)(0);
191 *MyData++ = (int)(0);
192 *MyData++ = (int)(0);
193 }
194 *MyData++ = 0xFF;
195 }
196
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);
203 *MyData++ = 0xFF;
204 }
205
7fd59977 206 switch (Type)
207 {
208 case Graphic3d_TOT_1D:
5f8b738e 209 TexId = GetTextureData1D (fileName, aGlWidth, aGlHeight, MyImageData);
7fd59977 210 break;
211
212 case Graphic3d_TOT_2D:
5f8b738e 213 TexId = GetTextureData2D (fileName, aGlWidth, aGlHeight, MyImageData);
7fd59977 214 break;
215
216 case Graphic3d_TOT_2D_MIPMAP:
5f8b738e 217 TexId = GetTextureData2DMipMap (fileName, aGlWidth, aGlHeight, MyImageData);
7fd59977 218 break;
219
220 default:
221 TexId = -1;
222 }
223
9d35f668 224 delete [] MyImageData;
7fd59977 225 return TexId;
226
227}
228
5f8b738e 229void OpenGl_GraphicDriver::DestroyTexture (const Standard_Integer theTexId) const
7fd59977 230{
5e27df78 231 FreeTexture (GetSharedContext(), theTexId);
7fd59977 232}
233
5f8b738e 234void OpenGl_GraphicDriver::ModifyTexture (const Standard_Integer theTexId,
235 const Graphic3d_CInitTexture& theInfo) const
7fd59977 236{
5f8b738e 237 if (theInfo.doModulate)
238 SetTextureModulate (theTexId);
239 else
240 SetTextureDecal (theTexId);
241
242 if (theInfo.doRepeat)
243 SetTextureRepeat (theTexId);
244 else
245 SetTextureClamp (theTexId);
246
247 switch (theInfo.Mode)
248 {
249 case 0:
250 SetModeObject (theTexId, theInfo.sparams, theInfo.tparams);
251 break;
252
253 case 1:
254 SetModeSphere (theTexId);
255 break;
256
257 case 2:
258 SetModeEye (theTexId, theInfo.sparams, theInfo.tparams);
259 break;
260
261 case 3:
262 SetModeManual (theTexId);
263 break;
264 }
265
266 if (theInfo.doLinear)
267 SetRenderLinear (theTexId);
268 else
269 SetRenderNearest (theTexId);
270
271 SetTexturePosition (theTexId,
272 theInfo.sx, theInfo.sy,
273 theInfo.tx, theInfo.ty,
274 theInfo.angle);
7fd59977 275}