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 myModelViewMatrix (myDefaultMatrix),
108 PolygonOffset_applied (NULL)
110 theDisplay->InitAttributes();
112 // General initialization of the context
114 // Eviter d'avoir les faces mal orientees en noir.
115 // Pourrait etre utiliser pour detecter les problemes d'orientation
116 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
118 // Optimisation pour le Fog et l'antialiasing
119 glHint (GL_FOG_HINT, GL_FASTEST);
120 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
121 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
122 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
125 EnablePolygonOffset();
129 myComputeInitStatus = OpenGl_CLIS_NONE;
131 myViewModificationStatus = 0;
132 myLayersModificationStatus = 0;
134 myRaytraceOutputTexture[0] = 0;
135 myRaytraceOutputTexture[1] = 0;
137 myIsRaytraceDataValid = Standard_False;
138 myToUpdateRaytraceData = Standard_False;
143 // =======================================================================
144 // function : SetImmediateModeDrawToFront
146 // =======================================================================
147 Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
149 const Standard_Boolean aPrevMode = myTransientDrawToFront;
150 myTransientDrawToFront = theDrawToFrontBuffer;
154 // =======================================================================
155 // function : ~OpenGl_Workspace
157 // =======================================================================
158 OpenGl_Workspace::~OpenGl_Workspace()
165 // =======================================================================
166 // function : Activate
168 // =======================================================================
169 Standard_Boolean OpenGl_Workspace::Activate()
171 if (!OpenGl_Window::Activate())
172 return Standard_False;
174 ViewMatrix_applied = &myDefaultMatrix;
175 StructureMatrix_applied = &myDefaultMatrix;
177 ResetAppliedAspect();
179 return Standard_True;
182 // =======================================================================
183 // function : UseTransparency
184 // purpose : call_togl_transparency
185 // =======================================================================
186 void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
188 myUseTransparency = theFlag;
191 //=======================================================================
192 //function : ResetAppliedAspect
193 //purpose : Sets default values of GL parameters in accordance with default aspects
194 //=======================================================================
195 void OpenGl_Workspace::ResetAppliedAspect()
197 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
198 HighlightColor = &myDefaultHighlightColor;
199 AspectLine_set = &myDefaultAspectLine;
200 AspectLine_applied = NULL;
201 AspectFace_set = &myDefaultAspectFace;
202 AspectFace_applied = NULL;
203 AspectMarker_set = &myDefaultAspectMarker;
204 AspectMarker_applied = NULL;
205 AspectText_set = &myDefaultAspectText;
206 AspectText_applied = NULL;
207 TextParam_set = &myDefaultTextParam;
208 TextParam_applied = NULL;
209 PolygonOffset_applied = NULL;
211 AspectLine(Standard_True);
212 AspectFace(Standard_True);
213 AspectMarker(Standard_True);
214 AspectText(Standard_True);
217 // =======================================================================
218 // function : DisableTexture
220 // =======================================================================
221 Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
223 if (myTextureBound.IsNull())
225 return myTextureBound;
228 // reset texture matrix because some code may expect it is identity
229 GLint aMatrixMode = GL_TEXTURE;
230 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
231 glMatrixMode (GL_TEXTURE);
233 glMatrixMode (aMatrixMode);
235 myTextureBound->Unbind (myGlContext);
236 switch (myTextureBound->GetTarget())
240 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
242 glDisable (GL_TEXTURE_GEN_S);
244 glDisable (GL_TEXTURE_1D);
249 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
251 glDisable (GL_TEXTURE_GEN_S);
252 glDisable (GL_TEXTURE_GEN_T);
253 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
255 glDisable (GL_POINT_SPRITE);
258 glDisable (GL_TEXTURE_2D);
264 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
265 myTextureBound.Nullify();
269 // =======================================================================
270 // function : setTextureParams
272 // =======================================================================
273 void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
274 const Handle(Graphic3d_TextureParams)& theParams)
276 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
277 if (aParams.IsNull())
282 GLint aMatrixMode = GL_TEXTURE;
283 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
285 // setup texture matrix
286 glMatrixMode (GL_TEXTURE);
288 const Graphic3d_Vec2& aScale = aParams->Scale();
289 const Graphic3d_Vec2& aTrans = aParams->Translation();
290 glScalef ( aScale.x(), aScale.y(), 1.0f);
291 glTranslatef (-aTrans.x(), -aTrans.y(), 0.0f);
292 glRotatef (-aParams->Rotation(), 0.0f, 0.0f, 1.0f);
294 // setup generation of texture coordinates
295 switch (aParams->GenMode())
297 case Graphic3d_TOTM_OBJECT:
299 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
300 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
301 if (theTexture->GetTarget() != GL_TEXTURE_1D)
303 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
304 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
308 case Graphic3d_TOTM_SPHERE:
310 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
311 if (theTexture->GetTarget() != GL_TEXTURE_1D)
313 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
317 case Graphic3d_TOTM_EYE:
319 glMatrixMode (GL_MODELVIEW);
323 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
324 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
326 if (theTexture->GetTarget() != GL_TEXTURE_1D)
328 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
329 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
334 case Graphic3d_TOTM_SPRITE:
336 if (GetGlContext()->core20 != NULL)
338 glEnable (GL_POINT_SPRITE);
339 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
340 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
341 GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
345 case Graphic3d_TOTM_MANUAL:
350 if (aParams->GenMode() != Graphic3d_TOTM_SPRITE)
352 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
355 // setup texture filtering and wrapping
356 //if (theTexture->GetParams() != theParams)
357 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
358 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : GL_CLAMP;
359 switch (theTexture->GetTarget())
363 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
364 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
365 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
370 GLenum aFilterMin = aFilter;
371 if (theTexture->HasMipmaps())
373 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
374 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
376 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
378 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
380 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
383 if (myGlContext->extAnis)
385 // setup degree of anisotropy filter
386 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
387 switch (aParams->AnisoFilter())
389 case Graphic3d_LOTA_QUALITY:
391 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aMaxDegree);
394 case Graphic3d_LOTA_MIDDLE:
397 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2));
400 case Graphic3d_LOTA_FAST:
402 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2);
405 case Graphic3d_LOTA_OFF:
408 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
414 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
415 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
416 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
417 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
423 switch (theTexture->GetTarget())
427 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
429 glEnable (GL_TEXTURE_GEN_S);
431 glEnable (GL_TEXTURE_1D);
436 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
438 glEnable (GL_TEXTURE_GEN_S);
439 glEnable (GL_TEXTURE_GEN_T);
441 glEnable (GL_TEXTURE_2D);
447 glMatrixMode (aMatrixMode); // turn back active matrix
448 theTexture->SetParams (aParams);
451 // =======================================================================
452 // function : EnableTexture
454 // =======================================================================
455 Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
456 const Handle(Graphic3d_TextureParams)& theParams)
458 if (theTexture.IsNull() || !theTexture->IsValid())
460 return DisableTexture();
463 if (myTextureBound == theTexture
464 && (theParams.IsNull() || theParams == theTexture->GetParams()))
467 return myTextureBound;
470 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
471 myTextureBound = theTexture;
472 myTextureBound->Bind (myGlContext);
473 setTextureParams (myTextureBound, theParams);
478 // =======================================================================
481 // =======================================================================
482 void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
483 const Aspect_CLayer2d& theCUnderLayer,
484 const Aspect_CLayer2d& theCOverLayer)
491 // release pending GL resources
492 Handle(OpenGl_Context) aGlCtx = GetGlContext();
493 aGlCtx->ReleaseDelayed();
495 // cache render mode state
496 GLint aRendMode = GL_RENDER;
497 glGetIntegerv (GL_RENDER_MODE, &aRendMode);
498 aGlCtx->SetFeedback (aRendMode == GL_FEEDBACK);
500 Tint toSwap = (aRendMode == GL_RENDER); // swap buffers
501 GLint aViewPortBack[4];
502 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
503 if (aFrameBuffer != NULL)
505 glGetIntegerv (GL_VIEWPORT, aViewPortBack);
506 aFrameBuffer->SetupViewport (aGlCtx);
507 aFrameBuffer->BindBuffer (aGlCtx);
508 toSwap = 0; // no need to swap buffers
512 if (!theCView.IsRaytracing || myComputeInitStatus == OpenGl_CLIS_FAIL)
515 Redraw1 (theCView, theCUnderLayer, theCOverLayer, toSwap);
516 if (aFrameBuffer == NULL || !myTransientDrawToFront)
518 RedrawImmediatMode();
521 theCView.WasRedrawnGL = Standard_True;
526 int aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
527 int aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
529 Raytrace (theCView, aSizeX, aSizeY, toSwap);
531 theCView.WasRedrawnGL = Standard_False;
535 if (aFrameBuffer != NULL)
537 aFrameBuffer->UnbindBuffer (aGlCtx);
538 // move back original viewport
539 glViewport (aViewPortBack[0], aViewPortBack[1], aViewPortBack[2], aViewPortBack[3]);
542 #if (defined(_WIN32) || defined(__WIN32__)) && defined(HAVE_VIDEOCAPTURE)
543 if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
546 glGetIntegerv (GL_VIEWPORT, params);
547 int nWidth = params[2] & ~0x7;
548 int nHeight = params[3] & ~0x7;
550 const int nBitsPerPixel = 24;
551 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
553 glPixelStorei (GL_PACK_ALIGNMENT, 1);
554 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
555 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
560 // reset render mode state
561 aGlCtx->SetFeedback (Standard_False);