1 // Created on: 2013-11-11
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_Flipper.hxx>
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_ShaderManager.hxx>
20 #include <OpenGl_Vec.hxx>
21 #include <OpenGl_Workspace.hxx>
25 // =======================================================================
26 // function : Constructor
28 // =======================================================================
29 OpenGl_Flipper::OpenGl_Flipper (const gp_Ax2& theReferenceSystem)
31 myReferenceOrigin ((Standard_ShortReal )theReferenceSystem.Location().X(),
32 (Standard_ShortReal )theReferenceSystem.Location().Y(),
33 (Standard_ShortReal )theReferenceSystem.Location().Z(),
35 myReferenceX ((Standard_ShortReal )theReferenceSystem.XDirection().X(),
36 (Standard_ShortReal )theReferenceSystem.XDirection().Y(),
37 (Standard_ShortReal )theReferenceSystem.XDirection().Z(),
39 myReferenceY ((Standard_ShortReal )theReferenceSystem.YDirection().X(),
40 (Standard_ShortReal )theReferenceSystem.YDirection().Y(),
41 (Standard_ShortReal )theReferenceSystem.YDirection().Z(),
43 myReferenceZ ((Standard_ShortReal )theReferenceSystem.Axis().Direction().X(),
44 (Standard_ShortReal )theReferenceSystem.Axis().Direction().Y(),
45 (Standard_ShortReal )theReferenceSystem.Axis().Direction().Z(),
47 myIsEnabled (Standard_True)
52 // =======================================================================
55 // =======================================================================
56 void OpenGl_Flipper::Release (OpenGl_Context*)
61 // =======================================================================
64 // =======================================================================
65 void OpenGl_Flipper::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
67 // Check if rendering is to be in immediate mode
68 const Standard_Boolean isImmediate = (theWorkspace->NamedStatus & OPENGL_NS_IMMEDIATE) != 0;
69 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
71 #if !defined(GL_ES_VERSION_2_0)
72 GLint aCurrMode = GL_MODELVIEW;
73 glGetIntegerv (GL_MATRIX_MODE, &aCurrMode);
77 // Restore transformation
80 if (aCurrMode != GL_MODELVIEW)
82 glMatrixMode (GL_MODELVIEW);
87 if (aCurrMode != GL_MODELVIEW)
89 glMatrixMode (aCurrMode);
92 Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
93 { 0.f, 1.f, 0.f, 0.f },
94 { 0.f, 0.f, 1.f, 0.f },
95 { 0.f, 0.f, 0.f, 1.f } };
97 aContext->ShaderManager()->RevertModelWorldStateTo (&aModelWorldState);
101 // Update current model-view matrix in the top of the stack
102 // replacing it with StructureMatrixT*ViewMatrix from the workspace.
103 theWorkspace->UpdateModelViewMatrix();
111 if (!aContext->ShaderManager()->IsEmpty())
114 glGetFloatv (GL_MODELVIEW_MATRIX, *aWorldView);
116 Tmatrix3 aProjection;
117 glGetFloatv (GL_PROJECTION_MATRIX, *aProjection);
119 aContext->ShaderManager()->UpdateWorldViewStateTo (&aWorldView);
120 aContext->ShaderManager()->UpdateProjectionStateTo (&aProjection);
123 if (aCurrMode != GL_MODELVIEW)
125 glMatrixMode (GL_MODELVIEW);
130 if (aCurrMode != GL_MODELVIEW)
132 glMatrixMode (aCurrMode);
136 OpenGl_Mat4 aMatrixMV;
137 glGetFloatv (GL_MODELVIEW_MATRIX, aMatrixMV.ChangeData());
139 const OpenGl_Vec4 aMVReferenceOrigin = aMatrixMV * myReferenceOrigin;
140 const OpenGl_Vec4 aMVReferenceX = aMatrixMV * OpenGl_Vec4 (myReferenceX.xyz() + myReferenceOrigin.xyz(), 1.0f);
141 const OpenGl_Vec4 aMVReferenceY = aMatrixMV * OpenGl_Vec4 (myReferenceY.xyz() + myReferenceOrigin.xyz(), 1.0f);
142 const OpenGl_Vec4 aMVReferenceZ = aMatrixMV * OpenGl_Vec4 (myReferenceZ.xyz() + myReferenceOrigin.xyz(), 1.0f);
144 const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin;
145 const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin;
146 const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin;
148 Standard_Boolean isReversedX = aDirX.xyz().Dot (OpenGl_Vec3::DX()) < 0.0f;
149 Standard_Boolean isReversedY = aDirY.xyz().Dot (OpenGl_Vec3::DY()) < 0.0f;
150 Standard_Boolean isReversedZ = aDirZ.xyz().Dot (OpenGl_Vec3::DZ()) < 0.0f;
152 // compute flipping (rotational transform)
153 OpenGl_Mat4 aTransform;
154 if ((isReversedX || isReversedY) && !isReversedZ)
156 // invert by Z axis: left, up vectors mirrored
157 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
158 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
160 else if (isReversedY && isReversedZ)
162 // rotate by X axis: up, forward vectors mirrored
163 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
164 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
166 else if (isReversedZ)
168 // rotate by Y axis: left, forward vectors mirrored
169 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
170 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
177 // do rotation in origin around reference system "forward" direction
178 OpenGl_Mat4 aRefAxes;
180 aRefAxes.SetColumn (0, myReferenceX.xyz());
181 aRefAxes.SetColumn (1, myReferenceY.xyz());
182 aRefAxes.SetColumn (2, myReferenceZ.xyz());
183 aRefAxes.SetColumn (3, myReferenceOrigin.xyz());
184 aRefAxes.Inverted (aRefInv);
186 aTransform = aRefAxes * aTransform * aRefInv;
188 // transform model-view matrix
189 aMatrixMV = aMatrixMV * aTransform;
191 // load transformed model-view matrix
192 if (aCurrMode != GL_MODELVIEW)
194 glMatrixMode (GL_MODELVIEW);
197 glLoadMatrixf ((GLfloat*) aMatrixMV);
199 if (aCurrMode != GL_MODELVIEW)
201 glMatrixMode (aCurrMode);