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.
25 #include <OpenGl_GlCore15.hxx>
27 #include <InterfaceGraphic.hxx>
29 #include <OpenGl_AspectLine.hxx>
30 #include <OpenGl_AspectFace.hxx>
31 #include <OpenGl_AspectMarker.hxx>
32 #include <OpenGl_AspectText.hxx>
33 #include <OpenGl_Context.hxx>
34 #include <OpenGl_FrameBuffer.hxx>
35 #include <OpenGl_Texture.hxx>
36 #include <OpenGl_View.hxx>
37 #include <OpenGl_Workspace.hxx>
38 #include <OpenGl_Element.hxx>
40 #include <Graphic3d_TextureParams.hxx>
42 #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
43 #include <OpenGl_AVIWriter.hxx>
46 IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
47 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
51 static const TEL_COLOUR myDefaultHighlightColor = { { 1.F, 1.F, 1.F, 1.F } };
53 static const OpenGl_AspectLine myDefaultAspectLine;
54 static const OpenGl_AspectFace myDefaultAspectFace;
55 static const OpenGl_AspectMarker myDefaultAspectMarker;
56 static const OpenGl_AspectText myDefaultAspectText;
58 static const OpenGl_TextParam myDefaultTextParam =
60 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
63 static const OpenGl_Matrix myDefaultMatrix =
65 {{ 1.0F, 0.0F, 0.0F, 0.0F },
66 { 0.0F, 1.0F, 0.0F, 0.0F },
67 { 0.0F, 0.0F, 1.0F, 0.0F },
68 { 0.0F, 0.0F, 0.0F, 1.0F }}
73 // =======================================================================
74 // function : OpenGl_Workspace
76 // =======================================================================
77 OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
78 const CALL_DEF_WINDOW& theCWindow,
79 Aspect_RenderingContext theGContext,
80 const Handle(OpenGl_Caps)& theCaps,
81 const Handle(OpenGl_Context)& theShareCtx)
82 : OpenGl_Window (theDisplay, theCWindow, theGContext, theCaps, theShareCtx),
84 HighlightColor (&myDefaultHighlightColor),
86 myIsTransientOpen (Standard_False),
87 myRetainMode (Standard_False),
88 myTransientDrawToFront (Standard_True),
89 myUseTransparency (Standard_False),
90 myUseZBuffer (Standard_False),
91 myUseDepthTest (Standard_True),
92 myUseGLLight (Standard_True),
93 myBackBufferRestored (Standard_False),
95 AspectLine_set (&myDefaultAspectLine),
96 AspectLine_applied (NULL),
97 AspectFace_set (&myDefaultAspectFace),
98 AspectFace_applied (NULL),
99 AspectMarker_set (&myDefaultAspectMarker),
100 AspectMarker_applied (NULL),
101 AspectText_set (&myDefaultAspectText),
102 AspectText_applied (NULL),
103 TextParam_set (&myDefaultTextParam),
104 TextParam_applied (NULL),
105 ViewMatrix_applied (&myDefaultMatrix),
106 StructureMatrix_applied (&myDefaultMatrix),
107 PolygonOffset_applied (NULL)
109 theDisplay->InitAttributes();
111 // General initialization of the context
113 // Eviter d'avoir les faces mal orientees en noir.
114 // Pourrait etre utiliser pour detecter les problemes d'orientation
115 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
117 // Optimisation pour le Fog et l'antialiasing
118 glHint (GL_FOG_HINT, GL_FASTEST);
119 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
120 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
121 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
124 EnablePolygonOffset();
128 myComputeInitStatus = OpenGl_CLIS_NONE;
130 myViewModificationStatus = 0;
131 myLayersModificationStatus = 0;
133 myRaytraceOutputTexture[0] = 0;
134 myRaytraceOutputTexture[1] = 0;
136 myIsRaytraceDataValid = Standard_False;
137 myToUpdateRaytraceData = Standard_False;
142 // =======================================================================
143 // function : SetImmediateModeDrawToFront
145 // =======================================================================
146 Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
148 const Standard_Boolean aPrevMode = myTransientDrawToFront;
149 myTransientDrawToFront = theDrawToFrontBuffer;
153 // =======================================================================
154 // function : ~OpenGl_Workspace
156 // =======================================================================
157 OpenGl_Workspace::~OpenGl_Workspace()
164 // =======================================================================
165 // function : Activate
167 // =======================================================================
168 Standard_Boolean OpenGl_Workspace::Activate()
170 if (!OpenGl_Window::Activate())
171 return Standard_False;
173 ViewMatrix_applied = &myDefaultMatrix;
174 StructureMatrix_applied = &myDefaultMatrix;
176 ResetAppliedAspect();
178 return Standard_True;
181 // =======================================================================
182 // function : UseTransparency
183 // purpose : call_togl_transparency
184 // =======================================================================
185 void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
187 myUseTransparency = theFlag;
190 //=======================================================================
191 //function : ResetAppliedAspect
192 //purpose : Sets default values of GL parameters in accordance with default aspects
193 //=======================================================================
194 void OpenGl_Workspace::ResetAppliedAspect()
196 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
197 HighlightColor = &myDefaultHighlightColor;
198 AspectLine_set = &myDefaultAspectLine;
199 AspectLine_applied = NULL;
200 AspectFace_set = &myDefaultAspectFace;
201 AspectFace_applied = NULL;
202 AspectMarker_set = &myDefaultAspectMarker;
203 AspectMarker_applied = NULL;
204 AspectText_set = &myDefaultAspectText;
205 AspectText_applied = NULL;
206 TextParam_set = &myDefaultTextParam;
207 TextParam_applied = NULL;
208 PolygonOffset_applied = NULL;
210 AspectLine(Standard_True);
211 AspectFace(Standard_True);
212 AspectMarker(Standard_True);
213 AspectText(Standard_True);
216 // =======================================================================
217 // function : DisableTexture
219 // =======================================================================
220 Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
222 if (myTextureBound.IsNull())
224 return myTextureBound;
227 // reset texture matrix because some code may expect it is identity
228 GLint aMatrixMode = GL_TEXTURE;
229 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
230 glMatrixMode (GL_TEXTURE);
232 glMatrixMode (aMatrixMode);
234 myTextureBound->Unbind (myGlContext);
235 switch (myTextureBound->GetTarget())
239 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
241 glDisable (GL_TEXTURE_GEN_S);
243 glDisable (GL_TEXTURE_1D);
248 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
250 glDisable (GL_TEXTURE_GEN_S);
251 glDisable (GL_TEXTURE_GEN_T);
252 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
254 glDisable (GL_POINT_SPRITE);
257 glDisable (GL_TEXTURE_2D);
263 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
264 myTextureBound.Nullify();
268 // =======================================================================
269 // function : setTextureParams
271 // =======================================================================
272 void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
273 const Handle(Graphic3d_TextureParams)& theParams)
275 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
276 if (aParams.IsNull())
281 GLint aMatrixMode = GL_TEXTURE;
282 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
284 // setup texture matrix
285 glMatrixMode (GL_TEXTURE);
287 const Graphic3d_Vec2& aScale = aParams->Scale();
288 const Graphic3d_Vec2& aTrans = aParams->Translation();
289 glScalef ( aScale.x(), aScale.y(), 1.0f);
290 glTranslatef (-aTrans.x(), -aTrans.y(), 0.0f);
291 glRotatef (-aParams->Rotation(), 0.0f, 0.0f, 1.0f);
293 // setup generation of texture coordinates
294 switch (aParams->GenMode())
296 case Graphic3d_TOTM_OBJECT:
298 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
299 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
300 if (theTexture->GetTarget() != GL_TEXTURE_1D)
302 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
303 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
307 case Graphic3d_TOTM_SPHERE:
309 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
310 if (theTexture->GetTarget() != GL_TEXTURE_1D)
312 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
316 case Graphic3d_TOTM_EYE:
318 glMatrixMode (GL_MODELVIEW);
322 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
323 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
325 if (theTexture->GetTarget() != GL_TEXTURE_1D)
327 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
328 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
333 case Graphic3d_TOTM_SPRITE:
335 if (GetGlContext()->core20 != NULL)
337 glEnable (GL_POINT_SPRITE);
338 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
339 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
340 GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
344 case Graphic3d_TOTM_MANUAL:
349 if (aParams->GenMode() != Graphic3d_TOTM_SPRITE)
351 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
354 // setup texture filtering and wrapping
355 //if (theTexture->GetParams() != theParams)
356 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
357 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : GL_CLAMP;
358 switch (theTexture->GetTarget())
362 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
363 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
364 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
369 GLenum aFilterMin = aFilter;
370 if (theTexture->HasMipmaps())
372 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
373 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
375 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
377 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
379 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
382 if (myGlContext->extAnis)
384 // setup degree of anisotropy filter
385 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
386 switch (aParams->AnisoFilter())
388 case Graphic3d_LOTA_QUALITY:
390 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aMaxDegree);
393 case Graphic3d_LOTA_MIDDLE:
396 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2));
399 case Graphic3d_LOTA_FAST:
401 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2);
404 case Graphic3d_LOTA_OFF:
407 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
413 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
414 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
415 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
416 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
422 switch (theTexture->GetTarget())
426 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
428 glEnable (GL_TEXTURE_GEN_S);
430 glEnable (GL_TEXTURE_1D);
435 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
437 glEnable (GL_TEXTURE_GEN_S);
438 glEnable (GL_TEXTURE_GEN_T);
440 glEnable (GL_TEXTURE_2D);
446 glMatrixMode (aMatrixMode); // turn back active matrix
447 theTexture->SetParams (aParams);
450 // =======================================================================
451 // function : EnableTexture
453 // =======================================================================
454 Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
455 const Handle(Graphic3d_TextureParams)& theParams)
457 if (theTexture.IsNull() || !theTexture->IsValid())
459 return DisableTexture();
462 if (myTextureBound == theTexture
463 && (theParams.IsNull() || theParams == theTexture->GetParams()))
466 return myTextureBound;
469 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
470 myTextureBound = theTexture;
471 myTextureBound->Bind (myGlContext);
472 setTextureParams (myTextureBound, theParams);
477 // =======================================================================
480 // =======================================================================
481 void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
482 const Aspect_CLayer2d& theCUnderLayer,
483 const Aspect_CLayer2d& theCOverLayer)
490 // release pending GL resources
491 Handle(OpenGl_Context) aGlCtx = GetGlContext();
492 aGlCtx->ReleaseDelayed();
494 // cache render mode state
495 GLint aRendMode = GL_RENDER;
496 glGetIntegerv (GL_RENDER_MODE, &aRendMode);
497 aGlCtx->SetFeedback (aRendMode == GL_FEEDBACK);
499 Tint toSwap = (aRendMode == GL_RENDER); // swap buffers
500 GLint aViewPortBack[4];
501 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
502 if (aFrameBuffer != NULL)
504 glGetIntegerv (GL_VIEWPORT, aViewPortBack);
505 aFrameBuffer->SetupViewport (aGlCtx);
506 aFrameBuffer->BindBuffer (aGlCtx);
507 toSwap = 0; // no need to swap buffers
511 if (!theCView.IsRaytracing || myComputeInitStatus == OpenGl_CLIS_FAIL)
514 Redraw1 (theCView, theCUnderLayer, theCOverLayer, toSwap);
515 if (aFrameBuffer == NULL || !myTransientDrawToFront)
517 RedrawImmediatMode();
520 theCView.WasRedrawnGL = Standard_True;
525 int aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
526 int aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
528 Raytrace (theCView, aSizeX, aSizeY, toSwap);
530 theCView.WasRedrawnGL = Standard_False;
534 if (aFrameBuffer != NULL)
536 aFrameBuffer->UnbindBuffer (aGlCtx);
537 // move back original viewport
538 glViewport (aViewPortBack[0], aViewPortBack[1], aViewPortBack[2], aViewPortBack[3]);
541 #if (defined(_WIN32) || defined(__WIN32__)) && defined(HAVE_VIDEOCAPTURE)
542 if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
545 glGetIntegerv (GL_VIEWPORT, params);
546 int nWidth = params[2] & ~0x7;
547 int nHeight = params[3] & ~0x7;
549 const int nBitsPerPixel = 24;
550 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
552 glPixelStorei (GL_PACK_ALIGNMENT, 1);
553 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
554 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
559 // reset render mode state
560 aGlCtx->SetFeedback (Standard_False);