MatrixStack.hh 3.51 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

6
7
8
9
10
11
12
13
14
15
#ifndef ACGL_SCENE_MATRIXSTACK_HH
#define ACGL_SCENE_MATRIXSTACK_HH

/*
 * A matrix stack that can mimic the old OpenGL matrix stack. It is only based on
 * glm, so it can be used without OpenGL easily.
 *
 * You can create as many independent stacks as you like or just use the
 * pre-defined ModelViewMatrixStack singleton.
 */
Robert Menzel's avatar
Robert Menzel committed
16

Robert Menzel's avatar
Robert Menzel committed
17
#include <ACGL/ACGL.hh>
Robert Menzel's avatar
Robert Menzel committed
18
19
20
21
22
23
#include <ACGL/Base/Singleton.hh>
#include <ACGL/Math/Math.hh>

#include <stack>

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
24
namespace Scene{
Robert Menzel's avatar
Robert Menzel committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

class MatrixStack
{
public:
    MatrixStack() {}

    // does not change the stack
    // returns the top modelview matrix
    glm::mat4 topMatrix()
    {
        if (mStack.empty()) return glm::mat4();
        return mStack.top();
    }

    // returns the inverse transpose of the upper 3*3
    // modelview matrix
    glm::mat3 topNormalMatrix()
    {
        return Math::Functions::normalMatrix( topMatrix() );
    }

    void pushMatrix()
    {
        mStack.push( topMatrix() );
    }

    void popMatrix()
    {
        if (!mStack.empty()) mStack.pop();
    }


    // if the stack is empty this will return the identity
    glm::mat4 popAndGetMatrix()
    {
        if (mStack.empty()) {
            return glm::mat4();
        }
        glm::mat4 tmp = mStack.top();
        mStack.pop();
        return tmp;
    }

    // will change the top matrix
    void loadIdentity()
    {
        loadMatrix( glm::mat4() );
    }

    // will replace the top matrix with the given matrix
    void loadMatrix( glm::mat4 matrixToLoad )
    {
77
78
79
80
81
        if (!mStack.empty()) {
            mStack.top() = matrixToLoad;
        } else {
            mStack.push( matrixToLoad );
        }
Robert Menzel's avatar
Robert Menzel committed
82
83
84
85
86
87
    }

    // the top matrix will be multiplied with the given one and
    // will then replace the top matrix
    void multMatrix( const glm::mat4 &rhs )
    {
88
89
90
91
92
93
94
95
96
        if (mStack.empty()) mStack.push( glm::mat4() );
        mStack.top() *= rhs;
    }

    // pushes the product of top and rhs
    void pushAndMultMatrix( const glm::mat4 &rhs )
    {
        if (mStack.empty()) mStack.push( glm::mat4() );
        mStack.push( mStack.top() * rhs );
Robert Menzel's avatar
Robert Menzel committed
97
98
99
100
101
102
    }

    // like glRotate rotates the top matrix around a given axis a given degree phi
    void rotate(const float phi, const float x, const float y, const float z) { rotate( phi, glm::vec3( x,y,z )); }
    void rotate(const float phi, const glm::vec3 &axis )
    {
103
104
        if (mStack.empty()) mStack.push( glm::mat4() );
        mStack.top() = glm::rotate( mStack.top(), phi, axis );
Robert Menzel's avatar
Robert Menzel committed
105
106
107
108
109
110
    }

    // like glTranslate
    void translate(float x, float y, float z) { translate( glm::vec3(x,y,z) ); }
    void translate( glm::vec3 vector )
    {
111
112
        if (mStack.empty()) mStack.push( glm::mat4() );
        mStack.top() = glm::translate( mStack.top(), vector );
Robert Menzel's avatar
Robert Menzel committed
113
114
115
116
117
118
119
    }

    // like glScale
    void scale( float s ) { scale( glm::vec3(s,s,s) ); }
    void scale( float x, float y, float z ) { scale( glm::vec3(x,y,z)); }
    void scale( const glm::vec3 &factor )
    {
120
121
        if (mStack.empty()) mStack.push( glm::mat4() );
        mStack.top() = glm::scale( mStack.top(), factor );
Robert Menzel's avatar
Robert Menzel committed
122
123
124
125
126
127
128
129
    }

private:
    std::stack< glm::mat4 > mStack;
};

typedef Base::Singleton< MatrixStack > ModelViewMatrixStack;

Robert Menzel's avatar
Robert Menzel committed
130
} // Scene
Robert Menzel's avatar
Robert Menzel committed
131
132
} // ACGL

133
#endif // ACGL_SCENE_MATRIXSTACK_HH