0025133: TKOpenGl - Crash on closing a view containing presentations with capping
[occt.git] / src / OpenGl / OpenGl_Flipper.cxx
... / ...
CommitLineData
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// =======================================================================
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// =======================================================================
56void OpenGl_Flipper::Release (OpenGl_Context*)
57{
58 //
59}
60
61// =======================================================================
62// function : Render
63// purpose :
64// =======================================================================
65void OpenGl_Flipper::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
66{
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);
72
73 if (!myIsEnabled)
74 {
75 // Restore transformation
76 if (isImmediate)
77 {
78 if (aCurrMode != GL_MODELVIEW)
79 {
80 glMatrixMode (GL_MODELVIEW);
81 }
82
83 glPopMatrix();
84
85 if (aCurrMode != GL_MODELVIEW)
86 {
87 glMatrixMode (aCurrMode);
88 }
89
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 } };
94
95 aContext->ShaderManager()->RevertModelWorldStateTo (&aModelWorldState);
96 }
97 else
98 {
99 // Update current model-view matrix in the top of the stack
100 // replacing it with StructureMatrixT*ViewMatrix from the workspace.
101 theWorkspace->UpdateModelViewMatrix();
102 }
103 return;
104 }
105
106 if (isImmediate)
107 {
108
109 if (!aContext->ShaderManager()->IsEmpty())
110 {
111 Tmatrix3 aWorldView;
112 glGetFloatv (GL_MODELVIEW_MATRIX, *aWorldView);
113
114 Tmatrix3 aProjection;
115 glGetFloatv (GL_PROJECTION_MATRIX, *aProjection);
116
117 aContext->ShaderManager()->UpdateWorldViewStateTo (&aWorldView);
118 aContext->ShaderManager()->UpdateProjectionStateTo (&aProjection);
119 }
120
121 if (aCurrMode != GL_MODELVIEW)
122 {
123 glMatrixMode (GL_MODELVIEW);
124 }
125
126 glPushMatrix();
127
128 if (aCurrMode != GL_MODELVIEW)
129 {
130 glMatrixMode (aCurrMode);
131 }
132 }
133
134 OpenGl_Mat4 aMatrixMV;
135 glGetFloatv (GL_MODELVIEW_MATRIX, aMatrixMV.ChangeData());
136
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);
141
142 const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin;
143 const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin;
144 const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin;
145
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;
149
150 // compute flipping (rotational transform)
151 OpenGl_Mat4 aTransform;
152 if ((isReversedX || isReversedY) && !isReversedZ)
153 {
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());
157 }
158 else if (isReversedY && isReversedZ)
159 {
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());
163 }
164 else if (isReversedZ)
165 {
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());
169 }
170 else
171 {
172 return;
173 }
174
175 // do rotation in origin around reference system "forward" direction
176 OpenGl_Mat4 aRefAxes;
177 OpenGl_Mat4 aRefInv;
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);
183
184 aTransform = aRefAxes * aTransform * aRefInv;
185
186 // transform model-view matrix
187 aMatrixMV = aMatrixMV * aTransform;
188
189 // load transformed model-view matrix
190 if (aCurrMode != GL_MODELVIEW)
191 {
192 glMatrixMode (GL_MODELVIEW);
193 }
194
195 glLoadMatrixf ((GLfloat*) aMatrixMV);
196
197 if (aCurrMode != GL_MODELVIEW)
198 {
199 glMatrixMode (aCurrMode);
200 }
201}