From b5b9fdb6803f5c5c70a39bd1abe65c6b651886a9 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 28 Apr 2012 22:56:07 +0200 Subject: Structs continued --- src/math/func.h | 2 +- src/math/matrix.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++-------- src/math/point.h | 47 ++++++++++++++++++++++++++++++++- src/math/vector.h | 16 ++++++++++++ 4 files changed, 130 insertions(+), 13 deletions(-) (limited to 'src/math') diff --git a/src/math/func.h b/src/math/func.h index 30ba2ae..d9a3c59 100644 --- a/src/math/func.h +++ b/src/math/func.h @@ -89,7 +89,7 @@ inline float Norm(float a) //! Returns the absolute value inline float Abs(float a) { - return (float)fabs(a); + return (float)std::fabs(a); } //! Swaps two integers diff --git a/src/math/matrix.h b/src/math/matrix.h index 3dd95e3..864789e 100644 --- a/src/math/matrix.h +++ b/src/math/matrix.h @@ -35,6 +35,8 @@ namespace Math All methods are made inline to maximize optimization. + TODO test + **/ struct Matrix { @@ -164,43 +166,97 @@ struct Matrix } } - inline void LoadViewMatrix(const Vector &from, const Vector &at, const Vector &up) + //! Loads view matrix from the given vectors + /** \a from origin + \a at direction + \a up up vector */ + inline void LoadView(const Vector &from, const Vector &at, const Vector &up) { - // TODO + // Get the z basis vector, which points straight ahead. This is the + // difference from the eyepoint to the lookat point. + Vector view = at - from; + + FLOAT length = view.Length(); + if( IsZero(length) ) + return; + + // Normalize the z basis vector + view /= length; + + // Get the dot product, and calculate the projection of the z basis + // vector onto the up vector. The projection is the y basis vector. + float dotProduct = DotProduct(worldUp, view); + + Vector up = worldUp - dotProduct * view; + + // If this vector has near-zero length because the input specified a + // bogus up vector, let's try a default up vector + if ( IsZero(length = up.Length()) ) + { + up = Vector(0.0f, 1.0f, 0.0f) - view.y * view; + + // If we still have near-zero length, resort to a different axis. + if ( IsZero(length = up.Length()) ) + { + up = Vector(0.0f, 0.0f, 1.0f) - view.z * view; + + if ( IsZero(up.Length()) ) + return; + } + } + + // Normalize the y basis vector + up /= length; + + // The x basis vector is found simply with the cross product of the y + // and z basis vectors + Vector right = CrossProduct(up, view); + + // Start building the matrix. The first three rows contains the basis + // vectors used to rotate the view to point at the lookat point + LoadIdentity(); + m[0 ] = right.x; m[1 ] = up.x; m[2 ] = view.x; + m[4 ] = right.y; m[5 ] = up.y; m[6 ] = view.y; + m[8 ] = right.z; m[9 ] = up.z; m[10] = view.z; + + // Do the translation values (rotations are still about the eyepoint) + m[3 ] = - DotProduct(from, right); + m[7 ] = - DotProduct(from, up); + m[11] = - DotProduct(from, view); } - inline void LoadProjectionMatrix(float fov = 1.570795f, float aspect = 1.0f, - float nearPlane = 1.0f, float farPlane = 1000.0f) + inline void LoadProjection(float fov = 1.570795f, float aspect = 1.0f, + float nearPlane = 1.0f, float farPlane = 1000.0f) { // TODO } - inline void LoadTranslateMatrix(const Vector &trans) + inline void LoadTranslation(const Vector &trans) { // TODO } - inline void LoadScaleMatrix(const Vector &scale) + inline void LoadScale(const Vector &scale) { // TODO } - inline void LoadRotateXMatrix(float angle) + inline void LoadRotationX(float angle) { // TODO } - inline void LoadRotateYMatrix(float angle) + inline void LoadRotationY(float angle) { // TODO } - inline void LoadRotateZMatrix(float angle) + inline void LoadRotationZ(float angle) { // TODO } - inline void LoadRotationMatrix(const Vector &dir, float angle) + inline void LoadRotation(const Vector &dir, float angle) { // TODO } @@ -217,7 +273,7 @@ struct Matrix } //! Calculates the matrix to make three rotations in the order Z, X and Y - inline void MatRotateZXY(const Vector &angle) + inline void RotateZXY(const Vector &angle) { Matrix temp; temp.SetRotateZMatrix(angle.z); diff --git a/src/math/point.h b/src/math/point.h index 3d42540..f28411e 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -20,6 +20,49 @@ #pragma once +#include + +namespace Math +{ + +/** 2D Point + + Represents a 2D point (x, y). + Contains the required methods for operating on points. + + All methods are made inline to maximize optimization. + + TODO test + + */ +struct Point +{ + //! X coord + float x; + //! Y coord + float y; + + inline Point() + { + LoadZero(); + } + + inline Point(float x, float y) + { + this->x = x; + this->y = y; + } + + inline void LoadZero() + { + x = y = 0.0f; + } + + inline float Length() + { + return std::sqrt(x*x + y*y); + } +}; /* TODO FPOINT RotatePoint(FPOINT center, float angle, FPOINT p); FPOINT RotatePoint(float angle, FPOINT p); @@ -36,4 +79,6 @@ BOOL IsInsideTriangle(FPOINT a, FPOINT b, FPOINT c, FPOINT p); BOOL LineFunction(FPOINT p1, FPOINT p2, float &a, float &b); -*/ \ No newline at end of file +*/ + +}; diff --git a/src/math/vector.h b/src/math/vector.h index e8feca6..d8c5511 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -59,6 +59,8 @@ namespace Math All methods are made inline to maximize optimization. + TODO test + */ struct Vector { @@ -150,4 +152,18 @@ struct Vector } }; +//! Convenience function for calculating dot product +float DotProduct(const Vector &left, const Vector &right) +{ + return left.DotProduct(right); +} + +//! Convenience function for calculating cross product +Vector CrossProduct(const Vector &left, const Vector &right) +{ + Vector result = left; + result.CrossProduct(right); + return result; +} + }; // namespace Math -- cgit v1.2.3-1-g7c22