0025219: Visualization, TKOpenGl - disable code paths unavailable on OpenGL ES 2.0
[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 Standard_Boolean isImmediate = (theWorkspace->NamedStatus & OPENGL_NS_IMMEDIATE) != 0;
69   const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
70
71 #if !defined(GL_ES_VERSION_2_0)
72   GLint aCurrMode = GL_MODELVIEW;
73   glGetIntegerv (GL_MATRIX_MODE, &aCurrMode);
74
75   if (!myIsEnabled)
76   {
77     // Restore transformation
78     if (isImmediate)
79     {
80       if (aCurrMode != GL_MODELVIEW)
81       {
82         glMatrixMode (GL_MODELVIEW);
83       }
84
85       glPopMatrix();
86
87       if (aCurrMode != GL_MODELVIEW)
88       {
89         glMatrixMode (aCurrMode);
90       }
91
92       Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
93                                     { 0.f, 1.f, 0.f, 0.f },
94                                     { 0.f, 0.f, 1.f, 0.f },
95                                     { 0.f, 0.f, 0.f, 1.f } };
96
97       aContext->ShaderManager()->RevertModelWorldStateTo (&aModelWorldState);
98     }
99     else
100     {
101       // Update current model-view matrix in the top of the stack
102       // replacing it with StructureMatrixT*ViewMatrix from the workspace.
103       theWorkspace->UpdateModelViewMatrix();
104     }
105     return;
106   }
107
108   if (isImmediate)
109   {
110
111     if (!aContext->ShaderManager()->IsEmpty())
112     {
113       Tmatrix3 aWorldView;
114       glGetFloatv (GL_MODELVIEW_MATRIX, *aWorldView);
115
116       Tmatrix3 aProjection;
117       glGetFloatv (GL_PROJECTION_MATRIX, *aProjection);
118
119       aContext->ShaderManager()->UpdateWorldViewStateTo (&aWorldView);
120       aContext->ShaderManager()->UpdateProjectionStateTo (&aProjection);
121     }
122
123     if (aCurrMode != GL_MODELVIEW)
124     {
125       glMatrixMode (GL_MODELVIEW);
126     }
127
128     glPushMatrix();
129
130     if (aCurrMode != GL_MODELVIEW)
131     {
132       glMatrixMode (aCurrMode);
133     }
134   }
135
136   OpenGl_Mat4 aMatrixMV;
137   glGetFloatv (GL_MODELVIEW_MATRIX, aMatrixMV.ChangeData());
138
139   const OpenGl_Vec4 aMVReferenceOrigin = aMatrixMV * myReferenceOrigin;
140   const OpenGl_Vec4 aMVReferenceX      = aMatrixMV * OpenGl_Vec4 (myReferenceX.xyz() + myReferenceOrigin.xyz(), 1.0f);
141   const OpenGl_Vec4 aMVReferenceY      = aMatrixMV * OpenGl_Vec4 (myReferenceY.xyz() + myReferenceOrigin.xyz(), 1.0f);
142   const OpenGl_Vec4 aMVReferenceZ      = aMatrixMV * OpenGl_Vec4 (myReferenceZ.xyz() + myReferenceOrigin.xyz(), 1.0f);
143
144   const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin;
145   const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin;
146   const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin;
147
148   Standard_Boolean isReversedX = aDirX.xyz().Dot (OpenGl_Vec3::DX()) < 0.0f;
149   Standard_Boolean isReversedY = aDirY.xyz().Dot (OpenGl_Vec3::DY()) < 0.0f;
150   Standard_Boolean isReversedZ = aDirZ.xyz().Dot (OpenGl_Vec3::DZ()) < 0.0f;
151
152   // compute flipping (rotational transform)
153   OpenGl_Mat4 aTransform;
154   if ((isReversedX || isReversedY) && !isReversedZ)
155   {
156     // invert by Z axis: left, up vectors mirrored
157     aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
158     aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
159   }
160   else if (isReversedY && isReversedZ)
161   {
162     // rotate by X axis: up, forward vectors mirrored
163     aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
164     aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
165   }
166   else if (isReversedZ)
167   {
168     // rotate by Y axis: left, forward vectors mirrored
169     aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
170     aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
171   }
172   else
173   {
174     return;
175   }
176
177   // do rotation in origin around reference system "forward" direction
178   OpenGl_Mat4 aRefAxes;
179   OpenGl_Mat4 aRefInv;
180   aRefAxes.SetColumn (0, myReferenceX.xyz());
181   aRefAxes.SetColumn (1, myReferenceY.xyz());
182   aRefAxes.SetColumn (2, myReferenceZ.xyz());
183   aRefAxes.SetColumn (3, myReferenceOrigin.xyz());
184   aRefAxes.Inverted (aRefInv);
185
186   aTransform = aRefAxes * aTransform * aRefInv;
187
188   // transform model-view matrix
189   aMatrixMV = aMatrixMV * aTransform;
190
191   // load transformed model-view matrix
192   if (aCurrMode != GL_MODELVIEW)
193   {
194     glMatrixMode (GL_MODELVIEW);
195   }
196
197   glLoadMatrixf ((GLfloat*) aMatrixMV);
198
199   if (aCurrMode != GL_MODELVIEW)
200   {
201     glMatrixMode (aCurrMode);
202   }
203 #endif
204 }