From 4040a48e40e80d35a65d79493374d5657ad3ed20 Mon Sep 17 00:00:00 2001 From: kearwood Date: Mon, 9 Aug 2021 19:16:04 -0700 Subject: [PATCH] Bump CMake version, fix warnings --- CMakeLists.txt | 2 +- include/matrix4.h | 4 ++-- src/matrix2.cpp | 4 ++-- src/matrix2x3.cpp | 4 ++-- src/matrix4.cpp | 17 +++++++++-------- src/quaternion.cpp | 34 +++++++++++++++++----------------- src/triangle3.cpp | 8 ++++---- src/vector3.cpp | 4 ++-- src/vector4.cpp | 4 ++-- 9 files changed, 41 insertions(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd5e7a9..8858779 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 2.6) +cmake_minimum_required (VERSION 3.16) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) diff --git a/include/matrix4.h b/include/matrix4.h index 28d8759..02ba172 100644 --- a/include/matrix4.h +++ b/include/matrix4.h @@ -38,11 +38,11 @@ namespace kraken { -typedef enum { +enum class AXIS { X_AXIS, Y_AXIS, Z_AXIS -} AXIS; +}; class Quaternion; diff --git a/src/matrix2.cpp b/src/matrix2.cpp index f6a8085..49f1324 100644 --- a/src/matrix2.cpp +++ b/src/matrix2.cpp @@ -107,8 +107,8 @@ void Matrix2::rotate(float angle) { Matrix2 newMatrix; // Create new identity matrix newMatrix.init(); - newMatrix.c[0] = cos(angle); - newMatrix.c[1] = -sin(angle); + newMatrix.c[0] = cosf(angle); + newMatrix.c[1] = -sinf(angle); newMatrix.c[2] = -newMatrix.c[1]; newMatrix.c[3] = newMatrix.c[0]; diff --git a/src/matrix2x3.cpp b/src/matrix2x3.cpp index 5a2dbd7..8beebd2 100644 --- a/src/matrix2x3.cpp +++ b/src/matrix2x3.cpp @@ -127,8 +127,8 @@ void Matrix2x3::translate(const Vector2 &v) void Matrix2x3::rotate(float angle) { Matrix2x3 newMatrix; newMatrix.init(); - newMatrix.c[0] = cos(angle); - newMatrix.c[1] = -sin(angle); + newMatrix.c[0] = cosf(angle); + newMatrix.c[1] = -sinf(angle); newMatrix.c[2] = -newMatrix.c[1]; newMatrix.c[3] = newMatrix.c[0]; diff --git a/src/matrix4.cpp b/src/matrix4.cpp index 7509ccd..cf062af 100644 --- a/src/matrix4.cpp +++ b/src/matrix4.cpp @@ -129,7 +129,7 @@ void Matrix4::perspective(float fov, float aspect, float nearz, float farz) { memset(c, 0, sizeof(float) * 16); - float range= tan(fov * 0.5f) * nearz; + float range= tanf(fov * 0.5f) * nearz; c[0] = (2 * nearz) / ((range * aspect) - (-range * aspect)); c[5] = (2 * nearz) / (2 * range); c[10] = -(farz + nearz) / (farz - nearz); @@ -199,10 +199,11 @@ void Matrix4::rotate(float angle, AXIS axis) { Matrix4 newMatrix; // Create new identity matrix newMatrix.init(); - newMatrix.c[cos1[axis]] = cos(angle); - newMatrix.c[sin1[axis]] = -sin(angle); - newMatrix.c[sin2[axis]] = -newMatrix.c[sin1[axis]]; - newMatrix.c[cos2[axis]] = newMatrix.c[cos1[axis]]; + uint8_t index = static_cast(axis); + newMatrix.c[cos1[index]] = cosf(angle); + newMatrix.c[sin1[index]] = -sinf(angle); + newMatrix.c[sin2[index]] = -newMatrix.c[sin1[index]]; + newMatrix.c[cos2[index]] = newMatrix.c[cos1[index]]; *this *= newMatrix; } @@ -441,9 +442,9 @@ Matrix4 Matrix4::Rotation(const Vector3 &v) { Matrix4 m; m.init(); - m.rotate(v.x, X_AXIS); - m.rotate(v.y, Y_AXIS); - m.rotate(v.z, Z_AXIS); + m.rotate(v.x, AXIS::X_AXIS); + m.rotate(v.y, AXIS::Y_AXIS); + m.rotate(v.z, AXIS::Z_AXIS); return m; } diff --git a/src/quaternion.cpp b/src/quaternion.cpp index 7eaf578..99ca927 100644 --- a/src/quaternion.cpp +++ b/src/quaternion.cpp @@ -94,7 +94,7 @@ void Quaternion::init(const Vector3 &from_vector, const Vector3 &to_vector) { c[0] = a[0]; c[1] = a[1]; c[2] = a[2]; - c[3] = sqrt(from_vector.sqrMagnitude() * to_vector.sqrMagnitude()) + Vector3::Dot(from_vector, to_vector); + c[3] = sqrtf(from_vector.sqrMagnitude() * to_vector.sqrMagnitude()) + Vector3::Dot(from_vector, to_vector); normalize(); } @@ -114,12 +114,12 @@ void Quaternion::setEulerXYZ(const Vector3 &euler) void Quaternion::setEulerZYX(const Vector3 &euler) { // ZYX Order! - float c1 = cos(euler[0] * 0.5f); - float c2 = cos(euler[1] * 0.5f); - float c3 = cos(euler[2] * 0.5f); - float s1 = sin(euler[0] * 0.5f); - float s2 = sin(euler[1] * 0.5f); - float s3 = sin(euler[2] * 0.5f); + float c1 = cosf(euler[0] * 0.5f); + float c2 = cosf(euler[1] * 0.5f); + float c3 = cosf(euler[2] * 0.5f); + float s1 = sinf(euler[0] * 0.5f); + float s2 = sinf(euler[1] * 0.5f); + float s3 = sinf(euler[2] * 0.5f); c[0] = c1 * c2 * c3 + s1 * s2 * s3; c[1] = s1 * c2 * c3 - c1 * s2 * s3; @@ -139,13 +139,13 @@ Vector3 Quaternion::eulerXYZ() const { float a2 = 2 * (c[0] * c[2] - c[1] * c[3]); if(a2 <= -0.99999) { return Vector3::Create( - 2.0f * atan2(c[1], c[0]), + 2.0f * atan2f(c[1], c[0]), -PI * 0.5f, 0 ); } else if(a2 >= 0.99999) { return Vector3::Create( - 2.0f * atan2(c[1], c[0]), + 2.0f * atan2f(c[1], c[0]), PI * 0.5f, 0 ); @@ -345,8 +345,8 @@ Matrix4 Quaternion::rotationMatrix() const { Quaternion Quaternion::FromAngleAxis(const Vector3 &axis, float angle) { float ha = angle * 0.5f; - float sha = sin(ha); - return Quaternion::Create(cos(ha), axis.x * sha, axis.y * sha, axis.z * sha); + float sha = sinf(ha); + return Quaternion::Create(cosf(ha), axis.x * sha, axis.y * sha, axis.z * sha); } Quaternion Quaternion::FromRotationMatrix(const Matrix4 &m) @@ -355,25 +355,25 @@ Quaternion Quaternion::FromRotationMatrix(const Matrix4 &m) const float trace = m[0] + m[5] + m[10]; float w, x, y, z; if (trace > 0.0) { - const float s = 0.5f / sqrt(trace + 1.0f); + const float s = 0.5f / sqrtf(trace + 1.0f); w = 0.25f / s; x = (m[9] - m[6]) * s; y = (m[2] - m[8]) * s; z = (m[4] - m[1]) * s; } else if (m[0] > m[5] && m[0] > m[10]) { - const float s = 2.0f * sqrt(1.0f + m[0] - m[5] - m[10]); + const float s = 2.0f * sqrtf(1.0f + m[0] - m[5] - m[10]); w = (m[9] - m[6]) / s; x = 0.25f * s; y = (m[1] + m[4]) / s; z = (m[2] + m[8]) / s; } else if (m[5] > m[10]) { - const float s = 2.0f * sqrt(1.0f + m[5] - m[0] - m[10]); + const float s = 2.0f * sqrtf(1.0f + m[5] - m[0] - m[10]); w = (m[2] - m[8]) / s; x = (m[1] + m[4]) / s; y = 0.25f * s; z = (m[6] + m[9]) / s; } else { - const float s = 2.0f * sqrt(1.0f + m[10] - m[0] - m[5]); + const float s = 2.0f * sqrtf(1.0f + m[10] - m[0] - m[5]); w = (m[4] - m[1]) / s; x = (m[2] + m[8]) / s; y = (m[6] + m[9]) / s; @@ -423,9 +423,9 @@ Quaternion Quaternion::Slerp(const Quaternion &a, const Quaternion &b, float t) return Lerp(c, b, t); } - float halftheta = acos(coshalftheta); + float halftheta = acosf(coshalftheta); - return (c * sin((1.0f - t) * halftheta) + b * sin(t * halftheta)) / sin(halftheta); + return (c * sinf((1.0f - t) * halftheta) + b * sinf(t * halftheta)) / sinf(halftheta); } } // namespace kraken diff --git a/src/triangle3.cpp b/src/triangle3.cpp index 78093ce..bdfd7e8 100644 --- a/src/triangle3.cpp +++ b/src/triangle3.cpp @@ -55,7 +55,7 @@ namespace { // Return the distance to the [first] intersecting point - distance = v - sqrt(d); + distance = v - sqrtf(d); if (distance < 0.0f) { return false; } @@ -175,7 +175,7 @@ bool Triangle3::rayCast(const Vector3 &start, const Vector3 &dir, Vector3 &hit_p // get intersect point of ray with triangle plane r = a / b; - if (r < 0.0) // ray goes away from triangle + if (r < 0.0f) // ray goes away from triangle return false; // => no intersect // for a segment, also test if (r > 1.0) => no intersect @@ -195,10 +195,10 @@ bool Triangle3::rayCast(const Vector3 &start, const Vector3 &dir, Vector3 &hit_p // get and test parametric coords float s, t; s = (uv * wv - vv * wu) / D; - if (s < 0.0 || s > 1.0) // plane_hit_point is outside triangle + if (s < 0.0f || s > 1.0f) // plane_hit_point is outside triangle return false; t = (uv * wu - uu * wv) / D; - if (t < 0.0 || (s + t) > 1.0) // plane_hit_point is outside triangle + if (t < 0.0f || (s + t) > 1.0f) // plane_hit_point is outside triangle return false; // plane_hit_point is inside the triangle diff --git a/src/vector3.cpp b/src/vector3.cpp index 533730e..50a30ca 100644 --- a/src/vector3.cpp +++ b/src/vector3.cpp @@ -278,11 +278,11 @@ Vector3 Vector3::Slerp(const Vector3 &v1, const Vector3 &v2, float d) { // Acos(dot) returns the angle between start and end, // And multiplying that by percent returns the angle between // start and the final result. - float theta = acos(dot)*d; + float theta = acosf(dot)*d; Vector3 RelativeVec = v2 - v1*dot; RelativeVec.normalize(); // Orthonormal basis // The final result. - return ((v1*cos(theta)) + (RelativeVec*sin(theta))); + return ((v1*cosf(theta)) + (RelativeVec*sinf(theta))); } void Vector3::OrthoNormalize(Vector3 &normal, Vector3 &tangent) { diff --git a/src/vector4.cpp b/src/vector4.cpp index 7b26187..82be4fa 100644 --- a/src/vector4.cpp +++ b/src/vector4.cpp @@ -175,11 +175,11 @@ Vector4 Vector4::Slerp(const Vector4 &v1, const Vector4 &v2, float d) { // Acos(dot) returns the angle between start and end, // And multiplying that by percent returns the angle between // start and the final result. - float theta = acos(dot)*d; + float theta = acosf(dot)*d; Vector4 RelativeVec = v2 - v1*dot; RelativeVec.normalize(); // Orthonormal basis // The final result. - return ((v1*cos(theta)) + (RelativeVec*sin(theta))); + return ((v1*cosf(theta)) + (RelativeVec*sinf(theta))); } void Vector4::OrthoNormalize(Vector4 &normal, Vector4 &tangent) {