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