0032308: Configuration - make Xlib dependency optional
[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();
938d4544 69 if (!myIsEnabled)
70 {
c827ea3a 71 // restore matrix state
ab9e277f 72 aContext->ModelWorldState.Pop();
0f8c0fb8 73
c827ea3a 74 // Apply since we probably in the middle of something.
ab9e277f 75 aContext->ApplyModelWorldMatrix();
c827ea3a 76 return;
0f8c0fb8 77 }
78
ab9e277f 79 aContext->ModelWorldState.Push();
80
81 OpenGl_Mat4 aModelWorldMatrix;
82 aModelWorldMatrix.Convert (aContext->ModelWorldState.Current());
83
84 OpenGl_Mat4 aMatrixMV = aContext->WorldViewState.Current() * aModelWorldMatrix;
938d4544 85
86 const OpenGl_Vec4 aMVReferenceOrigin = aMatrixMV * myReferenceOrigin;
87 const OpenGl_Vec4 aMVReferenceX = aMatrixMV * OpenGl_Vec4 (myReferenceX.xyz() + myReferenceOrigin.xyz(), 1.0f);
88 const OpenGl_Vec4 aMVReferenceY = aMatrixMV * OpenGl_Vec4 (myReferenceY.xyz() + myReferenceOrigin.xyz(), 1.0f);
89 const OpenGl_Vec4 aMVReferenceZ = aMatrixMV * OpenGl_Vec4 (myReferenceZ.xyz() + myReferenceOrigin.xyz(), 1.0f);
90
91 const OpenGl_Vec4 aDirX = aMVReferenceX - aMVReferenceOrigin;
92 const OpenGl_Vec4 aDirY = aMVReferenceY - aMVReferenceOrigin;
93 const OpenGl_Vec4 aDirZ = aMVReferenceZ - aMVReferenceOrigin;
94
95 Standard_Boolean isReversedX = aDirX.xyz().Dot (OpenGl_Vec3::DX()) < 0.0f;
96 Standard_Boolean isReversedY = aDirY.xyz().Dot (OpenGl_Vec3::DY()) < 0.0f;
97 Standard_Boolean isReversedZ = aDirZ.xyz().Dot (OpenGl_Vec3::DZ()) < 0.0f;
98
99 // compute flipping (rotational transform)
100 OpenGl_Mat4 aTransform;
101 if ((isReversedX || isReversedY) && !isReversedZ)
102 {
103 // invert by Z axis: left, up vectors mirrored
104 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
105 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
106 }
107 else if (isReversedY && isReversedZ)
108 {
109 // rotate by X axis: up, forward vectors mirrored
110 aTransform.SetColumn (1, -aTransform.GetColumn (1).xyz());
111 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
112 }
113 else if (isReversedZ)
114 {
115 // rotate by Y axis: left, forward vectors mirrored
116 aTransform.SetColumn (0, -aTransform.GetColumn (0).xyz());
117 aTransform.SetColumn (2, -aTransform.GetColumn (2).xyz());
118 }
119 else
120 {
121 return;
122 }
123
124 // do rotation in origin around reference system "forward" direction
125 OpenGl_Mat4 aRefAxes;
126 OpenGl_Mat4 aRefInv;
127 aRefAxes.SetColumn (0, myReferenceX.xyz());
128 aRefAxes.SetColumn (1, myReferenceY.xyz());
129 aRefAxes.SetColumn (2, myReferenceZ.xyz());
130 aRefAxes.SetColumn (3, myReferenceOrigin.xyz());
131 aRefAxes.Inverted (aRefInv);
132
133 aTransform = aRefAxes * aTransform * aRefInv;
134
135 // transform model-view matrix
ab9e277f 136 aModelWorldMatrix = aModelWorldMatrix * aTransform;
938d4544 137
138 // load transformed model-view matrix
ab9e277f 139 aContext->ModelWorldState.SetCurrent (aModelWorldMatrix);
140 aContext->ApplyModelWorldMatrix();
938d4544 141}
bc73b006 142
143// =======================================================================
144// function : DumpJson
145// purpose :
146// =======================================================================
147void OpenGl_Flipper::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
148{
149 OCCT_DUMP_CLASS_BEGIN (theOStream, OpenGl_Flipper)
150
151 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_Element)
152}