General bug fixes (#142)

* Core/Bitset: Fix TestAll method

* Fix documentation

* Fix color and their conversions

* Core/ByteStream: Fix return of Write

* Fix compiler warnings

* Math/Algorithm: Fix angle normalization

* Math/BoundingVolume: Fix lerp

* Math: Fix relation between Matrix4 and Quaternion

* More tests

* X11/Window: Fix mouse moved event generated when doing Mouse::SetPosition

* Update ChangeLog

* Should fix compilation on Windows

* Should fix compilation on Windows

Forgot to include array for Windows
This commit is contained in:
Gawaboumga
2017-11-21 12:16:46 +01:00
committed by Jérôme Leclercq
parent f2506ee918
commit f991a9529e
52 changed files with 1287 additions and 272 deletions

View File

@@ -130,8 +130,6 @@ namespace Nz
* \brief Returns the number of elements in a C-array
* \return The number of elements
*
* \param name C-array
*
* \see CountOf
*/
template<typename T, std::size_t N>

View File

@@ -815,7 +815,7 @@ namespace Nz
for (std::size_t i = 0; i < m_blocks.size(); ++i)
{
Block mask = (i == m_blocks.size() - 1) ? lastBlockMask : fullBitMask;
if (m_blocks[i] == mask) // The extra bits are set to zero, thus we can't test without proceeding with a mask
if (m_blocks[i] != mask) // The extra bits are set to zero, thus we can't test without proceeding with a mask
return false;
}

View File

@@ -421,7 +421,6 @@ namespace Nz
/*!
* \brief Resizes the string
* \return A reference to this
*
* \param newSize Target size
*/
@@ -433,7 +432,6 @@ namespace Nz
/*!
* \brief Resizes the string
* \return A reference to this
*
* \param newSize Target size
* \param byte Byte to add if newSize is greather than actual size

View File

@@ -41,7 +41,7 @@ namespace Nz
void SetStream(void* ptr, Nz::UInt64 size);
void SetStream(const void* ptr, Nz::UInt64 size);
inline void Write(const void* data, std::size_t size);
inline std::size_t Write(const void* data, std::size_t size);
template<typename T>
ByteStream& operator>>(T& value);

View File

@@ -101,7 +101,7 @@ namespace Nz
* \brief Reads data
* \return Number of data read
*
* \param buffer Preallocated buffer to contain information read
* \param ptr Preallocated buffer to contain information read
* \param size Size of the read and thus of the buffer
*/
@@ -117,7 +117,7 @@ namespace Nz
/*!
* \brief Sets the stream endianness
*
* \param Type of the endianness
* \param endiannes Type of the endianness
*/
inline void ByteStream::SetDataEndianness(Endianness endiannes)
@@ -154,13 +154,13 @@ namespace Nz
* \remark Produces a NazaraAssert if buffer is nullptr
*/
inline void ByteStream::Write(const void* data, std::size_t size)
inline std::size_t ByteStream::Write(const void* data, std::size_t size)
{
if (!m_context.stream)
OnEmptyStream();
FlushBits();
m_context.stream->Write(data, size);
return m_context.stream->Write(data, size);
}
/*!

View File

@@ -23,6 +23,7 @@ namespace Nz
inline explicit Color(UInt8 lightness);
inline Color(UInt8 color[3], UInt8 alpha = 255);
inline Color(const Color& color) = default;
inline Color(Color&& color) = default;
inline ~Color() = default;
inline bool IsOpaque() const;
@@ -32,6 +33,9 @@ namespace Nz
inline Color operator+(const Color& angles) const;
inline Color operator*(const Color& angles) const;
inline Color& operator=(const Color& other) = default;
inline Color& operator=(Color&& other) = default;
inline Color operator+=(const Color& angles);
inline Color operator*=(const Color& angles);
@@ -40,13 +44,13 @@ namespace Nz
static inline Color FromCMY(float cyan, float magenta, float yellow);
static inline Color FromCMYK(float cyan, float magenta, float yellow, float black);
static inline Color FromHSL(UInt8 hue, UInt8 saturation, UInt8 lightness);
static inline Color FromHSL(float hue, float saturation, float lightness);
static inline Color FromHSV(float hue, float saturation, float value);
static inline Color FromXYZ(const Vector3f& vec);
static inline Color FromXYZ(float x, float y, float z);
static inline void ToCMY(const Color& color, float* cyan, float* magenta, float* yellow);
static inline void ToCMYK(const Color& color, float* cyan, float* magenta, float* yellow, float* black);
static inline void ToHSL(const Color& color, UInt8* hue, UInt8* saturation, UInt8* lightness);
static inline void ToHSL(const Color& color, float* hue, float* saturation, float* lightness);
static inline void ToHSV(const Color& color, float* hue, float* saturation, float* value);
static inline void ToXYZ(const Color& color, Vector3f* vec);
static inline void ToXYZ(const Color& color, float* x, float* y, float* z);

View File

@@ -219,35 +219,29 @@ namespace Nz
* \brief Converts HSL representation to RGB
* \return Color resulting
*
* \param hue Hue component
* \param saturation Saturation component
* \param lightness Lightness component
* \param hue Hue component in [0, 360]
* \param saturation Saturation component [0, 1]
* \param lightness Lightness component [0, 1]
*/
inline Color Color::FromHSL(UInt8 hue, UInt8 saturation, UInt8 lightness)
inline Color Color::FromHSL(float hue, float saturation, float lightness)
{
if (saturation == 0)
if (NumberEquals(saturation, 0.f))
{
// RGB results from 0 to 255
return Color(lightness * 255,
lightness * 255,
lightness * 255);
return Color(static_cast<UInt8>(lightness * 255.f));
}
else
{
// Norme Windows
float l = lightness/240.f;
float h = hue/240.f;
float s = saturation/240.f;
float v2;
if (l < 0.5f)
v2 = l * (1.f + s);
if (lightness < 0.5f)
v2 = lightness * (1.f + saturation);
else
v2 = (l + s) - (s*l);
v2 = (lightness + saturation) - (saturation * lightness);
float v1 = 2.f * l - v2;
float v1 = 2.f * lightness - v2;
float h = hue / 360.f;
return Color(static_cast<UInt8>(255.f * Hue2RGB(v1, v2, h + (1.f/3.f))),
static_cast<UInt8>(255.f * Hue2RGB(v1, v2, h)),
static_cast<UInt8>(255.f * Hue2RGB(v1, v2, h - (1.f/3.f))));
@@ -258,9 +252,9 @@ namespace Nz
* \brief Converts HSV representation to RGB
* \return Color resulting
*
* \param hue Hue component
* \param saturation Saturation component
* \param value Value component
* \param hue Hue component in [0, 360]
* \param saturation Saturation component in [0, 1]
* \param value Value component in [0, 1]
*/
inline Color Color::FromHSV(float hue, float saturation, float value)
@@ -269,16 +263,15 @@ namespace Nz
return Color(static_cast<UInt8>(value * 255.f));
else
{
float h = hue/360.f * 6.f;
float s = saturation/360.f;
float h = (hue / 360.f) * 6.f;
if (NumberEquals(h, 6.f))
h = 0; // hue must be < 1
if (NumberEquals(h , 6.f))
h = 0.f; // hue must be < 1
int i = static_cast<unsigned int>(h);
float v1 = value * (1.f - s);
float v2 = value * (1.f - s * (h - i));
float v3 = value * (1.f - s * (1.f - (h - i)));
int i = static_cast<int>(h);
float v1 = value * (1.f - saturation);
float v2 = value * (1.f - saturation * (h - i));
float v3 = value * (1.f - saturation * (1.f - (h - i)));
float r, g, b;
switch (i)
@@ -321,7 +314,7 @@ namespace Nz
}
// RGB results from 0 to 255
return Color(static_cast<UInt8>(r*255.f), static_cast<UInt8>(g*255.f), static_cast<UInt8>(b*255.f));
return Color(static_cast<UInt8>(r * 255.f), static_cast<UInt8>(g * 255.f), static_cast<UInt8>(b * 255.f));
}
}
@@ -338,7 +331,7 @@ namespace Nz
}
/*!
* \brief Converts XYZ representation to RGB
* \brief Converts XYZ representation (D65/2°) to RGB
* \return Color resulting
*
* \param x X component
@@ -362,12 +355,12 @@ namespace Nz
r *= 12.92f;
if (g > 0.0031308f)
g = 1.055f * (std::pow(r, 1.f/2.4f)) - 0.055f;
g = 1.055f * (std::pow(g, 1.f/2.4f)) - 0.055f;
else
g *= 12.92f;
if (b > 0.0031308f)
b = 1.055f * (std::pow(r, 1.f/2.4f)) - 0.055f;
b = 1.055f * (std::pow(b, 1.f/2.4f)) - 0.055f;
else
b *= 12.92f;
@@ -427,12 +420,12 @@ namespace Nz
* \brief Converts RGB representation to HSL
*
* \param color Color to transform
* \param hue Hue component
* \param saturation Saturation component
* \param lightness Lightness component
* \param hue Hue component [0, 360]
* \param saturation Saturation component in [0, 1]
* \param lightness Lightness component in [0, 1]
*/
inline void Color::ToHSL(const Color& color, UInt8* hue, UInt8* saturation, UInt8* lightness)
inline void Color::ToHSL(const Color& color, float* hue, float* saturation, float* lightness)
{
float r = color.r / 255.f;
float g = color.g / 255.f;
@@ -443,42 +436,41 @@ namespace Nz
float deltaMax = max - min; //Delta RGB value
float l = (max + min)/2.f;
float l = (max + min) / 2.f;
*lightness = l;
if (NumberEquals(deltaMax, 0.f))
{
//This is a gray, no chroma...
*hue = 0; //HSL results from 0 to 1
*saturation = 0;
*hue = 0.f;
*saturation = 0.f;
}
else
{
//Chromatic data...
if (l < 0.5f)
*saturation = static_cast<UInt8>(deltaMax/(max+min)*240.f);
if (l <= 0.5f)
*saturation = deltaMax / (max + min);
else
*saturation = static_cast<UInt8>(deltaMax/(2.f-max-min)*240.f);
*saturation = (deltaMax / (2.f - max - min));
*lightness = static_cast<UInt8>(l*240.f);
float deltaR = ((max - r)/6.f + deltaMax/2.f)/deltaMax;
float deltaG = ((max - g)/6.f + deltaMax/2.f)/deltaMax;
float deltaB = ((max - b)/6.f + deltaMax/2.f)/deltaMax;
float deltaR = ((max - r) / 6.f + deltaMax / 2.f) / deltaMax;
float deltaG = ((max - g) / 6.f + deltaMax / 2.f) / deltaMax;
float deltaB = ((max - b) / 6.f + deltaMax / 2.f) / deltaMax;
float h;
if (NumberEquals(r, max))
h = deltaB - deltaG;
else if (NumberEquals(g, max))
h = (1.f/3.f) + deltaR - deltaB;
h = (1.f / 3.f) + deltaR - deltaB;
else
h = (2.f/3.f) + deltaG - deltaR;
h = (2.f / 3.f) + deltaG - deltaR;
if (h < 0.f)
h += 1.f;
else if (h > 1.f)
h -= 1.f;
*hue = static_cast<UInt8>(h*240.f);
*hue = h * 360.f;
}
}
@@ -507,33 +499,33 @@ namespace Nz
if (NumberEquals(deltaMax, 0.f))
{
//This is a gray, no chroma...
*hue = 0; //HSV results from 0 to 1
*saturation = 0;
*hue = 0.f;
*saturation = 0.f;
}
else
{
//Chromatic data...
*saturation = deltaMax/max*360.f;
*saturation = deltaMax / max;
float deltaR = ((max - r)/6.f + deltaMax/2.f)/deltaMax;
float deltaG = ((max - g)/6.f + deltaMax/2.f)/deltaMax;
float deltaB = ((max - b)/6.f + deltaMax/2.f)/deltaMax;
float deltaR = ((max - r) / 6.f + deltaMax / 2.f) / deltaMax;
float deltaG = ((max - g) / 6.f + deltaMax / 2.f) / deltaMax;
float deltaB = ((max - b) / 6.f + deltaMax / 2.f) / deltaMax;
float h;
if (NumberEquals(r, max))
h = deltaB - deltaG;
else if (NumberEquals(g, max))
h = (1.f/3.f) + deltaR - deltaB;
h = (1.f / 3.f) + deltaR - deltaB;
else
h = (2.f/3.f) + deltaG - deltaR;
h = (2.f / 3.f) + deltaG - deltaR;
if (h < 0.f)
h += 1.f;
else if (h > 1.f)
h -= 1.f;
*hue = h*360.f;
*hue = h * 360.f;
}
}

