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 Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
71 // restore matrix state
72 aContext->WorldViewState.Pop();
74 // Apply since we probably in the middle of something.
75 aContext->ApplyModelViewMatrix();
79 aContext->WorldViewState.Push();
80 OpenGl_Mat4 aMatrixMV = aContext->WorldViewState.Current() * aContext->ModelWorldState.Current();
82 const OpenGl_Vec4 aMVReferenceOrigin = aMatrixMV * myReferenceOrigin;
83 const OpenGl_Vec4 aMVReferenceX = aMatrixMV * OpenGl_Vec4 (myReferenceX.xyz() + myReferenceOrigin.xyz(), 1.0f);
84 const OpenGl_Vec4 aMVReferenceY = aMatrixMV * OpenGl_Vec4 (myReferenceY.xyz() + myReferenceOrigin.xyz(), 1.0f);
85 const OpenGl_Vec4 aMVReferenceZ = aMatrixMV * OpenGl_Vec4 (myReferenceZ.xyz() + myReferenceOrigin.xyz(), 1.0f);
87 const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin;
88 const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin;
89 const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin;
91 Standard_Boolean isReversedX = aDirX.xyz().Dot (OpenGl_Vec3::DX()) < 0.0f;
92 Standard_Boolean isReversedY = aDirY.xyz().Dot (OpenGl_Vec3::DY()) < 0.0f;
93 Standard_Boolean isReversedZ = aDirZ.xyz().Dot (OpenGl_Vec3::DZ()) < 0.0f;
95 // compute flipping (rotational transform)
96 OpenGl_Mat4 aTransform;
97 if ((isReversedX || isReversedY) && !isReversedZ)
99 // invert by Z axis: left, up vectors mirrored
100 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
101 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
103 else if (isReversedY && isReversedZ)
105 // rotate by X axis: up, forward vectors mirrored
106 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
107 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
109 else if (isReversedZ)
111 // rotate by Y axis: left, forward vectors mirrored
112 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
113 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
120 // do rotation in origin around reference system "forward" direction
121 OpenGl_Mat4 aRefAxes;
123 aRefAxes.SetColumn (0, myReferenceX.xyz());
124 aRefAxes.SetColumn (1, myReferenceY.xyz());
125 aRefAxes.SetColumn (2, myReferenceZ.xyz());
126 aRefAxes.SetColumn (3, myReferenceOrigin.xyz());
127 aRefAxes.Inverted (aRefInv);
129 aTransform = aRefAxes * aTransform * aRefInv;
131 // transform model-view matrix
132 aMatrixMV = aMatrixMV * aTransform;
134 // load transformed model-view matrix
135 aContext->WorldViewState.SetCurrent (aMatrixMV);
136 aContext->ApplyWorldViewMatrix();