First part of render texture commit

Added NzContext::EnsureContext and NzContext::GetThreadContext
Added NzCube
Added NzRect::GetCenter
Added methods to send vectors to shaders
Added NzRenderer::SetViewport
Fixed NzRect::ExtendTo calculations
Fixed NzImage::Update checks with level > 0
No longer use glTexStorage when creating a texture to prevent a bug
NzBuffer's Lock and Unlock operations renamed to Map and Unmap
NzVector2/3/4 can now cast implicitly to a pointer
Optimized compilation time of String.hpp
Optimized normalisaton of quaternions
Optimized passing uniforms to shaders
Quaternion now automaticaly Normalize h
Removed macro definition of NAZARA_RENDERER_OPENGL from Renderer
Removed implicit cast from NzVector2/3/4 to NzString
Renamed nzBufferLock to nzBufferAccess
Renamed NzRenderTarget::CanActivate to IsValid
This commit is contained in:
Lynix
2012-06-13 07:40:31 +02:00
parent b1632842ae
commit e2a38b3790
55 changed files with 1400 additions and 417 deletions

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CUBE_HPP
#define NAZARA_CUBE_HPP
#include <Nazara/Core/String.hpp>
#include <Nazara/Math/Vector3.hpp>
template<typename T>
class NzCube
{
public:
NzCube();
NzCube(T X, T Y, T Z, T Width, T Height, T Depth);
NzCube(T cube[6]);
template<typename U> explicit NzCube(const NzCube<U>& rect);
NzCube(const NzCube& rect) = default;
~NzCube() = default;
bool Contains(T X, T Y, T Z) const;
bool Contains(const NzVector3<T>& point) const;
bool Contains(const NzCube& rect) const;
void ExtendTo(const NzVector3<T>& point);
void ExtendTo(const NzCube& rect);
NzVector3<T> GetCenter() const;
bool Intersect(const NzCube& rect) const;
bool Intersect(const NzCube& rect, NzCube& intersection) const;
bool IsValid() const;
NzString ToString() const;
operator NzString() const;
T& operator[](unsigned int i);
T operator[](unsigned int i) const;
T x, y, z, width, height, depth;
};
template<typename T>
std::ostream& operator<<(std::ostream& out, const NzCube<T>& vec);
typedef NzCube<double> NzCubed;
typedef NzCube<float> NzCubef;
typedef NzCube<int> NzCubei;
typedef NzCube<unsigned int> NzCubeui;
#include <Nazara/Math/Cube.inl>
#endif // NAZARA_CUBE_HPP

View File

