gl_matrices.h 3.22 KB
Newer Older
1 2 3
/*******************************************************************************
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
4
 * Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *                                                                              *
 * This library is free software; you can redistribute it and/or modify it      *
 * under the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation; either version 2.1 of the License, or (at your     *
 * option) any later version.                                                   *
 *                                                                              *
 * This library is distributed in the hope that it will be useful, but WITHOUT  *
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
 * for more details.                                                            *
 *                                                                              *
 * You should have received a copy of the GNU Lesser General Public License     *
 * along with this library; if not, write to the Free Software Foundation,      *
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
 *                                                                              *
20
 * Web site: http://cgogn.unistra.fr/                                           *
21 22 23 24 25 26 27 28 29
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/


#ifndef __GL_MATRICES_H_
#define __GL_MATRICES_H_

#include "glm/glm.hpp"
Sylvain Thery's avatar
Sylvain Thery committed
30
#include "glm/gtc/matrix_transform.hpp"
31
#include <stack>
Sylvain Thery's avatar
Sylvain Thery committed
32
#include "Geometry/vector_gen.h"
33 34 35 36 37 38 39 40 41

namespace CGoGN
{
namespace Utils
{

class GL_Matrices
{
public:
Sylvain Thery's avatar
Sylvain Thery committed
42 43
	/// 0: projection / 1: modelView /2: transfo / 3:PMV /4:normal
	glm::mat4 m_matrices[5];
44 45 46
	/// stack of transfo matrix
	std::stack<glm::mat4> m_stack;

Sylvain Thery's avatar
Sylvain Thery committed
47 48 49 50 51 52 53 54
public:
	void pushTransfo()
	{
		m_stack.push(m_matrices[2]);
	}

	void popTransfo()
	{
Sylvain Thery's avatar
Sylvain Thery committed
55 56
		if (m_stack.empty())
			return;
Sylvain Thery's avatar
Sylvain Thery committed
57 58 59 60
		m_matrices[2] = m_stack.top();
		m_stack.pop();
	}

Sylvain Thery's avatar
Sylvain Thery committed
61 62 63 64 65 66
	const glm::mat4&  getTransfo() const
	{
		return m_matrices[2];
	}

	glm::mat4& getTransfo()
Sylvain Thery's avatar
Sylvain Thery committed
67 68 69 70 71 72
	{
		return m_matrices[2];
	}

	void rotate(float angle, const Geom::Vec3f& Axis)
	{
boustila's avatar
boustila committed
73
		glm::mat4 X = glm::rotate(glm::mat4(1.f), angle, glm::vec3(Axis[0],Axis[1],Axis[2])) * m_matrices[2];
Sylvain Thery's avatar
Sylvain Thery committed
74
		m_matrices[2] = X;
Sylvain Thery's avatar
Sylvain Thery committed
75 76 77 78
	}

	void translate(const Geom::Vec3f& P)
	{
boustila's avatar
boustila committed
79 80
		
		glm::mat4 X = glm::translate(glm::mat4(1.f), glm::vec3(P[0],P[1],P[2])) * m_matrices[2];
Sylvain Thery's avatar
Sylvain Thery committed
81
		m_matrices[2] = X;
Sylvain Thery's avatar
Sylvain Thery committed
82 83 84 85
	}

	void scale(const Geom::Vec3f& S)
	{
boustila's avatar
boustila committed
86
		glm::mat4 X = glm::scale(glm::mat4(1.f), glm::vec3(S[0],S[1],S[2])) * m_matrices[2];
Sylvain Thery's avatar
Sylvain Thery committed
87
		m_matrices[2] = X;
Sylvain Thery's avatar
Sylvain Thery committed
88 89 90 91
	}

	void scale(float s)
	{
boustila's avatar
boustila committed
92
		glm::mat4 X	= glm::scale(glm::mat4(1.f), glm::vec3(s,s,s)) * m_matrices[2];
Sylvain Thery's avatar
Sylvain Thery committed
93
		m_matrices[2] = X;
Sylvain Thery's avatar
Sylvain Thery committed
94 95
	}

Sylvain Thery's avatar
Sylvain Thery committed
96 97
	void apply (const glm::mat4& m)
	{
Sylvain Thery's avatar
Sylvain Thery committed
98 99
		glm::mat4 X = m * m_matrices[2];
		m_matrices[2] = X;
Sylvain Thery's avatar
Sylvain Thery committed
100 101
	}

102 103 104 105 106 107 108
};


}
}

#endif