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_GlCore11.hxx>
22 #include <OpenGl_View.hxx>
23 #include <OpenGl_Context.hxx>
24 #include <OpenGl_Workspace.hxx>
25 #include <OpenGl_Display.hxx>
27 #include <OpenGl_Texture.hxx>
28 #include <OpenGl_Trihedron.hxx>
29 #include <OpenGl_GraduatedTrihedron.hxx>
31 #include <OpenGl_transform_persistence.hxx>
33 #include <Graphic3d_TextureEnv.hxx>
35 #include <GL/glu.h> // gluUnProject()
37 IMPLEMENT_STANDARD_HANDLE(OpenGl_View,MMgt_TShared)
38 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,MMgt_TShared)
40 /*----------------------------------------------------------------------*/
42 static const OPENGL_BG_TEXTURE myDefaultBgTexture = { 0, 0, 0, Aspect_FM_CENTERED };
43 static const OPENGL_BG_GRADIENT myDefaultBgGradient = { {{ 0.F, 0.F, 0.F, 1.F }}, {{ 0.F, 0.F, 0.F, 1.F }}, Aspect_GFM_NONE };
44 static const Tmatrix3 myDefaultMatrix = { { 1.F, 0.F, 0.F, 0.F }, { 0.F, 1.F, 0.F, 0.F }, { 0.F, 0.F, 1.F, 0.F }, { 0.F, 0.F, 0.F, 1.F } };
45 static const OPENGL_ZCLIP myDefaultZClip = { { Standard_True, 0.F }, { Standard_True, 1.F } };
46 static const OPENGL_EXTRA_REP myDefaultExtra =
57 { 0.F, 0.F, 1.F, 1.F },
59 { 0.F, 0.F, 0.F, 1.F, 1.F, 1.F },
75 static const OPENGL_FOG myDefaultFog = { Standard_False, 0.F, 1.F, { { 0.F, 0.F, 0.F, 1.F } } };
76 static const TEL_TRANSFORM_PERSISTENCE myDefaultTransPers = { 0, 0.F, 0.F, 0.F };
77 static const GLdouble THE_IDENTITY_MATRIX[4][4] =
85 /*----------------------------------------------------------------------*/
87 OpenGl_View::OpenGl_View (const CALL_DEF_VIEWCONTEXT &AContext)
88 : mySurfaceDetail(Visual3d_TOD_NONE),
90 myBgTexture(myDefaultBgTexture),
91 myBgGradient(myDefaultBgGradient),
92 //myOrientationMatrix(myDefaultMatrix),
93 //myMappingMatrix(myDefaultMatrix),
94 //shield_indicator = TOn,
95 //shield_colour = { { 0.F, 0.F, 0.F, 1.F } },
96 //border_indicator = TOff,
97 //border_colour = { { 0.F, 0.F, 0.F, 1.F } },
98 //active_status = TOn,
99 myZClip(myDefaultZClip),
100 myExtra(myDefaultExtra),
102 myVisualization(AContext.Visualization),
103 myIntShadingMethod(TEL_SM_GOURAUD),
104 myAntiAliasing(Standard_False),
105 myAnimationListIndex(0),
106 myAnimationListReady(Standard_False),
107 myTransPers(&myDefaultTransPers),
108 myIsTransPers(Standard_False),
109 myResetFLIST(Standard_False)
111 // Initialize matrices
112 memcpy(myOrientationMatrix,myDefaultMatrix,sizeof(Tmatrix3));
113 memcpy(myMappingMatrix,myDefaultMatrix,sizeof(Tmatrix3));
116 switch (AContext.Model)
118 case 1 : /* VISUAL3D_TOM_INTERP_COLOR */
119 case 3 : /* VISUAL3D_TOM_VERTEX */
120 myIntShadingMethod = TEL_SM_GOURAUD;
123 myIntShadingMethod = TEL_SM_FLAT;
128 /*----------------------------------------------------------------------*/
130 OpenGl_View::~OpenGl_View ()
132 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
135 void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
137 if (!myTextureEnv.IsNull())
139 theCtx->DelayedRelease (myTextureEnv);
140 myTextureEnv.Nullify();
142 if (myBgTexture.TexId != 0)
144 glDeleteTextures (1, (GLuint*)&(myBgTexture.TexId));
145 myBgTexture.TexId = 0;
147 if (myAnimationListIndex)
149 glDeleteLists ((GLuint )myAnimationListIndex, 1);
150 myAnimationListIndex = 0;
154 void OpenGl_View::SetTextureEnv (const Handle(OpenGl_Context)& theCtx,
155 const Handle(Graphic3d_TextureEnv)& theTexture)
157 if (!myTextureEnv.IsNull())
159 theCtx->DelayedRelease (myTextureEnv);
160 myTextureEnv.Nullify();
163 if (theTexture.IsNull())
168 myTextureEnv = new OpenGl_Texture (theTexture->GetParams());
169 Handle(Image_PixMap) anImage = theTexture->GetImage();
170 myTextureEnv->Init (theCtx, *anImage.operator->(), theTexture->Type());
173 /*----------------------------------------------------------------------*/
175 void OpenGl_View::SetBackfacing (const Standard_Integer AMode)
177 myBackfacing = AMode;
178 myResetFLIST = Standard_True;
181 /*----------------------------------------------------------------------*/
184 void OpenGl_View::SetLights (const CALL_DEF_VIEWCONTEXT &AContext)
188 const int nb_lights = AContext.NbActiveLight;
191 const CALL_DEF_LIGHT *alight = &(AContext.ActiveLight[0]);
192 for ( ; i < nb_lights; i++, alight++ )
196 switch( alight->LightType )
198 case 0 : /* TOLS_AMBIENT */
199 rep.type = TLightAmbient;
200 rep.col.rgb[0] = alight->Color.r;
201 rep.col.rgb[1] = alight->Color.g;
202 rep.col.rgb[2] = alight->Color.b;
205 case 1 : /* TOLS_DIRECTIONAL */
206 rep.type = TLightDirectional;
207 rep.col.rgb[0] = alight->Color.r;
208 rep.col.rgb[1] = alight->Color.g;
209 rep.col.rgb[2] = alight->Color.b;
210 rep.dir[0] = alight->Direction.x;
211 rep.dir[1] = alight->Direction.y;
212 rep.dir[2] = alight->Direction.z;
215 case 2 : /* TOLS_POSITIONAL */
216 rep.type = TLightPositional;
217 rep.col.rgb[0] = alight->Color.r;
218 rep.col.rgb[1] = alight->Color.g;
219 rep.col.rgb[2] = alight->Color.b;
220 rep.pos[0] = alight->Position.x;
221 rep.pos[1] = alight->Position.y;
222 rep.pos[2] = alight->Position.z;
223 rep.atten[0] = alight->Attenuation[0];
224 rep.atten[1] = alight->Attenuation[1];
227 case 3 : /* TOLS_SPOT */
228 rep.type = TLightSpot;
229 rep.col.rgb[0] = alight->Color.r;
230 rep.col.rgb[1] = alight->Color.g;
231 rep.col.rgb[2] = alight->Color.b;
232 rep.pos[0] = alight->Position.x;
233 rep.pos[1] = alight->Position.y;
234 rep.pos[2] = alight->Position.z;
235 rep.dir[0] = alight->Direction.x;
236 rep.dir[1] = alight->Direction.y;
237 rep.dir[2] = alight->Direction.z;
238 rep.shine = alight->Concentration;
239 rep.atten[0] = alight->Attenuation[0];
240 rep.atten[1] = alight->Attenuation[1];
241 rep.angle = alight->Angle;
245 rep.HeadLight = alight->Headlight;
247 myLights.Append(rep);
251 /*----------------------------------------------------------------------*/
254 void OpenGl_View::SetClippingPlanes (const CALL_DEF_VIEWCONTEXT &AContext)
256 // clear clipping planes information
257 myClippingPlanes.Clear();
258 // update information
260 for (; i < AContext.NbActivePlane; i++)
262 const CALL_DEF_PLANE &aCPlane = AContext.ActivePlane[i];
263 if ( aCPlane.Active && aCPlane.PlaneId > 0 )
265 OPENGL_CLIP_REP aPlane;
266 aPlane.equation[0] = aCPlane.CoefA;
267 aPlane.equation[1] = aCPlane.CoefB;
268 aPlane.equation[2] = aCPlane.CoefC;
269 aPlane.equation[3] = aCPlane.CoefD;
270 myClippingPlanes.Append( aPlane );
275 /*----------------------------------------------------------------------*/
277 //call_togl_setvisualisation
278 void OpenGl_View::SetVisualisation (const CALL_DEF_VIEWCONTEXT &AContext)
280 myVisualization = AContext.Visualization;
282 switch (AContext.Model)
284 case 1 : /* VISUAL3D_TOM_INTERP_COLOR */
285 case 3 : /* VISUAL3D_TOM_VERTEX */
286 myIntShadingMethod = TEL_SM_GOURAUD;
289 myIntShadingMethod = TEL_SM_FLAT;
294 /*----------------------------------------------------------------------*/
296 //call_togl_cliplimit
297 void OpenGl_View::SetClipLimit (const Graphic3d_CView& theCView)
300 (theCView.Context.ZClipBackPlane - theCView.Mapping.BackPlaneDistance) /
301 (theCView.Mapping.FrontPlaneDistance - theCView.Mapping.BackPlaneDistance);
302 myZClip.Front.Limit =
303 (theCView.Context.ZClipFrontPlane - theCView.Mapping.BackPlaneDistance) /
304 (theCView.Mapping.FrontPlaneDistance - theCView.Mapping.BackPlaneDistance);
305 if (myZClip.Back.Limit < 0.0f)
306 myZClip.Back.Limit = 0.0f;
307 if (myZClip.Front.Limit > 1.0f)
308 myZClip.Front.Limit = 1.0f;
309 if (myZClip.Back.Limit > myZClip.Front.Limit)
311 myZClip.Back.Limit = 0.0f;
312 myZClip.Front.Limit = 1.0f;
315 myZClip.Back.IsOn = (theCView.Context.BackZClipping != 0);
316 myZClip.Front.IsOn = (theCView.Context.FrontZClipping != 0);
319 /*----------------------------------------------------------------------*/
321 //call_togl_viewmapping
322 void OpenGl_View::SetMapping (const Graphic3d_CView& theCView)
324 const float ratio = theCView.DefWindow.dy / theCView.DefWindow.dx;
325 const float r_ratio = theCView.DefWindow.dx / theCView.DefWindow.dy;
327 TEL_VIEW_MAPPING Map;
329 Map.window.xmin = theCView.Mapping.WindowLimit.um;
330 Map.window.ymin = theCView.Mapping.WindowLimit.vm;
331 Map.window.xmax = theCView.Mapping.WindowLimit.uM;
332 Map.window.ymax = theCView.Mapping.WindowLimit.vM;
334 Map.viewport.xmin = 0.F;
335 Map.viewport.xmax = ( 1.F < r_ratio ? 1.F : r_ratio );
336 Map.viewport.ymin = 0.F;
337 Map.viewport.ymax = ( 1.F < ratio ? 1.F : ratio );
338 Map.viewport.zmin = 0.F;
339 Map.viewport.zmax = 1.F;
342 switch (theCView.Mapping.Projection)
345 Map.proj = TelPerspective;
348 Map.proj = TelParallel;
352 // projection reference point
353 Map.prp[0] = theCView.Mapping.ProjectionReferencePoint.x;
354 Map.prp[1] = theCView.Mapping.ProjectionReferencePoint.y;
355 Map.prp[2] = theCView.Mapping.ProjectionReferencePoint.z;
356 if (!openglDisplay.IsNull() && !openglDisplay->Walkthrough())
357 Map.prp[2] += theCView.Mapping.FrontPlaneDistance;
359 // view plane distance
360 Map.vpd = theCView.Mapping.ViewPlaneDistance;
362 // back plane distance
363 Map.bpd = theCView.Mapping.BackPlaneDistance;
365 // front plane distance
366 Map.fpd = theCView.Mapping.FrontPlaneDistance;
370 // use user-defined matrix
371 if (theCView.Mapping.IsCustomMatrix)
374 for( i = 0; i < 4; i++ )
375 for( j = 0; j < 4; j++ )
376 myMappingMatrix[i][j] = theCView.Mapping.ProjectionMatrix[i][j];
379 TelEvalViewMappingMatrix( &Map, &err_ind, myMappingMatrix );
385 /*----------------------------------------------------------------------*/
387 //call_togl_vieworientation
388 void OpenGl_View::SetOrientation (const Graphic3d_CView& theCView)
393 Tfloat ScaleFactors[3];
395 Vrp[0] = theCView.Orientation.ViewReferencePoint.x;
396 Vrp[1] = theCView.Orientation.ViewReferencePoint.y;
397 Vrp[2] = theCView.Orientation.ViewReferencePoint.z;
399 Vpn[0] = theCView.Orientation.ViewReferencePlane.x;
400 Vpn[1] = theCView.Orientation.ViewReferencePlane.y;
401 Vpn[2] = theCView.Orientation.ViewReferencePlane.z;
403 Vup[0] = theCView.Orientation.ViewReferenceUp.x;
404 Vup[1] = theCView.Orientation.ViewReferenceUp.y;
405 Vup[2] = theCView.Orientation.ViewReferenceUp.z;
407 ScaleFactors[0] = theCView.Orientation.ViewScaleX;
408 ScaleFactors[1] = theCView.Orientation.ViewScaleY;
409 ScaleFactors[2] = theCView.Orientation.ViewScaleZ;
413 // use user-defined matrix
414 if (theCView.Orientation.IsCustomMatrix)
417 for( i = 0; i < 4; i++ )
418 for( j = 0; j < 4; j++ )
419 myOrientationMatrix[i][j] = theCView.Orientation.ModelViewMatrix[i][j];
423 TelEvalViewOrientationMatrix (Vrp, Vpn, Vup, ScaleFactors, &err_ind, myOrientationMatrix);
428 myExtra.vrp[0] = Vrp[0];
429 myExtra.vrp[1] = Vrp[1];
430 myExtra.vrp[2] = Vrp[2];
432 myExtra.vpn[0] = Vpn[0];
433 myExtra.vpn[1] = Vpn[1];
434 myExtra.vpn[2] = Vpn[2];
436 myExtra.vup[0] = Vup[0];
437 myExtra.vup[1] = Vup[1];
438 myExtra.vup[2] = Vup[2];
440 myExtra.scaleFactors[0] = ScaleFactors[0],
441 myExtra.scaleFactors[1] = ScaleFactors[1],
442 myExtra.scaleFactors[2] = ScaleFactors[2];
446 /*----------------------------------------------------------------------*/
448 void OpenGl_View::SetFog (const Graphic3d_CView& theCView,
449 const Standard_Boolean theFlag)
453 myFog.IsOn = Standard_False;
457 myFog.IsOn = Standard_True;
460 (theCView.Context.DepthFrontPlane - theCView.Mapping.BackPlaneDistance) /
461 (theCView.Mapping.FrontPlaneDistance - theCView.Mapping.BackPlaneDistance);
464 (theCView.Context.DepthBackPlane - theCView.Mapping.BackPlaneDistance) /
465 (theCView.Mapping.FrontPlaneDistance - theCView.Mapping.BackPlaneDistance);
467 if (myFog.Front < 0.F)
469 else if (myFog.Front > 1.F)
472 if (myFog.Back < 0.F)
474 else if (myFog.Back > 1.F)
477 if (myFog.Back > myFog.Front)
483 myFog.Color.rgb[0] = theCView.DefWindow.Background.r;
484 myFog.Color.rgb[1] = theCView.DefWindow.Background.g;
485 myFog.Color.rgb[2] = theCView.DefWindow.Background.b;
486 myFog.Color.rgb[3] = 1.0f;
490 /*----------------------------------------------------------------------*/
492 void OpenGl_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition APosition, const Quantity_NameOfColor AColor,
493 const Standard_Real AScale, const Standard_Boolean AsWireframe)
495 myTrihedron = new OpenGl_Trihedron (APosition, AColor, AScale, AsWireframe);
498 /*----------------------------------------------------------------------*/
500 void OpenGl_View::TriedronErase ()
502 myTrihedron.Nullify();
505 /*----------------------------------------------------------------------*/
507 void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_CGraduatedTrihedron &data)
509 myGraduatedTrihedron = new OpenGl_GraduatedTrihedron(data);
512 /*----------------------------------------------------------------------*/
514 void OpenGl_View::GraduatedTrihedronErase ()
516 myGraduatedTrihedron.Nullify();
519 /*----------------------------------------------------------------------*/
521 //transform_persistence_end
522 void OpenGl_View::EndTransformPersistence()
527 glMatrixMode (GL_PROJECTION);
529 glMatrixMode (GL_MODELVIEW);
531 myIsTransPers = Standard_False;
535 /*----------------------------------------------------------------------*/
537 //transform_persistence_begin
538 const TEL_TRANSFORM_PERSISTENCE* OpenGl_View::BeginTransformPersistence (const TEL_TRANSFORM_PERSISTENCE* theTransPers)
540 const TEL_TRANSFORM_PERSISTENCE* aTransPersPrev = myTransPers;
541 myTransPers = theTransPers;
542 if (theTransPers->mode == 0)
544 EndTransformPersistence();
545 return aTransPersPrev;
549 GLdouble aModelMatrix[4][4];
550 GLdouble aProjMatrix[4][4];
551 glGetIntegerv (GL_VIEWPORT, aViewport);
552 glGetDoublev (GL_MODELVIEW_MATRIX, (GLdouble* )aModelMatrix);
553 glGetDoublev (GL_PROJECTION_MATRIX, (GLdouble *)aProjMatrix);
554 const GLdouble aViewportW = (GLdouble )aViewport[2];
555 const GLdouble aViewportH = (GLdouble )aViewport[3];
559 // pop matrix stack - it will be overridden later
560 glMatrixMode (GL_PROJECTION);
562 glMatrixMode (GL_MODELVIEW);
567 myIsTransPers = Standard_True;
570 // push matrices into stack and reset them
571 glMatrixMode (GL_MODELVIEW);
575 glMatrixMode (GL_PROJECTION);
579 // get the window's (fixed) coordinates for theTransPers->point before matrixes modifications
580 GLdouble aWinX = 0.0, aWinY = 0.0, aWinZ = 0.0;
581 if ((theTransPers->mode & TPF_PAN) != TPF_PAN)
583 gluProject (theTransPers->pointX, theTransPers->pointY, theTransPers->pointZ,
584 (GLdouble* )aModelMatrix, (GLdouble* )aProjMatrix, aViewport,
585 &aWinX, &aWinY, &aWinZ);
589 if ((theTransPers->mode & TPF_ZOOM)
590 || (theTransPers->mode == TPF_TRIEDRON))
592 // compute fixed-zoom multiplier
593 // actually function works ugly with TelPerspective!
594 const GLdouble aDet2 = 0.002 / (aViewportW > aViewportH ? aProjMatrix[1][1] : aProjMatrix[0][0]);
595 aProjMatrix[0][0] *= aDet2;
596 aProjMatrix[1][1] *= aDet2;
597 aProjMatrix[2][2] *= aDet2;
600 // prevent translation - annulate translate matrix
601 if ((theTransPers->mode & TPF_PAN)
602 || (theTransPers->mode == TPF_TRIEDRON))
604 aModelMatrix[3][0] = 0.0;
605 aModelMatrix[3][1] = 0.0;
606 aModelMatrix[3][2] = 0.0;
607 aProjMatrix [3][0] = 0.0;
608 aProjMatrix [3][1] = 0.0;
609 aProjMatrix [3][2] = 0.0;
612 // prevent scaling-on-axis
613 if (theTransPers->mode & TPF_ZOOM)
615 const double aScaleX = myExtra.scaleFactors[0];
616 const double aScaleY = myExtra.scaleFactors[1];
617 const double aScaleZ = myExtra.scaleFactors[2];
618 for (int i = 0; i < 3; ++i)
620 aModelMatrix[0][i] /= aScaleX;
621 aModelMatrix[1][i] /= aScaleY;
622 aModelMatrix[2][i] /= aScaleZ;
626 // prevent rotating - annulate rotate matrix
627 if (theTransPers->mode & TPF_ROTATE)
629 aModelMatrix[0][0] = 1.0;
630 aModelMatrix[1][1] = 1.0;
631 aModelMatrix[2][2] = 1.0;
633 aModelMatrix[1][0] = 0.0;
634 aModelMatrix[2][0] = 0.0;
635 aModelMatrix[0][1] = 0.0;
636 aModelMatrix[2][1] = 0.0;
637 aModelMatrix[0][2] = 0.0;
638 aModelMatrix[1][2] = 0.0;
641 // load computed matrices
642 glMatrixMode (GL_MODELVIEW);
643 glMultMatrixd ((GLdouble* )aModelMatrix);
645 glMatrixMode (GL_PROJECTION);
646 glMultMatrixd ((GLdouble* )aProjMatrix);
648 if (theTransPers->mode == TPF_TRIEDRON)
650 // move to the window corner
651 if (theTransPers->pointX != 0.0
652 && theTransPers->pointY != 0.0)
654 GLdouble aW1, aH1, aW2, aH2, aDummy;
655 glMatrixMode (GL_PROJECTION);
656 gluUnProject ( 0.5 * aViewportW, 0.5 * aViewportH, 0.0,
657 (GLdouble* )THE_IDENTITY_MATRIX, (GLdouble* )aProjMatrix, aViewport,
658 &aW1, &aH1, &aDummy);
659 gluUnProject (-0.5 * aViewportW, -0.5 * aViewportH, 0.0,
660 (GLdouble* )THE_IDENTITY_MATRIX, (GLdouble* )aProjMatrix, aViewport,
661 &aW2, &aH2, &aDummy);
662 GLdouble aMoveX = 0.5 * (aW1 - aW2 - theTransPers->pointZ);
663 GLdouble aMoveY = 0.5 * (aH1 - aH2 - theTransPers->pointZ);
664 aMoveX = (theTransPers->pointX > 0.0) ? aMoveX : -aMoveX;
665 aMoveY = (theTransPers->pointY > 0.0) ? aMoveY : -aMoveY;
666 glTranslated (aMoveX, aMoveY, 0.0);
669 else if ((theTransPers->mode & TPF_PAN) != TPF_PAN)
671 // move to thePoint using saved win-coordinates ('marker-behaviour')
672 GLdouble aMoveX, aMoveY, aMoveZ;
673 glGetDoublev (GL_MODELVIEW_MATRIX, (GLdouble* )aModelMatrix);
674 glGetDoublev (GL_PROJECTION_MATRIX, (GLdouble* )aProjMatrix);
675 gluUnProject (aWinX, aWinY, aWinZ,
676 (GLdouble* )aModelMatrix, (GLdouble* )aProjMatrix, aViewport,
677 &aMoveX, &aMoveY, &aMoveZ);
679 glMatrixMode (GL_MODELVIEW);
680 glTranslated (aMoveX, aMoveY, aMoveZ);
683 return aTransPersPrev;