@@ -0,0 +1,183 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/StringStream.hpp>
#include <algorithm>
#include <Nazara/Core/Debug.hpp>
template<typename T>
NzCube<T>::NzCube()
{
}
template<typename T>
NzCube<T>::NzCube(T X, T Y, T Z, T Width, T Height, T Depth) :
x(X),
y(Y),
z(Z),
width(Width),
height(Height),
depth(Depth)
{
}
template<typename T>
NzCube<T>::NzCube(T vec[6]) :
x(vec[0]),
y(vec[1]),
z(vec[2]),
width(vec[3]),
height(vec[4]),
depth(vec[5])
{
}
template<typename T>
template<typename U>
NzCube<T>::NzCube(const NzCube<U>& rect) :
x(static_cast<T>(rect.x)),
y(static_cast<T>(rect.y)),
z(static_cast<T>(rect.z)),
width(static_cast<T>(rect.width)),
height(static_cast<T>(rect.height)),
depth(static_cast<T>(rect.depth))
{
}
template<typename T>
bool NzCube<T>::Contains(T X, T Y, T Z) const
{
return X >= x && X < x+width &&
Y >= y && Y < y+height &&
Z >= z && Z < z+depth;
}
template<typename T>
bool NzCube<T>::Contains(const NzVector3<T>& point) const
{
return Contains(point.x, point.y, point.z);
}
template<typename T>
bool NzCube<T>::Contains(const NzCube<T>& rect) const
{
return Contains(rect.x, rect.y, rect.z) &&
Contains(rect.x + rect.width, rect.y + rect.height, rect.z + rect.depth);
}
template<typename T>
void NzCube<T>::ExtendTo(const NzVector3<T>& point)
{
x = std::min(x, point.x);
y = std::min(y, point.y);
z = std::min(z, point.z);
width = std::max(x+width, point.x)-x;
height = std::max(y+height, point.x)-y;
depth = std::max(z+depth, point.x)-z;
}
template<typename T>
void NzCube<T>::ExtendTo(const NzCube& rect)
{
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;
}
template<typename T>
NzVector3<T> NzCube<T>::GetCenter() const
{
return NzVector3<T>((x+width)/2, (y+height)/2, (z+depth)/2);
}
template<typename T>
bool NzCube<T>::Intersect(const NzCube& rect) const
{
NzCube intersection; // Optimisé par le compilateur
return Intersect(rect, intersection);
}
template<typename T>
bool NzCube<T>::Intersect(const NzCube& rect, 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);
if (left < right && top < bottom && up < down)
{
intersection.x = left;
intersection.y = top;
intersection.z = up;
intersection.width = right-left;
intersection.height = bottom-top;
intersection.depth = down-up;
return true;
}
else
return false;
}
template<typename T>
bool NzCube<T>::IsValid() const
{
return width > 0 && height > 0 && depth > 0;
}
template<typename T>
NzString NzCube<T>::ToString() const
{
NzStringStream ss;
return ss << "Cube(" << x << ", " << y << ", " << z << ", " << width << ", " << height << ", " << depth << ')';
}
template<typename T>
NzCube<T>::operator NzString() const
{
return ToString();
}
template<typename T>
T& NzCube<T>::operator[](unsigned int i)
{
if (i >= 6)
{
NzStringStream ss;
ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 4)";
throw std::domain_error(ss.ToString());
}
return *(&x+i);
}
template<typename T>
T NzCube<T>::operator[](unsigned int i) const
{
if (i >= 6)
{
NzStringStream ss;
ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 4)";
throw std::domain_error(ss.ToString());
}
return *(&x+i);
}
template<typename T>
std::ostream& operator<<(std::ostream& out, const NzCube<T>& rect)
{
return out << rect.ToString();
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -47,13 +47,11 @@ template<typename T> class NzQuaternion
//NzMatrix3<T> ToRotationMatrix() const;
NzString ToString() const;
NzQuaternion operator+(const NzQuaternion& quat) const;
NzQuaternion operator*(const NzQuaternion& quat) const;
NzVector3<T> operator*(const NzVector3<T>& vec) const;
NzQuaternion operator*(T scale) const;
NzQuaternion operator/(const NzQuaternion& quat) const;
NzQuaternion operator+=(const NzQuaternion& quat);
NzQuaternion operator*=(const NzQuaternion& quat);
NzQuaternion operator*=(T scale);
NzQuaternion operator/=(const NzQuaternion& quat);

View File

@@ -76,17 +76,21 @@ double NzQuaternion<T>::Magnitude() const
template<typename T>
double NzQuaternion<T>::Normalize()
{
double length = Magnitude();
T squaredLength = SquaredMagnitude();
if (length != 0.0)
if (std::fabs(squaredLength) > 0.00001 && std::fabs(squaredLength - 1.0) > 0.00001)
{
double length = std::sqrt(squaredLength);
w /= length;
x /= length;
y /= length;
z /= length;
}
return length;
return length;
}
else
return std::sqrt(squaredLength);
}
template<typename T>
@@ -102,6 +106,8 @@ void NzQuaternion<T>::Set(T W, T X, T Y, T Z)
x = X;
y = Y;
z = Z;
Normalize();
}
template<typename T>
@@ -111,6 +117,8 @@ void NzQuaternion<T>::Set(T quat[4])
x = quat[1];
y = quat[2];
z = quat[3];
Normalize();
}
template<typename T>
@@ -195,35 +203,32 @@ NzString NzQuaternion<T>::ToString() const
return ss << "Quaternion(" << w << " | " << x << ", " << y << ", " << z << ')';
}
template<typename T>
NzQuaternion<T> NzQuaternion<T>::operator+(const NzQuaternion& quat) const
{
return NzQuaternion(w + quat.w,
x + quat.x,
y + quat.y,
z + quat.z);
}
template<typename T>
NzQuaternion<T> NzQuaternion<T>::operator*(const NzQuaternion& quat) const
{
return NzQuaternion(w * quat.w - x * quat.x - y * quat.y - z * quat.z,
w * quat.x + x * quat.w + y * quat.z - z * quat.y,
w * quat.y + y * quat.w + z * quat.x - x * quat.z,
w * quat.z + z * quat.w + x * quat.y - y * quat.x);
NzQuaternion result(w * quat.w - x * quat.x - y * quat.y - z * quat.z,
w * quat.x + x * quat.w + y * quat.z - z * quat.y,
w * quat.y + y * quat.w + z * quat.x - x * quat.z,
w * quat.z + z * quat.w + x * quat.y - y * quat.x);
result.Normalize();
return result;
}
template<typename T>
NzVector3<T> NzQuaternion<T>::operator*(const NzVector3<T>& vec) const
{
NzVector3<T> uv, uuv;
NzVector3<T> qvec(x, y, z);
uv = qvec.CrossProduct(vec);
uuv = qvec.CrossProduct(uv);
uv *= 2.0 * w;
uuv *= 2.0;
NzVector3<T> normal(vec);
normal.Normalise();
NzQuaternion qvec(0.0, normal.x, normal.y, normal.z);
NzQuaternion result;
result = operator*(qvec * GetConjugate());
return NzVector3<T>(result.x, result.y, result.z);
return vec + uv + uuv;
}
template<typename T>
@@ -241,24 +246,12 @@ NzQuaternion<T> NzQuaternion<T>::operator/(const NzQuaternion& quat) const
return GetConjugate(quat) * (*this);
}
template<typename T>
NzQuaternion<T> NzQuaternion<T>::operator+=(const NzQuaternion& quat)
{
w += quat.w;
x += quat.x;
y += quat.y;
z += quat.z;
return *this;
}
template<typename T>
NzQuaternion<T> NzQuaternion<T>::operator*=(const NzQuaternion& quat)
{
NzQuaternion q(*this);
operator=(q * quat);
return *this;
return operator=(q * quat);
}
template<typename T>
@@ -276,9 +269,8 @@ template<typename T>
NzQuaternion<T> NzQuaternion<T>::operator/=(const NzQuaternion& quat)
{
NzQuaternion q(*this);
operator=(q / quat);
return *this;
return operator=(q / quat);
}
template<typename T>

