10 #include <glm/glm.hpp>
11 #include <glm/gtc/matrix_transform.hpp>
12 #include <glm/gtc/type_ptr.hpp>
13 #include <glm/gtc/noise.hpp>
14 #include <glm/gtc/epsilon.hpp>
15 #include <glm/gtc/matrix_transform.hpp>
18 class MatrixStack :
public std::stack<glm::mat4> {
26 explicit MatrixStack(
const MatrixStack& other) : std::stack<glm::mat4>() {
27 *((std::stack<glm::mat4>*)
this) = *((std::stack<glm::mat4>*)&other);
30 operator const glm::mat4 & ()
const {
35 std::stack<glm::mat4>::pop();
40 MatrixStack & push() {
45 MatrixStack & identity() {
50 MatrixStack & push(
const glm::mat4 & mat) {
51 std::stack<glm::mat4>::push(mat);
55 MatrixStack & rotate(
const glm::mat3 & rotation) {
56 return postMultiply(glm::mat4(rotation));
59 MatrixStack & rotate(
const glm::quat & rotation) {
60 return postMultiply(glm::mat4_cast(rotation));
63 MatrixStack & rotate(
float theta,
const glm::vec3 & axis) {
64 return postMultiply(glm::rotate(glm::mat4(), theta, axis));
67 MatrixStack & translate(
float translation) {
68 return translate(glm::vec3(translation, 0, 0));
71 MatrixStack & translate(
const glm::vec2 & translation) {
72 return translate(glm::vec3(translation, 0));
75 MatrixStack & translate(
const glm::vec3 & translation) {
76 return postMultiply(glm::translate(glm::mat4(), translation));
79 MatrixStack & preTranslate(
const glm::vec3 & translation) {
80 return preMultiply(glm::translate(glm::mat4(), translation));
84 MatrixStack & scale(
float factor) {
85 return scale(glm::vec3(factor));
88 MatrixStack & scale(
const glm::vec3 & scale) {
89 return postMultiply(glm::scale(glm::mat4(), scale));
92 MatrixStack & transform(
const glm::mat4 & xfm) {
93 return postMultiply(xfm);
96 MatrixStack & preMultiply(
const glm::mat4 & xfm) {
101 MatrixStack & postMultiply(
const glm::mat4 & xfm) {
107 MatrixStack & unrotate() {
108 glm::quat inverse = glm::inverse(glm::quat_cast(top()));
109 top() = top() * glm::mat4_cast(inverse);
114 MatrixStack & untranslate() {
115 top()[3] = glm::vec4(0, 0, 0, 1);
119 template <
typename Function>
120 void withPush(Function f) {
122 size_t startingDepth = size();
128 assert(startingDepth == size());
132 template <
typename Function>
133 void withIdentity(Function f) {
140 static MatrixStack & projection() {
141 static MatrixStack projection;
145 static MatrixStack & modelview() {
146 static MatrixStack modelview;
150 template <
typename Function>
151 static void withPushAll(Function f) {
152 withPush(projection(), modelview(), f);
155 template <
typename Function>
156 static void withIdentityAll(Function f) {
157 withPush(projection(), modelview(), [=] {
158 projection().identity();
159 modelview().identity();
164 template <
typename Function>
165 static void withPush(MatrixStack& stack, Function f) {
169 template <
typename Function>
170 static void withPush(MatrixStack& stack1, MatrixStack& stack2, Function f) {