Browse Source

Removed convenience functions with "unwrapped" Vector parameters.

With C++11, objects can be passed nearly as easy as without these
convenience functions, for example:

    Matrix4::scaling({0.5f, 1.0f, 0.5f});

which is nearly the same as the following, using convenience function:

    Matrix4::scaling(0.5f, 1.0f, 0.5f);

Convenience functions can also be pretty confusing, for example:

    Matrix4::rotation(1.0f, -1.0f, 2.0f, 2.0f);   // wtf?
    Matrix4::rotation(1.0f, {-1.0f, 2.0f, 2.0f}); // ah, okay!

There are also a few neat tricks, which cannot be done using convenience
functions, for example:

    Matrix4::translate(Vector3::xAxis(3.0f));  // {3.0f, 0.0f, 0.0f}
    Camera::setClearColor({0.1f, 0.1f, 0.1f}); // default 1.0f for alpha
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
7911c22eff
  1. 16
      src/Camera.cpp
  2. 10
      src/Camera.h
  3. 33
      src/Math/Matrix4.h
  4. 6
      src/Math/Test/Matrix4Test.cpp
  5. 16
      src/Object.h

16
src/Camera.cpp

@ -29,10 +29,10 @@ void Camera::setOrthographic(GLfloat size, GLfloat near, GLfloat far) {
/* Scale the volume down so it fits in (-1, 1) in all directions */
GLfloat xyScale = 2/size;
GLfloat zScale = 2/(far-near);
rawProjectionMatrix = Matrix4::scaling(xyScale, xyScale, -zScale);
rawProjectionMatrix = Matrix4::scaling({xyScale, xyScale, -zScale});
/* Move the volume on z into (-1, 1) range */
rawProjectionMatrix = Matrix4::translation(0, 0, -1-near*zScale)*rawProjectionMatrix;
rawProjectionMatrix = Matrix4::translation(Vector3::zAxis(-1-near*zScale))*rawProjectionMatrix;
fixAspectRatio();
}
@ -42,7 +42,7 @@ void Camera::setPerspective(GLfloat fov, GLfloat near, GLfloat far) {
_far = far;
/* First move the volume on z in (-1, 1) range */
rawProjectionMatrix = Matrix4::translation(0, 0, 2*far*near/(far+near));
rawProjectionMatrix = Matrix4::translation(Vector3::zAxis(2*far*near/(far+near)));
/* Then apply magic perspective matrix (with reversed Z) */
static GLfloat a[] = { 1, 0, 0, 0,
@ -54,7 +54,7 @@ void Camera::setPerspective(GLfloat fov, GLfloat near, GLfloat far) {
/* Then scale the volume down so it fits in (-1, 1) in all directions */
GLfloat xyScale = 1/tan(fov/2);
GLfloat zScale = 1+2*near/(far-near);
rawProjectionMatrix = Matrix4::scaling(xyScale, xyScale, zScale)*rawProjectionMatrix;
rawProjectionMatrix = Matrix4::scaling({xyScale, xyScale, zScale})*rawProjectionMatrix;
/* And... another magic */
rawProjectionMatrix.set(3, 3, 0);
@ -85,15 +85,15 @@ void Camera::fixAspectRatio() {
/* Extend on larger side = scale larger side down */
if(_aspectRatioPolicy == Extend) {
_projectionMatrix = ((_viewport.x() > _viewport.y()) ?
Matrix4::scaling(static_cast<GLfloat>(_viewport.y())/_viewport.x(), 1, 1) :
Matrix4::scaling(1, static_cast<GLfloat>(_viewport.x())/_viewport.y(), 1)
Matrix4::scaling({static_cast<GLfloat>(_viewport.y())/_viewport.x(), 1, 1}) :
Matrix4::scaling({1, static_cast<GLfloat>(_viewport.x())/_viewport.y(), 1})
)*rawProjectionMatrix;
/* Clip on smaller side = scale smaller side up */
} else if(_aspectRatioPolicy == Clip) {
_projectionMatrix = ((_viewport.x() > _viewport.y()) ?
Matrix4::scaling(1, static_cast<GLfloat>(_viewport.x())/_viewport.y(), 1) :
Matrix4::scaling(static_cast<GLfloat>(_viewport.y())/_viewport.x(), 1, 1)
Matrix4::scaling({1, static_cast<GLfloat>(_viewport.x())/_viewport.y(), 1}) :
Matrix4::scaling({static_cast<GLfloat>(_viewport.y())/_viewport.x(), 1, 1})
)*rawProjectionMatrix;
/* Don't preserve anything */

10
src/Camera.h

@ -113,22 +113,12 @@ class MAGNUM_EXPORT Camera: public Object {
*/
virtual void setViewport(const Math::Vector2<GLsizei>& size);
/** @copydoc setViewport(const Math::Vector2<GLsizei>& size); */
inline void setViewport(GLsizei width, GLsizei height) {
setViewport({width, height});
}
/** @brief Clear color */
inline Vector4 clearColor() const { return _clearColor; }
/** @brief Set clear color */
void setClearColor(const Vector4& color);
/** @copydoc setClearColor(const Vector4&) */
inline void setClearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) {
setClearColor(Vector4(r, g, b, a));
}
/**
* @brief Draw the scene
*

33
src/Math/Matrix4.h

@ -27,24 +27,12 @@ namespace Magnum { namespace Math {
/**
* @brief 4x4 matrix
*
* @todo @c PERFORMANCE - make (T,T,T) - (Vec3&lt;T&gt;) alternatives calling
* direction based on statistics, which is used more frequently
* @todo Rotation with Euler angles
* @todo Shearing
* @todo Reflection
*/
template<class T> class Matrix4: public Matrix<T, 4> {
public:
/**
* @brief Translation matrix
* @param x Translation along X
* @param y Translation along Y
* @param z Translation along Z
*/
inline static Matrix4 translation(T x, T y, T z) {
return translation(Vector3<T>(x, y, z));
}
/**
* @brief Translation matrix
* @param vec Translation vector
@ -57,16 +45,6 @@ template<class T> class Matrix4: public Matrix<T, 4> {
return out;
}
/**
* @brief Scaling matrix
* @param x Scaling along X
* @param y Scaling along Y
* @param z Scaling along Z
*/
inline static Matrix4 scaling(T x, T y, T z) {
return scaling(Vector3<T>(x, y, z));
}
/**
* @brief Scaling matrix
* @param vec Scaling vector
@ -79,17 +57,6 @@ template<class T> class Matrix4: public Matrix<T, 4> {
return out;
}
/**
* @brief Rotation matrix
* @param angle Rotation angle (counterclockwise, in radians)
* @param x Rotation axis X coordinates
* @param y Rotation axis Y coordinates
* @param z Rotation axis Z coordinates
*/
inline static Matrix4 rotation(T angle, T x, T y, T z) {
return rotation(angle, Vector3<T>(x, y, z));
}
/**
* @brief Rotation matrix
* @param angle Rotation angle (counterclockwise, in radians)

6
src/Math/Test/Matrix4Test.cpp

@ -38,7 +38,7 @@ void Matrix4Test::translation() {
3.0f, 1.0f, 2.0f, 1.0f
};
QVERIFY(Matrix4::translation(3.0f, 1.0f, 2.0f) == Matrix4(matrix));
QVERIFY(Matrix4::translation({3.0f, 1.0f, 2.0f}) == Matrix4(matrix));
}
void Matrix4Test::scaling() {
@ -49,7 +49,7 @@ void Matrix4Test::scaling() {
0.0f, 0.0f, 0.0f, 1.0f
};
QVERIFY(Matrix4::scaling(3.0f, 1.5f, 2.0f) == Matrix4(matrix));
QVERIFY(Matrix4::scaling({3.0f, 1.5f, 2.0f}) == Matrix4(matrix));
}
void Matrix4Test::rotation() {
@ -60,7 +60,7 @@ void Matrix4Test::rotation() {
0.0f, 0.0f, 0.0f, 1.0f
};
QVERIFY(Matrix4::rotation(deg(-74.0f), -1.0f, 2.0f, 2.0f) == Matrix4(matrix));
QVERIFY(Matrix4::rotation(deg(-74.0f), {-1.0f, 2.0f, 2.0f}) == Matrix4(matrix));
}
void Matrix4Test::debug() {

16
src/Object.h

@ -134,11 +134,6 @@ class MAGNUM_EXPORT Object {
multiplyTransformation(Matrix4::translation(vec), global);
}
/** @copydoc translate(Vector3, bool) */
inline void translate(GLfloat x, GLfloat y, GLfloat z, bool global = true) {
translate(Vector3(x, y, z), global);
}
/**
* @brief Scale object
*
@ -148,15 +143,11 @@ class MAGNUM_EXPORT Object {
multiplyTransformation(Matrix4::scaling(vec), global);
}
/** @copydoc scale(Vector3, bool) */
inline void scale(GLfloat x, GLfloat y, GLfloat z, bool global = true) {
scale(Vector3(x, y, z), global);
}
/**
* @copydoc scale(Vector3, bool)
*
* Scales the object proportionally in all dimensions.
* @todo Make this functionality in Math::Vector3?
*/
inline void scale(GLfloat xyz, bool global = true) {
scale({xyz, xyz, xyz}, global);
@ -171,11 +162,6 @@ class MAGNUM_EXPORT Object {
multiplyTransformation(Matrix4::rotation(angle, vec), global);
}
/** @copydoc rotate(GLfloat, Vector3, bool) */
inline void rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z, bool global = true) {
rotate(angle, Vector3(x, y, z), global);
}
/** @{ @name Caching helpers
*
* If the object transformation is used many times when drawing (such

Loading…
Cancel
Save