0024381: Visualization, TKOpenGl - revise matrices stack and usage of temporary matrices
[occt.git] / src / OpenGl / OpenGl_Flipper.cxx
CommitLineData
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
18#include <OpenGl_Context.hxx>
19#include <OpenGl_ShaderManager.hxx>
938d4544 20#include <OpenGl_Vec.hxx>
21#include <OpenGl_Workspace.hxx>
22
23#include <gp_Ax2.hxx>
24
25// =======================================================================
26// function : Constructor
27// purpose :
28// =======================================================================
29OpenGl_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// =======================================================================
10b9c7df 56void OpenGl_Flipper::Release (OpenGl_Context*)
938d4544 57{
58 //
59}
60
61// =======================================================================
62// function : Render
63// purpose :
64// =======================================================================
65void OpenGl_Flipper::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
66{
0f8c0fb8 67 // Check if rendering is to be in immediate mode
0f8c0fb8 68 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
ca3c13d1 69
70#if !defined(GL_ES_VERSION_2_0)
0f8c0fb8 71 GLint aCurrMode = GL_MODELVIEW;
72 glGetIntegerv (GL_MATRIX_MODE, &aCurrMode);
73
938d4544 74 if (!myIsEnabled)
75 {
c827ea3a 76 // restore matrix state
77 aContext->WorldViewState.Pop();
0f8c0fb8 78
c827ea3a 79 // Apply since we probably in the middle of something.
80 aContext->ApplyModelViewMatrix();
0f8c0fb8 81
c827ea3a 82 return;
0f8c0fb8 83 }
84
c827ea3a 85 aContext->WorldViewState.Push();
86 OpenGl_Mat4 aMatrixMV = aContext->WorldViewState.Current() * aContext->ModelWorldState.Current();
938d4544 87
88 const OpenGl_Vec4 aMVReferenceOrigin = aMatrixMV * myReferenceOrigin;
89 const OpenGl_Vec4 aMVReferenceX = aMatrixMV * OpenGl_Vec4 (myReferenceX.xyz() + myReferenceOrigin.xyz(), 1.0f);
90 const OpenGl_Vec4 aMVReferenceY = aMatrixMV * OpenGl_Vec4 (myReferenceY.xyz() + myReferenceOrigin.xyz(), 1.0f);
91 const OpenGl_Vec4 aMVReferenceZ = aMatrixMV * OpenGl_Vec4 (myReferenceZ.xyz() + myReferenceOrigin.xyz(), 1.0f);
92
93 const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin;
94 const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin;
95 const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin;
96
97 Standard_Boolean isReversedX = aDirX.xyz().Dot (OpenGl_Vec3::DX()) < 0.0f;
98 Standard_Boolean isReversedY = aDirY.xyz().Dot (OpenGl_Vec3::DY()) < 0.0f;
99 Standard_Boolean isReversedZ = aDirZ.xyz().Dot (OpenGl_Vec3::DZ()) < 0.0f;
100
101 // compute flipping (rotational transform)
102 OpenGl_Mat4 aTransform;
103 if ((isReversedX || isReversedY) && !isReversedZ)
104 {
105 // invert by Z axis: left, up vectors mirrored
106 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
107 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
108 }
109 else if (isReversedY && isReversedZ)
110 {
111 // rotate by X axis: up, forward vectors mirrored
112 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
113 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
114 }
115 else if (isReversedZ)
116 {
117 // rotate by Y axis: left, forward vectors mirrored
118 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
119 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
120 }
121 else
122 {
123 return;
124 }
125
126 // do rotation in origin around reference system "forward" direction
127 OpenGl_Mat4 aRefAxes;
128 OpenGl_Mat4 aRefInv;
129 aRefAxes.SetColumn (0, myReferenceX.xyz());
130 aRefAxes.SetColumn (1, myReferenceY.xyz());
131 aRefAxes.SetColumn (2, myReferenceZ.xyz());
132 aRefAxes.SetColumn (3, myReferenceOrigin.xyz());
133 aRefAxes.Inverted (aRefInv);
134
135 aTransform = aRefAxes * aTransform * aRefInv;
136
137 // transform model-view matrix
138 aMatrixMV = aMatrixMV * aTransform;
139
140 // load transformed model-view matrix
c827ea3a 141 aContext->WorldViewState.SetCurrent (aMatrixMV);
142 aContext->ApplyWorldViewMatrix();
938d4544 143
ca3c13d1 144#endif
938d4544 145}