View File

@@ -29,7 +29,7 @@ namespace Nz
/*!
* \brief Constructs a Flags object using an Enum value
*
* \param value enumVal
* \param enumVal enumVal
*
* Setup a Flags object with only one flag active (corresponding to the enum value passed as argument).
*/

View File

@@ -484,16 +484,17 @@ namespace Nz
namespace std
{
/*!
* \ingroup core
* \brief Gives a hash representation of the object, specialisation of std
* \return Hash of the ObjectRef
*
* \param object Object to hash
*/
template<typename T>
struct hash<Nz::ObjectRef<T>>
{
/*!
* \ingroup core
* \brief Gives a hash representation of the object, specialisation of std
* \return Hash of the ObjectRef
*
* \param object Object to hash
*/
size_t operator()(const Nz::ObjectRef<T>& object) const
{
hash<T*> h;

View File

@@ -385,7 +385,7 @@ namespace Nz
*
* \param size (Width, Depth)
* \param subdivision Number of subdivision for the axis
* \param planeInfo Information for the plane
* \param plane Information for the plane
* \param uvCoords Coordinates for texture
*/
inline Primitive Primitive::Plane(const Vector2f& size, const Vector2ui& subdivision, const Planef& plane, const Rectf& uvCoords)

View File

@@ -121,7 +121,7 @@ namespace std
const char* ptr = str.GetConstBuffer();
do
h = ((h << 5) + h) + *ptr;
h = ((h << 5) + h) + static_cast<size_t>(*ptr);
while (*++ptr);
}

View File

@@ -265,7 +265,8 @@ namespace Nz
/*!
* \brief Sets the inner angle in spot light
* \return innerAngle Inner angle
*
* \param innerAngle Inner angle
*/
inline void Light::SetInnerAngle(float innerAngle)
@@ -289,7 +290,8 @@ namespace Nz
/*!
* \brief Sets the outer angle in spot light
* \return outerAngle Outer angle
*
* \param outerAngle Outer angle
*
* \remark Invalidates the bounding volume
*/
@@ -305,7 +307,8 @@ namespace Nz
/*!
* \brief Sets the radius of the light
* \return radius Light radius
*
* \param radius Light radius
*
* \remark Invalidates the bounding volume
*/

View File

@@ -552,11 +552,11 @@ namespace Nz
#endif
const T twoLimit = limit * T(2);
angle = std::fmod(angle + limit, twoLimit);
angle = std::fmod(angle, twoLimit);
if (angle < T(0))
angle += twoLimit;
return angle - limit;
return angle;
}
/*!

View File

@@ -541,7 +541,7 @@ namespace Nz
return Infinite();
case Extend_Null:
return from.obb * interpolation;
return to.obb * interpolation;
}
// If we arrive here, the extend is invalid

View File

@@ -77,7 +77,6 @@ namespace Nz
/*!
* \brief Makes the euler angle (0, 0, 0)
* \return A reference to this euler angle with components (0, 0, 0)
*
* \see Zero
*/
@@ -276,7 +275,7 @@ namespace Nz
* \brief Substracts the components of other euler angle to this euler angle
* \return A reference to this euler angle where components are the difference of this euler angle and the other one
*
* \param angle The other euler angle to substract components with
* \param angles The other euler angle to substract components with
*/
template<typename T>

View File

@@ -664,9 +664,9 @@ namespace Nz
template<typename T>
Vector3<T> Matrix4<T>::GetSquaredScale() const
{
return Vector3<T>(m11*m11 + m21*m21 + m31*m31,
m12*m12 + m22*m22 + m32*m32,
m13*m13 + m23*m23 + m33*m33);
return Vector3<T>(m11 * m11 + m12 * m12 + m13 * m13,
m21 * m21 + m22 * m22 + m23 * m23,
m31 * m31 + m32 * m32 + m33 * m33);
}
/*!
@@ -1153,36 +1153,32 @@ namespace Nz
*
* \param rotation Quaternion representing a rotation of space
*
* \remark 3rd column and row are unchanged
* \remark 3rd column and row are unchanged. Scale is removed.
*/
template<typename T>
Matrix4<T>& Matrix4<T>::SetRotation(const Quaternion<T>& rotation)
{
T tx = rotation.x + rotation.x;
T ty = rotation.y + rotation.y;
T tz = rotation.z + rotation.z;
T twx = tx * rotation.w;
T twy = ty * rotation.w;
T twz = tz * rotation.w;
T txx = tx * rotation.x;
T txy = ty * rotation.x;
T txz = tz * rotation.x;
T tyy = ty * rotation.y;
T tyz = tz * rotation.y;
T tzz = tz * rotation.z;
T qw = rotation.w;
T qx = rotation.x;
T qy = rotation.y;
T qz = rotation.z;
m11 = F(1.0) - (tyy + tzz);
m12 = txy + twz;
m13 = txz - twy;
T qx2 = qx * qx;
T qy2 = qy * qy;
T qz2 = qz * qz;
m21 = txy - twz;
m22 = F(1.0) - (txx + tzz);
m23 = tyz + twx;
m11 = F(1.0) - F(2.0) * qy2 - F(2.0) * qz2;
m21 = F(2.0) * qx * qy - F(2.0) * qz * qw;
m31 = F(2.0) * qx * qz + F(2.0) * qy * qw;
m31 = txz + twy;
m32 = tyz - twx;
m33 = F(1.0) - (txx + tyy);
m12 = F(2.0) * qx * qy + F(2.0) * qz * qw;
m22 = F(1.0) - F(2.0) * qx2 - F(2.0) * qz2;
m32 = F(2.0) * qy * qz - F(2.0) * qx * qw;
m13 = F(2.0) * qx * qz - F(2.0) * qy * qw;
m23 = F(2.0) * qy * qz + F(2.0) * qx * qw;
m33 = F(1.0) - F(2.0) * qx2 - F(2.0) * qy2;
return *this;
}

View File

@@ -106,9 +106,9 @@ namespace Nz
* \brief Returns the distance from the plane to the point
* \return Distance to the point
*
* \param X X position of the point
* \param Y Y position of the point
* \param Z Z position of the point
* \param x X position of the point
* \param y Y position of the point
* \param z Z position of the point
*
* \remark If T is negative, it means that the point is in the opposite direction of the normal
*
@@ -319,7 +319,7 @@ namespace Nz
* \brief Compares the plane to other one
* \return true if the planes are the same
*
* \param vec Other vector to compare with
* \param plane Other vector to compare with
*
* \remark Plane with normal N and distance D is the same than with normal -N et distance -D
*/

View File

@@ -251,35 +251,19 @@ namespace Nz
* \param from Initial vector
* \param to Target vector
*
* \remark Vectors are not required to be normalized
*
* \see RotationBetween
*/
template<typename T>
Quaternion<T>& Quaternion<T>::MakeRotationBetween(const Vector3<T>& from, const Vector3<T>& to)
{
// TODO (Gawaboumga): Replace by http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors ?
T dot = from.DotProduct(to);
if (NumberEquals(dot, F(-1.0)))
{
Vector3<T> cross = Vector3<T>::CrossProduct(Vector3<T>::UnitX(), from);
if (NumberEquals(cross.GetLength(), F(0.0)))
cross = Vector3<T>::CrossProduct(Vector3<T>::UnitY(), from);
return Set(F(180.0), cross);
}
else if (NumberEquals(dot, F(1.0)))
return MakeIdentity();
else
{
Vector3<T> a = from.CrossProduct(to);
x = a.x;
y = a.y;
z = a.z;
w = T(1.0) + dot;
return Normalize();
}
// Based on: http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors
T norm = std::sqrt(from.GetSquaredLength() * to.GetSquaredLength());
Vector3<T> crossProduct = from.CrossProduct(to);
Set(norm + from.DotProduct(to), crossProduct.x, crossProduct.y, crossProduct.z);
return Normalize();
}
/*!
@@ -425,7 +409,7 @@ namespace Nz
* \brief Sets the components of the quaternion from another quaternion
* \return A reference to this quaternion
*
* \param vec The other quaternion
* \param quat The other quaternion
*/
template<typename T>
@@ -647,7 +631,7 @@ namespace Nz
* \brief Compares the quaternion to other one
* \return true if the quaternions are the same
*
* \param vec Other quaternion to compare with
* \param quat Other quaternion to compare with
*/
template<typename T>
@@ -663,7 +647,7 @@ namespace Nz
* \brief Compares the quaternion to other one
* \return false if the quaternions are the same
*
* \param vec Other quaternion to compare with
* \param quat Other quaternion to compare with
*/
template<typename T>

View File

@@ -500,7 +500,7 @@ namespace Nz
* \brief Multiplies the radius of the sphere with a scalar
* \return A sphere where the center is the same and radius is the product of this radius and the scalar
*
* \param scale The scalar to multiply radius with
* \param scalar The scalar to multiply radius with
*/
template<typename T>
@@ -513,7 +513,7 @@ namespace Nz
* \brief Multiplies the radius of other sphere with a scalar
* \return A reference to this sphere where the center is the same and radius is the product of this radius and the scalar
*
* \param scale The scalar to multiply radius with
* \param scalar The scalar to multiply radius with
*/
template<typename T>

View File

@@ -692,7 +692,7 @@ namespace Nz
* \brief Multiplies the components of other vector with a scalar
* \return A reference to this vector where components are the product of this vector and the scalar
*
* \param vec The other vector to multiply components with
* \param scale The scalar to multiply components with
*/
template<typename T>
@@ -737,7 +737,7 @@ namespace Nz
* \brief Divides the components of other vector with a scalar
* \return A reference to this vector where components are the quotient of this vector and the scalar
*
* \param vec The other vector to divide components with
* \param scale The scalar to divide components with
*
* \remark Produce a NazaraError if scale is null with NAZARA_MATH_SAFE defined
* \throw std::domain_error if NAZARA_MATH_SAFE is defined and scale is null

View File

@@ -810,7 +810,7 @@ namespace Nz
* \brief Multiplies the components of other vector with a scalar
* \return A reference to this vector where components are the product of this vector and the scalar
*
* \param vec The other vector to multiply components with
* \param scale The scalar to multiply components with
*/
template<typename T>
Vector3<T>& Vector3<T>::operator*=(T scale)
@@ -853,7 +853,7 @@ namespace Nz
* \brief Divides the components of other vector with a scalar
* \return A reference to this vector where components are the quotient of this vector and the scalar
*
* \param vec The other vector to divide components with
* \param scale The scalar to divide components with
*
* \remark Produce a NazaraError if scale is null with NAZARA_MATH_SAFE defined
* \throw std::domain_error if NAZARA_MATH_SAFE is defined and scale is null

View File

@@ -728,7 +728,7 @@ namespace Nz
* \brief Multiplies the components of other vector with a scalar
* \return A reference to this vector where components are the product of this vector and the scalar
*
* \param vec The other vector to multiply components with
* \param scale The scalar to multiply components with
*/
template<typename T>
@@ -777,7 +777,7 @@ namespace Nz
* \brief Divides the components of other vector with a scalar
* \return A reference to this vector where components are the quotient of this vector and the scalar
*
* \param vec The other vector to divide components with
* \param scale The scalar to divide components with
*
* \remark Produce a NazaraError if scale is null with NAZARA_MATH_SAFE defined
* \throw std::domain_error if NAZARA_MATH_SAFE is defined and scale is null