Browse Source

secure pi constant

Kajetan Johannes Hammerle 2 years ago
parent
commit
ed8d223859
5 changed files with 53 additions and 22 deletions
  1. 30 16
      math/Frustum.cpp
  2. 8 0
      math/MathConstants.h
  3. 11 5
      math/Matrix.cpp
  4. 1 0
      math/Quaternion.cpp
  5. 3 1
      math/Vector.cpp

+ 30 - 16
math/Frustum.cpp

@@ -1,7 +1,10 @@
 #include "math/Frustum.h"
+#include "math/MathConstants.h"
 
-Frustum::Frustum(float fieldOfView, float nearClip, float farClip, const Size& size)
-    : size(size), fieldOfView(fieldOfView), nearClip(nearClip), farClip(farClip) {
+Frustum::Frustum(float fieldOfView, float nearClip, float farClip,
+                 const Size& size)
+    : size(size), fieldOfView(fieldOfView), nearClip(nearClip),
+      farClip(farClip) {
 }
 
 Matrix& Frustum::updateProjection() {
@@ -12,12 +15,14 @@ Matrix& Frustum::updateProjection() {
 
     projection.set(0, Vector4(q / aspect, 0.0f, 0.0f, 0.0f));
     projection.set(1, Vector4(0.0f, q, 0.0f, 0.0f));
-    projection.set(2, Vector4(0.0f, 0.0f, (nearClip + farClip) * diff, (2.0f * nearClip * farClip) * diff));
+    projection.set(2, Vector4(0.0f, 0.0f, (nearClip + farClip) * diff,
+                              (2.0f * nearClip * farClip) * diff));
     projection.set(3, Vector4(0.0f, 0.0f, -1.0f, 0.0f));
     return projection;
 }
 
-void Frustum::updatePlanes(const Vector3& pos, const Vector3& right, const Vector3& up, const Vector3& front) {
+void Frustum::updatePlanes(const Vector3& pos, const Vector3& right,
+                           const Vector3& up, const Vector3& front) {
     float tan = tanf(fieldOfView * (0.5f * M_PI / 180.0f));
     float aspect = static_cast<float>(size.width) / size.height;
 
@@ -28,21 +33,30 @@ void Frustum::updatePlanes(const Vector3& pos, const Vector3& right, const Vecto
     float halfFarWidth = halfFarHeight * aspect;
 
     Vector3 farCenter = pos + front * farClip;
-    Vector3 farTopLeft = farCenter + (up * halfFarHeight) - (right * halfFarWidth);
-    Vector3 farTopRight = farCenter + (up * halfFarHeight) + (right * halfFarWidth);
-    Vector3 farBottomRight = farCenter - (up * halfFarHeight) + (right * halfFarWidth);
+    Vector3 farTopLeft =
+        farCenter + (up * halfFarHeight) - (right * halfFarWidth);
+    Vector3 farTopRight =
+        farCenter + (up * halfFarHeight) + (right * halfFarWidth);
+    Vector3 farBottomRight =
+        farCenter - (up * halfFarHeight) + (right * halfFarWidth);
 
     Vector3 nearCenter = pos + front * nearClip;
-    Vector3 nearTopLeft = nearCenter + (up * halfNearHeight) - (right * halfNearWidth);
-    Vector3 nearBottomLeft = nearCenter - (up * halfNearHeight) - (right * halfNearWidth);
-    Vector3 nearBottomRight = nearCenter - (up * halfNearHeight) + (right * halfNearWidth);
+    Vector3 nearTopLeft =
+        nearCenter + (up * halfNearHeight) - (right * halfNearWidth);
+    Vector3 nearBottomLeft =
+        nearCenter - (up * halfNearHeight) - (right * halfNearWidth);
+    Vector3 nearBottomRight =
+        nearCenter - (up * halfNearHeight) + (right * halfNearWidth);
 
-    planes[0] = Plane(nearBottomRight, nearTopLeft, nearBottomLeft);    // near plane
-    planes[1] = Plane(farTopRight, farBottomRight, farTopLeft);         // far plane
-    planes[2] = Plane(nearBottomRight, nearBottomLeft, farBottomRight); // bottom plane
-    planes[3] = Plane(farTopLeft, nearTopLeft, farTopRight);            // top plane
-    planes[4] = Plane(nearBottomLeft, nearTopLeft, farTopLeft);         // left plane
-    planes[5] = Plane(farBottomRight, farTopRight, nearBottomRight);    // right plane
+    planes[0] =
+        Plane(nearBottomRight, nearTopLeft, nearBottomLeft);    // near plane
+    planes[1] = Plane(farTopRight, farBottomRight, farTopLeft); // far plane
+    planes[2] =
+        Plane(nearBottomRight, nearBottomLeft, farBottomRight); // bottom plane
+    planes[3] = Plane(farTopLeft, nearTopLeft, farTopRight);    // top plane
+    planes[4] = Plane(nearBottomLeft, nearTopLeft, farTopLeft); // left plane
+    planes[5] =
+        Plane(farBottomRight, farTopRight, nearBottomRight); // right plane
 }
 
 bool Frustum::isInside(const Vector3& pos) const {

+ 8 - 0
math/MathConstants.h

@@ -0,0 +1,8 @@
+#ifndef MATH_CONSTANTS_H
+#define MATH_CONSTANTS_H
+
+#ifndef M_PI
+#define M_PI 3.14159265358979323846
+#endif
+
+#endif

+ 11 - 5
math/Matrix.cpp

@@ -1,5 +1,6 @@
 #include <cmath>
 
+#include "math/MathConstants.h"
 #include "math/Matrix.h"
 
 Matrix::Matrix() {
@@ -29,10 +30,14 @@ const float* Matrix::getValues() const {
 }
 
 Matrix& Matrix::operator*=(const Matrix& m) {
-    data[0] = data[0][0] * m.data[0] + data[0][1] * m.data[1] + data[0][2] * m.data[2] + data[0][3] * m.data[3];
-    data[1] = data[1][0] * m.data[0] + data[1][1] * m.data[1] + data[1][2] * m.data[2] + data[1][3] * m.data[3];
-    data[2] = data[2][0] * m.data[0] + data[2][1] * m.data[1] + data[2][2] * m.data[2] + data[2][3] * m.data[3];
-    data[3] = data[3][0] * m.data[0] + data[3][1] * m.data[1] + data[3][2] * m.data[2] + data[3][3] * m.data[3];
+    data[0] = data[0][0] * m.data[0] + data[0][1] * m.data[1] +
+              data[0][2] * m.data[2] + data[0][3] * m.data[3];
+    data[1] = data[1][0] * m.data[0] + data[1][1] * m.data[1] +
+              data[1][2] * m.data[2] + data[1][3] * m.data[3];
+    data[2] = data[2][0] * m.data[0] + data[2][1] * m.data[1] +
+              data[2][2] * m.data[2] + data[2][3] * m.data[3];
+    data[3] = data[3][0] * m.data[0] + data[3][1] * m.data[1] +
+              data[3][2] * m.data[2] + data[3][3] * m.data[3];
     return *this;
 }
 
@@ -44,7 +49,8 @@ Matrix Matrix::operator*(const Matrix& other) const {
 
 Vector3 Matrix::operator*(const Vector3& v) const {
     Vector4 v4(v[0], v[1], v[2], 1.0f);
-    return Vector3(data[0].dot(v4), data[1].dot(v4), data[2].dot(v4)) * (1.0f / data[3].dot(v4));
+    return Vector3(data[0].dot(v4), data[1].dot(v4), data[2].dot(v4)) *
+           (1.0f / data[3].dot(v4));
 }
 
 Matrix& Matrix::scale(const Vector3& v) {

+ 1 - 0
math/Quaternion.cpp

@@ -1,5 +1,6 @@
 #include <cmath>
 
+#include "math/MathConstants.h"
 #include "math/Quaternion.h"
 
 Quaternion::Quaternion() : w(1.0f) {

+ 3 - 1
math/Vector.cpp

@@ -1,5 +1,6 @@
 #include <cmath>
 
+#include "math/MathConstants.h"
 #include "math/Vector.h"
 
 template<>
@@ -13,7 +14,8 @@ Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle) {
     float sinLength = sinf(lengthAngle);
     float cosLength = cosf(lengthAngle);
 
-    return *this = Vector<3>(cosWidth * cosLength, sinWidth, -sinLength * cosWidth);
+    return *this =
+               Vector<3>(cosWidth * cosLength, sinWidth, -sinLength * cosWidth);
 }
 
 template<>