From 676ed6c9d818d3bdc68d9f18d92afe3a84f53081 Mon Sep 17 00:00:00 2001 From: Gawaboumga Date: Sat, 14 Jun 2014 22:10:37 +0200 Subject: [PATCH] Correction of mathematical functions. BoundingVolume: Add of constructor and setter OrientedBox. Extend is now up to date. Box: Use of . Return this Frustrum: Use of Matrix: Use of OrientedBox: m_corners is up to date Rect: Add of operators /= and /. Return this Sphere: Add of Intersect and Contains with box. Little corrections. Vector2: Return TaskSchedulerImpl: Add of include header cstdlib to compile because of std::div HardwareInfo/main: use of accentAigu Former-commit-id: a5a7f8e8c45448e5683eb13bff453d6f67478d03 --- examples/HardwareInfo/main.cpp | 10 +++--- include/Nazara/Math/BoundingVolume.hpp | 2 ++ include/Nazara/Math/BoundingVolume.inl | 24 ++++++++++--- include/Nazara/Math/Box.inl | 20 ++++++----- include/Nazara/Math/Frustum.inl | 6 ++-- include/Nazara/Math/Matrix4.inl | 14 ++++---- include/Nazara/Math/OrientedBox.inl | 15 ++++++-- include/Nazara/Math/Rect.hpp | 4 +++ include/Nazara/Math/Rect.inl | 36 +++++++++++++++++-- include/Nazara/Math/Sphere.hpp | 8 +++-- include/Nazara/Math/Sphere.inl | 39 ++++++++++++++++----- include/Nazara/Math/Vector2.inl | 2 +- src/Nazara/Core/Win32/TaskSchedulerImpl.cpp | 5 +-- 13 files changed, 141 insertions(+), 44 deletions(-) diff --git a/examples/HardwareInfo/main.cpp b/examples/HardwareInfo/main.cpp index f929501cb..07beced54 100644 --- a/examples/HardwareInfo/main.cpp +++ b/examples/HardwareInfo/main.cpp @@ -24,6 +24,9 @@ int main() // On va afficher le tout via un ostringstream, pour écrire dans la console et aussi dans un fichier std::ostringstream oss; + + char accentAigu = static_cast(130); // C'est crade, mais ça marche chez 95% des Windowsiens + oss << "--Processeur--" << std::endl; // Plutôt que d'initialiser le Renderer de Nazara, nous initialisons les deux classes utilisées ici // Elles sont compatibles avec NzInitialiser et seront donc libérées automatiquement @@ -41,7 +44,7 @@ int main() oss << std::endl; // Ensuite, Nazara récupère les capacités du processeur, dont des jeux d'extensions supplémentaires - oss << "Report des capacites: " << std::endl; + oss << "Report des capacit" << accentAigu << "s: " << std::endl; printCap(oss, "-64bits", NzHardwareInfo::HasCapability(nzProcessorCap_x64)); printCap(oss, "-AVX", NzHardwareInfo::HasCapability(nzProcessorCap_AVX)); printCap(oss, "-FMA3", NzHardwareInfo::HasCapability(nzProcessorCap_FMA3)); @@ -78,7 +81,7 @@ int main() oss << std::endl; // Ainsi qu'un report des capacités de la carte graphique (avec le driver actuel) - oss << "Report des capacites: " << std::endl; + oss << "Report des capacit" << accentAigu << "s: " << std::endl; printCap(oss, "-Calculs 64bits", NzOpenGL::IsSupported(nzOpenGLExtension_FP64)); printCap(oss, "-Compression de textures (s3tc)", NzOpenGL::IsSupported(nzOpenGLExtension_TextureCompression_s3tc)); printCap(oss, "-Filtrage anisotrope", NzOpenGL::IsSupported(nzOpenGLExtension_AnisotropicFilter)); @@ -105,8 +108,7 @@ int main() reportFile.Write(oss.str()); // Conversion implicite en NzString reportFile.Close(); - char accent = (char) 130; // C'est crade, mais ça marche chez 95% des Windowsiens - std::cout << "Un fichier (RapportHardwareInfo.txt) contenant le rapport a " << accent << 't' << accent << " cr" << accent << accent << std::endl; + std::cout << "Un fichier (RapportHardwareInfo.txt) contenant le rapport a " << accentAigu << 't' << accentAigu << " cr" << accentAigu << accentAigu << std::endl; } else std::cout << "Impossible de sauvegarder le rapport" << std::endl; diff --git a/include/Nazara/Math/BoundingVolume.hpp b/include/Nazara/Math/BoundingVolume.hpp index 40d32f873..999bfdd6a 100644 --- a/include/Nazara/Math/BoundingVolume.hpp +++ b/include/Nazara/Math/BoundingVolume.hpp @@ -20,6 +20,7 @@ class NzBoundingVolume NzBoundingVolume(nzExtend Extend); NzBoundingVolume(T X, T Y, T Z, T Width, T Height, T Depth); NzBoundingVolume(const NzBox& box); + NzBoundingVolume(const NzOrientedBox& orientedBox); NzBoundingVolume(const NzVector3& vec1, const NzVector3& vec2); template explicit NzBoundingVolume(const NzBoundingVolume& volume); NzBoundingVolume(const NzBoundingVolume& volume) = default; @@ -36,6 +37,7 @@ class NzBoundingVolume NzBoundingVolume& Set(T X, T Y, T Z, T Width, T Height, T Depth); NzBoundingVolume& Set(const NzBoundingVolume& volume); NzBoundingVolume& Set(const NzBox& box); + NzBoundingVolume& Set(const NzOrientedBox& orientedBox); NzBoundingVolume& Set(const NzVector3& vec1, const NzVector3& vec2); template NzBoundingVolume& Set(const NzBoundingVolume& volume); diff --git a/include/Nazara/Math/BoundingVolume.inl b/include/Nazara/Math/BoundingVolume.inl index a6a58c381..ed84258a1 100644 --- a/include/Nazara/Math/BoundingVolume.inl +++ b/include/Nazara/Math/BoundingVolume.inl @@ -1,4 +1,4 @@ -// Copyright (C) 2014 Jérôme Leclercq +// Copyright (C) 2014 Jérôme Leclercq // This file is part of the "Nazara Engine - Mathematics module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -35,6 +35,12 @@ NzBoundingVolume::NzBoundingVolume(const NzBox& box) Set(box); } +template +NzBoundingVolume::NzBoundingVolume(const NzOrientedBox& orientedBox) +{ + Set(orientedBox); +} + template NzBoundingVolume::NzBoundingVolume(const NzVector3& vec1, const NzVector3& vec2) { @@ -103,6 +109,7 @@ template NzBoundingVolume& NzBoundingVolume::Set(const NzBoundingVolume& volume) { obb.Set(volume.obb); // Seul l'OBB est importante pour la suite + extend = volume.extend; return *this; } @@ -116,6 +123,15 @@ NzBoundingVolume& NzBoundingVolume::Set(const NzBox& box) return *this; } +template +NzBoundingVolume& NzBoundingVolume::Set(const NzOrientedBox& orientedBox) +{ + obb.Set(orientedBox); + extend = nzExtend_Finite; + + return *this; +} + template NzBoundingVolume& NzBoundingVolume::Set(const NzVector3& vec1, const NzVector3& vec2) { @@ -209,17 +225,17 @@ template NzBoundingVolume NzBoundingVolume::Lerp(const NzBoundingVolume& from, const NzBoundingVolume& to, T interpolation) { #ifdef NAZARA_DEBUG - if (interpolation < 0.f || interpolation > 1.f) + if (interpolation < F(0.0) || interpolation > F(1.0)) { NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); return Null(); } #endif - if (NzNumberEquals(interpolation, 0.f)) + if (NzNumberEquals(interpolation, F(0.0))) return from; - if (NzNumberEquals(interpolation, 1.f)) + if (NzNumberEquals(interpolation, F(1.0))) return to; switch (to.extend) diff --git a/include/Nazara/Math/Box.inl b/include/Nazara/Math/Box.inl index 098e34e6d..989e7108e 100644 --- a/include/Nazara/Math/Box.inl +++ b/include/Nazara/Math/Box.inl @@ -122,32 +122,32 @@ NzVector3 NzBox::GetCorner(nzCorner corner) const switch (corner) { case nzCorner_FarLeftBottom: - return NzVector3f(x, y, z); + return NzVector3(x, y, z); case nzCorner_FarLeftTop: - return NzVector3f(x, y + height, z); + return NzVector3(x, y + height, z); case nzCorner_FarRightBottom: - return NzVector3f(x + width, y, z); + return NzVector3(x + width, y, z); case nzCorner_FarRightTop: - return NzVector3f(x + width, y + height, z); + return NzVector3(x + width, y + height, z); case nzCorner_NearLeftBottom: - return NzVector3f(x, y, z + depth); + return NzVector3(x, y, z + depth); case nzCorner_NearLeftTop: - return NzVector3f(x, y + height, z + depth); + return NzVector3(x, y + height, z + depth); case nzCorner_NearRightBottom: - return NzVector3f(x + width, y, z + depth); + return NzVector3(x + width, y, z + depth); case nzCorner_NearRightTop: - return NzVector3f(x + width, y + height, z + depth); + return NzVector3(x + width, y + height, z + depth); } NazaraError("Corner not handled (0x" + NzString::Number(corner, 16) + ')'); - return NzVector3f(); + return NzVector3(); } template @@ -465,6 +465,7 @@ NzBox& NzBox::operator*=(T scalar) width *= scalar; height *= scalar; depth *= scalar; + return *this; } template @@ -473,6 +474,7 @@ NzBox& NzBox::operator*=(const NzVector3& vec) width *= vec.x; height *= vec.y; depth *= vec.z; + return *this; } template diff --git a/include/Nazara/Math/Frustum.inl b/include/Nazara/Math/Frustum.inl index a1424a33f..300031d88 100644 --- a/include/Nazara/Math/Frustum.inl +++ b/include/Nazara/Math/Frustum.inl @@ -262,10 +262,10 @@ NzFrustum& NzFrustum::Extract(const NzMatrix4& clipMatrix) // Une fois les plans extraits, il faut extraire les points du frustum // Je me base sur cette page: http://www.gamedev.net/topic/393309-calculating-the-view-frustums-vertices/ - NzMatrix4f invClipMatrix; + NzMatrix4 invClipMatrix; if (clipMatrix.GetInverse(&invClipMatrix)) { - NzVector4f corner; + NzVector4 corner; // FarLeftBottom corner.Set(F(-1.0), F(-1.0), F(1.0)); @@ -332,7 +332,7 @@ NzFrustum& NzFrustum::Extract(const NzMatrix4& clipMatrix) template NzFrustum& NzFrustum::Extract(const NzMatrix4& view, const NzMatrix4& projection) { - NzMatrix4f clipMatrix(view); + NzMatrix4 clipMatrix(view); clipMatrix *= projection; return Extract(clipMatrix); diff --git a/include/Nazara/Math/Matrix4.inl b/include/Nazara/Math/Matrix4.inl index f577114ad..173653ebb 100644 --- a/include/Nazara/Math/Matrix4.inl +++ b/include/Nazara/Math/Matrix4.inl @@ -1,4 +1,4 @@ -// Copyright (C) 2014 Jérôme Leclercq +// Copyright (C) 2014 Jérôme Leclercq // This file is part of the "Nazara Engine - Mathematics module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -46,7 +46,7 @@ NzMatrix4::NzMatrix4(const NzMatrix4& matrix) template NzMatrix4& NzMatrix4::ApplyRotation(const NzQuaternion& rotation) { - return Concatenate(NzMatrix4f::Rotate(rotation)); + return Concatenate(NzMatrix4::Rotate(rotation)); } template @@ -404,7 +404,7 @@ NzQuaternion NzMatrix4::GetRotation() const T trace = m11 + m22 + m33; if (trace > F(0.0)) { - float s = F(0.5)/std::sqrt(trace + F(1.0)); + T s = F(0.5)/std::sqrt(trace + F(1.0)); quat.w = F(0.25) / s; quat.x = (m23 - m32) * s; quat.y = (m31 - m13) * s; @@ -414,7 +414,7 @@ NzQuaternion NzMatrix4::GetRotation() const { if (m11 > m22 && m11 > m33) { - float s = F(2.0) * std::sqrt(F(1.0) + m11 - m22 - m33); + T s = F(2.0) * std::sqrt(F(1.0) + m11 - m22 - m33); quat.w = (m23 - m32) / s; quat.x = F(0.25) * s; @@ -423,7 +423,7 @@ NzQuaternion NzMatrix4::GetRotation() const } else if (m22 > m33) { - float s = F(2.0) * std::sqrt(F(1.0) + m22 - m11 - m33); + T s = F(2.0) * std::sqrt(F(1.0) + m22 - m11 - m33); quat.w = (m31 - m13) / s; quat.x = (m21 + m12) / s; @@ -432,7 +432,7 @@ NzQuaternion NzMatrix4::GetRotation() const } else { - float s = F(2.0) * std::sqrt(F(1.0) + m33 - m11 - m22); + T s = F(2.0) * std::sqrt(F(1.0) + m33 - m11 - m22); quat.w = (m12 - m21) / s; quat.x = (m31 + m13) / s; @@ -658,7 +658,7 @@ template NzMatrix4& NzMatrix4::MakeViewMatrix(const NzVector3& translation, const NzQuaternion& rotation) { // Une matrice de vue doit appliquer une transformation opposée à la matrice "monde" - NzQuaternionf invRot = rotation.GetConjugate(); // Inverse de la rotation + NzQuaternion invRot = rotation.GetConjugate(); // Inverse de la rotation return MakeTransform(-(invRot*translation), invRot); } diff --git a/include/Nazara/Math/OrientedBox.inl b/include/Nazara/Math/OrientedBox.inl index fad8abbf9..fcc91e4d1 100644 --- a/include/Nazara/Math/OrientedBox.inl +++ b/include/Nazara/Math/OrientedBox.inl @@ -60,7 +60,8 @@ template NzOrientedBox& NzOrientedBox::MakeZero() { localBox.MakeZero(); - + for (unsigned int i = 0; i <= nzCorner_Max; ++i) + m_corners[i].Set(NzVector3::Zero()); return *this; } @@ -69,6 +70,8 @@ NzOrientedBox& NzOrientedBox::Set(T X, T Y, T Z, T Width, T Height, T Dept { localBox.Set(X, Y, Z, Width, Height, Depth); + for (unsigned int i = 0; i <= nzCorner_Max; ++i) + m_corners[i].Set(localBox.GetCorner(static_cast(i))); return *this; } @@ -77,6 +80,8 @@ NzOrientedBox& NzOrientedBox::Set(const NzBox& box) { localBox.Set(box); + for (unsigned int i = 0; i <= nzCorner_Max; ++i) + m_corners[i].Set(localBox.GetCorner(static_cast(i))); return *this; } @@ -93,6 +98,8 @@ NzOrientedBox& NzOrientedBox::Set(const NzVector3& vec1, const NzVector { localBox.Set(vec1, vec2); + for (unsigned int i = 0; i <= nzCorner_Max; ++i) + m_corners[i].Set(localBox.GetCorner(static_cast(i))); return *this; } @@ -127,7 +134,7 @@ template void NzOrientedBox::Update(const NzMatrix4& transformMatrix) { for (unsigned int i = 0; i <= nzCorner_Max; ++i) - m_corners[i] = transformMatrix.Transform(localBox.GetCorner(static_cast(i))); + m_corners[i] = transformMatrix.Transform(m_corners[i]); } template @@ -182,6 +189,8 @@ NzOrientedBox NzOrientedBox::operator*(T scalar) const NzOrientedBox box(*this); box *= scalar; + for (unsigned int i = 0; i <= nzCorner_Max; ++i) + box.m_corners[i].Set(box.GetCorner(static_cast(i))); return box; } @@ -190,6 +199,8 @@ NzOrientedBox& NzOrientedBox::operator*=(T scalar) { localBox *= scalar; + for (unsigned int i = 0; i <= nzCorner_Max; ++i) + m_corners[i].Set(localBox.GetCorner(static_cast(i))); return *this; } diff --git a/include/Nazara/Math/Rect.hpp b/include/Nazara/Math/Rect.hpp index fdaf7a826..df7611b13 100644 --- a/include/Nazara/Math/Rect.hpp +++ b/include/Nazara/Math/Rect.hpp @@ -63,9 +63,13 @@ class NzRect NzRect operator*(T scalar) const; NzRect operator*(const NzVector2& vec) const; + NzRect operator/(T scalar) const; + NzRect operator/(const NzVector2& vec) const; NzRect& operator*=(T scalar); NzRect& operator*=(const NzVector2& vec); + NzRect& operator/=(T scalar); + NzRect& operator/=(const NzVector2& vec); bool operator==(const NzRect& rect) const; bool operator!=(const NzRect& rect) const; diff --git a/include/Nazara/Math/Rect.inl b/include/Nazara/Math/Rect.inl index 7048666da..b2fd3a3e4 100644 --- a/include/Nazara/Math/Rect.inl +++ b/include/Nazara/Math/Rect.inl @@ -1,4 +1,4 @@ -// Copyright (C) 2014 Jérôme Leclercq +// Copyright (C) 2014 Jérôme Leclercq // This file is part of the "Nazara Engine - Mathematics module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -106,7 +106,7 @@ NzRect& NzRect::ExtendTo(const NzRect& rect) template NzVector2 NzRect::GetCenter() const { - return GetPosition() + F(0.5)*GetLengths(); + return GetPosition() + GetLengths() / F(2.0); } template @@ -336,11 +336,24 @@ NzRect NzRect::operator*(const NzVector2& vec) const return NzRect(x, y, width*vec.x, height*vec.y); } +template +NzRect NzRect::operator/(T scalar) const +{ + return NzRect(x, y, width/scalar, height/scalar); +} + +template +NzRect NzRect::operator/(const NzVector2& vec) const +{ + return NzRect(x, y, width/vec.x, height/vec.y); +} + template NzRect& NzRect::operator*=(T scalar) { width *= scalar; height *= scalar; + return *this; } template @@ -348,6 +361,25 @@ NzRect& NzRect::operator*=(const NzVector2& vec) { width *= vec.x; height *= vec.y; + return *this; +} + +template +NzRect& NzRect::operator/=(T scalar) +{ + width /= scalar; + height /= scalar; + + return *this; +} + +template +NzRect& NzRect::operator/=(const NzVector2& vec) +{ + width /= vec.x; + height /= vec.y; + + return *this; } template diff --git a/include/Nazara/Math/Sphere.hpp b/include/Nazara/Math/Sphere.hpp index 611dc25c2..975893076 100644 --- a/include/Nazara/Math/Sphere.hpp +++ b/include/Nazara/Math/Sphere.hpp @@ -8,8 +8,12 @@ #define NAZARA_SPHERE_HPP #include +#include #include +template +class NzBox; + template class NzSphere { @@ -24,7 +28,7 @@ class NzSphere ~NzSphere() = default; bool Contains(T X, T Y, T Z) const; - //bool Contains(const NzBox& box) const; + bool Contains(const NzBox& box) const; bool Contains(const NzVector3& point) const; T Distance(T X, T Y, T Z) const; @@ -37,7 +41,7 @@ class NzSphere NzVector3 GetPosition() const; NzVector3 GetPositiveVertex(const NzVector3& normal) const; - //bool Intersect(const NzBox& box) const; + bool Intersect(const NzBox& box) const; bool Intersect(const NzSphere& sphere) const; bool IsValid() const; diff --git a/include/Nazara/Math/Sphere.inl b/include/Nazara/Math/Sphere.inl index 2e360fef2..2f74945dc 100644 --- a/include/Nazara/Math/Sphere.inl +++ b/include/Nazara/Math/Sphere.inl @@ -29,7 +29,7 @@ NzSphere::NzSphere(const NzVector3& center, T Radius) } template -NzSphere::NzSphere(const T sphere[6]) +NzSphere::NzSphere(const T sphere[4]) { Set(sphere); } @@ -46,12 +46,18 @@ bool NzSphere::Contains(T X, T Y, T Z) const { return SquaredDistance(X, Y, Z) <= radius*radius; } -/* + template bool NzSphere::Contains(const NzBox& box) const { + if (box.GetMinimum().SquaredDistance(GetPosition()) <= radius * radius) + { + if (box.GetMaximum().SquaredDistance(GetPosition()) <= radius * radius) + return true; + } + + return false; } -*/ template bool NzSphere::Contains(const NzVector3& point) const { @@ -85,7 +91,7 @@ NzSphere& NzSphere::ExtendTo(T X, T Y, T Z) template NzSphere& NzSphere::ExtendTo(const NzVector3& point) { - return ExtendTo(point); + return ExtendTo(point.x, point.y, point.z); } template @@ -111,18 +117,35 @@ NzVector3 NzSphere::GetPositiveVertex(const NzVector3& normal) const return pos; } -/* template bool NzSphere::Intersect(const NzBox& box) const { + // Arvo's algorithm. + T dmin = T(0); + if (x < box.x) + dmin += (x - box.x) * (x - box.x); + else if (x > box.x + box.width) + dmin += (x - (box.x + box.width)) * (x - (box.x + box.width)); + if (y < box.y) + dmin += (y - box.y) * (y - box.y); + else if (x > box.x + box.width) + dmin += (y - (box.y + box.height)) * (y - (box.y + box.height)); + + if (z < box.z) + dmin += (z - box.z) * (z - box.z); + else if (x > box.x + box.width) + dmin += (z - (box.z + box.depth)) * (z - (box.z + box.depth)); + + if (dmin <= radius * radius) + return true; + return false; } -*/ template bool NzSphere::Intersect(const NzSphere& sphere) const { - return SquaredDistance(sphere.x, sphere.y, sphere.z) - radius*radius <= sphere.radius*sphere.radius; + return std::abs(SquaredDistance(sphere.x, sphere.y, sphere.z) - radius*radius) <= sphere.radius*sphere.radius; } template @@ -210,7 +233,7 @@ template T NzSphere::SquaredDistance(T X, T Y, T Z) const { NzVector3 distance(X-x, Y-y, Z-z); - return distance.SquaredLength(); + return distance.GetSquaredLength(); } template diff --git a/include/Nazara/Math/Vector2.inl b/include/Nazara/Math/Vector2.inl index e415125b8..5da704243 100644 --- a/include/Nazara/Math/Vector2.inl +++ b/include/Nazara/Math/Vector2.inl @@ -102,7 +102,7 @@ T NzVector2::GetSquaredLength() const template NzVector2& NzVector2::MakeUnitX() { - Set(F(1.0), F(0.0)); + return Set(F(1.0), F(0.0)); } template diff --git a/src/Nazara/Core/Win32/TaskSchedulerImpl.cpp b/src/Nazara/Core/Win32/TaskSchedulerImpl.cpp index fc35f833c..0dccbff11 100644 --- a/src/Nazara/Core/Win32/TaskSchedulerImpl.cpp +++ b/src/Nazara/Core/Win32/TaskSchedulerImpl.cpp @@ -4,9 +4,10 @@ #include #include -#include -#include #include +#include +#include +#include bool NzTaskSchedulerImpl::Initialize(unsigned int workerCount) {