View File

@@ -28,6 +28,8 @@ class NzRect
void ExtendTo(const NzVector2<T>& point);
void ExtendTo(const NzRect& rect);
NzVector2<T> GetCenter() const;
bool Intersect(const NzRect& rect) const;
bool Intersect(const NzRect& rect, NzRect& intersection) const;

View File

@@ -65,7 +65,7 @@ void NzRect<T>::ExtendTo(const NzVector2<T>& point)
x = std::min(x, point.x);
y = std::min(y, point.y);
width = std::max(x+width, point.x)-x;
height = std::max(x+width, point.x)-y;
height = std::max(y+height, point.y)-y;
}
template<typename T>
@@ -74,7 +74,13 @@ void NzRect<T>::ExtendTo(const NzRect& rect)
x = std::min(x, rect.x);
y = std::min(y, rect.y);
width = std::max(x+width, rect.x+rect.width)-x;
height = std::max(x+width, rect.x+rect.height)-y;
height = std::max(x+height, rect.y+rect.height)-y;
}
template<typename T>
NzVector2<T> NzRect<T>::GetCenter() const
{
return NzVector2<T>((x+width)/2, (y+height)/2);
}
template<typename T>
@@ -97,7 +103,7 @@ bool NzRect<T>::Intersect(const NzRect& rect, NzRect& intersection) const
intersection.x = left;
intersection.y = top;
intersection.width = right-left;
intersection.height = top-bottom;
intersection.height = bottom-top;
return true;
}

View File

@@ -33,7 +33,8 @@ template<typename T> class NzVector2
NzString ToString() const;
operator NzString() const;
operator T*();
operator const T*() const;
T& operator[](unsigned int i);
T operator[](unsigned int i) const;

View File

@@ -136,9 +136,15 @@ NzString NzVector2<T>::ToString() const
}
template<typename T>
NzVector2<T>::operator NzString() const
NzVector2<T>::operator T*()
{
return ToString();
return &x;
}
template<typename T>
NzVector2<T>::operator const T*() const
{
return &x;
}
template<typename T>

View File

@@ -34,7 +34,8 @@ template<typename T> class NzVector3
NzString ToString() const;
operator NzString() const;
operator T*();
operator const T*() const;
T& operator[](unsigned int i);
T operator[](unsigned int i) const;

View File

@@ -153,9 +153,15 @@ NzString NzVector3<T>::ToString() const
}
template<typename T>
NzVector3<T>::operator NzString() const
NzVector3<T>::operator T*()
{
return ToString();
return &x;
}
template<typename T>
NzVector3<T>::operator const T*() const
{
return &x;
}
template<typename T>

View File

@@ -28,7 +28,8 @@ template<typename T> class NzVector4
NzString ToString() const;
operator NzString() const;
operator T*();
operator const T*() const;
T& operator[](unsigned int i);
T operator[](unsigned int i) const;

View File

@@ -120,9 +120,15 @@ NzString NzVector4<T>::ToString() const
}
template<typename T>
NzVector4<T>::operator NzString() const
NzVector4<T>::operator T*()
{
return ToString();
return &x;
}
template<typename T>
NzVector4<T>::operator const T*() const
{
return &x;
}
template<typename T>