From fcacaba7ca968b5fa1da9e093ba576000eed8f92 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 4 Oct 2012 23:39:10 +0200 Subject: [PATCH 01/27] Cleaned code Former-commit-id: a61b8bf3470036cd8a61199e78609206d959966f --- src/Nazara/Core/Win32/DirectoryImpl.cpp | 4 ---- src/Nazara/Core/Win32/DirectoryImpl.hpp | 2 +- src/Nazara/Core/Win32/DynLibImpl.cpp | 4 ---- src/Nazara/Core/Win32/DynLibImpl.hpp | 2 +- src/Nazara/Core/Win32/FileImpl.cpp | 4 ---- src/Nazara/Core/Win32/FileImpl.hpp | 2 +- 6 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/Nazara/Core/Win32/DirectoryImpl.cpp b/src/Nazara/Core/Win32/DirectoryImpl.cpp index 03dce2f2c..6e07cc704 100644 --- a/src/Nazara/Core/Win32/DirectoryImpl.cpp +++ b/src/Nazara/Core/Win32/DirectoryImpl.cpp @@ -11,10 +11,6 @@ NzDirectoryImpl::NzDirectoryImpl(const NzDirectory* parent) NazaraUnused(parent); } -NzDirectoryImpl::~NzDirectoryImpl() -{ -} - void NzDirectoryImpl::Close() { FindClose(m_handle); diff --git a/src/Nazara/Core/Win32/DirectoryImpl.hpp b/src/Nazara/Core/Win32/DirectoryImpl.hpp index 6b6e25d94..a36a0eee3 100644 --- a/src/Nazara/Core/Win32/DirectoryImpl.hpp +++ b/src/Nazara/Core/Win32/DirectoryImpl.hpp @@ -18,7 +18,7 @@ class NzDirectoryImpl : NzNonCopyable { public: NzDirectoryImpl(const NzDirectory* parent); - ~NzDirectoryImpl(); + ~NzDirectoryImpl() = default; void Close(); diff --git a/src/Nazara/Core/Win32/DynLibImpl.cpp b/src/Nazara/Core/Win32/DynLibImpl.cpp index bfe2d9d87..672682aa9 100644 --- a/src/Nazara/Core/Win32/DynLibImpl.cpp +++ b/src/Nazara/Core/Win32/DynLibImpl.cpp @@ -13,10 +13,6 @@ m_parent(parent) { } -NzDynLibImpl::~NzDynLibImpl() -{ -} - NzDynLibFunc NzDynLibImpl::GetSymbol(const NzString& symbol) const { NzDynLibFunc sym = reinterpret_cast(GetProcAddress(m_handle, symbol.GetConstBuffer())); diff --git a/src/Nazara/Core/Win32/DynLibImpl.hpp b/src/Nazara/Core/Win32/DynLibImpl.hpp index 7d742d32f..df950cbbb 100644 --- a/src/Nazara/Core/Win32/DynLibImpl.hpp +++ b/src/Nazara/Core/Win32/DynLibImpl.hpp @@ -17,7 +17,7 @@ class NzDynLibImpl : NzNonCopyable { public: NzDynLibImpl(NzDynLib* m_parent); - ~NzDynLibImpl(); + ~NzDynLibImpl() = default; NzDynLibFunc GetSymbol(const NzString& symbol) const; bool Load(const NzString& libraryPath); diff --git a/src/Nazara/Core/Win32/FileImpl.cpp b/src/Nazara/Core/Win32/FileImpl.cpp index bf927a6f0..a3d718a92 100644 --- a/src/Nazara/Core/Win32/FileImpl.cpp +++ b/src/Nazara/Core/Win32/FileImpl.cpp @@ -14,10 +14,6 @@ m_endOfFileUpdated(true) NazaraUnused(parent); } -NzFileImpl::~NzFileImpl() -{ -} - void NzFileImpl::Close() { CloseHandle(m_handle); diff --git a/src/Nazara/Core/Win32/FileImpl.hpp b/src/Nazara/Core/Win32/FileImpl.hpp index 37d66219c..269e9dcb1 100644 --- a/src/Nazara/Core/Win32/FileImpl.hpp +++ b/src/Nazara/Core/Win32/FileImpl.hpp @@ -20,7 +20,7 @@ class NzFileImpl : NzNonCopyable { public: NzFileImpl(const NzFile* parent); - ~NzFileImpl(); + ~NzFileImpl() = default; void Close(); bool EndOfFile() const; From 91f0b97142244fd61351cae20eacf86159896a9c Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 4 Oct 2012 23:41:10 +0200 Subject: [PATCH 02/27] Fixed cracks in some Vorbis files Thanks to Marisa-Chan for the fix in libsndfile Former-commit-id: e60b7671f3ce4f03c5d387ca69e3883c496e7dc0 --- src/Nazara/Audio/Loaders/sndfile/Loader.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Nazara/Audio/Loaders/sndfile/Loader.cpp b/src/Nazara/Audio/Loaders/sndfile/Loader.cpp index 35a4b42d8..c925e6fa6 100644 --- a/src/Nazara/Audio/Loaders/sndfile/Loader.cpp +++ b/src/Nazara/Audio/Loaders/sndfile/Loader.cpp @@ -37,7 +37,7 @@ namespace break; case SEEK_END: - stream->SetCursorPos(stream->GetSize()+offset); + stream->SetCursorPos(stream->GetSize() + offset); // L'offset est négatif ici break; case SEEK_SET: @@ -95,11 +95,18 @@ namespace return false; } + // https://github.com/LaurentGomila/SFML/issues/271 + // http://www.mega-nerd.com/libsndfile/command.html#SFC_SET_SCALE_FLOAT_INT_READ + ///FIXME: Seulement le Vorbis ? + if (infos.format & SF_FORMAT_VORBIS) + sf_command(file, SFC_SET_SCALE_FLOAT_INT_READ, nullptr, SF_TRUE); + unsigned int sampleCount = infos.frames*infos.channels; nzInt16* samples = new nzInt16[sampleCount]; if (sf_read_short(file, samples, sampleCount) != sampleCount) { NazaraError("Failed to read samples"); + delete[] samples; sf_close(file); @@ -109,6 +116,8 @@ namespace if (!soundBuffer->Create(format, infos.frames*infos.channels, infos.samplerate, samples)) { NazaraError("Failed to create sound buffer"); + + delete[] samples; sf_close(file); return false; @@ -123,13 +132,11 @@ namespace void NzLoaders_sndfile_Register() { NzSoundBufferLoader::RegisterLoader("aiff,au,avr,caf,flac,htk,ircam,mat4,mat5,mpc2k,nist,ogg,paf,pvf,raw,rf64,sd2,sds,svx,voc,w64,wav,wve", - NzLoader_sndfile_Check, - NzLoader_sndfile_Load); + NzLoader_sndfile_Check, NzLoader_sndfile_Load); } void NzLoaders_sndfile_Unregister() { NzSoundBufferLoader::UnregisterLoader("aiff,au,avr,caf,flac,htk,ircam,mat4,mat5,mpc2k,nist,ogg,paf,pvf,raw,rf64,sd2,sds,svx,voc,w64,wav,wve", - NzLoader_sndfile_Check, - NzLoader_sndfile_Load); + NzLoader_sndfile_Check, NzLoader_sndfile_Load); } From 737f2a70bdb801141a56cca7125b23efd1972612 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 5 Oct 2012 20:14:01 +0200 Subject: [PATCH 03/27] Fixed compilation error Former-commit-id: f6be1fa1b43082fa295c7dd95337475365f0c453 --- include/Nazara/Renderer/Context.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/Nazara/Renderer/Context.hpp b/include/Nazara/Renderer/Context.hpp index 14d3263e3..1fc97f9e8 100644 --- a/include/Nazara/Renderer/Context.hpp +++ b/include/Nazara/Renderer/Context.hpp @@ -19,7 +19,7 @@ class NAZARA_API NzContext : public NzResource friend NzContextImpl; public: - NzContext(); + NzContext() = default; ~NzContext(); bool Create(const NzContextParameters& parameters = NzContextParameters()); From cfd54b859d477cdec37d0e5a17568b99b30b58ce Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 8 Oct 2012 14:45:29 +0200 Subject: [PATCH 04/27] Added linear interpolation (Lerp) to math module Former-commit-id: 5920e21f25d42701a1895734eca492fdf5351669 --- include/Nazara/Math/Basic.hpp | 1 + include/Nazara/Math/Basic.inl | 14 ++++++ include/Nazara/Math/Cube.hpp | 16 +++--- include/Nazara/Math/Cube.inl | 78 +++++++++++++++++++----------- include/Nazara/Math/Quaternion.hpp | 3 +- include/Nazara/Math/Quaternion.inl | 42 +++++++++++----- include/Nazara/Math/Rect.hpp | 2 + include/Nazara/Math/Rect.inl | 20 ++++++++ include/Nazara/Math/Vector2.hpp | 1 + include/Nazara/Math/Vector2.inl | 6 +++ include/Nazara/Math/Vector3.hpp | 1 + include/Nazara/Math/Vector3.inl | 6 +++ 12 files changed, 141 insertions(+), 49 deletions(-) diff --git a/include/Nazara/Math/Basic.hpp b/include/Nazara/Math/Basic.hpp index 47d344c7b..c1d98ca10 100644 --- a/include/Nazara/Math/Basic.hpp +++ b/include/Nazara/Math/Basic.hpp @@ -31,6 +31,7 @@ inline unsigned int NzGetNumberLength(unsigned long long number); inline unsigned int NzGetNumberLength(float number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); inline unsigned int NzGetNumberLength(double number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); inline unsigned int NzGetNumberLength(long double number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); +template T NzLerp(T from, T to, F interpolation); template T NzNormalizeAngle(T angle); template bool NzNumberEquals(T a, T b); inline NzString NzNumberToString(long long number, nzUInt8 radix = 10); diff --git a/include/Nazara/Math/Basic.inl b/include/Nazara/Math/Basic.inl index 457920a5a..57afcd2c0 100644 --- a/include/Nazara/Math/Basic.inl +++ b/include/Nazara/Math/Basic.inl @@ -132,6 +132,20 @@ unsigned int NzGetNumberLength(long double number, nzUInt8 precision) return NzGetNumberLength(static_cast(number)) + precision + 1; // Plus un pour le point } +template +T NzLerp(T from, T to, F interpolation) +{ + #ifdef NAZARA_DEBUG + if (interpolation < F(0.0) || interpolation > F(1.0)) + { + NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); + return Zero(); + } + #endif + + return from + interpolation*(to-from); +} + template T NzNormalizeAngle(T angle) { diff --git a/include/Nazara/Math/Cube.hpp b/include/Nazara/Math/Cube.hpp index 8fa678d38..8930831aa 100644 --- a/include/Nazara/Math/Cube.hpp +++ b/include/Nazara/Math/Cube.hpp @@ -20,28 +20,28 @@ class NzCube NzCube(const T cube[6]); NzCube(const NzRect& rect); NzCube(const NzVector3& vec1, const NzVector3& vec2); - template explicit NzCube(const NzCube& rect); - NzCube(const NzCube& rect) = default; + template explicit NzCube(const NzCube& cube); + NzCube(const NzCube& cube) = default; ~NzCube() = default; bool Contains(T X, T Y, T Z) const; bool Contains(const NzVector3& point) const; - bool Contains(const NzCube& rect) const; + bool Contains(const NzCube& cube) const; void ExtendTo(const NzVector3& point); - void ExtendTo(const NzCube& rect); + void ExtendTo(const NzCube& cube); NzVector3 GetCenter() const; - bool Intersect(const NzCube& rect, NzCube* intersection = nullptr) const; + bool Intersect(const NzCube& cube, NzCube* intersection = nullptr) const; bool IsValid() const; void Set(T X, T Y, T Z, T Width, T Height, T Depth); - void Set(const T rect[6]); + void Set(const T cube[6]); void Set(const NzRect& rect); void Set(const NzVector3& vec1, const NzVector3& vec2); - template void Set(const NzCube& rect); + template void Set(const NzCube& cube); NzString ToString() const; @@ -50,6 +50,8 @@ class NzCube T& operator[](unsigned int i); T operator[](unsigned int i) const; + static NzCube Lerp(const NzCube& from, const NzCube& to, T interpolation); + T x, y, z, width, height, depth; }; diff --git a/include/Nazara/Math/Cube.inl b/include/Nazara/Math/Cube.inl index d96c2cfcd..4ea4b07c1 100644 --- a/include/Nazara/Math/Cube.inl +++ b/include/Nazara/Math/Cube.inl @@ -39,9 +39,9 @@ NzCube::NzCube(const NzVector3& vec1, const NzVector3& vec2) template template -NzCube::NzCube(const NzCube& rect) +NzCube::NzCube(const NzCube& cube) { - Set(rect); + Set(cube); } template @@ -59,10 +59,10 @@ bool NzCube::Contains(const NzVector3& point) const } template -bool NzCube::Contains(const NzCube& rect) const +bool NzCube::Contains(const NzCube& cube) const { - return Contains(rect.x, rect.y, rect.z) && - Contains(rect.x + rect.width, rect.y + rect.height, rect.z + rect.depth); + return Contains(cube.x, cube.y, cube.z) && + Contains(cube.x + cube.width, cube.y + cube.height, cube.z + cube.depth); } template @@ -77,14 +77,14 @@ void NzCube::ExtendTo(const NzVector3& point) } template -void NzCube::ExtendTo(const NzCube& rect) +void NzCube::ExtendTo(const NzCube& cube) { - x = std::min(x, rect.x); - y = std::min(y, rect.y); - z = std::min(y, rect.z); - width = std::max(x+width, rect.x+rect.width)-x; - height = std::max(x+height, rect.y+rect.height)-y; - depth = std::max(x+depth, rect.z+rect.depth)-z; + x = std::min(x, cube.x); + y = std::min(y, cube.y); + z = std::min(y, cube.z); + width = std::max(x+width, cube.x+cube.width)-x; + height = std::max(x+height, cube.y+cube.height)-y; + depth = std::max(x+depth, cube.z+cube.depth)-z; } template @@ -94,14 +94,14 @@ NzVector3 NzCube::GetCenter() const } template -bool NzCube::Intersect(const NzCube& rect, NzCube* intersection) const +bool NzCube::Intersect(const NzCube& cube, NzCube* intersection) const { - T left = std::max(x, rect.x); - T right = std::min(x+width, rect.x+rect.width); - T top = std::max(y, rect.y); - T bottom = std::min(y+height, rect.y+rect.height); - T up = std::max(z, rect.z); - T down = std::min(z+depth, rect.z+rect.depth); + T left = std::max(x, cube.x); + T right = std::min(x+width, cube.x+cube.width); + T top = std::max(y, cube.y); + T bottom = std::min(y+height, cube.y+cube.height); + T up = std::max(z, cube.z); + T down = std::min(z+depth, cube.z+cube.depth); if (left < right && top < bottom && up < down) { @@ -139,14 +139,14 @@ void NzCube::Set(T X, T Y, T Z, T Width, T Height, T Depth) } template -void NzCube::Set(const T rect[6]) +void NzCube::Set(const T cube[6]) { - x = rect[0]; - y = rect[1]; - z = rect[2]; - width = rect[3]; - height = rect[4]; - depth = rect[5]; + x = cube[0]; + y = cube[1]; + z = cube[2]; + width = cube[3]; + height = cube[4]; + depth = cube[5]; } template @@ -230,9 +230,31 @@ T NzCube::operator[](unsigned int i) const } template -std::ostream& operator<<(std::ostream& out, const NzCube& rect) +NzCube NzCube::Lerp(const NzCube& from, const NzCube& to, T interpolation) { - return out << rect.ToString(); + #ifdef NAZARA_DEBUG + if (interpolation < F(0.0) || interpolation > F(1.0)) + { + NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); + return Zero(); + } + #endif + + NzCube cube; + cube.x = NzLerp(from.x, to.x, interpolation); + cube.y = NzLerp(from.y, to.y, interpolation); + cube.z = NzLerp(from.z, to.z, interpolation); + cube.width = NzLerp(from.width, to.width, interpolation); + cube.height = NzLerp(from.height, to.height, interpolation); + cube.depth = NzLerp(from.depth, to.depth, interpolation); + + return cube; +} + +template +std::ostream& operator<<(std::ostream& out, const NzCube& cube) +{ + return out << cube.ToString(); } #undef F diff --git a/include/Nazara/Math/Quaternion.hpp b/include/Nazara/Math/Quaternion.hpp index bed9d5277..7e9addfc9 100644 --- a/include/Nazara/Math/Quaternion.hpp +++ b/include/Nazara/Math/Quaternion.hpp @@ -77,7 +77,8 @@ template class NzQuaternion bool operator>=(const NzQuaternion& quat) const; static NzQuaternion Identity(); - static NzQuaternion Slerp(const NzQuaternion& quatA, const NzQuaternion& quatB, T interp); + static NzQuaternion Lerp(const NzQuaternion& from, const NzQuaternion& to, T interpolation); + static NzQuaternion Slerp(const NzQuaternion& from, const NzQuaternion& to, T interpolation); static NzQuaternion Zero(); T w, x, y, z; diff --git a/include/Nazara/Math/Quaternion.inl b/include/Nazara/Math/Quaternion.inl index d86f66ea9..6969aa3f4 100644 --- a/include/Nazara/Math/Quaternion.inl +++ b/include/Nazara/Math/Quaternion.inl @@ -365,32 +365,48 @@ NzQuaternion NzQuaternion::Identity() } template -NzQuaternion NzQuaternion::Slerp(const NzQuaternion& quatA, const NzQuaternion& quatB, T interp) +NzQuaternion NzQuaternion::Lerp(const NzQuaternion& from, const NzQuaternion& to, T interpolation) { - if (interp <= F(0.0)) - return quatA; + #ifdef NAZARA_DEBUG + if (interpolation < F(0.0) || interpolation > F(1.0)) + { + NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); + return Zero(); + } + #endif - if (interp >= F(1.0)) - return quatB; + return from + interpolation*(to-from); +} + +template +NzQuaternion NzQuaternion::Slerp(const NzQuaternion& from, const NzQuaternion& to, T interpolation) +{ + #ifdef NAZARA_DEBUG + if (interpolation < F(0.0) || interpolation > F(1.0)) + { + NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); + return Zero(); + } + #endif NzQuaternion q; - T cosOmega = quatA.DotProduct(quatB); + T cosOmega = from.DotProduct(to); if (cosOmega < F(0.0)) { // On inverse tout - q.Set(-quatB.w, -quatB.x, -quatB.y, -quatB.z); + q.Set(-to.w, -to.x, -to.y, -to.z); cosOmega = -cosOmega; } else - q.Set(quatB); + q.Set(to); T k0, k1; if (cosOmega > F(0.9999)) { // Interpolation linéaire pour éviter une division par zéro - k0 = F(1.0) - interp; - k1 = interp; + k0 = F(1.0) - interpolation; + k1 = interpolation; } else { @@ -400,11 +416,11 @@ NzQuaternion NzQuaternion::Slerp(const NzQuaternion& quatA, const NzQuater // Pour éviter deux divisions sinOmega = F(1.0)/sinOmega; - k0 = std::sin((F(1.0) - interp) * omega) * sinOmega; - k1 = std::sin(interp*omega) * sinOmega; + k0 = std::sin((F(1.0) - interpolation) * omega) * sinOmega; + k1 = std::sin(interpolation*omega) * sinOmega; } - NzQuaternion result(k0 * quatA.w, k0 * quatA.x, k0 * quatA.y, k0 * quatA.z); + NzQuaternion result(k0 * from.w, k0 * from.x, k0 * from.y, k0 * from.z); return result += q*k1; } diff --git a/include/Nazara/Math/Rect.hpp b/include/Nazara/Math/Rect.hpp index 938e9e1ea..d7a6162dd 100644 --- a/include/Nazara/Math/Rect.hpp +++ b/include/Nazara/Math/Rect.hpp @@ -47,6 +47,8 @@ class NzRect T& operator[](unsigned int i); T operator[](unsigned int i) const; + static NzRect Lerp(const NzRect& from, const NzRect& to, T interpolation); + T x, y, width, height; }; diff --git a/include/Nazara/Math/Rect.inl b/include/Nazara/Math/Rect.inl index 3984593c0..4d4e73e7b 100644 --- a/include/Nazara/Math/Rect.inl +++ b/include/Nazara/Math/Rect.inl @@ -195,6 +195,26 @@ T NzRect::operator[](unsigned int i) const return *(&x+i); } +template +NzRect NzRect::Lerp(const NzRect& from, const NzRect& to, T interpolation) +{ + #ifdef NAZARA_DEBUG + if (interpolation < F(0.0) || interpolation > F(1.0)) + { + NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); + return Zero(); + } + #endif + + NzRect rect; + rect.x = NzLerp(from.x, to.x, interpolation); + rect.y = NzLerp(from.y, to.y, interpolation); + rect.width = NzLerp(from.width, to.width, interpolation); + rect.height = NzLerp(from.height, to.height, interpolation); + + return rect; +} + template std::ostream& operator<<(std::ostream& out, const NzRect& rect) { diff --git a/include/Nazara/Math/Vector2.hpp b/include/Nazara/Math/Vector2.hpp index 1cbf1a65a..c60f1950f 100644 --- a/include/Nazara/Math/Vector2.hpp +++ b/include/Nazara/Math/Vector2.hpp @@ -83,6 +83,7 @@ template class NzVector2 bool operator>(const NzVector2& vec) const; bool operator>=(const NzVector2& vec) const; + static NzVector2 Lerp(const NzVector2& from, const NzVector2& to, T interpolation); static NzVector2 UnitX(); static NzVector2 UnitY(); static NzVector2 Zero(); diff --git a/include/Nazara/Math/Vector2.inl b/include/Nazara/Math/Vector2.inl index 4ea664b61..7e8ebaeab 100644 --- a/include/Nazara/Math/Vector2.inl +++ b/include/Nazara/Math/Vector2.inl @@ -428,6 +428,12 @@ bool NzVector2::operator>=(const NzVector2& vec) const return !operator<(vec); } +template +NzVector2 NzVector2::Lerp(const NzVector2& from, const NzVector2& to, T interpolation) +{ + return NzLerp(from, to, interpolation); +} + template NzVector2 NzVector2::UnitX() { diff --git a/include/Nazara/Math/Vector3.hpp b/include/Nazara/Math/Vector3.hpp index 2750c6b7b..8353af6fd 100644 --- a/include/Nazara/Math/Vector3.hpp +++ b/include/Nazara/Math/Vector3.hpp @@ -96,6 +96,7 @@ template class NzVector3 static T DotProduct(const NzVector3& vec1, const NzVector3& vec2); static NzVector3 Forward(); static NzVector3 Left(); + static NzVector3 Lerp(const NzVector3& from, const NzVector3& to, T interpolation); static NzVector3 Normalize(const NzVector3& vec); static NzVector3 UnitX(); static NzVector3 UnitY(); diff --git a/include/Nazara/Math/Vector3.inl b/include/Nazara/Math/Vector3.inl index d0c72b984..3725d5900 100644 --- a/include/Nazara/Math/Vector3.inl +++ b/include/Nazara/Math/Vector3.inl @@ -515,6 +515,12 @@ NzVector3 NzVector3::Left() return vector; } +template +NzVector3 NzVector3::Lerp(const NzVector3& from, const NzVector3& to, T interpolation) +{ + return NzLerp(from, to, interpolation); +} + template NzVector3 NzVector3::Normalize(const NzVector3& vec) { From 94268ae6b236fd523b34719608d3771a2b88c5af Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 8 Oct 2012 15:35:02 +0200 Subject: [PATCH 05/27] Fixed typo Former-commit-id: 3a47a8981692b57ccad50e1044c1139c34bf9c75 --- include/Nazara/Math/Matrix4.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/Nazara/Math/Matrix4.inl b/include/Nazara/Math/Matrix4.inl index 3d6d30f7c..9b12fecfb 100644 --- a/include/Nazara/Math/Matrix4.inl +++ b/include/Nazara/Math/Matrix4.inl @@ -309,7 +309,7 @@ void NzMatrix4::MakeRotation(const NzQuaternion& rotation) | 2XZ + 2YW 2YZ - 2XW 1 - 2X - 2Y | | | */ - ///FIXME: À corriger (Rotation quaternino != rotation matricielle) + ///FIXME: À corriger (Rotation quaternion != rotation matricielle) Set(F(1.0) - F(2.0)*rotation.y*rotation.y - F(2.0)*rotation.z*rotation.z, F(2.0)*rotation.x*rotation.y + F(2.0)*rotation.z*rotation.w, F(2.0)*rotation.x*rotation.z - F(2.0)*rotation.y*rotation.w, From 5bbc8d0fa44b8c553061d77703ab58953dceb0a1 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 8 Oct 2012 23:08:46 +0200 Subject: [PATCH 06/27] Fixed debug-mode compilation -Also added multiplication operator and equality comparison to Cube and Rect Former-commit-id: b4194a50fbe3025d3be1fc25d48a85d5a05fc5ac --- include/Nazara/Math/Basic.hpp | 2 +- include/Nazara/Math/Basic.inl | 12 ++-- include/Nazara/Math/Cube.hpp | 10 +++ include/Nazara/Math/Cube.inl | 47 ++++++++++++++ include/Nazara/Math/Rect.hpp | 10 +++ include/Nazara/Math/Rect.inl | 44 +++++++++++++ include/Nazara/Utility/AxisAlignedBox.hpp | 3 + src/Nazara/Utility/AxisAlignedBox.cpp | 78 ++++++++++++++++++++--- 8 files changed, 190 insertions(+), 16 deletions(-) diff --git a/include/Nazara/Math/Basic.hpp b/include/Nazara/Math/Basic.hpp index c1d98ca10..41f695235 100644 --- a/include/Nazara/Math/Basic.hpp +++ b/include/Nazara/Math/Basic.hpp @@ -31,7 +31,7 @@ inline unsigned int NzGetNumberLength(unsigned long long number); inline unsigned int NzGetNumberLength(float number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); inline unsigned int NzGetNumberLength(double number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); inline unsigned int NzGetNumberLength(long double number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); -template T NzLerp(T from, T to, F interpolation); +template T NzLerp(T from, T to, T2 interpolation); template T NzNormalizeAngle(T angle); template bool NzNumberEquals(T a, T b); inline NzString NzNumberToString(long long number, nzUInt8 radix = 10); diff --git a/include/Nazara/Math/Basic.inl b/include/Nazara/Math/Basic.inl index 57afcd2c0..f6323deee 100644 --- a/include/Nazara/Math/Basic.inl +++ b/include/Nazara/Math/Basic.inl @@ -11,6 +11,7 @@ #include #define F(a) static_cast(a) +#define F2(a) static_cast(a) template T NzApproach(T value, T objective, T increment) @@ -132,18 +133,18 @@ unsigned int NzGetNumberLength(long double number, nzUInt8 precision) return NzGetNumberLength(static_cast(number)) + precision + 1; // Plus un pour le point } -template -T NzLerp(T from, T to, F interpolation) +template +T NzLerp(T from, T to, T2 interpolation) { #ifdef NAZARA_DEBUG - if (interpolation < F(0.0) || interpolation > F(1.0)) + if (interpolation < F2(0.0) || interpolation > F2(1.0)) { NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); - return Zero(); + return F(0.0); } #endif - return from + interpolation*(to-from); + return from + interpolation*(to - from); } template @@ -284,6 +285,7 @@ long long NzStringToNumber(NzString str, nzUInt8 radix, bool* ok) return (negative) ? -static_cast(total) : total; } +#undef F2 #undef F #include diff --git a/include/Nazara/Math/Cube.hpp b/include/Nazara/Math/Cube.hpp index 8930831aa..0d3de354d 100644 --- a/include/Nazara/Math/Cube.hpp +++ b/include/Nazara/Math/Cube.hpp @@ -37,6 +37,8 @@ class NzCube bool IsValid() const; + void MakeZero(); + void Set(T X, T Y, T Z, T Width, T Height, T Depth); void Set(const T cube[6]); void Set(const NzRect& rect); @@ -50,7 +52,15 @@ class NzCube T& operator[](unsigned int i); T operator[](unsigned int i) const; + NzCube operator*(T scalar) const; + + NzCube& operator*=(T scalar); + + bool operator==(const NzCube& cube) const; + bool operator!=(const NzCube& cube) const; + static NzCube Lerp(const NzCube& from, const NzCube& to, T interpolation); + static NzCube Zero(); T x, y, z, width, height, depth; }; diff --git a/include/Nazara/Math/Cube.inl b/include/Nazara/Math/Cube.inl index 4ea4b07c1..3d02c2327 100644 --- a/include/Nazara/Math/Cube.inl +++ b/include/Nazara/Math/Cube.inl @@ -127,6 +127,17 @@ bool NzCube::IsValid() const return width > F(0.0) && height > F(0.0) && depth > F(0.0); } +template +void NzCube::MakeZero() +{ + x = F(0.0); + y = F(0.0); + z = F(0.0); + width = F(0.0); + height = F(0.0); + depth = F(0.0); +} + template void NzCube::Set(T X, T Y, T Z, T Width, T Height, T Depth) { @@ -229,6 +240,33 @@ T NzCube::operator[](unsigned int i) const return *(&x+i); } +template +NzCube NzCube::operator*(T scalar) const +{ + return NzCube(x, y, z, width*scalar, height*scalar, depth*scalar); +} + +template +NzCube& NzCube::operator*=(T scalar) +{ + width *= scalar; + height *= scalar; + depth *= scalar; +} + +template +bool NzCube::operator==(const NzCube& cube) const +{ + return NzNumberEquals(x, cube.x) && NzNumberEquals(y, cube.y) && NzNumberEquals(z, cube.z) && + NzNumberEquals(width, cube.width) && NzNumberEquals(height, cube.height) && NzNumberEquals(depth, cube.depth); +} + +template +bool NzCube::operator!=(const NzCube& cube) const +{ + return !operator==(cube); +} + template NzCube NzCube::Lerp(const NzCube& from, const NzCube& to, T interpolation) { @@ -251,6 +289,15 @@ NzCube NzCube::Lerp(const NzCube& from, const NzCube& to, T interpolation) return cube; } +template +NzCube NzCube::Zero() +{ + NzCube cube; + cube.MakeZero(); + + return cube; +} + template std::ostream& operator<<(std::ostream& out, const NzCube& cube) { diff --git a/include/Nazara/Math/Rect.hpp b/include/Nazara/Math/Rect.hpp index d7a6162dd..0d373507d 100644 --- a/include/Nazara/Math/Rect.hpp +++ b/include/Nazara/Math/Rect.hpp @@ -35,6 +35,8 @@ class NzRect bool IsValid() const; + void MakeZero(); + void Set(T X, T Y, T Width, T Height); void Set(const T rect[4]); void Set(const NzVector2& vec1, const NzVector2& vec2); @@ -47,7 +49,15 @@ class NzRect T& operator[](unsigned int i); T operator[](unsigned int i) const; + NzRect operator*(T scalar) const; + + NzRect& operator*=(T scalar); + + bool operator==(const NzRect& rect) const; + bool operator!=(const NzRect& rect) const; + static NzRect Lerp(const NzRect& from, const NzRect& to, T interpolation); + static NzRect Zero(); T x, y, width, height; }; diff --git a/include/Nazara/Math/Rect.inl b/include/Nazara/Math/Rect.inl index 4d4e73e7b..62f7684f4 100644 --- a/include/Nazara/Math/Rect.inl +++ b/include/Nazara/Math/Rect.inl @@ -112,6 +112,15 @@ bool NzRect::IsValid() const return width > F(0.0) && height > F(0.0); } +template +void NzRect::MakeZero() +{ + x = F(0.0); + y = F(0.0); + width = F(0.0); + height = F(0.0); +} + template void NzRect::Set(T X, T Y, T Width, T Height) { @@ -195,6 +204,32 @@ T NzRect::operator[](unsigned int i) const return *(&x+i); } +template +NzRect NzRect::operator*(T scalar) const +{ + return NzRect(x, y, width*scalar, height*scalar); +} + +template +NzRect& NzRect::operator*=(T scalar) +{ + width *= scalar; + height *= scalar; +} + +template +bool NzRect::operator==(const NzRect& rect) const +{ + return NzNumberEquals(x, rect.x) && NzNumberEquals(y, rect.y) && + NzNumberEquals(width, rect.width) && NzNumberEquals(height, rect.height); +} + +template +bool NzRect::operator!=(const NzRect& rect) const +{ + return !operator==(rect); +} + template NzRect NzRect::Lerp(const NzRect& from, const NzRect& to, T interpolation) { @@ -215,6 +250,15 @@ NzRect NzRect::Lerp(const NzRect& from, const NzRect& to, T interpolation) return rect; } +template +NzRect NzRect::Zero() +{ + NzRect rect; + rect.MakeZero(); + + return rect; +} + template std::ostream& operator<<(std::ostream& out, const NzRect& rect) { diff --git a/include/Nazara/Utility/AxisAlignedBox.hpp b/include/Nazara/Utility/AxisAlignedBox.hpp index 47934cda1..1f0a123be 100644 --- a/include/Nazara/Utility/AxisAlignedBox.hpp +++ b/include/Nazara/Utility/AxisAlignedBox.hpp @@ -15,6 +15,7 @@ class NAZARA_API NzAxisAlignedBox { public: NzAxisAlignedBox(); + NzAxisAlignedBox(const NzCubef& cube); NzAxisAlignedBox(const NzVector3f& vec1, const NzVector3f& vec2); NzAxisAlignedBox(nzExtend extend); @@ -37,6 +38,8 @@ class NAZARA_API NzAxisAlignedBox NzString ToString() const; + static NzAxisAlignedBox Lerp(const NzAxisAlignedBox& from, const NzAxisAlignedBox& to, float interpolation); + static const NzAxisAlignedBox Infinite; static const NzAxisAlignedBox Null; diff --git a/src/Nazara/Utility/AxisAlignedBox.cpp b/src/Nazara/Utility/AxisAlignedBox.cpp index 67a9fe15e..75b23468a 100644 --- a/src/Nazara/Utility/AxisAlignedBox.cpp +++ b/src/Nazara/Utility/AxisAlignedBox.cpp @@ -11,6 +11,12 @@ m_extend(nzExtend_Null) { } +NzAxisAlignedBox::NzAxisAlignedBox(const NzCubef& cube) : +m_extend(nzExtend_Finite), +m_cube(cube) +{ +} + NzAxisAlignedBox::NzAxisAlignedBox(const NzVector3f& vec1, const NzVector3f& vec2) : m_extend(nzExtend_Finite), m_cube(vec1, vec2) @@ -37,7 +43,6 @@ void NzAxisAlignedBox::ExtendTo(const NzAxisAlignedBox& box) switch (m_extend) { case nzExtend_Finite: - { switch (box.m_extend) { case nzExtend_Finite: @@ -50,18 +55,16 @@ void NzAxisAlignedBox::ExtendTo(const NzAxisAlignedBox& box) case nzExtend_Null: break; - - break; } + break; - case nzExtend_Infinite: - // Rien à faire - break; + case nzExtend_Infinite: + // Rien à faire + break; - case nzExtend_Null: - operator=(box); - break; - } + case nzExtend_Null: + operator=(box); + break; } } @@ -152,6 +155,61 @@ NzString NzAxisAlignedBox::ToString() const return "NzAxisAlignedBox(ERROR)"; } +NzAxisAlignedBox NzAxisAlignedBox::Lerp(const NzAxisAlignedBox& from, const NzAxisAlignedBox& to, float interpolation) +{ + #ifdef NAZARA_DEBUG + if (interpolation < 0.f || interpolation > 1.f) + { + NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); + return Null; + } + #endif + + if (NzNumberEquals(interpolation, 0.f)) + return from; + + if (NzNumberEquals(interpolation, 1.f)) + return to; + + switch (to.m_extend) + { + case nzExtend_Finite: + { + switch (from.m_extend) + { + case nzExtend_Finite: + return NzCubef::Lerp(from.m_cube, to.m_cube, interpolation); + + case nzExtend_Infinite: + return Infinite; + + case nzExtend_Null: + return from.m_cube * interpolation; + } + } + + case nzExtend_Infinite: + return Infinite; // Un petit peu d'infini est infini quand même ;) + + case nzExtend_Null: + { + switch (from.m_extend) + { + case nzExtend_Finite: + return from.m_cube * (1.f - interpolation); + + case nzExtend_Infinite: + return Infinite; + + case nzExtend_Null: + return Null; + } + } + } + + return Null; +} + const NzAxisAlignedBox NzAxisAlignedBox::Infinite(nzExtend_Infinite); const NzAxisAlignedBox NzAxisAlignedBox::Null(nzExtend_Null); From bb003885bf659495a67f2482c39f8465f9bf3ecc Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 24 Oct 2012 17:59:14 +0200 Subject: [PATCH 07/27] Fixed premake file of examples and template Former-commit-id: 9fb60b867d80450a6afb97dbc65904bfee89b377 --- NazaraModuleTemplate/build/scripts/module/modulename.lua | 5 ++--- examples/AnimatedMesh/build.lua | 2 +- examples/DopplerEffect/build.lua | 2 +- examples/ListSequences/build.lua | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/NazaraModuleTemplate/build/scripts/module/modulename.lua b/NazaraModuleTemplate/build/scripts/module/modulename.lua index 421c42306..0fa66f65e 100644 --- a/NazaraModuleTemplate/build/scripts/module/modulename.lua +++ b/NazaraModuleTemplate/build/scripts/module/modulename.lua @@ -1,4 +1,4 @@ -if (not _OPTIONS["one-library"]) then +if (not _OPTIONS["united"]) then project "NazaraModuleName" end @@ -16,7 +16,7 @@ else excludes { "../src/Nazara/ModuleName/Win32/*.hpp", "../src/Nazara/ModuleName/Win32/*.cpp" } end -if (_OPTIONS["one-library"]) then +if (_OPTIONS["united"]) then excludes "../src/Nazara/ModuleName/Debug/Leaks.cpp" else configuration "DebugStatic" @@ -30,5 +30,4 @@ else configuration "ReleaseDLL" links "NazaraCore" -end end \ No newline at end of file diff --git a/examples/AnimatedMesh/build.lua b/examples/AnimatedMesh/build.lua index a5d657395..ec434dd22 100644 --- a/examples/AnimatedMesh/build.lua +++ b/examples/AnimatedMesh/build.lua @@ -2,7 +2,7 @@ kind "ConsoleApp" files "main.cpp" -if (_OPTIONS["one-library"]) then +if (_OPTIONS["united"]) then configuration "DebugStatic" links "NazaraEngine-s-d" diff --git a/examples/DopplerEffect/build.lua b/examples/DopplerEffect/build.lua index 6e48bff00..92711ed35 100644 --- a/examples/DopplerEffect/build.lua +++ b/examples/DopplerEffect/build.lua @@ -2,7 +2,7 @@ kind "ConsoleApp" files "main.cpp" -if (_OPTIONS["one-library"]) then +if (_OPTIONS["united"]) then configuration "DebugStatic" links "NazaraEngine-s-d" diff --git a/examples/ListSequences/build.lua b/examples/ListSequences/build.lua index d43cb7d64..2e4c8c7a2 100644 --- a/examples/ListSequences/build.lua +++ b/examples/ListSequences/build.lua @@ -2,7 +2,7 @@ kind "ConsoleApp" files "main.cpp" -if (_OPTIONS["one-library"]) then +if (_OPTIONS["united"]) then configuration "DebugStatic" links "NazaraEngine-s-d" From 889a18b8be0ebfa3b18b32d2c931283a2a18155d Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 24 Oct 2012 17:59:22 +0200 Subject: [PATCH 08/27] Fixed typo Former-commit-id: 6695febce4d2d5d59d578c3219a5848b13b554f1 --- examples/DopplerEffect/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/DopplerEffect/main.cpp b/examples/DopplerEffect/main.cpp index 5176a8330..5f83fccab 100644 --- a/examples/DopplerEffect/main.cpp +++ b/examples/DopplerEffect/main.cpp @@ -34,7 +34,7 @@ int main() sound.EnableLooping(true); // La source du son se situe vers la gauche (Et un peu en avant) - sound.SetPosition(NzVector3f::Left()*50.f + NzVector3f::Forward()*5.); + sound.SetPosition(NzVector3f::Left()*50.f + NzVector3f::Forward()*5.f); // Et possède une vitesse de 10 par seconde vers la droite sound.SetVelocity(NzVector3f::Left()*-10.f); From 855b3676e832dcf4d235aab92ad6d0bf96f88519 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 24 Oct 2012 18:21:33 +0200 Subject: [PATCH 09/27] Fixed quaternion interpolation Former-commit-id: b43671708fb9e64517a67ba070167587688457fb --- include/Nazara/Math/Quaternion.inl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/Nazara/Math/Quaternion.inl b/include/Nazara/Math/Quaternion.inl index 6969aa3f4..ed88e5f5b 100644 --- a/include/Nazara/Math/Quaternion.inl +++ b/include/Nazara/Math/Quaternion.inl @@ -375,7 +375,13 @@ NzQuaternion NzQuaternion::Lerp(const NzQuaternion& from, const NzQuaterni } #endif - return from + interpolation*(to-from); + NzQuaternion interpolated; + interpolated.w = NzLerp(from.w, to.w, interpolation); + interpolated.x = NzLerp(from.x, to.x, interpolation); + interpolated.y = NzLerp(from.y, to.y, interpolation); + interpolated.z = NzLerp(from.z, to.z, interpolation); + + return interpolated; } template From 8712de311808fc5a31a3fb804217a228c1ca1235 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 24 Oct 2012 18:22:07 +0200 Subject: [PATCH 10/27] Cleaned NzNormalizeAngle code Former-commit-id: 3d984db7632e0c9cba032d22b1470828326c0ad9 --- include/Nazara/Math/Basic.inl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/Nazara/Math/Basic.inl b/include/Nazara/Math/Basic.inl index f6323deee..fa5d8bb70 100644 --- a/include/Nazara/Math/Basic.inl +++ b/include/Nazara/Math/Basic.inl @@ -151,7 +151,7 @@ template T NzNormalizeAngle(T angle) { #if NAZARA_MATH_ANGLE_RADIAN - const T limit = M_PI; + const T limit = F(M_PI); #else const T limit = F(180.0); #endif @@ -160,13 +160,13 @@ T NzNormalizeAngle(T angle) if (angle > F(0.0)) { angle += limit; - angle -= static_cast(angle/(F(2.0)*limit))*(F(2.0)*limit); + angle -= static_cast(angle / (F(2.0)*limit)) * (F(2.0)*limit); angle -= limit; } else { angle -= limit; - angle -= static_cast(angle/(F(2.0)*limit))*(F(2.0)*limit); + angle -= static_cast(angle / (F(2.0)*limit)) * (F(2.0)*limit); angle += limit; } From 5f95dbe82ec6109acb99fc74f761de409e0e056a Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 24 Oct 2012 18:41:13 +0200 Subject: [PATCH 11/27] (NzImage) Fixed Convert crash and optimized Fill Former-commit-id: 674acfd1e8e6fa28a8657ba45adedca311f54949 --- src/Nazara/Utility/Image.cpp | 39 +++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/Nazara/Utility/Image.cpp b/src/Nazara/Utility/Image.cpp index 021f4211d..8028de854 100644 --- a/src/Nazara/Utility/Image.cpp +++ b/src/Nazara/Utility/Image.cpp @@ -105,7 +105,8 @@ bool NzImage::Convert(nzPixelFormat format) for (unsigned int i = 0; i < m_sharedImage->levelCount; ++i) { unsigned int pixelsPerFace = width*height; - nzUInt8* ptr = new nzUInt8[pixelsPerFace*depth*NzPixelFormat::GetBytesPerPixel(format)]; + nzUInt8* face = new nzUInt8[pixelsPerFace*depth*NzPixelFormat::GetBytesPerPixel(format)]; + nzUInt8* ptr = face; nzUInt8* pixels = m_sharedImage->pixels[i]; unsigned int srcStride = pixelsPerFace * NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); unsigned int dstStride = pixelsPerFace * NzPixelFormat::GetBytesPerPixel(format); @@ -117,7 +118,7 @@ bool NzImage::Convert(nzPixelFormat format) NazaraError("Failed to convert image"); // Nettoyage de la mémoire - delete[] ptr; // Permet une optimisation de boucle (GCC) + delete[] face; // Permet une optimisation de boucle (GCC) for (unsigned int j = 0; j < i; ++j) delete[] levels[j]; @@ -130,7 +131,7 @@ bool NzImage::Convert(nzPixelFormat format) ptr += dstStride; } - levels[i] = ptr; + levels[i] = face; if (width > 1) width >>= 1; @@ -356,33 +357,39 @@ bool NzImage::Fill(const NzColor& color) } #endif - EnsureOwnership(); - nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); - nzUInt8* pixels = new nzUInt8[bpp]; - if (!NzPixelFormat::Convert(nzPixelFormat_RGBA8, m_sharedImage->format, &color.r, pixels)) + nzUInt8* colorBuffer = new nzUInt8[bpp]; + if (!NzPixelFormat::Convert(nzPixelFormat_RGBA8, m_sharedImage->format, &color.r, colorBuffer)) { NazaraError("Failed to convert RGBA8 to " + NzPixelFormat::ToString(m_sharedImage->format)); - delete[] pixels; + delete[] colorBuffer; return false; } + nzUInt8** levels = new nzUInt8*[m_sharedImage->levelCount]; + unsigned int width = m_sharedImage->width; unsigned int height = m_sharedImage->height; + + // Les images 3D et cubemaps sont stockés de la même façon unsigned int depth = (m_sharedImage->type == nzImageType_Cubemap) ? 6 : m_sharedImage->depth; - for (unsigned int level = 0; level < m_sharedImage->levelCount; ++level) + for (unsigned int i = 0; i < m_sharedImage->levelCount; ++i) { - nzUInt8* ptr = &m_sharedImage->pixels[level][0]; - nzUInt8* end = &m_sharedImage->pixels[level][width*height*depth*bpp]; + unsigned int size = width*height*depth*bpp; + nzUInt8* face = new nzUInt8[size]; + nzUInt8* ptr = face; + nzUInt8* end = &ptr[size]; while (ptr < end) { - std::memcpy(ptr, pixels, bpp); + std::memcpy(ptr, colorBuffer, bpp); ptr += bpp; } + levels[i] = face; + if (width > 1U) width >>= 1; @@ -393,7 +400,12 @@ bool NzImage::Fill(const NzColor& color) depth >>= 1; } - delete[] pixels; + delete[] colorBuffer; + + SharedImage* newImage = new SharedImage(1, m_sharedImage->type, m_sharedImage->format, m_sharedImage->levelCount, levels, m_sharedImage->width, m_sharedImage->height, m_sharedImage->depth); + + ReleaseImage(); + m_sharedImage = newImage; return true; } @@ -562,7 +574,6 @@ bool NzImage::FlipHorizontally() return true; } - bool NzImage::FlipVertically() { #if NAZARA_UTILITY_SAFE From 61c081ace3a533b15448b13444ea3e486b27752f Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 24 Oct 2012 22:32:46 +0200 Subject: [PATCH 12/27] Fixed and improved Matrix class (See description) -Added ApplyScale -Added Destroy() -Added GetInverseAffine -Added Inverse(Affine) -Added (Make)Transform method, generating a TRS transform matrix -Fixed COW -Inverse methods now takes an optionnal boolean pointer Former-commit-id: 84e7c679c242876f9b0888875b37afa12eed311a --- include/Nazara/Math/Matrix4.hpp | 44 ++-- include/Nazara/Math/Matrix4.inl | 427 +++++++++++++++++++++++++++----- 2 files changed, 397 insertions(+), 74 deletions(-) diff --git a/include/Nazara/Math/Matrix4.hpp b/include/Nazara/Math/Matrix4.hpp index 51bdaf0ce..849934710 100644 --- a/include/Nazara/Math/Matrix4.hpp +++ b/include/Nazara/Math/Matrix4.hpp @@ -38,11 +38,16 @@ class NzMatrix4 NzMatrix4(NzMatrix4&& matrix) noexcept; ~NzMatrix4(); + NzMatrix4& ApplyScale(const NzVector3& scale); + NzMatrix4 Concatenate(const NzMatrix4& matrix) const; NzMatrix4 ConcatenateAffine(const NzMatrix4& matrix) const; + void Destroy(); + T GetDeterminant() const; - NzMatrix4 GetInverse() const; + NzMatrix4 GetInverse(bool* succeeded = nullptr) const; + NzMatrix4 GetInverseAffine(bool* succeeded = nullptr) const; NzQuaternion GetRotation() const; //NzMatrix3 GetRotationMatrix() const; NzVector3 GetScale() const; @@ -52,30 +57,34 @@ class NzMatrix4 bool HasNegativeScale() const; bool HasScale() const; + NzMatrix4& Inverse(bool* succeeded = nullptr); + NzMatrix4& InverseAffine(bool* succeeded = nullptr); + bool IsAffine() const; bool IsDefined() const; - void MakeIdentity(); - void MakeLookAt(const NzVector3& eye, const NzVector3& target, const NzVector3& up = NzVector3::Up()); - void MakeOrtho(T left, T top, T width, T height, T zNear = -1.0, T zFar = 1.0); - void MakePerspective(T angle, T ratio, T zNear, T zFar); - void MakeRotation(const NzQuaternion& rotation); - void MakeScale(const NzVector3& scale); - void MakeTranslation(const NzVector3& translation); - void MakeZero(); + NzMatrix4& MakeIdentity(); + NzMatrix4& MakeLookAt(const NzVector3& eye, const NzVector3& target, const NzVector3& up = NzVector3::Up()); + NzMatrix4& MakeOrtho(T left, T top, T width, T height, T zNear = -1.0, T zFar = 1.0); + NzMatrix4& MakePerspective(T angle, T ratio, T zNear, T zFar); + NzMatrix4& MakeRotation(const NzQuaternion& rotation); + NzMatrix4& MakeScale(const NzVector3& scale); + NzMatrix4& MakeTranslation(const NzVector3& translation); + NzMatrix4& MakeTransform(const NzVector3& translation, const NzVector3& scale, const NzQuaternion& rotation); + NzMatrix4& MakeZero(); - void Set(T r11, T r12, T r13, T r14, + NzMatrix4& Set(T r11, T r12, T r13, T r14, T r21, T r22, T r23, T r24, T r31, T r32, T r33, T r34, T r41, T r42, T r43, T r44); - void Set(const T matrix[16]); + NzMatrix4& Set(const T matrix[16]); //NzMatrix4(const NzMatrix3& matrix); - void Set(const NzMatrix4& matrix); - void Set(NzMatrix4&& matrix); - template void Set(const NzMatrix4& matrix); - void SetRotation(const NzQuaternion& rotation); - void SetScale(const NzVector3& scale); - void SetTranslation(const NzVector3& translation); + NzMatrix4& Set(const NzMatrix4& matrix); + NzMatrix4& Set(NzMatrix4&& matrix); + template NzMatrix4& Set(const NzMatrix4& matrix); + NzMatrix4& SetRotation(const NzQuaternion& rotation); + NzMatrix4& SetScale(const NzVector3& scale); + NzMatrix4& SetTranslation(const NzVector3& translation); NzString ToString() const; @@ -117,6 +126,7 @@ class NzMatrix4 static NzMatrix4 Rotate(const NzQuaternion& rotation); static NzMatrix4 Scale(const NzVector3& scale); static NzMatrix4 Translate(const NzVector3& translation); + static NzMatrix4 Transform(const NzVector3& translation, const NzVector3& scale, const NzQuaternion& rotation); static NzMatrix4 Zero(); struct SharedMatrix diff --git a/include/Nazara/Math/Matrix4.inl b/include/Nazara/Math/Matrix4.inl index 9b12fecfb..0deb8f00b 100644 --- a/include/Nazara/Math/Matrix4.inl +++ b/include/Nazara/Math/Matrix4.inl @@ -16,7 +16,7 @@ #include #include //#include -///FIXME: Le MLT détecte de faux-leaks ici (Problème lié aux inline ?) +///FIXME: Le MLT détecte des leaks ici, mais dont la véracité n'a pu être prouvée (Problème lié aux classes inlines ?) #define F(a) static_cast(a) @@ -68,6 +68,24 @@ NzMatrix4::~NzMatrix4() ReleaseMatrix(); } +template +NzMatrix4& NzMatrix4::ApplyScale(const NzVector3& scale) +{ + m_sharedMatrix->m11 *= scale.x; + m_sharedMatrix->m12 *= scale.x; + m_sharedMatrix->m13 *= scale.x; + + m_sharedMatrix->m21 *= scale.y; + m_sharedMatrix->m22 *= scale.y; + m_sharedMatrix->m23 *= scale.y; + + m_sharedMatrix->m31 *= scale.z; + m_sharedMatrix->m32 *= scale.z; + m_sharedMatrix->m33 *= scale.z; + + return *this; +} + template NzMatrix4 NzMatrix4::Concatenate(const NzMatrix4& matrix) const { @@ -80,6 +98,12 @@ NzMatrix4 NzMatrix4::ConcatenateAffine(const NzMatrix4& matrix) const return ConcatenateAffine(*this, matrix); } +template +void NzMatrix4::Destroy() +{ + ReleaseMatrix(); +} + template T NzMatrix4::GetDeterminant() const { @@ -100,45 +124,17 @@ T NzMatrix4::GetDeterminant() const } template -NzMatrix4 NzMatrix4::GetInverse() const +NzMatrix4 NzMatrix4::GetInverse(bool* succeeded) const { - #if NAZARA_MATH_SAFE - if (!IsDefined()) - { - NazaraError("Matrix not defined"); - return NzMatrix4(); - } - #endif + NzMatrix4f matInv(*this); + return matInv.Inverse(succeeded); +} - T det = GetDeterminant(); - if (!NzNumberEquals(det, F(0.0))) - { - return NzMatrix4((m_sharedMatrix->m22*(m_sharedMatrix->m33*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m43) - m_sharedMatrix->m32*(m_sharedMatrix->m23*m_sharedMatrix->m44 - m_sharedMatrix->m43*m_sharedMatrix->m24) + m_sharedMatrix->m42*(m_sharedMatrix->m23*m_sharedMatrix->m34 - m_sharedMatrix->m33*m_sharedMatrix->m24)) / det, - -(m_sharedMatrix->m12*(m_sharedMatrix->m33*m_sharedMatrix->m44 - m_sharedMatrix->m43*m_sharedMatrix->m34) - m_sharedMatrix->m32*(m_sharedMatrix->m13*m_sharedMatrix->m44 - m_sharedMatrix->m43*m_sharedMatrix->m14) + m_sharedMatrix->m42*(m_sharedMatrix->m13*m_sharedMatrix->m34 - m_sharedMatrix->m33*m_sharedMatrix->m14)) / det, - (m_sharedMatrix->m12*(m_sharedMatrix->m23*m_sharedMatrix->m44 - m_sharedMatrix->m43*m_sharedMatrix->m24) - m_sharedMatrix->m22*(m_sharedMatrix->m13*m_sharedMatrix->m44 - m_sharedMatrix->m43*m_sharedMatrix->m14) + m_sharedMatrix->m42*(m_sharedMatrix->m13*m_sharedMatrix->m24 - m_sharedMatrix->m23*m_sharedMatrix->m14)) / det, - -(m_sharedMatrix->m12*(m_sharedMatrix->m23*m_sharedMatrix->m34 - m_sharedMatrix->m33*m_sharedMatrix->m24) - m_sharedMatrix->m22*(m_sharedMatrix->m13*m_sharedMatrix->m34 - m_sharedMatrix->m33*m_sharedMatrix->m14) + m_sharedMatrix->m32*(m_sharedMatrix->m13*m_sharedMatrix->m24 - m_sharedMatrix->m23*m_sharedMatrix->m14)) / det, - - -(m_sharedMatrix->m21*(m_sharedMatrix->m33*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m43) - m_sharedMatrix->m23*(m_sharedMatrix->m31*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m41) + m_sharedMatrix->m24*(m_sharedMatrix->m31*m_sharedMatrix->m43 - m_sharedMatrix->m33*m_sharedMatrix->m41)) / det, - (m_sharedMatrix->m11*(m_sharedMatrix->m33*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m43) - m_sharedMatrix->m13*(m_sharedMatrix->m31*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m41) + m_sharedMatrix->m14*(m_sharedMatrix->m31*m_sharedMatrix->m43 - m_sharedMatrix->m33*m_sharedMatrix->m41)) / det, - -(m_sharedMatrix->m11*(m_sharedMatrix->m23*m_sharedMatrix->m44 - m_sharedMatrix->m24*m_sharedMatrix->m43) - m_sharedMatrix->m13*(m_sharedMatrix->m21*m_sharedMatrix->m44 - m_sharedMatrix->m24*m_sharedMatrix->m41) + m_sharedMatrix->m14*(m_sharedMatrix->m21*m_sharedMatrix->m43 - m_sharedMatrix->m23*m_sharedMatrix->m41)) / det, - (m_sharedMatrix->m11*(m_sharedMatrix->m23*m_sharedMatrix->m34 - m_sharedMatrix->m24*m_sharedMatrix->m33) - m_sharedMatrix->m13*(m_sharedMatrix->m21*m_sharedMatrix->m34 - m_sharedMatrix->m24*m_sharedMatrix->m31) + m_sharedMatrix->m14*(m_sharedMatrix->m21*m_sharedMatrix->m33 - m_sharedMatrix->m23*m_sharedMatrix->m31)) / det, - - (m_sharedMatrix->m21*(m_sharedMatrix->m32*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m42) - m_sharedMatrix->m22*(m_sharedMatrix->m31*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m41) + m_sharedMatrix->m24*(m_sharedMatrix->m31*m_sharedMatrix->m42 - m_sharedMatrix->m32*m_sharedMatrix->m41)) / det, - -(m_sharedMatrix->m11*(m_sharedMatrix->m32*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m42) - m_sharedMatrix->m12*(m_sharedMatrix->m31*m_sharedMatrix->m44 - m_sharedMatrix->m34*m_sharedMatrix->m41) + m_sharedMatrix->m14*(m_sharedMatrix->m31*m_sharedMatrix->m42 - m_sharedMatrix->m32*m_sharedMatrix->m41)) / det, - (m_sharedMatrix->m11*(m_sharedMatrix->m22*m_sharedMatrix->m44 - m_sharedMatrix->m24*m_sharedMatrix->m42) - m_sharedMatrix->m12*(m_sharedMatrix->m21*m_sharedMatrix->m44 - m_sharedMatrix->m24*m_sharedMatrix->m41) + m_sharedMatrix->m14*(m_sharedMatrix->m21*m_sharedMatrix->m42 - m_sharedMatrix->m22*m_sharedMatrix->m41)) / det, - -(m_sharedMatrix->m11*(m_sharedMatrix->m22*m_sharedMatrix->m34 - m_sharedMatrix->m24*m_sharedMatrix->m32) - m_sharedMatrix->m12*(m_sharedMatrix->m21*m_sharedMatrix->m34 - m_sharedMatrix->m24*m_sharedMatrix->m31) + m_sharedMatrix->m14*(m_sharedMatrix->m21*m_sharedMatrix->m32 - m_sharedMatrix->m22*m_sharedMatrix->m31)) / det, - - -(m_sharedMatrix->m21*(m_sharedMatrix->m32*m_sharedMatrix->m43 - m_sharedMatrix->m33*m_sharedMatrix->m42) - m_sharedMatrix->m22*(m_sharedMatrix->m31*m_sharedMatrix->m43 - m_sharedMatrix->m33*m_sharedMatrix->m41) + m_sharedMatrix->m23*(m_sharedMatrix->m31*m_sharedMatrix->m42 - m_sharedMatrix->m32*m_sharedMatrix->m41)) / det, - (m_sharedMatrix->m11*(m_sharedMatrix->m32*m_sharedMatrix->m43 - m_sharedMatrix->m33*m_sharedMatrix->m42) - m_sharedMatrix->m12*(m_sharedMatrix->m31*m_sharedMatrix->m43 - m_sharedMatrix->m33*m_sharedMatrix->m41) + m_sharedMatrix->m13*(m_sharedMatrix->m31*m_sharedMatrix->m42 - m_sharedMatrix->m32*m_sharedMatrix->m41)) / det, - -(m_sharedMatrix->m11*(m_sharedMatrix->m22*m_sharedMatrix->m43 - m_sharedMatrix->m23*m_sharedMatrix->m42) - m_sharedMatrix->m12*(m_sharedMatrix->m21*m_sharedMatrix->m43 - m_sharedMatrix->m23*m_sharedMatrix->m41) + m_sharedMatrix->m13*(m_sharedMatrix->m21*m_sharedMatrix->m42 - m_sharedMatrix->m22*m_sharedMatrix->m41)) / det, - (m_sharedMatrix->m11*(m_sharedMatrix->m22*m_sharedMatrix->m33 - m_sharedMatrix->m23*m_sharedMatrix->m32) - m_sharedMatrix->m12*(m_sharedMatrix->m21*m_sharedMatrix->m33 - m_sharedMatrix->m23*m_sharedMatrix->m31) + m_sharedMatrix->m13*(m_sharedMatrix->m21*m_sharedMatrix->m32 - m_sharedMatrix->m22*m_sharedMatrix->m31)) / det); - } - else - { - NazaraError("Matrix has no inverse"); - - return Identity(); - } +template +NzMatrix4 NzMatrix4::GetInverseAffine(bool* succeeded) const +{ + NzMatrix4f matInv(*this); + return matInv.InverseAffine(succeeded); } template @@ -220,6 +216,259 @@ bool NzMatrix4::HasScale() const return false; } +template +NzMatrix4& NzMatrix4::Inverse(bool* succeeded) +{ + #if NAZARA_MATH_SAFE + if (!IsDefined()) + { + NazaraError("Matrix not defined"); + if (succeeded) + *succeeded = false; + + return *this; + } + #endif + + T det = GetDeterminant(); + if (!NzNumberEquals(det, F(0.0))) + { + // http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix + T inv[16]; + inv[0] = m_sharedMatrix->m22 * m_sharedMatrix->m33 * m_sharedMatrix->m44 - + m_sharedMatrix->m22 * m_sharedMatrix->m34 * m_sharedMatrix->m43 - + m_sharedMatrix->m32 * m_sharedMatrix->m23 * m_sharedMatrix->m44 + + m_sharedMatrix->m32 * m_sharedMatrix->m24 * m_sharedMatrix->m43 + + m_sharedMatrix->m42 * m_sharedMatrix->m23 * m_sharedMatrix->m34 - + m_sharedMatrix->m42 * m_sharedMatrix->m24 * m_sharedMatrix->m33; + + inv[1] = -m_sharedMatrix->m12 * m_sharedMatrix->m33 * m_sharedMatrix->m44 + + m_sharedMatrix->m12 * m_sharedMatrix->m34 * m_sharedMatrix->m43 + + m_sharedMatrix->m32 * m_sharedMatrix->m13 * m_sharedMatrix->m44 - + m_sharedMatrix->m32 * m_sharedMatrix->m14 * m_sharedMatrix->m43 - + m_sharedMatrix->m42 * m_sharedMatrix->m13 * m_sharedMatrix->m34 + + m_sharedMatrix->m42 * m_sharedMatrix->m14 * m_sharedMatrix->m33; + + inv[2] = m_sharedMatrix->m12 * m_sharedMatrix->m23 * m_sharedMatrix->m44 - + m_sharedMatrix->m12 * m_sharedMatrix->m24 * m_sharedMatrix->m43 - + m_sharedMatrix->m22 * m_sharedMatrix->m13 * m_sharedMatrix->m44 + + m_sharedMatrix->m22 * m_sharedMatrix->m14 * m_sharedMatrix->m43 + + m_sharedMatrix->m42 * m_sharedMatrix->m13 * m_sharedMatrix->m24 - + m_sharedMatrix->m42 * m_sharedMatrix->m14 * m_sharedMatrix->m23; + + inv[3] = -m_sharedMatrix->m12 * m_sharedMatrix->m23 * m_sharedMatrix->m34 + + m_sharedMatrix->m12 * m_sharedMatrix->m24 * m_sharedMatrix->m33 + + m_sharedMatrix->m22 * m_sharedMatrix->m13 * m_sharedMatrix->m34 - + m_sharedMatrix->m22 * m_sharedMatrix->m14 * m_sharedMatrix->m33 - + m_sharedMatrix->m32 * m_sharedMatrix->m13 * m_sharedMatrix->m24 + + m_sharedMatrix->m32 * m_sharedMatrix->m14 * m_sharedMatrix->m23; + + inv[4] = -m_sharedMatrix->m21 * m_sharedMatrix->m33 * m_sharedMatrix->m44 + + m_sharedMatrix->m21 * m_sharedMatrix->m34 * m_sharedMatrix->m43 + + m_sharedMatrix->m31 * m_sharedMatrix->m23 * m_sharedMatrix->m44 - + m_sharedMatrix->m31 * m_sharedMatrix->m24 * m_sharedMatrix->m43 - + m_sharedMatrix->m41 * m_sharedMatrix->m23 * m_sharedMatrix->m34 + + m_sharedMatrix->m41 * m_sharedMatrix->m24 * m_sharedMatrix->m33; + + inv[5] = m_sharedMatrix->m11 * m_sharedMatrix->m33 * m_sharedMatrix->m44 - + m_sharedMatrix->m11 * m_sharedMatrix->m34 * m_sharedMatrix->m43 - + m_sharedMatrix->m31 * m_sharedMatrix->m13 * m_sharedMatrix->m44 + + m_sharedMatrix->m31 * m_sharedMatrix->m14 * m_sharedMatrix->m43 + + m_sharedMatrix->m41 * m_sharedMatrix->m13 * m_sharedMatrix->m34 - + m_sharedMatrix->m41 * m_sharedMatrix->m14 * m_sharedMatrix->m33; + + inv[6] = -m_sharedMatrix->m11 * m_sharedMatrix->m23 * m_sharedMatrix->m44 + + m_sharedMatrix->m11 * m_sharedMatrix->m24 * m_sharedMatrix->m43 + + m_sharedMatrix->m21 * m_sharedMatrix->m13 * m_sharedMatrix->m44 - + m_sharedMatrix->m21 * m_sharedMatrix->m14 * m_sharedMatrix->m43 - + m_sharedMatrix->m41 * m_sharedMatrix->m13 * m_sharedMatrix->m24 + + m_sharedMatrix->m41 * m_sharedMatrix->m14 * m_sharedMatrix->m23; + + inv[7] = m_sharedMatrix->m11 * m_sharedMatrix->m23 * m_sharedMatrix->m34 - + m_sharedMatrix->m11 * m_sharedMatrix->m24 * m_sharedMatrix->m33 - + m_sharedMatrix->m21 * m_sharedMatrix->m13 * m_sharedMatrix->m34 + + m_sharedMatrix->m21 * m_sharedMatrix->m14 * m_sharedMatrix->m33 + + m_sharedMatrix->m31 * m_sharedMatrix->m13 * m_sharedMatrix->m24 - + m_sharedMatrix->m31 * m_sharedMatrix->m14 * m_sharedMatrix->m23; + + inv[8] = m_sharedMatrix->m21 * m_sharedMatrix->m32 * m_sharedMatrix->m44 - + m_sharedMatrix->m21 * m_sharedMatrix->m34 * m_sharedMatrix->m42 - + m_sharedMatrix->m31 * m_sharedMatrix->m22 * m_sharedMatrix->m44 + + m_sharedMatrix->m31 * m_sharedMatrix->m24 * m_sharedMatrix->m42 + + m_sharedMatrix->m41 * m_sharedMatrix->m22 * m_sharedMatrix->m34 - + m_sharedMatrix->m41 * m_sharedMatrix->m24 * m_sharedMatrix->m32; + + inv[9] = -m_sharedMatrix->m11 * m_sharedMatrix->m32 * m_sharedMatrix->m44 + + m_sharedMatrix->m11 * m_sharedMatrix->m34 * m_sharedMatrix->m42 + + m_sharedMatrix->m31 * m_sharedMatrix->m12 * m_sharedMatrix->m44 - + m_sharedMatrix->m31 * m_sharedMatrix->m14 * m_sharedMatrix->m42 - + m_sharedMatrix->m41 * m_sharedMatrix->m12 * m_sharedMatrix->m34 + + m_sharedMatrix->m41 * m_sharedMatrix->m14 * m_sharedMatrix->m32; + + inv[10] = m_sharedMatrix->m11 * m_sharedMatrix->m22 * m_sharedMatrix->m44 - + m_sharedMatrix->m11 * m_sharedMatrix->m24 * m_sharedMatrix->m42 - + m_sharedMatrix->m21 * m_sharedMatrix->m12 * m_sharedMatrix->m44 + + m_sharedMatrix->m21 * m_sharedMatrix->m14 * m_sharedMatrix->m42 + + m_sharedMatrix->m41 * m_sharedMatrix->m12 * m_sharedMatrix->m24 - + m_sharedMatrix->m41 * m_sharedMatrix->m14 * m_sharedMatrix->m22; + + inv[11] = -m_sharedMatrix->m11 * m_sharedMatrix->m22 * m_sharedMatrix->m34 + + m_sharedMatrix->m11 * m_sharedMatrix->m24 * m_sharedMatrix->m32 + + m_sharedMatrix->m21 * m_sharedMatrix->m12 * m_sharedMatrix->m34 - + m_sharedMatrix->m21 * m_sharedMatrix->m14 * m_sharedMatrix->m32 - + m_sharedMatrix->m31 * m_sharedMatrix->m12 * m_sharedMatrix->m24 + + m_sharedMatrix->m31 * m_sharedMatrix->m14 * m_sharedMatrix->m22; + + inv[12] = -m_sharedMatrix->m21 * m_sharedMatrix->m32 * m_sharedMatrix->m43 + + m_sharedMatrix->m21 * m_sharedMatrix->m33 * m_sharedMatrix->m42 + + m_sharedMatrix->m31 * m_sharedMatrix->m22 * m_sharedMatrix->m43 - + m_sharedMatrix->m31 * m_sharedMatrix->m23 * m_sharedMatrix->m42 - + m_sharedMatrix->m41 * m_sharedMatrix->m22 * m_sharedMatrix->m33 + + m_sharedMatrix->m41 * m_sharedMatrix->m23 * m_sharedMatrix->m32; + + inv[13] = m_sharedMatrix->m11 * m_sharedMatrix->m32 * m_sharedMatrix->m43 - + m_sharedMatrix->m11 * m_sharedMatrix->m33 * m_sharedMatrix->m42 - + m_sharedMatrix->m31 * m_sharedMatrix->m12 * m_sharedMatrix->m43 + + m_sharedMatrix->m31 * m_sharedMatrix->m13 * m_sharedMatrix->m42 + + m_sharedMatrix->m41 * m_sharedMatrix->m12 * m_sharedMatrix->m33 - + m_sharedMatrix->m41 * m_sharedMatrix->m13 * m_sharedMatrix->m32; + + inv[14] = -m_sharedMatrix->m11 * m_sharedMatrix->m22 * m_sharedMatrix->m43 + + m_sharedMatrix->m11 * m_sharedMatrix->m23 * m_sharedMatrix->m42 + + m_sharedMatrix->m21 * m_sharedMatrix->m12 * m_sharedMatrix->m43 - + m_sharedMatrix->m21 * m_sharedMatrix->m13 * m_sharedMatrix->m42 - + m_sharedMatrix->m41 * m_sharedMatrix->m12 * m_sharedMatrix->m23 + + m_sharedMatrix->m41 * m_sharedMatrix->m13 * m_sharedMatrix->m22; + + inv[15] = m_sharedMatrix->m11 * m_sharedMatrix->m22 * m_sharedMatrix->m33 - + m_sharedMatrix->m11 * m_sharedMatrix->m23 * m_sharedMatrix->m32 - + m_sharedMatrix->m21 * m_sharedMatrix->m12 * m_sharedMatrix->m33 + + m_sharedMatrix->m21 * m_sharedMatrix->m13 * m_sharedMatrix->m32 + + m_sharedMatrix->m31 * m_sharedMatrix->m12 * m_sharedMatrix->m23 - + m_sharedMatrix->m31 * m_sharedMatrix->m13 * m_sharedMatrix->m22; + + T invDet = F(1.0) / det; + for (unsigned int i = 0; i < 16; ++i) + inv[i] *= invDet; + + Set(inv); + if (succeeded) + *succeeded = true; + } + else + { + NazaraError("Matrix has no inverse"); + if (succeeded) + *succeeded = false; + } + + return *this; +} + +template +NzMatrix4& NzMatrix4::InverseAffine(bool* succeeded) +{ + #if NAZARA_MATH_SAFE + if (!IsDefined()) + { + NazaraError("Matrix not defined"); + if (succeeded) + *succeeded = false; + + return *this; + } + + if (!IsAffine()) + { + NazaraError("Matrix not affine"); + if (succeeded) + *succeeded = false; + + return *this; + } + #endif + + T det = GetDeterminant(); + if (!NzNumberEquals(det, F(0.0))) + { + // http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix + T inv[16]; + inv[0] = m_sharedMatrix->m22 * m_sharedMatrix->m33 - + m_sharedMatrix->m32 * m_sharedMatrix->m23; + + inv[1] = -m_sharedMatrix->m12 * m_sharedMatrix->m33 + + m_sharedMatrix->m32 * m_sharedMatrix->m13; + + inv[2] = m_sharedMatrix->m12 * m_sharedMatrix->m23 - + m_sharedMatrix->m22 * m_sharedMatrix->m13; + + inv[3] = F(0.0); + + inv[4] = -m_sharedMatrix->m21 * m_sharedMatrix->m33 + + m_sharedMatrix->m31 * m_sharedMatrix->m23; + + inv[5] = m_sharedMatrix->m11 * m_sharedMatrix->m33 - + m_sharedMatrix->m31 * m_sharedMatrix->m13; + + inv[6] = -m_sharedMatrix->m11 * m_sharedMatrix->m23 + + m_sharedMatrix->m21 * m_sharedMatrix->m13; + + inv[7] = F(0.0); + + inv[8] = m_sharedMatrix->m21 * m_sharedMatrix->m32 - + m_sharedMatrix->m31 * m_sharedMatrix->m22; + + inv[9] = -m_sharedMatrix->m11 * m_sharedMatrix->m32 + + m_sharedMatrix->m31 * m_sharedMatrix->m12; + + inv[10] = m_sharedMatrix->m11 * m_sharedMatrix->m22 - + m_sharedMatrix->m21 * m_sharedMatrix->m12; + + inv[11] = F(0.0); + + inv[12] = -m_sharedMatrix->m21 * m_sharedMatrix->m32 * m_sharedMatrix->m43 + + m_sharedMatrix->m21 * m_sharedMatrix->m33 * m_sharedMatrix->m42 + + m_sharedMatrix->m31 * m_sharedMatrix->m22 * m_sharedMatrix->m43 - + m_sharedMatrix->m31 * m_sharedMatrix->m23 * m_sharedMatrix->m42 - + m_sharedMatrix->m41 * m_sharedMatrix->m22 * m_sharedMatrix->m33 + + m_sharedMatrix->m41 * m_sharedMatrix->m23 * m_sharedMatrix->m32; + + inv[13] = m_sharedMatrix->m11 * m_sharedMatrix->m32 * m_sharedMatrix->m43 - + m_sharedMatrix->m11 * m_sharedMatrix->m33 * m_sharedMatrix->m42 - + m_sharedMatrix->m31 * m_sharedMatrix->m12 * m_sharedMatrix->m43 + + m_sharedMatrix->m31 * m_sharedMatrix->m13 * m_sharedMatrix->m42 + + m_sharedMatrix->m41 * m_sharedMatrix->m12 * m_sharedMatrix->m33 - + m_sharedMatrix->m41 * m_sharedMatrix->m13 * m_sharedMatrix->m32; + + inv[14] = -m_sharedMatrix->m11 * m_sharedMatrix->m22 * m_sharedMatrix->m43 + + m_sharedMatrix->m11 * m_sharedMatrix->m23 * m_sharedMatrix->m42 + + m_sharedMatrix->m21 * m_sharedMatrix->m12 * m_sharedMatrix->m43 - + m_sharedMatrix->m21 * m_sharedMatrix->m13 * m_sharedMatrix->m42 - + m_sharedMatrix->m41 * m_sharedMatrix->m12 * m_sharedMatrix->m23 + + m_sharedMatrix->m41 * m_sharedMatrix->m13 * m_sharedMatrix->m22; + + inv[15] = F(0.0); + + T invDet = F(1.0) / det; + for (unsigned int i = 0; i < 16; ++i) + inv[i] *= invDet; + + inv[15] = F(1.0); + + Set(inv); + if (succeeded) + *succeeded = true; + } + else + { + NazaraError("Matrix has no inverse"); + if (succeeded) + *succeeded = false; + } + + return *this; +} + template bool NzMatrix4::IsAffine() const { @@ -244,26 +493,30 @@ bool NzMatrix4::IsDefined() const } template -void NzMatrix4::MakeIdentity() +NzMatrix4& NzMatrix4::MakeIdentity() { Set(F(1.0), F(0.0), F(0.0), F(0.0), F(0.0), F(1.0), F(0.0), F(0.0), F(0.0), F(0.0), F(1.0), F(0.0), F(0.0), F(0.0), F(0.0), F(1.0)); + + return *this; } template -void NzMatrix4::MakeOrtho(T left, T top, T width, T height, T zNear, T zFar) +NzMatrix4& NzMatrix4::MakeOrtho(T left, T top, T width, T height, T zNear, T zFar) { // http://msdn.microsoft.com/en-us/library/windows/desktop/bb204941(v=vs.85).aspx Set(F(2.0)/(width-left), F(0.0), F(0.0), -(width+left)/(width-left), F(0.0), F(2.0)/(top-height), F(0.0), -(top+height)/(top-height), F(0.0), F(0.0), F(-2.0)/(zFar-zNear), -(zFar+zNear)/(zFar-zNear), F(0.0), F(0.0), F(0.0), F(1.0)); + + return *this; } template -void NzMatrix4::MakeLookAt(const NzVector3& eye, const NzVector3& target, const NzVector3& up) +NzMatrix4& NzMatrix4::MakeLookAt(const NzVector3& eye, const NzVector3& target, const NzVector3& up) { NzVector3 f = NzVector3::Normalize(target - eye); NzVector3 u(up.GetNormal()); @@ -274,10 +527,12 @@ void NzMatrix4::MakeLookAt(const NzVector3& eye, const NzVector3& targe s.y, u.y, -f.y, T(0.0), s.z, u.z, -f.z, T(0.0), -s.DotProduct(eye), -u.DotProduct(eye), f.DotProduct(eye), T(1.0)); + + return *this; } template -void NzMatrix4::MakePerspective(T angle, T ratio, T zNear, T zFar) +NzMatrix4& NzMatrix4::MakePerspective(T angle, T ratio, T zNear, T zFar) { // http://msdn.microsoft.com/en-us/library/windows/desktop/bb204944(v=vs.85).aspx #if NAZARA_MATH_ANGLE_RADIAN @@ -292,10 +547,12 @@ void NzMatrix4::MakePerspective(T angle, T ratio, T zNear, T zFar) F(0.0), yScale, F(0.0), F(0.0), F(0.0), F(0.0), zFar / (zNear-zFar), F(-1.0), F(0.0), F(0.0), (zNear*zFar) / (zNear-zFar), F(0.0)); + + return *this; } template -void NzMatrix4::MakeRotation(const NzQuaternion& rotation) +NzMatrix4& NzMatrix4::MakeRotation(const NzQuaternion& rotation) { // http://www.flipcode.com/documents/matrfaq.html#Q54 /* @@ -329,43 +586,71 @@ void NzMatrix4::MakeRotation(const NzQuaternion& rotation) F(0.0), F(0.0), F(1.0)); + + return *this; } template -void NzMatrix4::MakeScale(const NzVector3& scale) +NzMatrix4& NzMatrix4::MakeScale(const NzVector3& scale) { Set(scale.x, F(0.0), F(0.0), F(0.0), F(0.0), scale.y, F(0.0), F(0.0), F(0.0), F(0.0), scale.z, F(0.0), F(0.0), F(0.0), F(0.0), F(1.0)); + + return *this; } template -void NzMatrix4::MakeTranslation(const NzVector3& translation) +NzMatrix4& NzMatrix4::MakeTranslation(const NzVector3& translation) { Set(F(1.0), F(0.0), F(0.0), F(0.0), F(0.0), F(1.0), F(0.0), F(0.0), F(0.0), F(0.0), F(1.0), F(0.0), translation.x, translation.y, translation.z, F(1.0)); + + return *this; } template -void NzMatrix4::MakeZero() +NzMatrix4& NzMatrix4::MakeTransform(const NzVector3& translation, const NzVector3& scale, const NzQuaternion& rotation) +{ + // La rotation et la translation peuvent être appliquées directement + SetRotation(rotation); + SetTranslation(translation); + + // On complète la matrice (les transformations sont affines) + m_sharedMatrix->m14 = F(0.0); + m_sharedMatrix->m24 = F(0.0); + m_sharedMatrix->m34 = F(0.0); + m_sharedMatrix->m44 = F(1.0); + + // Ensuite on fait une mise à l'échelle des valeurs déjà présentes + ApplyScale(scale); + + return *this; +} + +template +NzMatrix4& NzMatrix4::MakeZero() { Set(F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0), F(0.0)); + + return *this; } template -void NzMatrix4::Set(T r11, T r12, T r13, T r14, +NzMatrix4& NzMatrix4::Set(T r11, T r12, T r13, T r14, T r21, T r22, T r23, T r24, T r31, T r32, T r33, T r34, T r41, T r42, T r43, T r44) { - EnsureOwnership(); + ReleaseMatrix(); + m_sharedMatrix = new SharedMatrix; m_sharedMatrix->m11 = r11; m_sharedMatrix->m12 = r12; m_sharedMatrix->m13 = r13; @@ -382,19 +667,24 @@ void NzMatrix4::Set(T r11, T r12, T r13, T r14, m_sharedMatrix->m42 = r42; m_sharedMatrix->m43 = r43; m_sharedMatrix->m44 = r44; + + return *this; } template -void NzMatrix4::Set(const T matrix[16]) +NzMatrix4& NzMatrix4::Set(const T matrix[16]) { - EnsureOwnership(); + ReleaseMatrix(); + m_sharedMatrix = new SharedMatrix; // Ici nous sommes certains de la continuité des éléments en mémoire std::memcpy(&m_sharedMatrix->m11, matrix, 16*sizeof(T)); + + return *this; } template -void NzMatrix4::Set(const NzMatrix4& matrix) +NzMatrix4& NzMatrix4::Set(const NzMatrix4& matrix) { ReleaseMatrix(); @@ -405,26 +695,32 @@ void NzMatrix4::Set(const NzMatrix4& matrix) m_sharedMatrix->refCount++; NazaraMutexUnlock(m_sharedMatrix->mutex); } + + return *this; } template -void NzMatrix4::Set(NzMatrix4&& matrix) +NzMatrix4& NzMatrix4::Set(NzMatrix4&& matrix) { std::swap(m_sharedMatrix, matrix.m_sharedMatrix); + + return *this; } template template -void NzMatrix4::Set(const NzMatrix4& matrix) +NzMatrix4& NzMatrix4::Set(const NzMatrix4& matrix) { Set(F(matrix.m_sharedMatrix->m11), F(matrix.m_sharedMatrix->m12), F(matrix.m_sharedMatrix->m13), F(matrix.m_sharedMatrix->m14), F(matrix.m_sharedMatrix->m21), F(matrix.m_sharedMatrix->m22), F(matrix.m_sharedMatrix->m23), F(matrix.m_sharedMatrix->m24), F(matrix.m_sharedMatrix->m31), F(matrix.m_sharedMatrix->m32), F(matrix.m_sharedMatrix->m33), F(matrix.m_sharedMatrix->m34), F(matrix.m_sharedMatrix->m41), F(matrix.m_sharedMatrix->m42), F(matrix.m_sharedMatrix->m43), F(matrix.m_sharedMatrix->m44)); + + return *this; } template -void NzMatrix4::SetRotation(const NzQuaternion& rotation) +NzMatrix4& NzMatrix4::SetRotation(const NzQuaternion& rotation) { // http://www.flipcode.com/documents/matrfaq.html#Q54 EnsureOwnership(); @@ -439,20 +735,24 @@ void NzMatrix4::SetRotation(const NzQuaternion& rotation) m_sharedMatrix->m23 = F(2.0)*rotation.y*rotation.z - F(2.0)*rotation.x*rotation.w; m_sharedMatrix->m31 = F(2.0)*rotation.x*rotation.z - F(2.0)*rotation.y*rotation.w; m_sharedMatrix->m32 = F(2.0)*rotation.y*rotation.z + F(2.0)*rotation.x*rotation.w; + + return *this; } template -void NzMatrix4::SetScale(const NzVector3& scale) +NzMatrix4& NzMatrix4::SetScale(const NzVector3& scale) { EnsureOwnership(); m_sharedMatrix->m11 = scale.x; m_sharedMatrix->m22 = scale.y; m_sharedMatrix->m33 = scale.z; + + return *this; } template -void NzMatrix4::SetTranslation(const NzVector3& translation) +NzMatrix4& NzMatrix4::SetTranslation(const NzVector3& translation) { EnsureOwnership(); @@ -460,6 +760,8 @@ void NzMatrix4::SetTranslation(const NzVector3& translation) m_sharedMatrix->m42 = translation.y; m_sharedMatrix->m43 = translation.z; m_sharedMatrix->m44 = F(1.0); + + return *this; } template @@ -734,7 +1036,7 @@ bool NzMatrix4::operator==(const NzMatrix4& mat) const return false; for (unsigned int i = 0; i < 16; ++i) - if (!NzNumberEquals((&m_sharedMatrix->m11)[i])) + if (!NzNumberEquals((&m_sharedMatrix->m11)[i], (&mat.m_sharedMatrix->m11)[i])) return false; return true; @@ -904,6 +1206,15 @@ NzMatrix4 NzMatrix4::Translate(const NzVector3& translation) return mat; } +template +NzMatrix4 NzMatrix4::Transform(const NzVector3& translation, const NzVector3& scale, const NzQuaternion& rotation) +{ + NzMatrix4 mat; + mat.MakeTransform(translation, scale, rotation); + + return mat; +} + template NzMatrix4 NzMatrix4::Zero() { @@ -937,6 +1248,8 @@ void NzMatrix4::EnsureOwnership() SharedMatrix* sharedMatrix = new SharedMatrix; std::memcpy(&sharedMatrix->m11, &m_sharedMatrix->m11, 16*sizeof(T)); + + m_sharedMatrix = sharedMatrix; } } else From 4c23ccf1c676fb1696612e17815adb66ffada92a Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 26 Oct 2012 14:08:30 +0200 Subject: [PATCH 13/27] (Matrix4) Renamed Destroy to Undefine, fixed bug Fixed Transpose() not checking COW ownership Former-commit-id: de2c64b9aef5abf2f73ce6cff2fe8442c1c382c5 --- include/Nazara/Math/Matrix4.hpp | 4 ++-- include/Nazara/Math/Matrix4.inl | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/Nazara/Math/Matrix4.hpp b/include/Nazara/Math/Matrix4.hpp index 849934710..b9163d396 100644 --- a/include/Nazara/Math/Matrix4.hpp +++ b/include/Nazara/Math/Matrix4.hpp @@ -43,8 +43,6 @@ class NzMatrix4 NzMatrix4 Concatenate(const NzMatrix4& matrix) const; NzMatrix4 ConcatenateAffine(const NzMatrix4& matrix) const; - void Destroy(); - T GetDeterminant() const; NzMatrix4 GetInverse(bool* succeeded = nullptr) const; NzMatrix4 GetInverseAffine(bool* succeeded = nullptr) const; @@ -94,6 +92,8 @@ class NzMatrix4 NzMatrix4& Transpose(); + NzMatrix4& Undefine(); + operator NzString() const; operator T*(); diff --git a/include/Nazara/Math/Matrix4.inl b/include/Nazara/Math/Matrix4.inl index 0deb8f00b..ae8c0c0af 100644 --- a/include/Nazara/Math/Matrix4.inl +++ b/include/Nazara/Math/Matrix4.inl @@ -98,12 +98,6 @@ NzMatrix4 NzMatrix4::ConcatenateAffine(const NzMatrix4& matrix) const return ConcatenateAffine(*this, matrix); } -template -void NzMatrix4::Destroy() -{ - ReleaseMatrix(); -} - template T NzMatrix4::GetDeterminant() const { @@ -836,6 +830,8 @@ NzMatrix4& NzMatrix4::Transpose() } #endif + EnsureOwnership(); + std::swap(m_sharedMatrix->m12, m_sharedMatrix->m21); std::swap(m_sharedMatrix->m13, m_sharedMatrix->m31); std::swap(m_sharedMatrix->m14, m_sharedMatrix->m41); @@ -846,6 +842,14 @@ NzMatrix4& NzMatrix4::Transpose() return *this; } +template +NzMatrix4& NzMatrix4::Undefine() +{ + ReleaseMatrix(); + + return *this; +} + template NzMatrix4::operator NzString() const { From 3a9e8850c0b47e4058cefb648b9b48d672461bd5 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 26 Oct 2012 14:09:09 +0200 Subject: [PATCH 14/27] Added Keyboard::GetKeyName (experimental) Former-commit-id: a937b98ceeed3cb7c64bf6c1f544bb7021ad01bd --- include/Nazara/Utility/Keyboard.hpp | 2 + src/Nazara/Utility/Keyboard.cpp | 5 ++ src/Nazara/Utility/Win32/InputImpl.cpp | 113 ++++++++++++++++++------- src/Nazara/Utility/Win32/InputImpl.hpp | 2 + 4 files changed, 91 insertions(+), 31 deletions(-) diff --git a/include/Nazara/Utility/Keyboard.hpp b/include/Nazara/Utility/Keyboard.hpp index a7bb39f3a..2d783f80f 100644 --- a/include/Nazara/Utility/Keyboard.hpp +++ b/include/Nazara/Utility/Keyboard.hpp @@ -10,6 +10,7 @@ #define NAZARA_KEYBOARD_HPP #include +#include class NAZARA_API NzKeyboard { @@ -159,6 +160,7 @@ class NAZARA_API NzKeyboard Count }; + static NzString GetKeyName(Key key); static bool IsKeyPressed(Key key); }; diff --git a/src/Nazara/Utility/Keyboard.cpp b/src/Nazara/Utility/Keyboard.cpp index d463c6cb2..86b810bb3 100644 --- a/src/Nazara/Utility/Keyboard.cpp +++ b/src/Nazara/Utility/Keyboard.cpp @@ -14,6 +14,11 @@ #include +NzString NzKeyboard::GetKeyName(Key key) +{ + return NzEventImpl::GetKeyName(key); +} + bool NzKeyboard::IsKeyPressed(Key key) { return NzEventImpl::IsKeyPressed(key); diff --git a/src/Nazara/Utility/Win32/InputImpl.cpp b/src/Nazara/Utility/Win32/InputImpl.cpp index 0bc96168d..1ffc06d99 100644 --- a/src/Nazara/Utility/Win32/InputImpl.cpp +++ b/src/Nazara/Utility/Win32/InputImpl.cpp @@ -8,37 +8,9 @@ #include #include -NzVector2i NzEventImpl::GetMousePosition() +namespace { - POINT pos; - GetCursorPos(&pos); - - return NzVector2i(pos.x, pos.y); -} - -NzVector2i NzEventImpl::GetMousePosition(const NzWindow& relativeTo) -{ - HWND handle = reinterpret_cast(relativeTo.GetHandle()); - if (handle) - { - POINT pos; - GetCursorPos(&pos); - ScreenToClient(handle, &pos); - - return NzVector2i(pos.x, pos.y); - } - else - { - NazaraError("Window's handle is invalid"); - - // Attention que (-1, -1) est une position tout à fait valide et ne doit pas être utilisée pour tester l'erreur - return NzVector2i(-1, -1); - } -} - -bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key) -{ - static int vKeys[NzKeyboard::Count] = { + int vKeys[NzKeyboard::Count] = { // Lettres 0x41, // Key::A 0x42, // Key::B @@ -161,7 +133,7 @@ bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key) VK_BROWSER_SEARCH, // Key::Browser_Search VK_BROWSER_STOP, // Key::Browser_Stop - // Touches de contr + // Touches de contrôle VK_MEDIA_NEXT_TRACK, // Key::Media_Next, VK_MEDIA_PLAY_PAUSE, // Key::Media_PlayPause, VK_MEDIA_PREV_TRACK, // Key::Media_Previous, @@ -177,7 +149,86 @@ bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key) VK_NUMLOCK, // Key::NumLock VK_SCROLL // Key::ScrollLock }; +} +NzString NzEventImpl::GetKeyName(NzKeyboard::Key key) +{ + // http://www.ffuts.org/blog/mapvirtualkey-getkeynametext-and-a-story-of-how-to/ + int vk = vKeys[key]; + unsigned int code = MapVirtualKeyW(vk, 0) << 16; + + ///FIXME: Liste complète ? + switch (vk) + { + case VK_ATTN: + case VK_DOWN: + case VK_DELETE: + case VK_DIVIDE: + case VK_END: + case VK_HOME: + case VK_INSERT: + case VK_LEFT: + case VK_LWIN: + case VK_OEM_1: + case VK_OEM_2: + case VK_OEM_3: + case VK_OEM_4: + case VK_OEM_5: + case VK_OEM_6: + case VK_OEM_7: + case VK_OEM_CLEAR: + case VK_OEM_COMMA: + case VK_OEM_MINUS: + case VK_OEM_PERIOD: + case VK_OEM_PLUS: + case VK_PAUSE: + case VK_NEXT: + case VK_NUMLOCK: + case VK_PRIOR: + case VK_RIGHT: + case VK_RWIN: + case VK_UP: + code |= 0x1000000; // 24ème bit pour l'extension + break; + } + + wchar_t keyName[20]; // Je ne pense pas que ça dépassera 20 caractères + if (!GetKeyNameTextW(code, &keyName[0], 20)) + return "Unknown"; + + return NzString::Unicode(keyName); +} + +NzVector2i NzEventImpl::GetMousePosition() +{ + POINT pos; + GetCursorPos(&pos); + + return NzVector2i(pos.x, pos.y); +} + +NzVector2i NzEventImpl::GetMousePosition(const NzWindow& relativeTo) +{ + HWND handle = reinterpret_cast(relativeTo.GetHandle()); + if (handle) + { + POINT pos; + GetCursorPos(&pos); + ScreenToClient(handle, &pos); + + return NzVector2i(pos.x, pos.y); + } + else + { + NazaraError("Window's handle is invalid"); + + // Attention que (-1, -1) est une position tout à fait valide et ne doit pas être utilisée pour tester l'erreur + return NzVector2i(-1, -1); + } +} + +bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key) +{ switch (key) { case NzKeyboard::CapsLock: diff --git a/src/Nazara/Utility/Win32/InputImpl.hpp b/src/Nazara/Utility/Win32/InputImpl.hpp index 41961312e..07bb61526 100644 --- a/src/Nazara/Utility/Win32/InputImpl.hpp +++ b/src/Nazara/Utility/Win32/InputImpl.hpp @@ -8,12 +8,14 @@ #define NAZARA_INPUTIMPL_HPP #include +#include #include #include class NzEventImpl { public: + static NzString GetKeyName(NzKeyboard::Key key); static NzVector2i GetMousePosition(); static NzVector2i GetMousePosition(const NzWindow& relativeTo); static bool IsKeyPressed(NzKeyboard::Key key); From 2831f596d289a693b55dc59074126896a3905627 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 29 Oct 2012 09:56:21 +0100 Subject: [PATCH 15/27] Added missing include Former-commit-id: c8cdcb907f85b064e5cf3a7faa23740131af9283 --- include/Nazara/Renderer/Context.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/Nazara/Renderer/Context.hpp b/include/Nazara/Renderer/Context.hpp index 1fc97f9e8..cafa24c1b 100644 --- a/include/Nazara/Renderer/Context.hpp +++ b/include/Nazara/Renderer/Context.hpp @@ -9,6 +9,7 @@ #ifdef NAZARA_RENDERER_OPENGL +#include #include #include From a608aa0e2afd3ec553a6883a320ee379a0079f4a Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 29 Oct 2012 09:59:18 +0100 Subject: [PATCH 16/27] Cleaning loader code Former-commit-id: a34fae25b7e70e1168fa6bfa089d8de991184036 --- src/Nazara/Utility/Loaders/PCX/Loader.cpp | 10 +++++----- src/Nazara/Utility/Loaders/STB/Loader.cpp | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Nazara/Utility/Loaders/PCX/Loader.cpp b/src/Nazara/Utility/Loaders/PCX/Loader.cpp index f78ce358d..ec453b566 100644 --- a/src/Nazara/Utility/Loaders/PCX/Loader.cpp +++ b/src/Nazara/Utility/Loaders/PCX/Loader.cpp @@ -37,7 +37,7 @@ namespace nzUInt8 padding[54]; }; - bool NzLoader_PCX_Check(NzInputStream& stream, const NzImageParams& parameters) + bool Check(NzInputStream& stream, const NzImageParams& parameters) { NazaraUnused(parameters); @@ -48,7 +48,7 @@ namespace return manufacturer == 0x0a; } - bool NzLoader_PCX_Load(NzImage* image, NzInputStream& stream, const NzImageParams& parameters) + bool Load(NzImage* image, NzInputStream& stream, const NzImageParams& parameters) { NazaraUnused(parameters); @@ -59,7 +59,7 @@ namespace return false; } - #if defined(NAZARA_BIG_ENDIAN) + #ifdef NAZARA_BIG_ENDIAN // Les fichiers PCX sont en little endian NzByteSwap(&header.xmin, sizeof(nzUInt16)); NzByteSwap(&header.ymin, sizeof(nzUInt16)); @@ -337,10 +337,10 @@ namespace void NzLoaders_PCX_Register() { - NzImageLoader::RegisterLoader("pcx", NzLoader_PCX_Check, NzLoader_PCX_Load); + NzImageLoader::RegisterLoader("pcx", Check, Load); } void NzLoaders_PCX_Unregister() { - NzImageLoader::UnregisterLoader("pcx", NzLoader_PCX_Check, NzLoader_PCX_Load); + NzImageLoader::UnregisterLoader("pcx", Check, Load); } diff --git a/src/Nazara/Utility/Loaders/STB/Loader.cpp b/src/Nazara/Utility/Loaders/STB/Loader.cpp index 86047d350..6baad965a 100644 --- a/src/Nazara/Utility/Loaders/STB/Loader.cpp +++ b/src/Nazara/Utility/Loaders/STB/Loader.cpp @@ -37,7 +37,7 @@ namespace static stbi_io_callbacks callbacks = {Read, Skip, Eof}; - bool NzLoader_STB_Check(NzInputStream& stream, const NzImageParams& parameters) + bool Check(NzInputStream& stream, const NzImageParams& parameters) { NazaraUnused(parameters); @@ -45,7 +45,7 @@ namespace return stbi_info_from_callbacks(&callbacks, &stream, &width, &height, &bpp); } - bool NzLoader_STB_Load(NzImage* image, NzInputStream& stream, const NzImageParams& parameters) + bool Load(NzImage* image, NzInputStream& stream, const NzImageParams& parameters) { static const nzPixelFormat formats[4] = { @@ -117,10 +117,10 @@ namespace void NzLoaders_STB_Register() { - NzImageLoader::RegisterLoader("bmp,gif,hdr,jpg,jpeg,pic,png,psd,tga", NzLoader_STB_Check, NzLoader_STB_Load); + NzImageLoader::RegisterLoader("bmp,gif,hdr,jpg,jpeg,pic,png,psd,tga", Check, Load); } void NzLoaders_STB_Unregister() { - NzImageLoader::UnregisterLoader("bmp,gif,hdr,jpg,jpeg,pic,png,psd,tga", NzLoader_STB_Check, NzLoader_STB_Load); + NzImageLoader::UnregisterLoader("bmp,gif,hdr,jpg,jpeg,pic,png,psd,tga", Check, Load); } From 8132812c239e5ff83afd5edde2daf6690f7563ac Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 29 Oct 2012 09:59:55 +0100 Subject: [PATCH 17/27] Interpolation factor is now only checked in debug Former-commit-id: b5cf2b63740a0827403cac7843aca4b820b7687f --- src/Nazara/Utility/SubMesh.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Nazara/Utility/SubMesh.cpp b/src/Nazara/Utility/SubMesh.cpp index 557bdf435..1d04b038f 100644 --- a/src/Nazara/Utility/SubMesh.cpp +++ b/src/Nazara/Utility/SubMesh.cpp @@ -46,7 +46,9 @@ void NzSubMesh::Animate(unsigned int frameA, unsigned int frameB, float interpol NazaraError("Frame B is out of range (" + NzString::Number(frameB) + " >= " + NzString::Number(frameCount) + ')'); return; } + #endif + #ifdef NAZARA_DEBUG if (interpolation < 0.f || interpolation > 1.f) { NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); From 93a1738c554e4594f4550a7cc6686d02d059e6b7 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 29 Oct 2012 10:00:57 +0100 Subject: [PATCH 18/27] Optimized VertexDeclaration::GetElement Former-commit-id: a56498837ae2f6101e0641aab23b2d1d0f9130f3 --- src/Nazara/Utility/VertexDeclaration.cpp | 44 +++++++++++++----------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/Nazara/Utility/VertexDeclaration.cpp b/src/Nazara/Utility/VertexDeclaration.cpp index 6b5ec7e2f..019a48fed 100644 --- a/src/Nazara/Utility/VertexDeclaration.cpp +++ b/src/Nazara/Utility/VertexDeclaration.cpp @@ -244,7 +244,6 @@ const NzVertexElement* NzVertexDeclaration::GetElement(nzElementStream stream, n #endif int elementPos = m_sharedImpl->elementPos[stream][usage]; - #if NAZARA_UTILITY_SAFE if (elementPos == -1) { @@ -253,27 +252,32 @@ const NzVertexElement* NzVertexDeclaration::GetElement(nzElementStream stream, n } #endif - elementPos += usageIndex; - - #if NAZARA_UTILITY_SAFE - if (static_cast(elementPos) >= m_sharedImpl->elements.size()) + if (usageIndex == 0) // Si l'usage index vaut zéro, alors nous sommes certains d'être sur le bon élément (Majorité des cas) + return &m_sharedImpl->elements[elementPos]; + else { - NazaraError("Element not found"); - return nullptr; + elementPos += usageIndex; + + #if NAZARA_UTILITY_SAFE + if (static_cast(elementPos) >= m_sharedImpl->elements.size()) + { + NazaraError("Element not found"); + return nullptr; + } + #endif + + NzVertexElement& element = m_sharedImpl->elements[elementPos]; + + #if NAZARA_UTILITY_SAFE + if (element.stream != stream || element.usage != usage || element.usageIndex != usageIndex) + { + NazaraError("Element not found"); + return nullptr; + } + #endif + + return &element; } - #endif - - NzVertexElement& element = m_sharedImpl->elements[elementPos]; - - #if NAZARA_UTILITY_SAFE - if (element.stream != stream || element.usage != usage || element.usageIndex != usageIndex) - { - NazaraError("Element not found"); - return nullptr; - } - #endif - - return &element; } unsigned int NzVertexDeclaration::GetElementCount() const From f1eb8597079f342937f29f311c4d1914235d5b53 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 30 Oct 2012 17:37:17 +0100 Subject: [PATCH 19/27] Fixed fprintf specifiers Thanks to Quentin Huot-Marchand Former-commit-id: 07bb2a1c2a0baaebac42f1664487624f8d39e16c --- src/Nazara/Core/Debug/MemoryLeakTracker.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Nazara/Core/Debug/MemoryLeakTracker.cpp b/src/Nazara/Core/Debug/MemoryLeakTracker.cpp index 7050c540f..adaf0e061 100644 --- a/src/Nazara/Core/Debug/MemoryLeakTracker.cpp +++ b/src/Nazara/Core/Debug/MemoryLeakTracker.cpp @@ -76,7 +76,7 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, { // Pas d'information de temps (Car nécessitant une allocation) FILE* log = std::fopen(MLTFileName, "a"); - std::fprintf(log, "Failed to allocate memory (%d bytes)\n", size); + std::fprintf(log, "Failed to allocate memory (%zu bytes)\n", size); std::fclose(log); return nullptr; // Impossible d'envoyer une exception car cela allouerait de la mémoire avec new (boucle infinie) @@ -126,9 +126,9 @@ void NzMemoryManager::Free(void* pointer, bool multi) if (nextFreeFile) { if (multi) - std::fprintf(log, "%s Warning: delete[] after new at %s:%d\n", time, nextFreeFile, nextFreeLine); + std::fprintf(log, "%s Warning: delete[] after new at %s:%u\n", time, nextFreeFile, nextFreeLine); else - std::fprintf(log, "%s Warning: delete after new[] at %s:%d\n", time, nextFreeFile, nextFreeLine); + std::fprintf(log, "%s Warning: delete after new[] at %s:%u\n", time, nextFreeFile, nextFreeLine); } else { @@ -232,16 +232,16 @@ void NzMemoryManager::Uninitialize() count++; totalSize += ptr->size; if (ptr->file) - std::fprintf(log, "-0x%p -> %d bytes allocated at %s:%d\n", reinterpret_cast(ptr)+sizeof(Block), ptr->size, ptr->file, ptr->line); + std::fprintf(log, "-0x%p -> %zu bytes allocated at %s:%u\n", reinterpret_cast(ptr)+sizeof(Block), ptr->size, ptr->file, ptr->line); else - std::fprintf(log, "-0x%p -> %d bytes allocated at unknown position\n", reinterpret_cast(ptr)+sizeof(Block), ptr->size); + std::fprintf(log, "-0x%p -> %zu bytes allocated at unknown position\n", reinterpret_cast(ptr)+sizeof(Block), ptr->size); void* pointer = ptr; ptr = ptr->next; std::free(pointer); } - std::fprintf(log, "\n%d blocks leaked (%d bytes)", count, totalSize); + std::fprintf(log, "\n%u blocks leaked (%u bytes)", count, totalSize); } std::free(time); From 1d217837cf17e2474134af4b1f6521f0db37a707 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 30 Oct 2012 18:52:38 +0100 Subject: [PATCH 20/27] Added compiler tests and fixed MinGW warnings Former-commit-id: 550d467e0997065943dc0b18a36d1be0edbb8114 --- include/Nazara/Prerequesites.hpp | 30 ++++++++++++++------- src/Nazara/Core/Debug/MemoryLeakTracker.cpp | 14 +++++++--- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/include/Nazara/Prerequesites.hpp b/include/Nazara/Prerequesites.hpp index facdf9b50..57127604f 100644 --- a/include/Nazara/Prerequesites.hpp +++ b/include/Nazara/Prerequesites.hpp @@ -37,20 +37,32 @@ ///TODO: Rajouter des tests d'identification de compilateurs // NAZARA_THREADLOCAL n'existe qu'en attendant le support complet de thread_local -#if defined(_MSC_VER) - #define NAZARA_COMPILER_MSVC - #define NAZARA_DEPRECATED(txt) __declspec(deprecated(txt)) - #define NAZARA_FUNCTION __FUNCSIG__ +#if defined(__BORLANDC__) + #define NAZARA_COMPILER_BORDLAND + #define NAZARA_DEPRECATED(txt) + #define NAZARA_FUNCTION __FUNC__ #define NAZARA_THREADLOCAL __declspec(thread) -#elif defined(__GNUC__) +#elif defined(__clang__) + #define NAZARA_COMPILER_CLANG + #define NAZARA_DEPRECATED(txt) __attribute__((__deprecated__(txt))) + #define NAZARA_FUNCTION __PRETTY_FUNCTION__ + #define NAZARA_THREADLOCAL __declspec(thread) +#elif defined(__GNUC__) || defined(__MINGW32__) #define NAZARA_COMPILER_GCC #define NAZARA_DEPRECATED(txt) __attribute__((__deprecated__(txt))) #define NAZARA_FUNCTION __PRETTY_FUNCTION__ #define NAZARA_THREADLOCAL __thread -#elif defined(__BORLANDC__) - #define NAZARA_COMPILER_BORDLAND - #define NAZARA_DEPRECATED(txt) - #define NAZARA_FUNCTION __FUNC__ + + #ifdef __MINGW32__ + #define NAZARA_COMPILER_MINGW + #ifdef __MINGW64_VERSION_MAJOR + #define NAZARA_COMPILER_MINGW_W64 + #endif + #endif +#elif defined(_MSC_VER) + #define NAZARA_COMPILER_MSVC + #define NAZARA_DEPRECATED(txt) __declspec(deprecated(txt)) + #define NAZARA_FUNCTION __FUNCSIG__ #define NAZARA_THREADLOCAL __declspec(thread) #else #define NAZARA_COMPILER_UNKNOWN diff --git a/src/Nazara/Core/Debug/MemoryLeakTracker.cpp b/src/Nazara/Core/Debug/MemoryLeakTracker.cpp index adaf0e061..8137ab9bb 100644 --- a/src/Nazara/Core/Debug/MemoryLeakTracker.cpp +++ b/src/Nazara/Core/Debug/MemoryLeakTracker.cpp @@ -14,6 +14,14 @@ #include #endif +// C'est malheureux mais le spécificateur %z de (f)printf n'est pas supporté partout +#ifdef NAZARA_COMPILER_MINGW + #define SIZE_T_SPECIFIER "%u" +#else + #define SIZE_T_SPECIFIER "%zu" // Standard +#endif +// Le seul fichier n'ayant pas à inclure Debug.hpp + namespace { struct Block @@ -76,7 +84,7 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, { // Pas d'information de temps (Car nécessitant une allocation) FILE* log = std::fopen(MLTFileName, "a"); - std::fprintf(log, "Failed to allocate memory (%zu bytes)\n", size); + std::fprintf(log, "Failed to allocate memory (" SIZE_T_SPECIFIER " bytes)\n", size); std::fclose(log); return nullptr; // Impossible d'envoyer une exception car cela allouerait de la mémoire avec new (boucle infinie) @@ -232,9 +240,9 @@ void NzMemoryManager::Uninitialize() count++; totalSize += ptr->size; if (ptr->file) - std::fprintf(log, "-0x%p -> %zu bytes allocated at %s:%u\n", reinterpret_cast(ptr)+sizeof(Block), ptr->size, ptr->file, ptr->line); + std::fprintf(log, "-0x%p -> " SIZE_T_SPECIFIER " bytes allocated at %s:%u\n", reinterpret_cast(ptr)+sizeof(Block), ptr->size, ptr->file, ptr->line); else - std::fprintf(log, "-0x%p -> %zu bytes allocated at unknown position\n", reinterpret_cast(ptr)+sizeof(Block), ptr->size); + std::fprintf(log, "-0x%p -> " SIZE_T_SPECIFIER " bytes allocated at unknown position\n", reinterpret_cast(ptr)+sizeof(Block), ptr->size); void* pointer = ptr; ptr = ptr->next; From d5416fb1391a41f96534f8a9ce22ff503ce2d3a8 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 30 Oct 2012 19:24:05 +0100 Subject: [PATCH 21/27] Fixed cast Former-commit-id: 2cbcf9c10c0d2bfd2cf441528ddf712bee80d7bd --- src/Nazara/Core/Debug/MemoryLeakTracker.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nazara/Core/Debug/MemoryLeakTracker.cpp b/src/Nazara/Core/Debug/MemoryLeakTracker.cpp index 8137ab9bb..eea5a287b 100644 --- a/src/Nazara/Core/Debug/MemoryLeakTracker.cpp +++ b/src/Nazara/Core/Debug/MemoryLeakTracker.cpp @@ -232,9 +232,9 @@ void NzMemoryManager::Uninitialize() std::fprintf(log, "%s ==============================\n\n", time); std::fputs("Leak list:\n", log); + std::size_t totalSize = 0; Block* ptr = ptrList.next; unsigned int count = 0; - unsigned int totalSize = 0; while (ptr != &ptrList) { count++; @@ -249,7 +249,7 @@ void NzMemoryManager::Uninitialize() std::free(pointer); } - std::fprintf(log, "\n%u blocks leaked (%u bytes)", count, totalSize); + std::fprintf(log, "\n%u blocks leaked (" SIZE_T_SPECIFIER " bytes)", count, totalSize); } std::free(time); From 5c2f94f953d6e418b946b60f878010c5adef3aa4 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 1 Nov 2012 18:20:15 +0100 Subject: [PATCH 22/27] You can now change the point size Former-commit-id: b0018b14973d36f822524e8e5b94ba940a63897f --- include/Nazara/Renderer/OpenGL.hpp | 2 ++ include/Nazara/Renderer/Renderer.hpp | 2 ++ src/Nazara/Renderer/OpenGL.cpp | 6 +++++- src/Nazara/Renderer/Renderer.cpp | 31 +++++++++++++++++++++++----- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/include/Nazara/Renderer/OpenGL.hpp b/include/Nazara/Renderer/OpenGL.hpp index 411627e93..55dc1c2ca 100644 --- a/include/Nazara/Renderer/OpenGL.hpp +++ b/include/Nazara/Renderer/OpenGL.hpp @@ -151,6 +151,7 @@ NAZARA_API extern PFNGLGENVERTEXARRAYSPROC glGenVertexArrays; NAZARA_API extern PFNGLGETBUFFERPARAMETERIVPROC glGetBufferParameteriv; NAZARA_API extern PFNGLGETDEBUGMESSAGELOGPROC glGetDebugMessageLog; NAZARA_API extern PFNGLGETERRORPROC glGetError; +NAZARA_API extern PFNGLGETFLOATVPROC glGetFloatv; NAZARA_API extern PFNGLGETINTEGERVPROC glGetIntegerv; NAZARA_API extern PFNGLGETPROGRAMIVPROC glGetProgramiv; NAZARA_API extern PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog; @@ -172,6 +173,7 @@ NAZARA_API extern PFNGLLINKPROGRAMPROC glLinkProgram; NAZARA_API extern PFNGLMAPBUFFERPROC glMapBuffer; NAZARA_API extern PFNGLMAPBUFFERRANGEPROC glMapBufferRange; NAZARA_API extern PFNGLPIXELSTOREIPROC glPixelStorei; +NAZARA_API extern PFNGLPOINTSIZEPROC glPointSize; NAZARA_API extern PFNGLPOLYGONMODEPROC glPolygonMode; NAZARA_API extern PFNGLPROGRAMUNIFORM1DPROC glProgramUniform1d; NAZARA_API extern PFNGLPROGRAMUNIFORM1FPROC glProgramUniform1f; diff --git a/include/Nazara/Renderer/Renderer.hpp b/include/Nazara/Renderer/Renderer.hpp index b34b16965..23f1964f9 100644 --- a/include/Nazara/Renderer/Renderer.hpp +++ b/include/Nazara/Renderer/Renderer.hpp @@ -41,6 +41,7 @@ class NAZARA_API NzRenderer static unsigned int GetMaxAnisotropyLevel(); static unsigned int GetMaxRenderTargets(); static unsigned int GetMaxTextureUnits(); + static float GetPointSize(); static NzShader* GetShader(); static NzRenderTarget* GetTarget(); static NzRectui GetViewport(); @@ -60,6 +61,7 @@ class NAZARA_API NzRenderer static void SetFaceFilling(nzFaceFilling fillingMode); static bool SetIndexBuffer(const NzIndexBuffer* indexBuffer); static void SetMatrix(nzMatrixType type, const NzMatrix4f& matrix); + static void SetPointSize(float size); static bool SetShader(NzShader* shader); static void SetStencilCompareFunction(nzRendererComparison compareFunc); static void SetStencilFailOperation(nzStencilOperation failOperation); diff --git a/src/Nazara/Renderer/OpenGL.cpp b/src/Nazara/Renderer/OpenGL.cpp index 4f4ffedf7..bb4f99a4b 100644 --- a/src/Nazara/Renderer/OpenGL.cpp +++ b/src/Nazara/Renderer/OpenGL.cpp @@ -262,10 +262,11 @@ namespace NzOpenGL glGenTextures = reinterpret_cast(LoadEntry("glGenTextures")); glGetBufferParameteriv = reinterpret_cast(LoadEntry("glGetBufferParameteriv")); glGetError = reinterpret_cast(LoadEntry("glGetError")); + glGetFloatv = reinterpret_cast(LoadEntry("glGetFloatv")); + glGetIntegerv = reinterpret_cast(LoadEntry("glGetIntegerv")); glGetQueryiv = reinterpret_cast(LoadEntry("glGetQueryiv")); glGetQueryObjectiv = reinterpret_cast(LoadEntry("glGetQueryObjectiv")); glGetQueryObjectuiv = reinterpret_cast(LoadEntry("glGetQueryObjectuiv")); - glGetIntegerv = reinterpret_cast(LoadEntry("glGetIntegerv")); glGetProgramiv = reinterpret_cast(LoadEntry("glGetProgramiv")); glGetProgramInfoLog = reinterpret_cast(LoadEntry("glGetProgramInfoLog")); glGetShaderInfoLog = reinterpret_cast(LoadEntry("glGetShaderInfoLog")); @@ -280,6 +281,7 @@ namespace NzOpenGL glLinkProgram = reinterpret_cast(LoadEntry("glLinkProgram")); glMapBuffer = reinterpret_cast(LoadEntry("glMapBuffer")); glPixelStorei = reinterpret_cast(LoadEntry("glPixelStorei")); + glPointSize = reinterpret_cast(LoadEntry("glPointSize")); glPolygonMode = reinterpret_cast(LoadEntry("glPolygonMode")); glReadPixels = reinterpret_cast(LoadEntry("glReadPixels")); glScissor = reinterpret_cast(LoadEntry("glScissor")); @@ -936,6 +938,7 @@ PFNGLGENVERTEXARRAYSPROC glGenVertexArrays = nullptr; PFNGLGETBUFFERPARAMETERIVPROC glGetBufferParameteriv = nullptr; PFNGLGETDEBUGMESSAGELOGPROC glGetDebugMessageLog = nullptr; PFNGLGETERRORPROC glGetError = nullptr; +PFNGLGETFLOATVPROC glGetFloatv = nullptr; PFNGLGETINTEGERVPROC glGetIntegerv = nullptr; PFNGLGETPROGRAMIVPROC glGetProgramiv = nullptr; PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog = nullptr; @@ -957,6 +960,7 @@ PFNGLLINKPROGRAMPROC glLinkProgram = nullptr; PFNGLMAPBUFFERPROC glMapBuffer = nullptr; PFNGLMAPBUFFERRANGEPROC glMapBufferRange = nullptr; PFNGLPIXELSTOREIPROC glPixelStorei = nullptr; +PFNGLPOINTSIZEPROC glPointSize = nullptr; PFNGLPOLYGONMODEPROC glPolygonMode = nullptr; PFNGLPROGRAMUNIFORM1DPROC glProgramUniform1d = nullptr; PFNGLPROGRAMUNIFORM1FPROC glProgramUniform1f = nullptr; diff --git a/src/Nazara/Renderer/Renderer.cpp b/src/Nazara/Renderer/Renderer.cpp index a1c280493..b3d1e68fe 100644 --- a/src/Nazara/Renderer/Renderer.cpp +++ b/src/Nazara/Renderer/Renderer.cpp @@ -250,6 +250,14 @@ unsigned int NzRenderer::GetMaxTextureUnits() return s_maxTextureUnit; } +float NzRenderer::GetPointSize() +{ + float pointSize; + glGetFloatv(GL_POINT_SIZE, &pointSize); + + return pointSize; +} + NzShader* NzRenderer::GetShader() { return s_shader; @@ -514,6 +522,19 @@ void NzRenderer::SetMatrix(nzMatrixType type, const NzMatrix4f& matrix) } } +void NzRenderer::SetPointSize(float size) +{ + #if NAZARA_RENDERER_SAFE + if (size <= 0.f) + { + NazaraError("Size must be over zero"); + return; + } + #endif + + glPointSize(size); +} + bool NzRenderer::SetShader(NzShader* shader) { if (s_shader == shader) @@ -856,15 +877,15 @@ bool NzRenderer::EnsureStateUpdate() NzHardwareBuffer* vertexBufferImpl = static_cast(s_vertexBuffer->GetBuffer()->GetImpl()); vertexBufferImpl->Bind(); - const nzUInt8* buffer = reinterpret_cast(s_vertexBuffer->GetPointer()); - + const nzUInt8* buffer = static_cast(s_vertexBuffer->GetPointer()); unsigned int stride = s_vertexDeclaration->GetStride(nzElementStream_VertexData); for (unsigned int i = 0; i <= nzElementUsage_Max; ++i) { - const NzVertexElement* element = s_vertexDeclaration->GetElement(nzElementStream_VertexData, static_cast(i)); - - if (element) + nzElementUsage usage = static_cast(i); + if (s_vertexDeclaration->HasElement(nzElementStream_VertexData, usage)) { + const NzVertexElement* element = s_vertexDeclaration->GetElement(nzElementStream_VertexData, usage); + glEnableVertexAttribArray(NzOpenGL::AttributeIndex[i]); glVertexAttribPointer(NzOpenGL::AttributeIndex[i], NzVertexDeclaration::GetElementCount(element->type), From 1b3129868fb33ccb854ecad1d5bae58eac5804e3 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 1 Nov 2012 18:28:49 +0100 Subject: [PATCH 23/27] Added NzRenderer::[Get/Set]LineWidth Former-commit-id: 827f762294bb67debb2a7faed07b595df2b9d20d --- include/Nazara/Renderer/OpenGL.hpp | 1 + include/Nazara/Renderer/Renderer.hpp | 2 ++ src/Nazara/Renderer/OpenGL.cpp | 2 ++ src/Nazara/Renderer/Renderer.cpp | 21 +++++++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/include/Nazara/Renderer/OpenGL.hpp b/include/Nazara/Renderer/OpenGL.hpp index 55dc1c2ca..b346d3cc0 100644 --- a/include/Nazara/Renderer/OpenGL.hpp +++ b/include/Nazara/Renderer/OpenGL.hpp @@ -169,6 +169,7 @@ NAZARA_API extern PFNGLGETTEXLEVELPARAMETERIVPROC glGetTexLevelParameteriv; NAZARA_API extern PFNGLGETTEXPARAMETERFVPROC glGetTexParameterfv; NAZARA_API extern PFNGLGETTEXPARAMETERIVPROC glGetTexParameteriv; NAZARA_API extern PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation; +NAZARA_API extern PFNGLLINEWIDTHPROC glLineWidth; NAZARA_API extern PFNGLLINKPROGRAMPROC glLinkProgram; NAZARA_API extern PFNGLMAPBUFFERPROC glMapBuffer; NAZARA_API extern PFNGLMAPBUFFERRANGEPROC glMapBufferRange; diff --git a/include/Nazara/Renderer/Renderer.hpp b/include/Nazara/Renderer/Renderer.hpp index 23f1964f9..10c4bfc3a 100644 --- a/include/Nazara/Renderer/Renderer.hpp +++ b/include/Nazara/Renderer/Renderer.hpp @@ -36,6 +36,7 @@ class NAZARA_API NzRenderer static void Enable(nzRendererParameter parameter, bool enable); + float GetLineWidth(); //static NzMatrix4f GetMatrix(nzMatrixCombination combination); static NzMatrix4f GetMatrix(nzMatrixType type); static unsigned int GetMaxAnisotropyLevel(); @@ -60,6 +61,7 @@ class NAZARA_API NzRenderer static void SetFaceCulling(nzFaceCulling cullingMode); static void SetFaceFilling(nzFaceFilling fillingMode); static bool SetIndexBuffer(const NzIndexBuffer* indexBuffer); + static void SetLineWidth(float size); static void SetMatrix(nzMatrixType type, const NzMatrix4f& matrix); static void SetPointSize(float size); static bool SetShader(NzShader* shader); diff --git a/src/Nazara/Renderer/OpenGL.cpp b/src/Nazara/Renderer/OpenGL.cpp index bb4f99a4b..c7486d946 100644 --- a/src/Nazara/Renderer/OpenGL.cpp +++ b/src/Nazara/Renderer/OpenGL.cpp @@ -278,6 +278,7 @@ namespace NzOpenGL glGetTexParameterfv = reinterpret_cast(LoadEntry("glGetTexParameterfv")); glGetTexParameteriv = reinterpret_cast(LoadEntry("glGetTexParameteriv")); glGetUniformLocation = reinterpret_cast(LoadEntry("glGetUniformLocation")); + glLineWidth = reinterpret_cast(LoadEntry("glLineWidth")); glLinkProgram = reinterpret_cast(LoadEntry("glLinkProgram")); glMapBuffer = reinterpret_cast(LoadEntry("glMapBuffer")); glPixelStorei = reinterpret_cast(LoadEntry("glPixelStorei")); @@ -956,6 +957,7 @@ PFNGLGETTEXLEVELPARAMETERIVPROC glGetTexLevelParameteriv = nullptr; PFNGLGETTEXPARAMETERFVPROC glGetTexParameterfv = nullptr; PFNGLGETTEXPARAMETERIVPROC glGetTexParameteriv = nullptr; PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = nullptr; +PFNGLLINEWIDTHPROC glLineWidth = nullptr; PFNGLLINKPROGRAMPROC glLinkProgram = nullptr; PFNGLMAPBUFFERPROC glMapBuffer = nullptr; PFNGLMAPBUFFERRANGEPROC glMapBufferRange = nullptr; diff --git a/src/Nazara/Renderer/Renderer.cpp b/src/Nazara/Renderer/Renderer.cpp index b3d1e68fe..2e78d6600 100644 --- a/src/Nazara/Renderer/Renderer.cpp +++ b/src/Nazara/Renderer/Renderer.cpp @@ -196,6 +196,14 @@ void NzRenderer::Enable(nzRendererParameter parameter, bool enable) break; } } + +float NzRenderer::GetLineWidth() +{ + float lineWidth; + glGetFloatv(GL_LINE_WIDTH, &lineWidth); + + return lineWidth; +} /* NzMatrix4f NzRenderer::GetMatrix(nzMatrixCombination combination) { @@ -505,6 +513,19 @@ bool NzRenderer::SetIndexBuffer(const NzIndexBuffer* indexBuffer) return true; } +void NzRenderer::SetLineWidth(float width) +{ + #if NAZARA_RENDERER_SAFE + if (width <= 0.f) + { + NazaraError("Width must be over zero"); + return; + } + #endif + + glLineWidth(width); +} + void NzRenderer::SetMatrix(nzMatrixType type, const NzMatrix4f& matrix) { s_matrix[type] = matrix; From 9199e60436ed4d846ab77497fd1899be88f1edfe Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 2 Nov 2012 21:50:33 +0100 Subject: [PATCH 24/27] Added implicit conversion from AABB to String Former-commit-id: c41510f4e0527a174d4acb5f8f26f60f4984ce15 --- include/Nazara/Utility/AxisAlignedBox.hpp | 2 ++ src/Nazara/Utility/AxisAlignedBox.cpp | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/include/Nazara/Utility/AxisAlignedBox.hpp b/include/Nazara/Utility/AxisAlignedBox.hpp index 1f0a123be..c52573a29 100644 --- a/include/Nazara/Utility/AxisAlignedBox.hpp +++ b/include/Nazara/Utility/AxisAlignedBox.hpp @@ -38,6 +38,8 @@ class NAZARA_API NzAxisAlignedBox NzString ToString() const; + operator NzString() const; + static NzAxisAlignedBox Lerp(const NzAxisAlignedBox& from, const NzAxisAlignedBox& to, float interpolation); static const NzAxisAlignedBox Infinite; diff --git a/src/Nazara/Utility/AxisAlignedBox.cpp b/src/Nazara/Utility/AxisAlignedBox.cpp index 75b23468a..a481fb703 100644 --- a/src/Nazara/Utility/AxisAlignedBox.cpp +++ b/src/Nazara/Utility/AxisAlignedBox.cpp @@ -155,6 +155,11 @@ NzString NzAxisAlignedBox::ToString() const return "NzAxisAlignedBox(ERROR)"; } +NzAxisAlignedBox::operator NzString() const +{ + return ToString(); +} + NzAxisAlignedBox NzAxisAlignedBox::Lerp(const NzAxisAlignedBox& from, const NzAxisAlignedBox& to, float interpolation) { #ifdef NAZARA_DEBUG From 6b9c88221a0b772575f71c8fa503fac43c12ae09 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 2 Nov 2012 23:47:09 +0100 Subject: [PATCH 25/27] Added Shader::IsValid() Former-commit-id: 829320e4fd92dafb829019d875135ca9b75a3db2 --- include/Nazara/Renderer/Shader.hpp | 1 + src/Nazara/Renderer/Shader.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/include/Nazara/Renderer/Shader.hpp b/include/Nazara/Renderer/Shader.hpp index 4b9e56454..c019a1dd0 100644 --- a/include/Nazara/Renderer/Shader.hpp +++ b/include/Nazara/Renderer/Shader.hpp @@ -43,6 +43,7 @@ class NAZARA_API NzShader : public NzResource, NzNonCopyable bool IsCompiled() const; bool IsLoaded(nzShaderType type) const; + bool IsValid() const; bool Load(nzShaderType type, const NzString& source); bool LoadFromFile(nzShaderType type, const NzString& source); diff --git a/src/Nazara/Renderer/Shader.cpp b/src/Nazara/Renderer/Shader.cpp index 5ef569b75..bb4d63ebb 100644 --- a/src/Nazara/Renderer/Shader.cpp +++ b/src/Nazara/Renderer/Shader.cpp @@ -214,6 +214,11 @@ bool NzShader::IsLoaded(nzShaderType type) const return m_impl->IsLoaded(type); } +bool NzShader::IsValid() const +{ + return m_impl != nullptr; +} + bool NzShader::Load(nzShaderType type, const NzString& source) { #if NAZARA_RENDERER_SAFE From c82b2510f05a9099cc9c6658c61f2e6f1b64d34c Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 3 Nov 2012 00:15:16 +0100 Subject: [PATCH 26/27] Fixed NULL caracter in shader logs Former-commit-id: 998033458dce185715f7539167fb88a402a34ff3 --- src/Nazara/Renderer/GLSLShader.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Nazara/Renderer/GLSLShader.cpp b/src/Nazara/Renderer/GLSLShader.cpp index 90198675a..d68bdb62c 100644 --- a/src/Nazara/Renderer/GLSLShader.cpp +++ b/src/Nazara/Renderer/GLSLShader.cpp @@ -97,9 +97,9 @@ bool NzGLSLShader::Compile() if (length > 1) { m_log.Clear(true); - m_log.Reserve(length+19-1); // La taille retournée est celle du buffer (Avec caractère de fin) + m_log.Reserve(length+15-2); // La taille retournée est celle du buffer (Avec caractère de fin) m_log.Prepend("Linkage error: "); - m_log.Resize(length+19-1); // Extension du buffer d'écriture pour ajouter le log + m_log.Resize(length+15-2); // Extension du buffer d'écriture pour ajouter le log glGetProgramInfoLog(m_program, length-1, nullptr, &m_log[19]); } @@ -264,9 +264,9 @@ bool NzGLSLShader::Load(nzShaderType type, const NzString& source) if (length > 1) { m_log.Clear(true); - m_log.Reserve(length+19-1); // La taille retournée est celle du buffer (Avec caractère de fin) + m_log.Reserve(length+19-2); // La taille retournée est celle du buffer (Avec caractère de fin) m_log.Prepend("Compilation error: "); - m_log.Resize(length+19-1); // Extension du buffer d'écriture pour ajouter le log + m_log.Resize(length+19-2); // Extension du buffer d'écriture pour ajouter le log glGetShaderInfoLog(shader, length-1, nullptr, &m_log[19]); } From 6301bd8f38a6b0c48abaaa22b81e3280556db00f Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 3 Nov 2012 00:34:12 +0100 Subject: [PATCH 27/27] Fixed matrices not updated when changing shader -Ugly fix Former-commit-id: 19140fcf8966b1db9c2c324f4654db77f6a4aeef --- src/Nazara/Renderer/Renderer.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Nazara/Renderer/Renderer.cpp b/src/Nazara/Renderer/Renderer.cpp index 2e78d6600..1a14cfb57 100644 --- a/src/Nazara/Renderer/Renderer.cpp +++ b/src/Nazara/Renderer/Renderer.cpp @@ -592,6 +592,10 @@ bool NzRenderer::SetShader(NzShader* shader) s_matrixLocation[nzMatrixCombination_ViewProj] = shader->GetUniformLocation("ViewProjMatrix"); s_matrixLocation[nzMatrixCombination_WorldView] = shader->GetUniformLocation("WorldViewMatrix"); s_matrixLocation[nzMatrixCombination_WorldViewProj] = shader->GetUniformLocation("WorldViewProjMatrix"); + + ///FIXME: Peut VRAIMENT être optimisé + for (unsigned int i = 0; i < totalMatrixCount; ++i) + s_matrixUpdated[i] = false; } s_shader = shader;