0032308: Configuration - make Xlib dependency optional
[occt.git] / src / OpenGl / OpenGl_Flipper.cxx
1 // Created on: 2013-11-11
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_Flipper.hxx>
17
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_ShaderManager.hxx>
20 #include <OpenGl_Vec.hxx>
21 #include <OpenGl_Workspace.hxx>
22
23 #include <gp_Ax2.hxx>
24
25 // =======================================================================
26 // function : Constructor
27 // purpose  :
28 // =======================================================================
29 OpenGl_Flipper::OpenGl_Flipper (const gp_Ax2& theReferenceSystem)
30 : OpenGl_Element(),
31   myReferenceOrigin ((Standard_ShortReal )theReferenceSystem.Location().X(),
32                      (Standard_ShortReal )theReferenceSystem.Location().Y(),
33                      (Standard_ShortReal )theReferenceSystem.Location().Z(),
34                      1.0f),
35   myReferenceX ((Standard_ShortReal )theReferenceSystem.XDirection().X(),
36                 (Standard_ShortReal )theReferenceSystem.XDirection().Y(),
37                 (Standard_ShortReal )theReferenceSystem.XDirection().Z(),
38                 1.0f),
39   myReferenceY ((Standard_ShortReal )theReferenceSystem.YDirection().X(),
40                 (Standard_ShortReal )theReferenceSystem.YDirection().Y(),
41                 (Standard_ShortReal )theReferenceSystem.YDirection().Z(),
42                 1.0f),
43   myReferenceZ ((Standard_ShortReal )theReferenceSystem.Axis().Direction().X(),
44                 (Standard_ShortReal )theReferenceSystem.Axis().Direction().Y(),
45                 (Standard_ShortReal )theReferenceSystem.Axis().Direction().Z(),
46                 1.0f),
47   myIsEnabled (Standard_True)
48 {
49   //
50 }
51
52 // =======================================================================
53 // function : Release
54 // purpose  :
55 // =======================================================================
56 void OpenGl_Flipper::Release (OpenGl_Context*)
57 {
58   //
59 }
60
61 // =======================================================================
62 // function : Render
63 // purpose  :
64 // =======================================================================
65 void OpenGl_Flipper::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
66 {
67   // Check if rendering is to be in immediate mode
68   const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
69   if (!myIsEnabled)
70   {
71     // restore matrix state
72     aContext->ModelWorldState.Pop();
73
74     // Apply since we probably in the middle of something.
75     aContext->ApplyModelWorldMatrix();
76     return;
77   }
78
79   aContext->ModelWorldState.Push();
80
81   OpenGl_Mat4 aModelWorldMatrix;
82   aModelWorldMatrix.Convert (aContext->ModelWorldState.Current());
83
84   OpenGl_Mat4 aMatrixMV = aContext->WorldViewState.Current() * aModelWorldMatrix;
85
86   const OpenGl_Vec4 aMVReferenceOrigin = aMatrixMV * myReferenceOrigin;
87   const OpenGl_Vec4 aMVReferenceX      = aMatrixMV * OpenGl_Vec4 (myReferenceX.xyz() + myReferenceOrigin.xyz(), 1.0f);
88   const OpenGl_Vec4 aMVReferenceY      = aMatrixMV * OpenGl_Vec4 (myReferenceY.xyz() + myReferenceOrigin.xyz(), 1.0f);
89   const OpenGl_Vec4 aMVReferenceZ      = aMatrixMV * OpenGl_Vec4 (myReferenceZ.xyz() + myReferenceOrigin.xyz(), 1.0f);
90
91   const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin;
92   const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin;
93   const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin;
94
95   Standard_Boolean isReversedX = aDirX.xyz().Dot (OpenGl_Vec3::DX()) < 0.0f;
96   Standard_Boolean isReversedY = aDirY.xyz().Dot (OpenGl_Vec3::DY()) < 0.0f;
97   Standard_Boolean isReversedZ = aDirZ.xyz().Dot (OpenGl_Vec3::DZ()) < 0.0f;
98
99   // compute flipping (rotational transform)
100   OpenGl_Mat4 aTransform;
101   if ((isReversedX || isReversedY) && !isReversedZ)
102   {
103     // invert by Z axis: left, up vectors mirrored
104     aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
105     aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
106   }
107   else if (isReversedY && isReversedZ)
108   {
109     // rotate by X axis: up, forward vectors mirrored
110     aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
111     aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
112   }
113   else if (isReversedZ)
114   {
115     // rotate by Y axis: left, forward vectors mirrored
116     aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
117     aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
118   }
119   else
120   {
121     return;
122   }
123
124   // do rotation in origin around reference system "forward" direction
125   OpenGl_Mat4 aRefAxes;
126   OpenGl_Mat4 aRefInv;
127   aRefAxes.SetColumn (0, myReferenceX.xyz());
128   aRefAxes.SetColumn (1, myReferenceY.xyz());
129   aRefAxes.SetColumn (2, myReferenceZ.xyz());
130   aRefAxes.SetColumn (3, myReferenceOrigin.xyz());
131   aRefAxes.Inverted (aRefInv);
132
133   aTransform = aRefAxes * aTransform * aRefInv;
134
135   // transform model-view matrix
136   aModelWorldMatrix = aModelWorldMatrix * aTransform;
137
138   // load transformed model-view matrix
139   aContext->ModelWorldState.SetCurrent (aModelWorldMatrix);
140   aContext->ApplyModelWorldMatrix();
141 }
142
143 // =======================================================================
144 // function : DumpJson
145 // purpose  :
146 // =======================================================================
147 void OpenGl_Flipper::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
148 {
149   OCCT_DUMP_CLASS_BEGIN (theOStream, OpenGl_Flipper)
150
151   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_Element)
152 }