zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Matrix4f.hxx
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef ZENI_MATRIX4F_HXX
19 #define ZENI_MATRIX4F_HXX
20 
21 // HXXed below
22 #include <Zeni/Coordinate.h>
23 #include <Zeni/Quaternion.h>
24 
25 #include <Zeni/Matrix4f.h>
26 
27 // Not HXXed
28 #include <cassert>
29 #include <cmath>
30 
31 namespace Zeni {
32 
34  : row(row_)
35  {
36  }
37 
38  const float & Matrix4f::Matrix4f_Row::operator[](const int &index) const {
39  assert(-1 < index && index < 4);
40  return row[index << 2];
41  }
42 
44  assert(-1 < index && index < 4);
45  return row[index << 2];
46  }
47 
49  return Matrix4f();
50  }
51 
53  return Matrix4f(1.0f, 0.0f, 0.0f, 0.0f,
54  0.0f, 1.0f, 0.0f, 0.0f,
55  0.0f, 0.0f, 1.0f, 0.0f,
56  0.0f, 0.0f, 0.0f, 1.0f);
57  }
58 
59  Matrix4f Matrix4f::Scale(const Vector3f &scaling_factor) {
60  return Matrix4f(scaling_factor.i, 0.0f, 0.0f, 0.0f,
61  0.0f, scaling_factor.j, 0.0f, 0.0f,
62  0.0f, 0.0f, scaling_factor.k, 0.0f,
63  0.0f, 0.0f, 0.0f, 1.0f);
64  }
65 
67  return rotation.get_matrix();
68  }
69 
70  Matrix4f Matrix4f::Translate(const Vector3f &translation_factor) {
71  return Matrix4f(1.0f, 0.0f, 0.0f, translation_factor.i,
72  0.0f, 1.0f, 0.0f, translation_factor.j,
73  0.0f, 0.0f, 1.0f, translation_factor.k,
74  0.0f, 0.0f, 0.0f, 1.0f);
75  }
76 
77  Matrix4f Matrix4f::View(const Point3f &position, const Vector3f &forward, const Vector3f &up) {
78  const Vector3f l = forward.normalized();
79  const Vector3f s = (l % up.normalized()).normalized();
80  const Vector3f u = s % l;
81 
82  return Matrix4f(s.i, s.j, s.k, 0.0f,
83  u.i, u.j, u.k, 0.0f,
84  -l.i, -l.j, -l.k, 0.0f,
85  0.0f, 0.0f, 0.0f, 1.0f) *
86  Translate(-Vector3f(position));
87  }
88 
89  Matrix4f Matrix4f::Orthographic(const float &left, const float &right, const float &bottom, const float &top, const float &near_, const float &far_) {
90  const float denom_x = left - right;
91  const float denom_y = bottom - top;
92  const float denom_z = near_ - far_;
93 
94  const float t_x = (left + right) / denom_x;
95  const float t_y = (bottom + top) / denom_y;
96  const float t_z = (near_ + far_) / denom_z;
97 
98  return Matrix4f(-2.0f / denom_x, 0.0f, 0.0f, t_x,
99  0.0f, -2.0f / denom_y, 0.0f, t_y,
100  0.0f, 0.0f, -2.0f / denom_z, t_z,
101  0.0f, 0.0f, 0.0f, 1.0f);
102  }
103 
104  Matrix4f Matrix4f::Perspective(const float &fov_rad_y, const float &aspect, const float &near_, const float &far_) {
105 #define cotangent(angle) (1.0f / tan(angle))
106  const float f = float(cotangent(0.5f * fov_rad_y));
107 #undef cotangent
108  const float denom = near_ - far_;
109 
110  return Matrix4f(f / aspect, 0.0f, 0.0f, 0.0f,
111  0.0f, f, 0.0f, 0.0f,
112  0.0f, 0.0f, (far_ + near_) / denom, 2.0f * far_ * near_ / denom,
113  0.0f, 0.0f, -1.0f, 0.0f);
114 
115  //{
116  // const float top = tan(0.5f * fov_rad_y) * near_;
117  // const float bottom = -top;
118  // return Frustum(Point2f(aspect * bottom, top), Point2f(aspect * top, bottom), near_, far_);
119  //}
120  }
121 
122  Matrix4f Matrix4f::Frustum(const Point2f &upper_left, const Point2f &lower_right, const float &near_, const float &far_) {
123  const float &left = upper_left.x;
124  const float &right = lower_right.x;
125  const float &top = upper_left.y;
126  const float &bottom = lower_right.y;
127 
128  const float denom_x = right - left;
129  const float denom_y = top - bottom;
130  const float denom_z = near_ - far_;
131 
132  return Matrix4f(2.0f * near_ / denom_x, 0.0f, (right + left) / denom_x, 0.0f,
133  0.0f, 2.0f * near_ / denom_y, (top + bottom) / denom_y, 0.0f,
134  0.0f, 0.0f, (near_ + far_) / denom_z, 2.0f * near_ * far_ / denom_z,
135  0.0f, 0.0f, -1.0f, 0.0f);
136 
137  //{
138  // const float fov_rad_y = 2.0f * atan(0.5f * denom_y / near_);
139  // const float aspect = denom_x / denom_y;
140  // return Perspective(fov_rad_y, aspect, near_, far_);
141  //}
142  }
143 
145  assert(-1 < index && index < 4);
146  return Matrix4f_Row(const_cast<float *>(&m_matrix[0][0] + index));
147  }
148 
150  assert(-1 < index && index < 4);
151  return Matrix4f_Row(&m_matrix[0][0] + index);
152  }
153 
156 
157  const float
158  *mp = reinterpret_cast<const float *>(m_matrix),
159  *rp = reinterpret_cast<const float *>(rhs.m_matrix);
160 
161  for(float *lp = reinterpret_cast<float *>(matrix.m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp, ++mp, ++rp)
162  *lp = *mp + *rp;
163 
164  return matrix;
165  }
166 
169 
170  const float
171  *mp = reinterpret_cast<const float *>(m_matrix),
172  *rp = reinterpret_cast<const float *>(rhs.m_matrix);
173 
174  for(float *lp = reinterpret_cast<float *>(matrix.m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp, ++mp, ++rp)
175  *lp = *mp - *rp;
176 
177  return matrix;
178  }
179 
182 
183  const float *rp = reinterpret_cast<const float *>(rhs.m_matrix);
184 
185  for(float *lp = reinterpret_cast<float *>(m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp, ++rp)
186  *lp += *rp;
187 
188  return *this;
189  }
190 
193 
194  const float *rp = reinterpret_cast<const float *>(rhs.m_matrix);
195 
196  for(float *lp = reinterpret_cast<float *>(m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp, ++rp)
197  *lp -= *rp;
198 
199  return *this;
200  }
201 
204 
205  for(int i = 0; i < 4; ++i)
206  for(int j = 0; j < 4; ++j)
207  matrix.m_matrix[j][i] =
208  m_matrix[0][i] * rhs.m_matrix[j][0] +
209  m_matrix[1][i] * rhs.m_matrix[j][1] +
210  m_matrix[2][i] * rhs.m_matrix[j][2] +
211  m_matrix[3][i] * rhs.m_matrix[j][3];
212 
213  return matrix;
214  }
215 
217  return *this = *this * rhs;
218  }
219 
221  return *this * rhs.inverted();
222  }
223 
225  return *this *= rhs.inverted();
226  }
227 
228  Matrix4f Matrix4f::operator*(const float &rhs) const {
230 
231  const float *mp = reinterpret_cast<const float *>(m_matrix);
232 
233  for(float *lp = reinterpret_cast<float *>(matrix.m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp, ++mp)
234  *lp = *mp * rhs;
235 
236  return matrix;
237  }
238 
239  Matrix4f Matrix4f::operator/(const float &rhs) const {
241 
242  const float *mp = reinterpret_cast<const float *>(m_matrix);
243 
244  for(float *lp = reinterpret_cast<float *>(matrix.m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp, ++mp)
245  *lp = *mp / rhs;
246 
247  return matrix;
248  }
249 
250  Matrix4f & Matrix4f::operator*=(const float &rhs) {
251  for(float *lp = reinterpret_cast<float *>(m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp)
252  *lp *= rhs;
253 
254  return *this;
255  }
256 
257  Matrix4f & Matrix4f::operator/=(const float &rhs) {
258  for(float *lp = reinterpret_cast<float *>(m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp)
259  *lp /= rhs;
260 
261  return *this;
262  }
263 
266 
267  const float *mp = reinterpret_cast<const float *>(m_matrix);
268 
269  for(float *lp = reinterpret_cast<float *>(matrix.m_matrix), *lp_end = lp + 16; lp != lp_end; ++lp, ++mp)
270  *lp = *mp * -1;
271 
272  return matrix;
273  }
274 
276  assert(-1 < column && column < 3);
277  return Vector3f(m_matrix[column][0], m_matrix[column][1], m_matrix[column][2]);
278  }
279 
280  Vector3f Matrix4f::get_row(const int &row) const {
281  assert(-1 < row && row < 3);
282  return Vector3f(m_matrix[0][row], m_matrix[1][row], m_matrix[2][row]);
283  }
284 
285 }
286 
287 #include <Zeni/Coordinate.hxx>
288 #include <Zeni/Quaternion.hxx>
289 
290 #endif
#define cotangent(angle)
GLint GLint bottom
Definition: glew.h:7291
GLdouble s
Definition: glew.h:1376
Matrix4f operator/(const Matrix4f &rhs) const
Get the product with the inverse.
Definition: Matrix4f.hxx:220
GLint left
Definition: glew.h:7291
GLclampf f
Definition: glew.h:3390
GLint GLenum GLboolean normalized
Definition: glew.h:1891
int32_t j
Definition: e_log.c:102
Matrix4f get_matrix() const
Get the matrix form of the rotation in row-major order.
Definition: Quaternion.hxx:199
#define assert(x)
Definition: SDL_malloc.c:1234
Matrix4f & operator+=(const Matrix4f &rhs)
Set equal to the sum.
Definition: Matrix4f.hxx:180
Matrix4f operator/=(const Matrix4f &rhs)
Set equal to the product with the inverse.
Definition: Matrix4f.hxx:224
static Matrix4f Frustum(const Point2f &upper_left, const Point2f &lower_right, const float &near, const float &far)
Similar to glFrustum.
Definition: Matrix4f.hxx:122
A 3D Point represented with floats.
Definition: Coordinate.h:133
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
GLenum GLenum GLvoid * row
Definition: glew.h:4447
ALuint u
Definition: alMain.h:58
Matrix4f operator-() const
Get the negation.
Definition: Matrix4f.hxx:264
Matrix4f_Row(float *const &row_)
Definition: Matrix4f.hxx:33
Matrix4f operator+(const Matrix4f &rhs) const
Get the sum.
Definition: Matrix4f.hxx:154
A Featureful Quaternion Class.
Definition: Quaternion.h:44
static Matrix4f Orthographic(const float &left, const float &right, const float &bottom, const float &top, const float &near, const float &far)
Similar to gluOrtho.
Definition: Matrix4f.hxx:89
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:4447
Vector3f get_column(const int &column) const
Get a column of the upper-left 3x3 matrix.
Definition: Matrix4f.hxx:275
GLuint index
Definition: glew.h:1800
Matrix4f operator*(const Matrix4f &rhs) const
Get the product.
Definition: Matrix4f.hxx:202
GLfloat GLfloat GLfloat top
Definition: glew.h:13816
GLuint GLenum matrix
Definition: glew.h:13408
static Matrix4f Translate(const Vector3f &translation_factor)
Definition: Matrix4f.hxx:70
static Matrix4f View(const Point3f &position, const Vector3f &forward, const Vector3f &up)
Similar to gluLookAt.
Definition: Matrix4f.hxx:77
GLdouble l
Definition: glew.h:8383
Matrix4f operator*=(const Matrix4f &rhs)
Get the product.
Definition: Matrix4f.hxx:216
static Matrix4f Identity()
Definition: Matrix4f.hxx:52
Matrix4f inverted() const
Get the inverse.
Definition: Matrix4f.cpp:98
const Matrix4f_Row operator[](const int &index) const
Get row &#39;index&#39;.
Definition: Matrix4f.hxx:144
const float & operator[](const int &index) const
Get &#39;index&#39;.
Definition: Matrix4f.hxx:38
static Matrix4f Rotate(const Quaternion &rotation)
Definition: Matrix4f.hxx:66
A Featureful 4-Space Matrix Class.
Definition: Matrix4f.h:47
int i
Definition: pngrutil.c:1377
static Matrix4f Perspective(const float &fov_rad_y, const float &aspect, const float &near, const float &far)
Similar to gluPerspective.
Definition: Matrix4f.hxx:104
GLfloat right
Definition: glew.h:13816
Vector3f get_row(const int &row) const
Get a row of the upper left 3x3 matrix.
Definition: Matrix4f.hxx:280
Matrix4f & operator-=(const Matrix4f &rhs)
Set equal to the difference.
Definition: Matrix4f.hxx:191
A 2D Point represented with floats.
Definition: Coordinate.h:98
Vector3f normalized() const
Get the normalized vector.
Definition: Vector3f.cpp:58
static Matrix4f Scale(const Vector3f &scaling_factor)
Definition: Matrix4f.hxx:59
static Matrix4f Zero()
Definition: Matrix4f.hxx:48