#include
<oglplus/math/constants.hpp>
This headers implements several functions returning math constants commonly used by the rest of OGLplus.
auto Pi(void); auto TwoPi(void); auto HalfPi(void);
#include
<oglplus/math/angle.hpp>
Angle is a lightweight
class allowing more natural construction and usage of planar angle values.
The storage requirements are the same as for the template parameter type
T
,
but the Angle
template gives the T
type special meaning and implements a set of angle-related member and
friend functions. There are also several associated free functions for
creating new instances of Angle
.
template <typename T> class Angle { public:
Angle(void); Angle(const Angle&); template <typename U> Angle(const Angle<U>& other);
These functions allow to construct instances of Angle
from values in various units.
static Angle Radians(T val_rad); static Angle Degrees(T val_deg); static Angle ArcSin(T x); static Angle ArcCos(T x);
Constructs a new angle from a value in radians. |
|
Constructs a new angle from a value in degrees. |
|
Constructs a new angle as the arc sine of the specified value. |
|
Constructs a new angle as the arc sine of the specified value. |
T Value(void) const; T ValueInDegrees(void) const; T ValueInRightAngles(void) const; T ValueInFullCircles(void) const;
Returns the value of the angle in radians. |
|
Returns the value of the angle in degrees. |
|
Returns the value of the angle in number of right angles. |
|
Returns the value of the angle in number of full circles. |
friend bool operator == (const Angle& a, const Angle& b); friend bool operator != (const Angle& a, const Angle& b); friend bool operator <= (const Angle& a, const Angle& b); friend bool operator < (const Angle& a, const Angle& b); friend bool operator >= (const Angle& a, const Angle& b); friend bool operator > (const Angle& a, const Angle& b);
Operators modifying this
instance of Angle
.
Angle& operator += (const Angle& a); Angle& operator -= (const Angle& a); Angle& operator *= (T c); Angle& operator /= (T c);
Adds angle |
|
Subtracts angle |
|
Multiplies |
|
Divides |
Friend operators returning new instance of Angle
without
modifying the arguments.
friend Angle operator - (const Angle& a); friend Angle operator + (const Angle& a, const Angle& b); friend Angle operator - (const Angle& a, const Angle& b); friend Angle operator * (const Angle& a, T c); friend Angle operator * (T c, const Angle& a); friend Angle operator / (const Angle& a, T c); friend T operator / (const Angle& a, const Angle& b);
Negation. |
|
Addition. |
|
Subtraction. |
|
Multiplication by a constant. |
|
Division by a constant. |
|
Ratio of two angles. |
Angle& operator Add(const Angle& a); Angle& operator Subtract(const Angle& a); Angle& operator Multiply(T c); Angle& operator Divide(T c);
friend Angle Negated(const Angle& a); friend Angle Added(const Angle& a, const Angle& b); friend Angle Subtracted(const Angle& a, const Angle& b); friend Angle Multiplied(const Angle& a, T c); friend Angle Divided(const Angle& a, T c); friend T Ratio(const Angle& a, const Angle& b);
T Sin(void) const; T Cos(void) const; T Tan(void) const; friend inline T Sin(const Angle& a); friend inline T Cos(const Angle& a); friend inline T Tan(const Angle& a);
}; // class Angle
#ifdef GL_FLOAT typedef Angle<GLfloat> Anglef; Angle<GLfloat> Radians(GLfloat value); Angle<GLfloat> Degrees(GLfloat value); Angle<GLfloat> FullCircles(GLfloat value); Angle<GLfloat> FullCircle(void); Angle<GLfloat> RightAngles(GLfloat value); Angle<GLfloat> RightAngle(void); Angle<GLfloat> ArcSin(GLfloat x); Angle<GLfloat> ArcCos(GLfloat x); Angle<GLfloat> ArcTan(GLfloat x); #endif // GL_FLOAT template <typename T> T SineWave(T t); template <typename T> T SineWave01(T t); template <typename T> T CosineWave(T t); template <typename T> T CosineWave01(T t);
Constructs a new angle from a value in radians. |
|
Constructs a new angle from a value in degrees. |
|
Constructs a new angle from a number of full turns. |
|
Constructs a new angle with a value of |
|
Constructs a new angle with a value of |
|
Constructs a new angle as the arc-sine of the specified value. |
|
Constructs a new angle as the arc-cosine of the specified value. |
|
Constructs a new angle as the arc-tangent of the specified value. |
|
Returns a value on a sine wave at the specified point. This function
returns the value of |
|
Returns a value on a sine wave transformed to range <0, 1>
This function returns the value of |
|
Returns a value on a cosine wave at the specified point. This function
returns the value of |
|
Returns a value on a cosine wave transformed to range <0, 1>
This function returns the value of |
#include
<oglplus/math/vector.hpp>
The Vector
class is used by OGLplus as the native
template for (degenerate) 1-D and 2-D,
3-D and 4-D vector types. Values
of Vector
are mostly used to specify GLSL vec2
,
vec3
, vec4
uniform values, but
Vector
is powerful enough to be used as a general-purpose
N-dimensional space vector type in many other situations and is used
ubiquitously in the oglplus examples.
The Vector
class is a template with a pair of parameters;
T
and N
denoting the underlying data-type and the dimension of the vector. The
actual class is implemented as a set of template specializations which
have many common member and friend functions, but there are also some
differences between vectors of different dimensions.
The following unspecialized template is a synopsis of the interface common
to all instantiations of Vector
. The differences to
the specializations are noted below.
template <typename T, size_t N> class Vector { public:
Vector(const Vector&); Vector(void); Vector(const T (&v)[N]); Vector(const T* v, size_t n); Vector(const T* v, size_t n, T def); explicit Vector(T v); template <typename ... P> Vector(P ... p); template <typename U, size_t M> Vector(const Vector<U, M>& v) template <typename U, size_t M, typename ... P> Vector(const Vector<U, M>& v, P ... p); struct Unit_ { }; Vector(Unit_, size_t i); static Vector Unit(size_t i); explicit Vector(const Matrix<T, 1, N>& matrix); explicit Vector(const Matrix<T, N, 1>& matrix);
Vectors are copy-constructible. |
|
Constructs a vector with all components initialized to zero. |
|
Construction from a const C-array of |
|
Construction from a pointer to data and number of elements. The memory
pointed-to by |
|
Construction from a pointer to data, number of elements and a default.
The first |
|
Constructs a vector having all coordinates initialized to the specified
value |
|
Constructs a vector having its components initialized by the values
|
|
Truncation of a vector of higher or equal dimension possibly with
a different underlying type. The resulting |
|
Construction from a vector of smaller dimension and a pack of values.
The resulting vector has the first M components initialized from
the vector |
|
Helper structure for dispatching the following constructor. |
|
Constructs a vector with all components set to zero except for the
|
|
Returns a vector with all components set to zero except for the
|
|
Construction from a single row Matrix. |
|
Construction from a single column Matrix. |
T x(void) const; T y(void) const; T z(void) const; T w(void) const; Vector<T, 2> xy(void) const; Vector<T, 3> xyz(void) const;
Returns the |
|
Returns the |
|
Returns the |
|
Returns the |
|
Returns a subvector with the first two components. This function
is available only if |
|
Returns a subvector with the first three components. This function
is available only if |
Member operators modifying this
.
Vector& operator += (const Vector& a); Vector& operator -= (const Vector& a); Vector& operator *= (T c); Vector& operator *= (const Vector& a); Vector& operator /= (T c);
Friend operators returning new vectors without modifying the arguments.
friend Vector operator == (const Vector& a, const Vector& b); friend Vector operator != (const Vector& a, const Vector& b); friend Vector operator + (const Vector& a); friend Vector operator - (const Vector& a); friend Vector operator + (const Vector& a, const Vector& b); friend Vector operator - (const Vector& a, const Vector& b); friend Vector operator * (const Vector& a, T c); friend Vector operator * (T c, const Vector& a); friend Vector operator / (const Vector& a, T c); template <size_t Cols> friend Vector<T, Cols> operator * (const Vector& v, const Matrix<T, N, Cols>& m); template <size_t Rows> friend Vector<T, Rows> operator * (const Matrix<T, Rows, N>& m, const Vector& v);
Vector& operator Add(const Vector& a); Vector& operator Subtract(const Vector& a); Vector& operator Multiply(T c); Vector& operator Divide(T c);
friend bool Equal(const Vector& a, const Vector& b); friend Vector Negated(const Vector& a); friend Vector Added(const Vector& a, const Vector& b); friend Vector Subtracted(const Vector& a, const Vector& b); friend Vector Multiplied(const Vector& a, T c); friend Vector Divided(const Vector& a, T c); friend T Dot(const Vector& a, const Vector& b); friend T Length(const Vector& a); friend T Distance(const Vector& a, const Vector& b); friend Vector Normalized(Vector a);
Dot product of two vectors. |
|
The length (magnitude) of a vector. |
|
The distance between two vectors. |
|
Returns a normalized vector pointing in the same direction as the vector passed as argument. |
}; // class Vector
template <typename T> Vector<T, 2> Perpendicular(const Vector<T, 2>& a); template <typename T> Vector<T, 3> Cross(const Vector<T, 3>& a, const Vector<T, 3>& b);
Returns a vector perpendicular to the given vector. Available only for 2-dimensional vectors. |
|
Returns a vector perpendicular to the given two vectors. Available only for 3-dimensional vectors. |
This header also defines several 1 to 4-dimensional Vector
typedef
s for convenience.
#ifdef GL_INT typedef Vector<GLint, 1> Vec1i; typedef Vector<GLint, 2> Vec2i; typedef Vector<GLint, 3> Vec3i; typedef Vector<GLint, 4> Vec4i; #endif #ifdef GL_FLOAT typedef Vector<GLfloat, 1> Vec1f; typedef Vector<GLfloat, 2> Vec2f; typedef Vector<GLfloat, 3> Vec3f; typedef Vector<GLfloat, 4> Vec4f; #endif #ifdef GL_DOUBLE typedef Vector<GLdouble, 1> Vec1d; typedef Vector<GLdouble, 2> Vec2d; typedef Vector<GLdouble, 3> Vec3d; typedef Vector<GLdouble, 4> Vec4d; #endif
#include
<oglplus/math/matrix.hpp>
template <typename T, size_t Rows, size_t Cols> class Matrix { public:
Matrix(const Matrix&); Matrix(void); explicit Matrix(const T (&data)[Rows*Cols]); Matrix(const T* data, size_t n); template <typename U, size_t R, size_t C> explicit Matrix(const Matrix<U, R, C>& other); template <typename ... P> explicit Matrix(P ... p); template <typename ... C> explicit Matrix(const Vector<T, C> ... row);
Matrices are copy-constructible |
|
Constructs an identity matrix. |
|
Construction from a const C-array of |
|
Construction from a pointer to data and number of elements. The
memory pointed-to by |
|
Construction from a |
|
Construction from a pack of values. This constructor is available
only if the number of arguments in |
|
Construction from a pack of instances of Vector.
This constructor is only available if the number of vectors specified
is equal to the number of |
friend bool operator == (const Matrix& a, const Matrix& b); friend bool operator != (const Matrix& a, const Matrix& b);
void Fill(T value); const T* Data(void) const; size_t Size(void) const; T At(size_t i, size_t j) const; void Set(size_t i, size_t j, T v); Vector<T, Cols> Row(size_t i) const; Vector<T, Rows> Col(size_t j) const; template <size_t I, size_t J, size_t R, size_t C> Matrix<T, R, C> Submatrix(void) const
Fills all elements of the |
|
Returns a pointer to the matrix elements in row major order. |
|
Returns the number of elements of the matrix. |
|
Returns the value at the position specified by |
|
Sets the value of the element at the position specified by |
|
Returns the |
|
Returns the |
|
Returns a submatrix of |
friend const T* Data(const Matrix& matrix); friend size_t Size(const Matrix& matrix); friend size_t Rows(const Matrix& matrix); friend size_t Cols(const Matrix& matrix); friend bool Equal(const Matrix& a, const Matrix& b); friend Matrix Negated(const Matrix& a); friend Matrix Added(const Matrix& a, const Matrix& b); friend Matrix Subtracted(const Matrix& a, const Matrix& b); friend Matrix Multiplied(const Matrix& a, T c); template <size_t N> friend Matrix Multiplied( const Matrix<T, Rows, N>& a, const Matrix<T, N, Cols>& b ); friend Matrix Transposed(const Matrix<T, Cols, Rows>& a); friend Matrix Inverse(Matrix m); template <size_t I, size_t J, size_t R, size_t C> friend Matrix<T, R, C> Submatrix(const Matrix& a) const; friend Matrix<T, 2, 2> Sub2x2(const Matrix& a); friend Matrix<T, 3, 3> Sub3x3(const Matrix& a);
Returns a pointer to the matrix elements in row major order. |
|
Returns the number of elements of the matrix. |
|
Returns the number of rows of the matrix. |
|
Returns the number of columns of the matrix. |
|
Returns true if the two matrices |
|
Returns a copy of the matrix passed as argument with all elements negated. |
|
Matrix addition. |
|
Matrix subtraction. |
|
Matrix-by-constant multiplication. |
|
Matrix-by-matrix multiplication. |
|
Returns a transposed copy of the matrix specified as the argument. |
|
Returns the inverse for the specified matrix |
|
Returns a submatrix of the specified matrix with |
|
Returns a 2x2 submatrix of the specified matrix. |
|
Returns a 3x3 submatrix of the specified matrix. |
friend void RowSwap(Matrix& m, size_t a, size_t b); friend void RowMultiply(Matrix& m, size_t i, T c); friend void RowAdd(Matrix& m, size_t a, size_t b, T c); template <size_t C> friend bool Gauss(Matrix& a, Matrix<T, Rows, C>& b); template <std::size_t C> friend bool GaussJordan(Matrix& a, Matrix<T, Rows, C>& b);
Swaps the |
|
Multiplies the |
|
Adds the row |
|
Performs Gaussian
elimination on the specified pair of matrices. Returns
|
|
Performs Gauss-Jordan elimination on the specified pair of matrices.
Returns |
}; // class Matrix
#ifdef GL_FLOAT typedef Matrix<GLfloat, 2, 2> Mat2f; typedef Matrix<GLfloat, 2, 3> Mat2x3f; typedef Matrix<GLfloat, 2, 4> Mat2x4f; typedef Matrix<GLfloat, 3, 2> Mat3x2f; typedef Matrix<GLfloat, 3, 3> Mat3f; typedef Matrix<GLfloat, 3, 4> Mat3x4f; typedef Matrix<GLfloat, 4, 2> Mat4x2f; typedef Matrix<GLfloat, 4, 3> Mat4x3f; typedef Matrix<GLfloat, 4, 4> Mat4f; #endif #ifdef GL_DOUBLE typedef Matrix<GLdouble, 2, 2> Mat2d; typedef Matrix<GLdouble, 2, 3> Mat2x3d; typedef Matrix<GLdouble, 2, 4> Mat2x4d; typedef Matrix<GLdouble, 3, 2> Mat3x2d; typedef Matrix<GLdouble, 3, 3> Mat3d; typedef Matrix<GLdouble, 3, 4> Mat3x4d; typedef Matrix<GLdouble, 4, 2> Mat4x2d; typedef Matrix<GLdouble, 4, 3> Mat4x3d; typedef Matrix<GLdouble, 4, 4> Mat4d; #endif
template <typename T> class ModelMatrix : public Matrix<T, 4, 4> { public: ModelMatrix(const Matrix<T, 4, 4>& base); ModelMatrix(void); static ModelMatrix Translation(T dx, T dy, T dz); static ModelMatrix Translation(const Vector<T, 3>& dp); static ModelMatrix TranslationX(T dx); static ModelMatrix TranslationY(T dy); static ModelMatrix TranslationZ(T dz); static ModelMatrix Scale(T sx, T sy, T sz); static ModelMatrix Reflection(bool rx, bool ry, bool rz); static ModelMatrix RotationX(Angle<T> angle); static ModelMatrix RotationY(Angle<T> angle); static ModelMatrix RotationZ(Angle<T> angle); static ModelMatrix RotationA(const Vector<T, 3>& axis, Angle<T> angle); }; // ModelMatrix #ifdef GL_FLOAT typedef ModelMatrix<GLfloat> ModelMatrixf; #endif #ifdef GL_DOUBLE typedef ModelMatrix<GLdouble> ModelMatrixd; #endif
Construction from a 4x4 Matrix. |
|
Construction of an identity matrix. |
|
Functions constructing translation matrices with the specified offsets along the X, Y and Z axes. |
|
Function constructing scale matrix with the specified scale factors along the X, Y and Z axes. |
|
Function constructing reflection matrix, with |
|
Functions constructing matrices for rotation by the specified
|
|
Function constructing matrix for rotation by the specified |
template <typename T> class CameraMatrix : public Matrix<T, 4, 4> { public: CameraMatrix(const Matrix<T, 4, 4>& base); static CameraMatrix Perspective( T x_left, T x_right, T y_bottom, T y_top, T z_near, T z_far ); static CameraMatrix PerspectiveX(Angle<T> xfov, T aspect, T z_near, T z_far); static CameraMatrix PerspectiveY(Angle<T> yfov, T aspect, T z_near, T z_far); static CameraMatrix Ortho( T x_left, T x_right, T y_bottom, T y_top, T z_near, T z_far ); static CameraMatrix OrthoX(T width, T aspect, T z_near, T z_far); static CameraMatrix OrthoY(T height, T aspect, T z_near, T z_far); static CameraMatrix ScreenStretch(T x_left, T x_right, T y_bottom, T y_top); static CameraMatrix ScreenTile(unsigned x, unsigned y, unsigned nx, unsigned ny); static CameraMatrix LookingAt( const Vector<T, 3>& eye, const Vector<T, 3>& target ); static CameraMatrix Orbiting( const Vector<T, 3>& target, T radius, Angle<T> azimuth, Angle<T> elevation ); static CameraMatrix Pitch(Angle<T> angle); static CameraMatrix Yaw(Angle<T> angle); static CameraMatrix Roll(Angle<T> angle); Vector<T, 3> Position(void) const; Vector<T, 3> Direction(void) const; }; // CameraMatrix #ifdef GL_FLOAT typedef CameraMatrix<GLfloat> CamMatrixf; #endif #ifdef GL_DOUBLE typedef CameraMatrix<GLdouble> CamMatrixd; #endif
Creates a new perspective projection matrix with specified parameters. |
|
Creates a new perspective matrix from x-axis |
|
Creates a new perspective matrix from y-axis |
|
Creates an orthographic projection matrix with the specified parameters. |
|
Creates a new orthographic matrix from x-axis |
|
Creates a new orthographic matrix from y-axis |
|
Constructs a matrix that can be used to stretch the normalized device coordinates after projection is applied. |
|
Constructs a matrix, that divides the screen into rectangular tiles
with the specified numbers of divisions |
|
Constructs a matrix for a camera placed at |
|
Constructs a matrix for a camera orbiting the |
|
Constructs an X-axis rotation (Pitch/Elevation) matrix. The initial
heading is the negative Z-axis, top is the
Y-axis, right is the X-axis. Positive |
|
Constructs a Y-axis rotation (Heading/Yaw) matrix. The initial
heading is the negative Z-axis, top is the
Y-axis, right is the X-axis. Positive |
|
Constructs a Z-axis rotation (Bank/Roll) matrix. The initial position
is that top is the Y-axis, heading in the
negative Z-axis direction, right is the X-axis.
Positive |
|
The position of and the direction in which the camera represented by this matrix points to. |
#include
<oglplus/math/plane.hpp>
template <typename T> class Plane { public: }; // class Plane