Merge changes from The Witness.

This commit is contained in:
castano
2011-04-06 02:41:15 +00:00
parent 9ebcff93de
commit 8a837981b6
19 changed files with 212 additions and 45 deletions

View File

@ -169,6 +169,17 @@ namespace nv
return color;
}
inline Color32 toColor32(Vector4::Arg v)
{
Color32 color;
color.r = uint8(clamp(v.x, 0.0f, 1.0f) * 255);
color.g = uint8(clamp(v.y, 0.0f, 1.0f) * 255);
color.b = uint8(clamp(v.z, 0.0f, 1.0f) * 255);
color.a = uint8(clamp(v.w, 0.0f, 1.0f) * 255);
return color;
}
inline Vector4 toVector4(Color32 c)
{
const float scale = 1.0f / 255.0f;

View File

@ -12,6 +12,18 @@ namespace nv {
// Does not handle NaN or infinity.
uint32 fast_half_to_float( uint16 h );
inline uint16 to_half(float c) {
union { float f; uint32 u; } f;
f.f = c;
return nv::half_from_float( f.u );
}
inline float to_float(uint16 c) {
union { float f; uint32 u; } f;
f.u = nv::fast_half_to_float( c );
return f.f;
}
} // nv namespace
#endif // NV_MATH_HALF_H

View File

@ -46,6 +46,9 @@ namespace nv
};
};
// Helpers to convert vector types. Assume T has x,y members and 2 argument constructor.
template <typename T> T to(Vector2::Arg v) { return T(v.x, v.y); }
class NVMATH_CLASS Vector3
{
@ -85,7 +88,6 @@ namespace nv
};
// Helpers to convert vector types. Assume T has x,y,z members and 3 argument constructor.
template <typename T> Vector3 from(const T & v) { return Vector3(v.x, v.y, v.z); }
template <typename T> T to(Vector3::Arg v) { return T(v.x, v.y, v.z); }
@ -128,6 +130,10 @@ namespace nv
};
};
// Helpers to convert vector types. Assume T has x,y,z members and 3 argument constructor.
template <typename T> T to(Vector4::Arg v) { return T(v.x, v.y, v.z, v.w); }
// Vector2
@ -484,6 +490,14 @@ namespace nv
return isFinite(v.x) && isFinite(v.y);
}
inline Vector2 validate(Vector2::Arg v, Vector2::Arg fallback = Vector2(0.0f))
{
if (!isValid(v)) return fallback;
Vector2 vf = v;
nv::floatCleanup(vf.component, 2);
return vf;
}
// Vector3
@ -630,11 +644,6 @@ namespace nv
return Vector3(clamp(v.x, min, max), clamp(v.y, min, max), clamp(v.z, min, max));
}
inline bool isValid(Vector3::Arg v)
{
return isFinite(v.x) && isFinite(v.y) && isFinite(v.z);
}
inline Vector3 floor(Vector3::Arg v)
{
return Vector3(floorf(v.x), floorf(v.y), floorf(v.z));
@ -645,6 +654,21 @@ namespace nv
return Vector3(ceilf(v.x), ceilf(v.y), ceilf(v.z));
}
inline bool isValid(Vector3::Arg v)
{
return isFinite(v.x) && isFinite(v.y) && isFinite(v.z);
}
inline Vector3 validate(Vector3::Arg v, Vector3::Arg fallback = Vector3(0.0f))
{
if (!isValid(v)) return fallback;
Vector3 vf = v;
nv::floatCleanup(vf.component, 3);
return vf;
}
// Vector4
inline Vector4 add(Vector4::Arg a, Vector4::Arg b)
@ -758,6 +782,14 @@ namespace nv
return isFinite(v.x) && isFinite(v.y) && isFinite(v.z) && isFinite(v.w);
}
inline Vector4 validate(Vector4::Arg v, Vector4::Arg fallback = Vector4(0.0f))
{
if (!isValid(v)) return fallback;
Vector4 vf = v;
nv::floatCleanup(vf.component, 4);
return vf;
}
} // nv namespace
#endif // NV_MATH_VECTOR_H

View File

@ -176,7 +176,7 @@ namespace nv
// http://chrishecker.com/Miscellaneous_Technical_Articles#Floating_Point
inline int iround(float f)
{
return int(f);
return int(floorf(f + 0.5f));
}
inline int ifloor(float f)
@ -200,6 +200,16 @@ namespace nv
return float(iround(f));
}
// Eliminates negative zeros from a float array.
inline void floatCleanup(float * fp, int n)
{
nvDebugCheck(isFinite(*fp));
for (int i = 0; i < n; i++) {
union { float f; uint32 i; } x = { fp[i] };
if (x.i == 0x80000000) fp[i] = 0.0f;
}
}
} // nv
#endif // NV_MATH_H