This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
csce441pine64backup/L00/src/MatrixStack.cpp

115 lines
1.9 KiB
C++
Raw Normal View History

2017-02-17 17:32:44 -06:00
#include "MatrixStack.h"
#include <stdio.h>
#include <cassert>
#include <vector>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
using namespace std;
MatrixStack::MatrixStack()
{
mstack = make_shared< stack<glm::mat4> >();
mstack->push(glm::mat4(1.0));
}
MatrixStack::~MatrixStack()
{
}
void MatrixStack::pushMatrix()
{
const glm::mat4 &top = mstack->top();
mstack->push(top);
assert(mstack->size() < 100);
}
void MatrixStack::popMatrix()
{
assert(!mstack->empty());
mstack->pop();
// There should always be one matrix left.
assert(!mstack->empty());
}
void MatrixStack::loadIdentity()
{
glm::mat4 &top = mstack->top();
top = glm::mat4(1.0);
}
void MatrixStack::translate(const glm::vec3 &t)
{
glm::mat4 &top = mstack->top();
top *= glm::translate(t);
}
void MatrixStack::translate(float x, float y, float z)
{
translate(glm::vec3(x, y, z));
}
void MatrixStack::scale(const glm::vec3 &s)
{
glm::mat4 &top = mstack->top();
top *= glm::scale(s);
}
void MatrixStack::scale(float x, float y, float z)
{
scale(glm::vec3(x, y, z));
}
void MatrixStack::scale(float s)
{
scale(glm::vec3(s, s, s));
}
void MatrixStack::rotate(float angle, const glm::vec3 &axis)
{
glm::mat4 &top = mstack->top();
top *= glm::rotate(angle, axis);
}
void MatrixStack::rotate(float angle, float x, float y, float z)
{
rotate(angle, glm::vec3(x, y, z));
}
void MatrixStack::multMatrix(const glm::mat4 &matrix)
{
glm::mat4 &top = mstack->top();
top *= matrix;
}
const glm::mat4 &MatrixStack::topMatrix() const
{
return mstack->top();
}
void MatrixStack::print(const glm::mat4 &mat, const char *name)
{
if(name) {
printf("%s = [\n", name);
}
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 4; ++j) {
// mat[j] returns the jth column
printf("%- 5.2f ", mat[j][i]);
}
printf("\n");
}
if(name) {
printf("];");
}
printf("\n");
}
void MatrixStack::print(const char *name) const
{
print(mstack->top(), name);
}