-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathM3DMatrixStack.h
More file actions
executable file
·180 lines (143 loc) · 5.59 KB
/
M3DMatrixStack.h
File metadata and controls
executable file
·180 lines (143 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// GLMatrixStack.h
// Matrix stack functionality
/*
Copyright (c) 2009-2023, Richard S. Wright Jr.
All rights reserved.
Refactored to M3DMatrixStack for Vulkan - June 2021
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Neither the name of Richard S. Wright Jr. nor the names of other contributors may be used
to endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __M3D_MATRIX_STACK
#define __M3D_MATRIX_STACK
#include "math3d.h"
#include "M3DFrame.h"
enum M3D_STACK_ERROR { M3D_STACK_NOERROR = 0, M3D_STACK_OVERFLOW, M3D_STACK_UNDERFLOW };
class M3DMatrixStack
{
public:
M3DMatrixStack(int iStackDepth = 64) {
stackDepth = iStackDepth;
pStack = new M3DMatrix44f[iStackDepth];
stackPointer = 0;
m3dLoadIdentity44(pStack[0]);
lastError = M3D_STACK_NOERROR;
}
~M3DMatrixStack(void) {
delete [] pStack;
}
inline void LoadIdentity(void) {
m3dLoadIdentity44(pStack[stackPointer]);
}
inline void LoadMatrix(const M3DMatrix44f mMatrix) {
m3dCopyMatrix44(pStack[stackPointer], mMatrix);
}
inline void LoadMatrix(M3DFrame& frame) {
M3DMatrix44f m;
frame.GetMatrix(m);
LoadMatrix(m);
}
inline void MultMatrix(const M3DMatrix44f mMatrix) {
M3DMatrix44f mTemp;
m3dCopyMatrix44(mTemp, pStack[stackPointer]);
m3dMatrixMultiply44(pStack[stackPointer], mTemp, mMatrix);
}
inline void MultMatrix(M3DFrame& frame) {
M3DMatrix44f m;
frame.GetMatrix(m);
MultMatrix(m);
}
inline void PushMatrix(void) {
if(stackPointer < stackDepth-1) {
stackPointer++;
m3dCopyMatrix44(pStack[stackPointer], pStack[stackPointer-1]);
}
else
lastError = M3D_STACK_OVERFLOW;
}
inline void PopMatrix(void) {
if(stackPointer > 0)
stackPointer--;
else
lastError = M3D_STACK_UNDERFLOW;
}
void Scale(float x, float y, float z) {
M3DMatrix44f mTemp, mScale;
m3dScaleMatrix44(mScale, x, y, z);
m3dCopyMatrix44(mTemp, pStack[stackPointer]);
m3dMatrixMultiply44(pStack[stackPointer], mTemp, mScale);
}
void Translate(float x, float y, float z) {
M3DMatrix44f mTemp, mScale;
m3dTranslationMatrix44(mScale, x, y, z);
m3dCopyMatrix44(mTemp, pStack[stackPointer]);
m3dMatrixMultiply44(pStack[stackPointer], mTemp, mScale);
}
void Rotate(float angle, float x, float y, float z) {
M3DMatrix44f mTemp, mRotate;
m3dRotationMatrix44(mRotate, float(m3dDegToRad(angle)), x, y, z);
m3dCopyMatrix44(mTemp, pStack[stackPointer]);
m3dMatrixMultiply44(pStack[stackPointer], mTemp, mRotate);
}
// I've always wanted vector versions of these
void Scalev(const M3DVector3f vScale) {
M3DMatrix44f mTemp, mScale;
m3dScaleMatrix44(mScale, vScale);
m3dCopyMatrix44(mTemp, pStack[stackPointer]);
m3dMatrixMultiply44(pStack[stackPointer], mTemp, mScale);
}
void Translatev(const M3DVector3f vTranslate) {
M3DMatrix44f mTemp, mTranslate;
m3dLoadIdentity44(mTranslate);
memcpy(&mTranslate[12], vTranslate, sizeof(float) * 3);
m3dCopyMatrix44(mTemp, pStack[stackPointer]);
m3dMatrixMultiply44(pStack[stackPointer], mTemp, mTranslate);
}
void Rotatev(float angle, M3DVector3f vAxis) {
M3DMatrix44f mTemp, mRotation;
m3dRotationMatrix44(mRotation, float(m3dDegToRad(angle)), vAxis[0], vAxis[1], vAxis[2]);
m3dCopyMatrix44(mTemp, pStack[stackPointer]);
m3dMatrixMultiply44(pStack[stackPointer], mTemp, mRotation);
}
// I've also always wanted to be able to do this
void PushMatrix(const M3DMatrix44f mMatrix) {
if(stackPointer < stackDepth - 1) {
stackPointer++;
m3dCopyMatrix44(pStack[stackPointer], mMatrix);
}
else
lastError = M3D_STACK_OVERFLOW;
}
void PushMatrix(M3DFrame& frame) {
M3DMatrix44f m;
frame.GetMatrix(m);
PushMatrix(m);
}
// Two different ways to get the matrix
const M3DMatrix44f& GetMatrix(void) { return pStack[stackPointer]; }
void GetMatrix(M3DMatrix44f mMatrix) { m3dCopyMatrix44(mMatrix, pStack[stackPointer]); }
inline M3D_STACK_ERROR GetLastError(void) {
M3D_STACK_ERROR retval = lastError;
lastError = M3D_STACK_NOERROR;
return retval;
}
protected:
M3D_STACK_ERROR lastError;
int stackDepth;
int stackPointer;
M3DMatrix44f *pStack;
};
#endif