0027816: Visualization - provide an API for overriding clipping planes list
[occt.git] / src / OpenGl / OpenGl_MatrixState.hxx
1 // Created on: 2014-09-30
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 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 #ifndef _OpenGl_MatrixState_H__
17 #define _OpenGl_MatrixState_H__
18
19 #include <OpenGl_Vec.hxx>
20 #include <NCollection_Vector.hxx>
21
22 //! Software implementation for OpenGL matrix stack.
23 template<class T>
24 class OpenGl_MatrixState
25 {
26 public:
27
28   //! Constructs matrix state object.
29   OpenGl_MatrixState()
30   : myStack (8),
31     myStackHead (-1)
32   {
33     //
34   }
35
36   //! Pushes current matrix into stack.
37   void Push()
38   {
39     if (++myStackHead >= myStack.Size())
40     {
41       myStack.Append (myCurrent);
42     }
43     else
44     {
45       myStack.SetValue (myStackHead, myCurrent);
46     }
47   }
48
49   //! Pops matrix from stack to current.
50   void Pop()
51   {
52     Standard_ASSERT_RETURN (myStackHead != -1, "Matrix stack already empty when MatrixState.Pop() called.", );
53     myCurrent = myStack.Value (myStackHead--);
54   }
55
56   //! @return current matrix.
57   const typename OpenGl::MatrixType<T>::Mat4& Current()
58   {
59     return myCurrent;
60   }
61
62   //! Sets given matrix as current.
63   void SetCurrent (const typename OpenGl::MatrixType<T>::Mat4& theNewCurrent)
64   {
65     myCurrent = theNewCurrent;
66   }
67
68   //! Sets given matrix as current.
69   template <typename Other_t>
70   void SetCurrent (const typename OpenGl::MatrixType<Other_t>::Mat4& theNewCurrent)
71   {
72     myCurrent.Convert (theNewCurrent);
73   }
74
75   //! Sets current matrix to identity.
76   void SetIdentity()
77   {
78     myCurrent = typename OpenGl::MatrixType<T>::Mat4();
79   }
80
81 private:
82
83   NCollection_Vector<typename OpenGl::MatrixType<T>::Mat4> myStack;     //!< Collection used to maintenance matrix stack
84   typename OpenGl::MatrixType<T>::Mat4                     myCurrent;   //!< Current matrix
85   Standard_Integer                                         myStackHead; //!< Index of stack head
86 };
87
88 #endif // _OpenGl_MatrixState_H__