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();
70 GLint aCurrMode = GL_MODELVIEW;
71 glGetIntegerv (GL_MATRIX_MODE, &aCurrMode);
75 // Restore transformation
78 if (aCurrMode != GL_MODELVIEW)
80 glMatrixMode (GL_MODELVIEW);
85 if (aCurrMode != GL_MODELVIEW)
87 glMatrixMode (aCurrMode);
90 Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
91 { 0.f, 1.f, 0.f, 0.f },
92 { 0.f, 0.f, 1.f, 0.f },
93 { 0.f, 0.f, 0.f, 1.f } };
95 aContext->ShaderManager()->RevertModelWorldStateTo (&aModelWorldState);
99 // Update current model-view matrix in the top of the stack
100 // replacing it with StructureMatrixT*ViewMatrix from the workspace.
101 theWorkspace->UpdateModelViewMatrix();
109 if (!aContext->ShaderManager()->IsEmpty())
112 glGetFloatv (GL_MODELVIEW_MATRIX, *aWorldView);
114 Tmatrix3 aProjection;
115 glGetFloatv (GL_PROJECTION_MATRIX, *aProjection);
117 aContext->ShaderManager()->UpdateWorldViewStateTo (&aWorldView);
118 aContext->ShaderManager()->UpdateProjectionStateTo (&aProjection);
121 if (aCurrMode != GL_MODELVIEW)
123 glMatrixMode (GL_MODELVIEW);
128 if (aCurrMode != GL_MODELVIEW)
130 glMatrixMode (aCurrMode);
134 OpenGl_Mat4 aMatrixMV;
135 glGetFloatv (GL_MODELVIEW_MATRIX, aMatrixMV.ChangeData());
137 const OpenGl_Vec4 aMVReferenceOrigin = aMatrixMV * myReferenceOrigin;
138 const OpenGl_Vec4 aMVReferenceX = aMatrixMV * OpenGl_Vec4 (myReferenceX.xyz() + myReferenceOrigin.xyz(), 1.0f);
139 const OpenGl_Vec4 aMVReferenceY = aMatrixMV * OpenGl_Vec4 (myReferenceY.xyz() + myReferenceOrigin.xyz(), 1.0f);
140 const OpenGl_Vec4 aMVReferenceZ = aMatrixMV * OpenGl_Vec4 (myReferenceZ.xyz() + myReferenceOrigin.xyz(), 1.0f);
142 const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin;
143 const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin;
144 const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin;
146 Standard_Boolean isReversedX = aDirX.xyz().Dot (OpenGl_Vec3::DX()) < 0.0f;
147 Standard_Boolean isReversedY = aDirY.xyz().Dot (OpenGl_Vec3::DY()) < 0.0f;
148 Standard_Boolean isReversedZ = aDirZ.xyz().Dot (OpenGl_Vec3::DZ()) < 0.0f;
150 // compute flipping (rotational transform)
151 OpenGl_Mat4 aTransform;
152 if ((isReversedX || isReversedY) && !isReversedZ)
154 // invert by Z axis: left, up vectors mirrored
155 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
156 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
158 else if (isReversedY && isReversedZ)
160 // rotate by X axis: up, forward vectors mirrored
161 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
162 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
164 else if (isReversedZ)
166 // rotate by Y axis: left, forward vectors mirrored
167 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
168 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
175 // do rotation in origin around reference system "forward" direction
176 OpenGl_Mat4 aRefAxes;
178 aRefAxes.SetColumn (0, myReferenceX.xyz());
179 aRefAxes.SetColumn (1, myReferenceY.xyz());
180 aRefAxes.SetColumn (2, myReferenceZ.xyz());
181 aRefAxes.SetColumn (3, myReferenceOrigin.xyz());
182 aRefAxes.Inverted (aRefInv);
184 aTransform = aRefAxes * aTransform * aRefInv;
186 // transform model-view matrix
187 aMatrixMV = aMatrixMV * aTransform;
189 // load transformed model-view matrix
190 if (aCurrMode != GL_MODELVIEW)
192 glMatrixMode (GL_MODELVIEW);
195 glLoadMatrixf ((GLfloat*) aMatrixMV);
197 if (aCurrMode != GL_MODELVIEW)
199 glMatrixMode (aCurrMode);