| 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->WorldViewState.Pop(); |
| 73 | |
| 74 | // Apply since we probably in the middle of something. |
| 75 | aContext->ApplyModelViewMatrix(); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | aContext->WorldViewState.Push(); |
| 80 | OpenGl_Mat4 aMatrixMV = aContext->WorldViewState.Current() * aContext->ModelWorldState.Current(); |
| 81 | |
| 82 | const OpenGl_Vec4 aMVReferenceOrigin = aMatrixMV * myReferenceOrigin; |
| 83 | const OpenGl_Vec4 aMVReferenceX = aMatrixMV * OpenGl_Vec4 (myReferenceX.xyz() + myReferenceOrigin.xyz(), 1.0f); |
| 84 | const OpenGl_Vec4 aMVReferenceY = aMatrixMV * OpenGl_Vec4 (myReferenceY.xyz() + myReferenceOrigin.xyz(), 1.0f); |
| 85 | const OpenGl_Vec4 aMVReferenceZ = aMatrixMV * OpenGl_Vec4 (myReferenceZ.xyz() + myReferenceOrigin.xyz(), 1.0f); |
| 86 | |
| 87 | const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin; |
| 88 | const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin; |
| 89 | const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin; |
| 90 | |
| 91 | Standard_Boolean isReversedX = aDirX.xyz().Dot (OpenGl_Vec3::DX()) < 0.0f; |
| 92 | Standard_Boolean isReversedY = aDirY.xyz().Dot (OpenGl_Vec3::DY()) < 0.0f; |
| 93 | Standard_Boolean isReversedZ = aDirZ.xyz().Dot (OpenGl_Vec3::DZ()) < 0.0f; |
| 94 | |
| 95 | // compute flipping (rotational transform) |
| 96 | OpenGl_Mat4 aTransform; |
| 97 | if ((isReversedX || isReversedY) && !isReversedZ) |
| 98 | { |
| 99 | // invert by Z axis: left, up vectors mirrored |
| 100 | aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz()); |
| 101 | aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz()); |
| 102 | } |
| 103 | else if (isReversedY && isReversedZ) |
| 104 | { |
| 105 | // rotate by X axis: up, forward vectors mirrored |
| 106 | aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz()); |
| 107 | aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz()); |
| 108 | } |
| 109 | else if (isReversedZ) |
| 110 | { |
| 111 | // rotate by Y axis: left, forward vectors mirrored |
| 112 | aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz()); |
| 113 | aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz()); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // do rotation in origin around reference system "forward" direction |
| 121 | OpenGl_Mat4 aRefAxes; |
| 122 | OpenGl_Mat4 aRefInv; |
| 123 | aRefAxes.SetColumn (0, myReferenceX.xyz()); |
| 124 | aRefAxes.SetColumn (1, myReferenceY.xyz()); |
| 125 | aRefAxes.SetColumn (2, myReferenceZ.xyz()); |
| 126 | aRefAxes.SetColumn (3, myReferenceOrigin.xyz()); |
| 127 | aRefAxes.Inverted (aRefInv); |
| 128 | |
| 129 | aTransform = aRefAxes * aTransform * aRefInv; |
| 130 | |
| 131 | // transform model-view matrix |
| 132 | aMatrixMV = aMatrixMV * aTransform; |
| 133 | |
| 134 | // load transformed model-view matrix |
| 135 | aContext->WorldViewState.SetCurrent (aMatrixMV); |
| 136 | aContext->ApplyWorldViewMatrix(); |
| 137 | } |