1 // Created on: 2011-09-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.
20 #include <OpenGl_GlCore12.hxx>
22 #include <InterfaceGraphic.hxx>
24 #include <OpenGl_Workspace.hxx>
26 #include <OpenGl_AspectLine.hxx>
27 #include <OpenGl_AspectFace.hxx>
28 #include <OpenGl_AspectMarker.hxx>
29 #include <OpenGl_AspectText.hxx>
30 #include <OpenGl_Context.hxx>
32 #include <OpenGl_Texture.hxx>
34 #include <Graphic3d_TextureParams.hxx>
36 IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
37 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
41 static const TEL_COLOUR myDefaultHighlightColor = { { 1.F, 1.F, 1.F, 1.F } };
43 static const OpenGl_AspectLine myDefaultAspectLine;
44 static const OpenGl_AspectFace myDefaultAspectFace;
45 static const OpenGl_AspectMarker myDefaultAspectMarker;
46 static const OpenGl_AspectText myDefaultAspectText;
48 static const OpenGl_TextParam myDefaultTextParam =
50 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
53 static const OpenGl_Matrix myDefaultMatrix =
55 {{ 1.0F, 0.0F, 0.0F, 0.0F },
56 { 0.0F, 1.0F, 0.0F, 0.0F },
57 { 0.0F, 0.0F, 1.0F, 0.0F },
58 { 0.0F, 0.0F, 0.0F, 1.0F }}
63 // =======================================================================
64 // function : OpenGl_Workspace
66 // =======================================================================
67 OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
68 const CALL_DEF_WINDOW& theCWindow,
69 Aspect_RenderingContext theGContext,
70 const Handle(OpenGl_Context)& theShareCtx)
71 : OpenGl_Window (theDisplay, theCWindow, theGContext, theShareCtx),
73 myIsTransientOpen (Standard_False),
74 myTransientDrawToFront (Standard_True),
75 myRetainMode (Standard_False),
76 myUseTransparency (Standard_False),
77 myUseZBuffer (Standard_False),
78 myUseDepthTest (Standard_True),
79 myUseGLLight (Standard_True),
80 myBackBufferRestored (Standard_False),
85 HighlightColor (&myDefaultHighlightColor),
86 AspectLine_set (&myDefaultAspectLine),
87 AspectLine_applied (NULL),
88 AspectFace_set (&myDefaultAspectFace),
89 AspectFace_applied (NULL),
90 AspectMarker_set (&myDefaultAspectMarker),
91 AspectMarker_applied (NULL),
92 AspectText_set (&myDefaultAspectText),
93 AspectText_applied (NULL),
94 TextParam_set (&myDefaultTextParam),
95 TextParam_applied (NULL),
96 ViewMatrix_applied (&myDefaultMatrix),
97 StructureMatrix_applied (&myDefaultMatrix),
98 PolygonOffset_applied (NULL)
100 theDisplay->InitAttributes();
102 // General initialization of the context
104 // Eviter d'avoir les faces mal orientees en noir.
105 // Pourrait etre utiliser pour detecter les problemes d'orientation
106 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
108 // Optimisation pour le Fog et l'antialiasing
109 glHint (GL_FOG_HINT, GL_FASTEST);
110 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
111 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
112 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
115 EnablePolygonOffset();
118 // =======================================================================
119 // function : SetImmediateModeDrawToFront
121 // =======================================================================
122 Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
124 const Standard_Boolean aPrevMode = myTransientDrawToFront;
125 myTransientDrawToFront = theDrawToFrontBuffer;
129 // =======================================================================
130 // function : ~OpenGl_Workspace
132 // =======================================================================
133 OpenGl_Workspace::~OpenGl_Workspace()
137 // =======================================================================
138 // function : Activate
140 // =======================================================================
141 Standard_Boolean OpenGl_Workspace::Activate()
143 if (!OpenGl_Window::Activate())
144 return Standard_False;
148 ViewMatrix_applied = &myDefaultMatrix;
149 StructureMatrix_applied = &myDefaultMatrix;
151 ResetAppliedAspect();
153 return Standard_True;
156 // =======================================================================
157 // function : UseTransparency
158 // purpose : call_togl_transparency
159 // =======================================================================
160 void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
162 if ((myUseTransparency ? 1 : 0) != (theFlag ? 1 : 0))
164 myUseTransparency = theFlag;
169 //=======================================================================
170 //function : ResetAppliedAspect
171 //purpose : Sets default values of GL parameters in accordance with default aspects
172 //=======================================================================
173 void OpenGl_Workspace::ResetAppliedAspect()
175 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
176 HighlightColor = &myDefaultHighlightColor;
177 AspectLine_set = &myDefaultAspectLine;
178 AspectLine_applied = NULL;
179 AspectFace_set = &myDefaultAspectFace;
180 AspectFace_applied = NULL;
181 AspectMarker_set = &myDefaultAspectMarker;
182 AspectMarker_applied = NULL;
183 AspectText_set = &myDefaultAspectText;
184 AspectText_applied = NULL;
185 TextParam_set = &myDefaultTextParam;
186 TextParam_applied = NULL;
187 PolygonOffset_applied = NULL;
189 AspectLine(Standard_True);
190 AspectFace(Standard_True);
191 AspectMarker(Standard_True);
192 AspectText(Standard_True);
195 // =======================================================================
196 // function : DisableTexture
198 // =======================================================================
199 Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
201 if (myTextureBound.IsNull())
203 return myTextureBound;
206 // reset texture matrix because some code may expect it is identity
207 GLint aMatrixMode = GL_TEXTURE;
208 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
209 glMatrixMode (GL_TEXTURE);
211 glMatrixMode (aMatrixMode);
213 myTextureBound->Unbind (myGlContext);
214 switch (myTextureBound->GetTarget())
218 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
220 glDisable (GL_TEXTURE_GEN_S);
222 glDisable (GL_TEXTURE_1D);
227 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
229 glDisable (GL_TEXTURE_GEN_S);
230 glDisable (GL_TEXTURE_GEN_T);
232 glDisable (GL_TEXTURE_2D);
238 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
239 myTextureBound.Nullify();
243 // =======================================================================
244 // function : setTextureParams
246 // =======================================================================
247 void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
248 const Handle(Graphic3d_TextureParams)& theParams)
250 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
251 if (aParams.IsNull())
256 GLint aMatrixMode = GL_TEXTURE;
257 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
259 // setup texture matrix
260 glMatrixMode (GL_TEXTURE);
262 const Graphic3d_Vec2& aScale = aParams->Scale();
263 const Graphic3d_Vec2& aTrans = aParams->Translation();
264 glScalef ( aScale.x(), aScale.y(), 1.0f);
265 glTranslatef (-aTrans.x(), -aTrans.y(), 0.0f);
266 glRotatef (-aParams->Rotation(), 0.0f, 0.0f, 1.0f);
268 // setup generation of texture coordinates
269 switch (aParams->GenMode())
271 case Graphic3d_TOTM_OBJECT:
273 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
274 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
275 if (theTexture->GetTarget() != GL_TEXTURE_1D)
277 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
278 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
282 case Graphic3d_TOTM_SPHERE:
284 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
285 if (theTexture->GetTarget() != GL_TEXTURE_1D)
287 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
291 case Graphic3d_TOTM_EYE:
293 glMatrixMode (GL_MODELVIEW);
297 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
298 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
300 if (theTexture->GetTarget() != GL_TEXTURE_1D)
302 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
303 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
308 case Graphic3d_TOTM_MANUAL:
313 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
315 // setup texture filtering and wrapping
316 //if (theTexture->GetParams() != theParams)
317 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
318 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : GL_CLAMP;
319 switch (theTexture->GetTarget())
323 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
324 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
325 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
330 GLenum aFilterMin = aFilter;
331 if (theTexture->HasMipmaps())
333 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
334 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
336 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
338 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
340 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
343 if (myGlContext->extAnis)
345 // setup degree of anisotropy filter
346 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
347 switch (aParams->AnisoFilter())
349 case Graphic3d_LOTA_QUALITY:
351 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aMaxDegree);
354 case Graphic3d_LOTA_MIDDLE:
357 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2));
360 case Graphic3d_LOTA_FAST:
362 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2);
365 case Graphic3d_LOTA_OFF:
368 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
374 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
375 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
376 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
377 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
383 switch (theTexture->GetTarget())
387 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
389 glEnable (GL_TEXTURE_GEN_S);
391 glEnable (GL_TEXTURE_1D);
396 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
398 glEnable (GL_TEXTURE_GEN_S);
399 glEnable (GL_TEXTURE_GEN_T);
401 glEnable (GL_TEXTURE_2D);
407 glMatrixMode (aMatrixMode); // turn back active matrix
408 theTexture->SetParams (aParams);
411 // =======================================================================
412 // function : EnableTexture
414 // =======================================================================
415 Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
416 const Handle(Graphic3d_TextureParams)& theParams)
418 if (theTexture.IsNull() || !theTexture->IsValid())
420 return DisableTexture();
423 if (myTextureBound == theTexture
424 && (theParams.IsNull() || theParams == theTexture->GetParams()))
427 return myTextureBound;
430 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
431 myTextureBound = theTexture;
432 myTextureBound->Bind (myGlContext);
433 setTextureParams (myTextureBound, theParams);