diff --git a/src/libXFX/BoundingBox.cpp b/src/libXFX/BoundingBox.cpp index 20e37f2..da345e1 100644 --- a/src/libXFX/BoundingBox.cpp +++ b/src/libXFX/BoundingBox.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -41,6 +42,7 @@ using namespace System; namespace XFX { const int BoundingBox::CornerCount = 8; + const Type BoundingBoxTypeInfo("BoundingBox", "XFX::BoundingBox", TypeCode::Object); BoundingBox::BoundingBox(const Vector3 min, const Vector3 max) : Max(max), Min(min) @@ -66,20 +68,28 @@ namespace XFX void BoundingBox::Contains(BoundingBox box, out ContainmentType_t& result) const { - if( Max.X < box.Min.X || Min.X > box.Max.X ) - result = ContainmentType::Disjoint; - - if( Max.Y < box.Min.Y || Min.Y > box.Max.Y ) - result = ContainmentType::Disjoint; - - if( Max.Z < box.Min.Z || Min.Z > box.Max.Z ) - result = ContainmentType::Disjoint; - - if( Min.X <= box.Min.X && box.Max.X <= Max.X && Min.Y <= box.Min.Y && - box.Max.Y <= Max.Y && Min.Z <= box.Min.Z && box.Max.Z <= Max.Z ) - result = ContainmentType::Contains; - - result = ContainmentType::Intersects; + if( Max.X < box.Min.X || Min.X > box.Max.X ) + { + result = ContainmentType::Disjoint; + } + + if( Max.Y < box.Min.Y || Min.Y > box.Max.Y ) + { + result = ContainmentType::Disjoint; + } + + if( Max.Z < box.Min.Z || Min.Z > box.Max.Z ) + { + result = ContainmentType::Disjoint; + } + + if( Min.X <= box.Min.X && box.Max.X <= Max.X && Min.Y <= box.Min.Y && + box.Max.Y <= Max.Y && Min.Z <= box.Min.Z && box.Max.Z <= Max.Z ) + { + result = ContainmentType::Contains; + } + + result = ContainmentType::Intersects; } ContainmentType_t BoundingBox::Contains(BoundingSphere sphere) const @@ -92,90 +102,98 @@ namespace XFX void BoundingBox::Contains(BoundingSphere sphere, out ContainmentType_t& result) const { float dist; - Vector3 clamped; - - Vector3::Clamp(sphere.Center, Min, Max, clamped); - - float x = sphere.Center.X - clamped.X; - float y = sphere.Center.Y - clamped.Y; - float z = sphere.Center.Z - clamped.Z; - - dist = (x * x) + (y * y) + (z * z); - float radius = sphere.Radius; - - if(dist > (radius * radius)) - result = ContainmentType::Disjoint; - - if(Min.X + radius <= sphere.Center.X && sphere.Center.X <= Max.X - radius && - Max.X - Min.X > radius && Min.Y + radius <= sphere.Center.Y && - sphere.Center.Y <= Max.Y - radius && Max.Y - Min.Y > radius && - Min.Z + radius <= sphere.Center.Z && sphere.Center.Z <= Max.Z - radius && - Max.X - Min.X > radius) - result = ContainmentType::Contains; - - result = ContainmentType::Intersects; + Vector3 clamped; + + Vector3::Clamp(sphere.Center, Min, Max, clamped); + + float x = sphere.Center.X - clamped.X; + float y = sphere.Center.Y - clamped.Y; + float z = sphere.Center.Z - clamped.Z; + + dist = (x * x) + (y * y) + (z * z); + float radius = sphere.Radius; + + if(dist > (radius * radius)) + { + result = ContainmentType::Disjoint; + } + + if(Min.X + radius <= sphere.Center.X && sphere.Center.X <= Max.X - radius && + Max.X - Min.X > radius && Min.Y + radius <= sphere.Center.Y && + sphere.Center.Y <= Max.Y - radius && Max.Y - Min.Y > radius && + Min.Z + radius <= sphere.Center.Z && sphere.Center.Z <= Max.Z - radius && + Max.X - Min.X > radius) + { + result = ContainmentType::Contains; + } + + result = ContainmentType::Intersects; } ContainmentType_t BoundingBox::Contains(const Vector3 vector) const { - if(Min.X <= vector.X && vector.X <= Max.X && Min.Y <= vector.Y && - vector.Y <= Max.Y && Min.Z <= vector.Z && vector.Z <= Max.Z) - return ContainmentType::Contains; - - return ContainmentType::Disjoint; + if(Min.X <= vector.X && vector.X <= Max.X && Min.Y <= vector.Y && + vector.Y <= Max.Y && Min.Z <= vector.Z && vector.Z <= Max.Z) + { + return ContainmentType::Contains; + } + + return ContainmentType::Disjoint; } void BoundingBox::Contains(Vector3 vector, out ContainmentType_t& result) const { - if (Min.X <= vector.X && vector.X <= Max.X && Min.Y <= vector.Y && - vector.Y <= Max.Y && Min.Z <= vector.Z && vector.Z <= Max.Z) - result = ContainmentType::Contains; - - result = ContainmentType::Disjoint; + if (Min.X <= vector.X && vector.X <= Max.X && Min.Y <= vector.Y && + vector.Y <= Max.Y && Min.Z <= vector.Z && vector.Z <= Max.Z) + { + result = ContainmentType::Contains; + } + + result = ContainmentType::Disjoint; } BoundingBox BoundingBox::CreateFromPoints(Vector3 points[], int startIndex, int length) { sassert(points != null, String::Format("points; %s", FrameworkResources::ArgumentNull_Generic)); - Vector3 min = Single::MinValue; - Vector3 max = Single::MaxValue; - + Vector3 min = Single::MinValue; + Vector3 max = Single::MaxValue; + for(int i = startIndex; i < length; i++) - { - Vector3::Min(min, points[i], min); - Vector3::Max(max, points[i], max); - } - - return BoundingBox(min, max); + { + Vector3::Min(min, points[i], min); + Vector3::Max(max, points[i], max); + } + + return BoundingBox(min, max); } BoundingBox BoundingBox::CreateFromSphere(BoundingSphere sphere) { - BoundingBox result; - result.Min = Vector3(sphere.Center.X - sphere.Radius, sphere.Center.Y - sphere.Radius, sphere.Center.Z - sphere.Radius ); - result.Max = Vector3(sphere.Center.X + sphere.Radius, sphere.Center.Y + sphere.Radius, sphere.Center.Z + sphere.Radius ); - return result; + BoundingBox result; + result.Min = Vector3(sphere.Center.X - sphere.Radius, sphere.Center.Y - sphere.Radius, sphere.Center.Z - sphere.Radius ); + result.Max = Vector3(sphere.Center.X + sphere.Radius, sphere.Center.Y + sphere.Radius, sphere.Center.Z + sphere.Radius ); + return result; } void BoundingBox::CreateFromSphere(BoundingSphere sphere, out BoundingBox& result) { - result.Min = Vector3(sphere.Center.X - sphere.Radius, sphere.Center.Y - sphere.Radius, sphere.Center.Z - sphere.Radius ); - result.Max = Vector3(sphere.Center.X + sphere.Radius, sphere.Center.Y + sphere.Radius, sphere.Center.Z + sphere.Radius ); + result.Min = Vector3(sphere.Center.X - sphere.Radius, sphere.Center.Y - sphere.Radius, sphere.Center.Z - sphere.Radius ); + result.Max = Vector3(sphere.Center.X + sphere.Radius, sphere.Center.Y + sphere.Radius, sphere.Center.Z + sphere.Radius ); } BoundingBox BoundingBox::CreateMerged(BoundingBox box1, BoundingBox box2) { BoundingBox result; - Vector3::Min(box1.Min, box2.Min, result.Min); - Vector3::Max(box1.Max, box2.Max, result.Max); - return result; + Vector3::Min(box1.Min, box2.Min, result.Min); + Vector3::Max(box1.Max, box2.Max, result.Max); + return result; } void BoundingBox::CreateMerged(BoundingBox box1, BoundingBox box2, out BoundingBox& result) { - Vector3::Min(box1.Min, box2.Min, result.Min); - Vector3::Max(box1.Max, box2.Max, result.Max); + Vector3::Min(box1.Min, box2.Min, result.Min); + Vector3::Max(box1.Max, box2.Max, result.Max); } bool BoundingBox::Equals(Object const * const obj) const @@ -192,10 +210,10 @@ namespace XFX { return Min.GetHashCode() + Max.GetHashCode(); } - - int BoundingBox::GetType() + + const Type& BoundingBox::GetType() { - // TODO: implement + return BoundingBoxTypeInfo; } bool BoundingBox::Intersects(BoundingBox box) const @@ -207,13 +225,17 @@ namespace XFX void BoundingBox::Intersects(BoundingBox box, out bool& result) const { - if (Max.X < box.Min.X || Min.X > box.Max.X) - result = false; - - if (Max.Y < box.Min.Y || Min.Y > box.Max.Y) - result = false; - - result = (Max.Z >= box.Min.Z && Min.Z <= box.Max.Z); + if (Max.X < box.Min.X || Min.X > box.Max.X) + { + result = false; + } + + if (Max.Y < box.Min.Y || Min.Y > box.Max.Y) + { + result = false; + } + + result = (Max.Z >= box.Min.Z && Min.Z <= box.Max.Z); } bool BoundingBox::Intersects(BoundingSphere sphere) const @@ -225,18 +247,18 @@ namespace XFX void BoundingBox::Intersects(BoundingSphere sphere, out bool& result) const { - float dist; - Vector3 clamped; - - Vector3::Clamp(sphere.Center, Min, Max, clamped); - - float x = sphere.Center.X - clamped.X; - float y = sphere.Center.Y - clamped.Y; - float z = sphere.Center.Z - clamped.Z; - - dist = (x * x) + (y * y) + (z * z); - - result = (dist <= (sphere.Radius * sphere.Radius)); + float dist; + Vector3 clamped; + + Vector3::Clamp(sphere.Center, Min, Max, clamped); + + float x = sphere.Center.X - clamped.X; + float y = sphere.Center.Y - clamped.Y; + float z = sphere.Center.Z - clamped.Z; + + dist = (x * x) + (y * y) + (z * z); + + result = (dist <= (sphere.Radius * sphere.Radius)); } PlaneIntersectionType_t BoundingBox::Intersects(Plane plane) const diff --git a/src/libXFX/BoundingFrustum.cpp b/src/libXFX/BoundingFrustum.cpp index d93fd03..e95a2a6 100644 --- a/src/libXFX/BoundingFrustum.cpp +++ b/src/libXFX/BoundingFrustum.cpp @@ -142,147 +142,164 @@ namespace XFX Vector3 BoundingFrustum::ComputeIntersection(Plane plane, Ray ray) { float num = (-plane.D - Vector3::Dot(plane.Normal, ray.Position)) / Vector3::Dot(plane.Normal, ray.Direction); - return (ray.Position + (ray.Direction * num)); + return (ray.Position + (ray.Direction * num)); } Ray BoundingFrustum::ComputeIntersectionLine(Plane p1, Plane p2) { Ray ray = Ray(); ray.Direction = Vector3::Cross(p1.Normal, p2.Normal); - float num = ray.Direction.LengthSquared(); + float num = ray.Direction.LengthSquared(); ray.Position = (Vector3::Cross(((p2.Normal * -p1.D) + (p1.Normal * p2.D)), ray.Direction) / num); - return ray; + return ray; } ContainmentType_t BoundingFrustum::Contains(BoundingBox box) { bool flag = false; - for(int i = 0; i < 6; i++) - { - switch (box.Intersects(planes[i])) - { + for(int i = 0; i < 6; i++) + { + switch (box.Intersects(planes[i])) + { case PlaneIntersectionType::Front: return ContainmentType::Disjoint; case PlaneIntersectionType::Intersecting: - flag = true; - break; - } - } - if (!flag) - { + flag = true; + break; + } + } + + if (!flag) + { return ContainmentType::Contains; - } + } + return ContainmentType::Intersects; } ContainmentType_t BoundingFrustum::Contains(BoundingFrustum frustrum) { ContainmentType_t disjoint = ContainmentType::Disjoint; - if (Intersects(frustrum)) - { + + if (Intersects(frustrum)) + { disjoint = ContainmentType::Contains; for (int i = 0; i < 8; i++) - { + { if (Contains(frustrum.cornerArray[i]) == ContainmentType::Disjoint) - { + { return ContainmentType::Intersects; - } - } - } - return disjoint; + } + } + } + + return disjoint; } ContainmentType_t BoundingFrustum::Contains(BoundingSphere sphere) { Vector3 center = sphere.Center; - float radius = sphere.Radius; - int num2 = 0; - for (int i = 0; i < 6; i++) - { - float num5 = ((planes[i].Normal.X * center.X) + (planes[i].Normal.Y * center.Y)) + (planes[i].Normal.Z * center.Z); - float num3 = num5 + planes[i].D; - if (num3 > radius) - { + float radius = sphere.Radius; + int num2 = 0; + + for (int i = 0; i < 6; i++) + { + float num5 = ((planes[i].Normal.X * center.X) + (planes[i].Normal.Y * center.Y)) + (planes[i].Normal.Z * center.Z); + float num3 = num5 + planes[i].D; + if (num3 > radius) + { return ContainmentType::Disjoint; - } - if (num3 < -radius) - { - num2++; - } - } - if (num2 != 6) - { + } + if (num3 < -radius) + { + num2++; + } + } + + if (num2 != 6) + { return ContainmentType::Intersects; - } + } + return ContainmentType::Contains; } ContainmentType_t BoundingFrustum::Contains(Vector3 point) { for (int i = 0; i < 6; i++) - { - float num2 = (((planes[i].Normal.X * point.X) + (planes[i].Normal.Y * point.Y)) + (planes[i].Normal.Z * point.Z)) + planes[i].D; - if (num2 > 1E-05f) - { + { + float num2 = (((planes[i].Normal.X * point.X) + (planes[i].Normal.Y * point.Y)) + (planes[i].Normal.Z * point.Z)) + planes[i].D; + + if (num2 > 1E-05f) + { return ContainmentType::Disjoint; - } - } + } + } + return ContainmentType::Contains; } void BoundingFrustum::Contains(BoundingBox box, out ContainmentType_t& result) { bool flag = false; + for (int i = 0; i < 6; i++) - { - switch (box.Intersects(planes[i])) - { + { + switch (box.Intersects(planes[i])) + { case PlaneIntersectionType::Front: result = ContainmentType::Disjoint; - return; + return; case PlaneIntersectionType::Intersecting: - flag = true; - break; - } - } + flag = true; + break; + } + } + result = flag ? ContainmentType::Intersects : ContainmentType::Contains; } void BoundingFrustum::Contains(BoundingSphere sphere, out ContainmentType_t& result) { Vector3 center = sphere.Center; - float radius = sphere.Radius; - int num2 = 0; + float radius = sphere.Radius; + int num2 = 0; + for (int i = 0; i < 6; i++) - { - float num5 = ((planes[i].Normal.X * center.X) + (planes[i].Normal.Y * center.Y)) + (planes[i].Normal.Z * center.Z); - float num3 = num5 + planes[i].D; - if (num3 > radius) - { + { + float num5 = ((planes[i].Normal.X * center.X) + (planes[i].Normal.Y * center.Y)) + (planes[i].Normal.Z * center.Z); + float num3 = num5 + planes[i].D; + + if (num3 > radius) + { result = ContainmentType::Disjoint; - return; - } - if (num3 < -radius) - { - num2++; - } - } + return; + } + + if (num3 < -radius) + { + num2++; + } + } + result = (num2 == 6) ? ContainmentType::Contains : ContainmentType::Intersects; } void BoundingFrustum::Contains(Vector3 point, out ContainmentType_t& result) { for (int i = 0; i < 6; i++) - { - float num2 = (((planes[i].Normal.X * point.X) + (planes[i].Normal.Y * point.Y)) + (planes[i].Normal.Z * point.Z)) + planes[i].D; - if (num2 > 1E-05f) - { + { + float num2 = (((planes[i].Normal.X * point.X) + (planes[i].Normal.Y * point.Y)) + (planes[i].Normal.Z * point.Z)) + planes[i].D; + + if (num2 > 1E-05f) + { result = ContainmentType::Disjoint; - return; - } - } + return; + } + } + result = ContainmentType::Contains; } @@ -306,7 +323,9 @@ namespace XFX sassert(corners != null, "corners cannot be null."); for (int i = 0; i < 8; i++) + { corners[i] = cornerArray[i]; + } } int BoundingFrustum::GetHashCode() const @@ -322,8 +341,8 @@ namespace XFX bool BoundingFrustum::Intersects(BoundingBox box) { bool flag = false; - Intersects(box, flag); - return flag; + Intersects(box, flag); + return flag; } bool BoundingFrustum::Intersects(BoundingFrustum frustrum) @@ -343,10 +362,12 @@ namespace XFX PlaneIntersectionType_t BoundingFrustum::Intersects(Plane plane) { int num = 0; + for (int i = 0; i < 8; i++) { float num3 = 0; Vector3::Dot(cornerArray[i], plane.Normal, num3); + if ((num3 + plane.D) > 0) { num |= 1; @@ -355,15 +376,18 @@ namespace XFX { num |= 2; } + if (num == 3) { return PlaneIntersectionType::Intersecting; } } + if (num != 1) { return PlaneIntersectionType::Back; } + return PlaneIntersectionType::Front; } @@ -387,10 +411,12 @@ namespace XFX void BoundingFrustum::Intersects(Plane plane, out PlaneIntersectionType_t& result) { int num = 0; + for (int i = 0; i < 8; i++) { float num3 = 0; Vector3::Dot(cornerArray[i], plane.Normal, num3); + if ((num3 + plane.D) > 0) { num |= 1; @@ -399,18 +425,21 @@ namespace XFX { num |= 2; } + if (num == 3) { result = PlaneIntersectionType::Intersecting; return; } } + result = (num == 1) ? PlaneIntersectionType::Front : PlaneIntersectionType::Back; } void BoundingFrustum::Intersects(Ray ray, out float& result) { ContainmentType_t type = Contains(ray.Position); + if (type == ContainmentType::Contains) { result = 0.0f; @@ -420,6 +449,7 @@ namespace XFX float minValue = Single::MinValue; float maxValue = Single::MaxValue; result = 0; + for (int i = 0; i < 6; i++) { float num3 = 0; @@ -428,6 +458,7 @@ namespace XFX Vector3::Dot(ray.Direction, normal, num6); Vector3::Dot(ray.Position, normal, num3); num3 += planes[i].D; + if (Math::Abs(num6) < 1E-05f) { if (num3 > 0.0f) @@ -438,12 +469,14 @@ namespace XFX else { float num = -num3 / num6; + if (num6 < 0.0f) { if (num > maxValue) { return; } + if (num > minValue) { minValue = num; @@ -455,6 +488,7 @@ namespace XFX { return; } + if (num < maxValue) { maxValue = num; @@ -462,7 +496,9 @@ namespace XFX } } } + float num7 = (minValue >= 0) ? minValue : maxValue; + if (num7 >= 0) { result = float(num7); @@ -497,12 +533,14 @@ namespace XFX planes[1].Normal.Y = -value.M24 + value.M23; planes[1].Normal.Z = -value.M34 + value.M33; planes[1].D = -value.M44 + value.M43; + for (int i = 0; i < 6; i++) { float num2 = planes[i].Normal.Length(); planes[i].Normal = (planes[i].Normal / num2); planes[i].D /= num2; } + Ray ray = ComputeIntersectionLine(planes[0], planes[2]); cornerArray[0] = ComputeIntersection(planes[4], ray); cornerArray[3] = ComputeIntersection(planes[5], ray); diff --git a/src/libXFX/BoundingSphere.cpp b/src/libXFX/BoundingSphere.cpp index fc2bae0..a232dd9 100644 --- a/src/libXFX/BoundingSphere.cpp +++ b/src/libXFX/BoundingSphere.cpp @@ -27,9 +27,12 @@ #include #include +#include namespace XFX { + const Type BoundingSphereTypeInfo("BoundingSphere", "XFX::BoundingSphere", TypeCode::Object); + BoundingSphere::BoundingSphere(const Vector3 center, const float radius) : Center(center), Radius(radius) { @@ -55,6 +58,11 @@ namespace XFX return (*this == other); } + const Type& BoundingSphere::GetType() + { + return BoundingSphereTypeInfo; + } + int BoundingSphere::GetHashCode() const { return Center.GetHashCode() + (int)Radius; diff --git a/src/libXFX/MathHelper.cpp b/src/libXFX/MathHelper.cpp index 2607ec0..0ce424e 100644 --- a/src/libXFX/MathHelper.cpp +++ b/src/libXFX/MathHelper.cpp @@ -47,9 +47,9 @@ namespace XFX float squared = amount * amount; float cubed = amount * squared; - return 0.5f * ((((2.0f * value2) + ((-value1 + value3) * amount)) + - (((((2.0f * value1) - (5.0f * value2)) + (4.0f * value3)) - value4) * squared)) + - ((((-value1 + (3.0f * value2)) - (3.0f * value3)) + value4) * cubed)); + return 0.5f * ((((2.0f * value2) + ((-value1 + value3) * amount)) + + (((((2.0f * value1) - (5.0f * value2)) + (4.0f * value3)) - value4) * squared)) + + ((((-value1 + (3.0f * value2)) - (3.0f * value3)) + value4) * cubed)); } float MathHelper::Clamp(const float value, const float min, const float max) diff --git a/src/libXFX/Matrix.cpp b/src/libXFX/Matrix.cpp index 37c5b75..7fd58ac 100644 --- a/src/libXFX/Matrix.cpp +++ b/src/libXFX/Matrix.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -40,6 +41,7 @@ using namespace System; namespace XFX { const Matrix Matrix::Identity = Matrix(1.0f, 0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 1.0f); + const Type MatrixTypeInfo("Matrix", "XFX::Matrix", TypeCode::Object); Matrix::Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44) : M11(m11), M12(m12), M13(m13), M14(m14), @@ -178,47 +180,49 @@ namespace XFX Matrix Matrix::CreateBillboard(Vector3 objectPosition, Vector3 cameraPosition, Vector3 cameraUpVector, Vector3* cameraForwardVector) { - Matrix result; + Matrix result; CreateBillboard(objectPosition, cameraPosition, cameraUpVector, cameraForwardVector, result); - return result; + return result; } void Matrix::CreateBillboard(Vector3 objectPosition, Vector3 cameraPosition, Vector3 cameraUpVector, Vector3* cameraForwardVector, out Matrix& result) { Vector3 vector; - Vector3 vector2; - Vector3 vector3; - vector.X = objectPosition.X - cameraPosition.X; - vector.Y = objectPosition.Y - cameraPosition.Y; - vector.Z = objectPosition.Z - cameraPosition.Z; - float num = vector.LengthSquared(); - if (num < 0.0001f) - { + Vector3 vector2; + Vector3 vector3; + vector.X = objectPosition.X - cameraPosition.X; + vector.Y = objectPosition.Y - cameraPosition.Y; + vector.Z = objectPosition.Z - cameraPosition.Z; + float num = vector.LengthSquared(); + + if (num < 0.0001f) + { vector = (cameraForwardVector != null) ? -Vector3(cameraForwardVector->X, cameraForwardVector->Y, cameraForwardVector->Z) : Vector3::Forward; - } - else - { + } + else + { Vector3::Multiply(vector, (float) (1.0f / ((float) Math::Sqrt((double) num))), vector); - } + } + Vector3::Cross(cameraUpVector, vector, vector3); vector3.Normalize(); Vector3::Cross(vector, vector3, vector2); - result.M11 = vector3.X; - result.M12 = vector3.Y; - result.M13 = vector3.Z; - result.M14 = 0.0f; - result.M21 = vector2.X; - result.M22 = vector2.Y; - result.M23 = vector2.Z; - result.M24 = 0.0f; - result.M31 = vector.X; - result.M32 = vector.Y; - result.M33 = vector.Z; - result.M34 = 0.0f; - result.M41 = objectPosition.X; - result.M42 = objectPosition.Y; - result.M43 = objectPosition.Z; - result.M44 = 1.0f; + result.M11 = vector3.X; + result.M12 = vector3.Y; + result.M13 = vector3.Z; + result.M14 = 0.0f; + result.M21 = vector2.X; + result.M22 = vector2.Y; + result.M23 = vector2.Z; + result.M24 = 0.0f; + result.M31 = vector.X; + result.M32 = vector.Y; + result.M33 = vector.Z; + result.M34 = 0.0f; + result.M41 = objectPosition.X; + result.M42 = objectPosition.Y; + result.M43 = objectPosition.Z; + result.M44 = 1.0f; } Matrix Matrix::CreateConstrainedBillboard(Vector3 objectPosition, Vector3 cameraPosition, Vector3 rotateAxis, Vector3* cameraForwardVector, Vector3* objectForwardVector) @@ -231,110 +235,118 @@ namespace XFX void Matrix::CreateConstrainedBillboard(Vector3 objectPosition, Vector3 cameraPosition, Vector3 rotateAxis, Vector3* cameraForwardVector, Vector3* objectForwardVector, out Matrix& result) { float num = 0.0f; - Vector3 vector; - Vector3 vector2; - Vector3 vector3; - vector2.X = objectPosition.X - cameraPosition.X; - vector2.Y = objectPosition.Y - cameraPosition.Y; - vector2.Z = objectPosition.Z - cameraPosition.Z; - float num2 = vector2.LengthSquared(); - if (num2 < 0.0001f) - { + Vector3 vector; + Vector3 vector2; + Vector3 vector3; + vector2.X = objectPosition.X - cameraPosition.X; + vector2.Y = objectPosition.Y - cameraPosition.Y; + vector2.Z = objectPosition.Z - cameraPosition.Z; + float num2 = vector2.LengthSquared(); + + if (num2 < 0.0001f) + { vector2 = (cameraForwardVector != null) ? -Vector3(cameraForwardVector->X, cameraForwardVector->Y, cameraForwardVector->Z) : Vector3::Forward; - } - else - { + } + else + { Vector3::Multiply(vector2, (float) (1.0f / ((float) Math::Sqrt((double) num2))), vector2); - } - Vector3 vector4 = rotateAxis; + } + + Vector3 vector4 = rotateAxis; Vector3::Dot(rotateAxis, vector2, num); + if (Math::Abs(num) > 0.9982547f) - { - if (objectForwardVector != null) - { - vector = Vector3(objectForwardVector->X, objectForwardVector->Y, objectForwardVector->Z); + { + if (objectForwardVector != null) + { + vector = Vector3(objectForwardVector->X, objectForwardVector->Y, objectForwardVector->Z); Vector3::Dot(rotateAxis, vector, num); + if (Math::Abs(num) > 0.9982547f) - { + { num = ((rotateAxis.X * Vector3::Forward.X) + (rotateAxis.Y * Vector3::Forward.Y)) + (rotateAxis.Z * Vector3::Forward.Z); vector = (Math::Abs(num) > 0.9982547f) ? Vector3::Right : Vector3::Forward; - } - } - else - { + } + } + else + { num = ((rotateAxis.X * Vector3::Forward.X) + (rotateAxis.Y * Vector3::Forward.Y)) + (rotateAxis.Z * Vector3::Forward.Z); vector = (Math::Abs(num) > 0.9982547f) ? Vector3::Right : Vector3::Forward; - } + } + Vector3::Cross(rotateAxis, vector, vector3); - vector3.Normalize(); + vector3.Normalize(); Vector3::Cross(vector3, rotateAxis, vector); - vector.Normalize(); - } - else - { + vector.Normalize(); + } + else + { Vector3::Cross(rotateAxis, vector2, vector3); vector3.Normalize(); Vector3::Cross(vector3, vector4, vector); - vector.Normalize(); - } - result.M11 = vector3.X; - result.M12 = vector3.Y; - result.M13 = vector3.Z; - result.M14 = 0.0f; - result.M21 = vector4.X; - result.M22 = vector4.Y; - result.M23 = vector4.Z; - result.M24 = 0.0f; - result.M31 = vector.X; - result.M32 = vector.Y; - result.M33 = vector.Z; - result.M34 = 0.0f; - result.M41 = objectPosition.X; - result.M42 = objectPosition.Y; - result.M43 = objectPosition.Z; - result.M44 = 1.0f; + vector.Normalize(); + } + + result.M11 = vector3.X; + result.M12 = vector3.Y; + result.M13 = vector3.Z; + result.M14 = 0.0f; + result.M21 = vector4.X; + result.M22 = vector4.Y; + result.M23 = vector4.Z; + result.M24 = 0.0f; + result.M31 = vector.X; + result.M32 = vector.Y; + result.M33 = vector.Z; + result.M34 = 0.0f; + result.M41 = objectPosition.X; + result.M42 = objectPosition.Y; + result.M43 = objectPosition.Z; + result.M44 = 1.0f; } Matrix Matrix::CreateFromAxisAngle(Vector3 axis,float angle) { Matrix result; CreateFromAxisAngle(axis, angle, result); - return result; + return result; } void Matrix::CreateFromAxisAngle(Vector3 axis, float angle, out Matrix& result) - { - if(axis.LengthSquared() != 1.0f) - axis.Normalize(); + { + if(axis.LengthSquared() != 1.0f) + { + axis.Normalize(); + } - float x = axis.X; - float y = axis.Y; - float z = axis.Z; - float cos = Math::Cos(angle); - float sin = Math::Sin(angle); - float xx = x * x; - float yy = y * y; - float zz = z * z; - float xy = x * y; - float xz = x * z; - float yz = y * z; + float x = axis.X; + float y = axis.Y; + float z = axis.Z; + float cos = Math::Cos(angle); + float sin = Math::Sin(angle); + float xx = x * x; + float yy = y * y; + float zz = z * z; + float xy = x * y; + float xz = x * z; + float yz = y * z; - result.M11 = xx + (cos * (1.0f - xx)); - result.M12 = (xy - (cos * xy)) + (sin * z); - result.M13 = (xz - (cos * xz)) - (sin * y); - result.M14 = 0.0f; - result.M21 = (xy - (cos * xy)) - (sin * z); - result.M22 = yy + (cos * (1.0f - yy)); - result.M23 = (yz - (cos * yz)) + (sin * x); - result.M24 = 0.0f; - result.M31 = (xz - (cos * xz)) + (sin * y); - result.M32 = (yz - (cos * yz)) - (sin * x); - result.M33 = zz + (cos * (1.0f - zz)); - result.M34 = 0.0f; - result.M41 = 0.0f; - result.M42 = 0.0f; - result.M43 = 0.0f; - result.M44 = 1.0f; + result.M11 = xx + (cos * (1.0f - xx)); + result.M12 = (xy - (cos * xy)) + (sin * z); + result.M13 = (xz - (cos * xz)) - (sin * y); + result.M14 = 0.0f; + result.M21 = (xy - (cos * xy)) - (sin * z); + result.M22 = yy + (cos * (1.0f - yy)); + result.M23 = (yz - (cos * yz)) + (sin * x); + result.M24 = 0.0f; + result.M31 = (xz - (cos * xz)) + (sin * y); + result.M32 = (yz - (cos * yz)) - (sin * x); + result.M33 = zz + (cos * (1.0f - zz)); + result.M34 = 0.0f; + result.M41 = 0.0f; + result.M42 = 0.0f; + result.M43 = 0.0f; + result.M44 = 1.0f; } Matrix Matrix::CreateFromQuaternion(Quaternion rotation) @@ -344,8 +356,8 @@ namespace XFX return result; } - void Matrix::CreateFromQuaternion(Quaternion rotation, out Matrix& result) - { + void Matrix::CreateFromQuaternion(Quaternion rotation, out Matrix& result) + { float xx = rotation.X * rotation.X; float yy = rotation.Y * rotation.Y; float zz = rotation.Z * rotation.Z; @@ -356,36 +368,36 @@ namespace XFX float yz = rotation.Y * rotation.Z; float xw = rotation.X * rotation.W; - result.M11 = 1.0f - (2.0f * (yy + zz)); + result.M11 = 1.0f - (2.0f * (yy + zz)); result.M12 = 2.0f * (xy + zw); - result.M13 = 2.0f * (zx - yw); - result.M14 = 0.0f; + result.M13 = 2.0f * (zx - yw); + result.M14 = 0.0f; result.M21 = 2.0f * (xy - zw); - result.M22 = 1.0f - (2.0f * (zz + xx)); - result.M23 = 2.0f * (yz + xw); - result.M24 = 0.0f; - result.M31 = 2.0f * (zx + yw); - result.M32 = 2.0f * (yz - xw); - result.M33 = 1.0f - (2.0f * (yy + xx)); - result.M34 = 0.0f; - result.M41 = 0.0f; - result.M42 = 0.0f; - result.M43 = 0.0f; - result.M44 = 1.0f; + result.M22 = 1.0f - (2.0f * (zz + xx)); + result.M23 = 2.0f * (yz + xw); + result.M24 = 0.0f; + result.M31 = 2.0f * (zx + yw); + result.M32 = 2.0f * (yz - xw); + result.M33 = 1.0f - (2.0f * (yy + xx)); + result.M34 = 0.0f; + result.M41 = 0.0f; + result.M42 = 0.0f; + result.M43 = 0.0f; + result.M44 = 1.0f; } Matrix Matrix::CreateFromYawPitchRoll(float yaw, float pitch, float roll) - { - Matrix result; - CreateFromYawPitchRoll(yaw, pitch, roll, result); - return result; + { + Matrix result; + CreateFromYawPitchRoll(yaw, pitch, roll, result); + return result; } void Matrix::CreateFromYawPitchRoll(float yaw, float pitch, float roll, out Matrix& result) - { - Quaternion quaternion; - Quaternion::CreateFromYawPitchRoll(yaw, pitch, roll, quaternion); - CreateFromQuaternion(quaternion, result); + { + Quaternion quaternion; + Quaternion::CreateFromYawPitchRoll(yaw, pitch, roll, quaternion); + CreateFromQuaternion(quaternion, result); } Matrix Matrix::CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector) @@ -514,63 +526,63 @@ namespace XFX } void Matrix::CreateReflection(Plane value, out Matrix& result) - { - value.Normalize(); - float x = value.Normal.X; - float y = value.Normal.Y; - float z = value.Normal.Z; - float x2 = -2.0f * x; - float y2 = -2.0f * y; - float z2 = -2.0f * z; - result.M11 = (x2 * x) + 1.0f; - result.M12 = y2 * x; - result.M13 = z2 * x; - result.M14 = 0.0f; - result.M21 = x2 * y; - result.M22 = (y2 * y) + 1.0f; - result.M23 = z2 * y; - result.M24 = 0.0f; - result.M31 = x2 * z; - result.M32 = y2 * z; - result.M33 = (z2 * z) + 1.0f; - result.M34 = 0.0f; - result.M41 = x2 * value.D; - result.M42 = y2 * value.D; - result.M43 = z2 * value.D; - result.M44 = 1.0f; - } - - Matrix Matrix::CreateReflection(Plane value) - { - Matrix result; - value.Normalize(); - float x = value.Normal.X; - float y = value.Normal.Y; - float z = value.Normal.Z; - float x2 = -2.0f * x; - float y2 = -2.0f * y; - float z2 = -2.0f * z; - result.M11 = (x2 * x) + 1.0f; - result.M12 = y2 * x; - result.M13 = z2 * x; - result.M14 = 0.0f; - result.M21 = x2 * y; - result.M22 = (y2 * y) + 1.0f; - result.M23 = z2 * y; - result.M24 = 0.0f; - result.M31 = x2 * z; - result.M32 = y2 * z; - result.M33 = (z2 * z) + 1.0f; - result.M34 = 0.0f; - result.M41 = x2 * value.D; - result.M42 = y2 * value.D; - result.M43 = z2 * value.D; - result.M44 = 1.0f; - return result; - } - - void Matrix::CreateRotationX(float radians, out Matrix& result) - { + { + value.Normalize(); + float x = value.Normal.X; + float y = value.Normal.Y; + float z = value.Normal.Z; + float x2 = -2.0f * x; + float y2 = -2.0f * y; + float z2 = -2.0f * z; + result.M11 = (x2 * x) + 1.0f; + result.M12 = y2 * x; + result.M13 = z2 * x; + result.M14 = 0.0f; + result.M21 = x2 * y; + result.M22 = (y2 * y) + 1.0f; + result.M23 = z2 * y; + result.M24 = 0.0f; + result.M31 = x2 * z; + result.M32 = y2 * z; + result.M33 = (z2 * z) + 1.0f; + result.M34 = 0.0f; + result.M41 = x2 * value.D; + result.M42 = y2 * value.D; + result.M43 = z2 * value.D; + result.M44 = 1.0f; + } + + Matrix Matrix::CreateReflection(Plane value) + { + Matrix result; + value.Normalize(); + float x = value.Normal.X; + float y = value.Normal.Y; + float z = value.Normal.Z; + float x2 = -2.0f * x; + float y2 = -2.0f * y; + float z2 = -2.0f * z; + result.M11 = (x2 * x) + 1.0f; + result.M12 = y2 * x; + result.M13 = z2 * x; + result.M14 = 0.0f; + result.M21 = x2 * y; + result.M22 = (y2 * y) + 1.0f; + result.M23 = z2 * y; + result.M24 = 0.0f; + result.M31 = x2 * z; + result.M32 = y2 * z; + result.M33 = (z2 * z) + 1.0f; + result.M34 = 0.0f; + result.M41 = x2 * value.D; + result.M42 = y2 * value.D; + result.M43 = z2 * value.D; + result.M44 = 1.0f; + return result; + } + + void Matrix::CreateRotationX(float radians, out Matrix& result) + { float num2 = (float) Math::Cos((double) radians); float num = (float) Math::Sin((double) radians); result.M11 = 1.0f; @@ -590,16 +602,16 @@ namespace XFX result.M43 = 0.0f; result.M44 = 1.0f; } - + Matrix Matrix::CreateRotationX(float radians) - { - Matrix result; + { + Matrix result; CreateRotationX(radians, result); - return result; - } - - void Matrix::CreateRotationY(float radians, out Matrix& result) - { + return result; + } + + void Matrix::CreateRotationY(float radians, out Matrix& result) + { float num2 = (float) Math::Cos((double) radians); float num = (float) Math::Sin((double) radians); result.M11 = num2; @@ -618,17 +630,17 @@ namespace XFX result.M42 = 0.0f; result.M43 = 0.0f; result.M44 = 1.0f; - } - - Matrix Matrix::CreateRotationY(float radians) - { - Matrix result; + } + + Matrix Matrix::CreateRotationY(float radians) + { + Matrix result; CreateRotationY(radians, result); - return result; - } - - void Matrix::CreateRotationZ(float radians, out Matrix& result) - { + return result; + } + + void Matrix::CreateRotationZ(float radians, out Matrix& result) + { float num2 = (float) Math::Cos((double) radians); float num = (float) Math::Sin((double) radians); result.M11 = num2; @@ -647,199 +659,199 @@ namespace XFX result.M42 = 0.0f; result.M43 = 0.0f; result.M44 = 1.0f; - } - - Matrix Matrix::CreateRotationZ(float radians) - { - Matrix result; - CreateRotationZ(radians, result); - return result; - } - - void Matrix::CreateScale(float scale, out Matrix& result) - { - float num = scale; - result.M11 = num; - result.M12 = 0.0f; - result.M13 = 0.0f; - result.M14 = 0.0f; - result.M21 = 0.0f; - result.M22 = num; - result.M23 = 0.0f; - result.M24 = 0.0f; - result.M31 = 0.0f; - result.M32 = 0.0f; - result.M33 = num; - result.M34 = 0.0f; - result.M41 = 0.0f; - result.M42 = 0.0f; - result.M43 = 0.0f; - result.M44 = 1.0f; - } - - Matrix Matrix::CreateScale(float scale) - { - Matrix result; - CreateScale(scale, result); - return result; - } - - void Matrix::CreateScale(float xScale, float yScale, float zScale, out Matrix& result) - { - float num3 = xScale; - float num2 = yScale; - float num = zScale; - result.M11 = num3; - result.M12 = 0.0f; - result.M13 = 0.0f; - result.M14 = 0.0f; - result.M21 = 0.0f; - result.M22 = num2; - result.M23 = 0.0f; - result.M24 = 0.0f; - result.M31 = 0.0f; - result.M32 = 0.0f; - result.M33 = num; - result.M34 = 0.0f; - result.M41 = 0.0f; - result.M42 = 0.0f; - result.M43 = 0.0f; - result.M44 = 1.0f; - } - - Matrix Matrix::CreateScale(float xScale, float yScale, float zScale) - { - Matrix result; - CreateScale(xScale, yScale, zScale, result); - return result; - } - - void Matrix::CreateScale(Vector3 scales, out Matrix& result) - { - float x = scales.X; - float y = scales.Y; - float z = scales.Z; - result.M11 = x; - result.M12 = 0.0f; - result.M13 = 0.0f; - result.M14 = 0.0f; - result.M21 = 0.0f; - result.M22 = y; - result.M23 = 0.0f; - result.M24 = 0.0f; - result.M31 = 0.0f; - result.M32 = 0.0f; - result.M33 = z; - result.M34 = 0.0f; - result.M41 = 0.0f; - result.M42 = 0.0f; - result.M43 = 0.0f; - result.M44 = 1.0f; - } - - Matrix Matrix::CreateScale(Vector3 scales) - { - Matrix result; - CreateScale(scales, result); - return result; - } - - void Matrix::CreateShadow(Vector3 lightDirection, Plane plane, out Matrix& result) - { - plane.Normalize(); - float dot = ((plane.Normal.X * lightDirection.X) + (plane.Normal.Y * lightDirection.Y)) + (plane.Normal.Z * lightDirection.Z); - float x = -plane.Normal.X; - float y = -plane.Normal.Y; - float z = -plane.Normal.Z; - float d = -plane.D; - result.M11 = (x * lightDirection.X) + dot; - result.M21 = y * lightDirection.X; - result.M31 = z * lightDirection.X; - result.M41 = d * lightDirection.X; - result.M12 = x * lightDirection.Y; - result.M22 = (y * lightDirection.Y) + dot; - result.M32 = z * lightDirection.Y; - result.M42 = d * lightDirection.Y; - result.M13 = x * lightDirection.Z; - result.M23 = y * lightDirection.Z; - result.M33 = (z * lightDirection.Z) + dot; - result.M43 = d * lightDirection.Z; - result.M14 = 0.0f; - result.M24 = 0.0f; - result.M34 = 0.0f; - result.M44 = dot; - } - - Matrix Matrix::CreateShadow(Vector3 lightDirection, Plane plane) - { - Matrix result; - CreateShadow(lightDirection, plane, result); - return result; - } - - void Matrix::CreateTranslation(float xPosition, float yPosition, float zPosition, out Matrix& result) - { - result.M11 = 1.0f; - result.M12 = 0.0f; - result.M13 = 0.0f; - result.M14 = 0.0f; - result.M21 = 0.0f; - result.M22 = 1.0f; - result.M23 = 0.0f; - result.M24 = 0.0f; - result.M31 = 0.0f; - result.M32 = 0.0f; - result.M33 = 1.0f; - result.M34 = 0.0f; - result.M41 = xPosition; - result.M42 = yPosition; - result.M43 = zPosition; - result.M44 = 1.0f; - } - - Matrix Matrix::CreateTranslation(float xPosition, float yPosition, float zPosition) - { - Matrix result; - CreateTranslation(xPosition, yPosition, zPosition, result); - return result; - } - - void Matrix::CreateTranslation(Vector3 position, out Matrix& result) - { - result.M11 = 1.0f; - result.M12 = 0.0f; - result.M13 = 0.0f; - result.M14 = 0.0f; - result.M21 = 0.0f; - result.M22 = 1.0f; - result.M23 = 0.0f; - result.M24 = 0.0f; - result.M31 = 0.0f; - result.M32 = 0.0f; - result.M33 = 1.0f; - result.M34 = 0.0f; - result.M41 = position.X; - result.M42 = position.Y; - result.M43 = position.Z; - result.M44 = 1.0f; - } - - Matrix Matrix::CreateTranslation(Vector3 position) - { - Matrix result; - CreateTranslation(position, result); - return result; - } - - Matrix Matrix::CreateWorld(Vector3 position, Vector3 forward, Vector3 up) - { - Matrix ret; - CreateWorld(position, forward, up, out ret); - return ret; } - void Matrix::CreateWorld(Vector3 position, Vector3 forward, Vector3 up, out Matrix& result) - { + Matrix Matrix::CreateRotationZ(float radians) + { + Matrix result; + CreateRotationZ(radians, result); + return result; + } + + void Matrix::CreateScale(float scale, out Matrix& result) + { + float num = scale; + result.M11 = num; + result.M12 = 0.0f; + result.M13 = 0.0f; + result.M14 = 0.0f; + result.M21 = 0.0f; + result.M22 = num; + result.M23 = 0.0f; + result.M24 = 0.0f; + result.M31 = 0.0f; + result.M32 = 0.0f; + result.M33 = num; + result.M34 = 0.0f; + result.M41 = 0.0f; + result.M42 = 0.0f; + result.M43 = 0.0f; + result.M44 = 1.0f; + } + + Matrix Matrix::CreateScale(float scale) + { + Matrix result; + CreateScale(scale, result); + return result; + } + + void Matrix::CreateScale(float xScale, float yScale, float zScale, out Matrix& result) + { + float num3 = xScale; + float num2 = yScale; + float num = zScale; + result.M11 = num3; + result.M12 = 0.0f; + result.M13 = 0.0f; + result.M14 = 0.0f; + result.M21 = 0.0f; + result.M22 = num2; + result.M23 = 0.0f; + result.M24 = 0.0f; + result.M31 = 0.0f; + result.M32 = 0.0f; + result.M33 = num; + result.M34 = 0.0f; + result.M41 = 0.0f; + result.M42 = 0.0f; + result.M43 = 0.0f; + result.M44 = 1.0f; + } + + Matrix Matrix::CreateScale(float xScale, float yScale, float zScale) + { + Matrix result; + CreateScale(xScale, yScale, zScale, result); + return result; + } + + void Matrix::CreateScale(Vector3 scales, out Matrix& result) + { + float x = scales.X; + float y = scales.Y; + float z = scales.Z; + result.M11 = x; + result.M12 = 0.0f; + result.M13 = 0.0f; + result.M14 = 0.0f; + result.M21 = 0.0f; + result.M22 = y; + result.M23 = 0.0f; + result.M24 = 0.0f; + result.M31 = 0.0f; + result.M32 = 0.0f; + result.M33 = z; + result.M34 = 0.0f; + result.M41 = 0.0f; + result.M42 = 0.0f; + result.M43 = 0.0f; + result.M44 = 1.0f; + } + + Matrix Matrix::CreateScale(Vector3 scales) + { + Matrix result; + CreateScale(scales, result); + return result; + } + + void Matrix::CreateShadow(Vector3 lightDirection, Plane plane, out Matrix& result) + { + plane.Normalize(); + float dot = ((plane.Normal.X * lightDirection.X) + (plane.Normal.Y * lightDirection.Y)) + (plane.Normal.Z * lightDirection.Z); + float x = -plane.Normal.X; + float y = -plane.Normal.Y; + float z = -plane.Normal.Z; + float d = -plane.D; + result.M11 = (x * lightDirection.X) + dot; + result.M21 = y * lightDirection.X; + result.M31 = z * lightDirection.X; + result.M41 = d * lightDirection.X; + result.M12 = x * lightDirection.Y; + result.M22 = (y * lightDirection.Y) + dot; + result.M32 = z * lightDirection.Y; + result.M42 = d * lightDirection.Y; + result.M13 = x * lightDirection.Z; + result.M23 = y * lightDirection.Z; + result.M33 = (z * lightDirection.Z) + dot; + result.M43 = d * lightDirection.Z; + result.M14 = 0.0f; + result.M24 = 0.0f; + result.M34 = 0.0f; + result.M44 = dot; + } + + Matrix Matrix::CreateShadow(Vector3 lightDirection, Plane plane) + { + Matrix result; + CreateShadow(lightDirection, plane, result); + return result; + } + + void Matrix::CreateTranslation(float xPosition, float yPosition, float zPosition, out Matrix& result) + { + result.M11 = 1.0f; + result.M12 = 0.0f; + result.M13 = 0.0f; + result.M14 = 0.0f; + result.M21 = 0.0f; + result.M22 = 1.0f; + result.M23 = 0.0f; + result.M24 = 0.0f; + result.M31 = 0.0f; + result.M32 = 0.0f; + result.M33 = 1.0f; + result.M34 = 0.0f; + result.M41 = xPosition; + result.M42 = yPosition; + result.M43 = zPosition; + result.M44 = 1.0f; + } + + Matrix Matrix::CreateTranslation(float xPosition, float yPosition, float zPosition) + { + Matrix result; + CreateTranslation(xPosition, yPosition, zPosition, result); + return result; + } + + void Matrix::CreateTranslation(Vector3 position, out Matrix& result) + { + result.M11 = 1.0f; + result.M12 = 0.0f; + result.M13 = 0.0f; + result.M14 = 0.0f; + result.M21 = 0.0f; + result.M22 = 1.0f; + result.M23 = 0.0f; + result.M24 = 0.0f; + result.M31 = 0.0f; + result.M32 = 0.0f; + result.M33 = 1.0f; + result.M34 = 0.0f; + result.M41 = position.X; + result.M42 = position.Y; + result.M43 = position.Z; + result.M44 = 1.0f; + } + + Matrix Matrix::CreateTranslation(Vector3 position) + { + Matrix result; + CreateTranslation(position, result); + return result; + } + + Matrix Matrix::CreateWorld(Vector3 position, Vector3 forward, Vector3 up) + { + Matrix ret; + CreateWorld(position, forward, up, out ret); + return ret; + } + + void Matrix::CreateWorld(Vector3 position, Vector3 forward, Vector3 up, out Matrix& result) + { Vector3 vector = Vector3::Normalize(position - forward); Vector3 vector2 = Vector3::Normalize(Vector3::Cross(up, vector)); Vector3 vector3 = Vector3::Cross(vector, vector2); @@ -867,26 +879,44 @@ namespace XFX translation.Y = M42; translation.Z = M43; float xs, ys, zs; + if (Math::Sign(M11 * M12 * M13 * M14) < 0) + { xs = -1.0f; + } else + { xs = 1.0f; + } + if (Math::Sign(M21 * M22 * M23 * M24) < 0) + { ys = -1.0f; + } else + { ys = 1.0f; + } + if (Math::Sign(M31 * M32 * M33 * M34) < 0) + { zs = -1.0f; + } else + { zs = 1.0f; + } + scale.X = xs * (float)Math::Sqrt(M11 * M11 + M12 * M12 + M13 * M13); scale.Y = ys * (float)Math::Sqrt(M21 * M21 + M22 * M22 + M23 * M23); scale.Z = zs * (float)Math::Sqrt(M31 * M31 + M32 * M32 + M33 * M33); + if (scale.X == 0.0 || scale.Y == 0.0 || scale.Z == 0.0) { rotation = Quaternion::Identity; return false; } + Matrix m1 = Matrix(M11/scale.X, M12/scale.X, M13/scale.X, 0, M21/scale.Y, M22/scale.Y, M23/scale.Y, 0, M31/scale.Z, M32/scale.Z, M33/scale.Z, 0, @@ -895,36 +925,36 @@ namespace XFX return true; } - float Matrix::Determinant() - { - float temp1 = (M33 * M44) - (M34 * M43); - float temp2 = (M32 * M44) - (M34 * M42); - float temp3 = (M32 * M43) - (M33 * M42); - float temp4 = (M31 * M44) - (M34 * M41); - float temp5 = (M31 * M43) - (M33 * M41); - float temp6 = (M31 * M42) - (M32 * M41); - - return ((((M11 * (((M22 * temp1) - (M23 * temp2)) + (M24 * temp3))) - (M12 * (((M21 * temp1) - - (M23 * temp4)) + (M24 * temp5)))) + (M13 * (((M21 * temp2) - (M22 * temp4)) + (M24 * temp6)))) - - (M14 * (((M21 * temp3) - (M22 * temp5)) + (M23 * temp6)))); - } + float Matrix::Determinant() + { + float temp1 = (M33 * M44) - (M34 * M43); + float temp2 = (M32 * M44) - (M34 * M42); + float temp3 = (M32 * M43) - (M33 * M42); + float temp4 = (M31 * M44) - (M34 * M41); + float temp5 = (M31 * M43) - (M33 * M41); + float temp6 = (M31 * M42) - (M32 * M41); + + return ((((M11 * (((M22 * temp1) - (M23 * temp2)) + (M24 * temp3))) - (M12 * (((M21 * temp1) - + (M23 * temp4)) + (M24 * temp5)))) + (M13 * (((M21 * temp2) - (M22 * temp4)) + (M24 * temp6)))) - + (M14 * (((M21 * temp3) - (M22 * temp5)) + (M23 * temp6)))); + } Matrix Matrix::Divide(Matrix matrix1, Matrix matrix2) - { - Matrix result; - Divide(matrix1, matrix2, result); - return result; - } + { + Matrix result; + Divide(matrix1, matrix2, result); + return result; + } - void Matrix::Divide(Matrix matrix1, Matrix matrix2, out Matrix& result) - { - Matrix inverse = Matrix::Invert(matrix2); - Matrix::Multiply(matrix1, inverse, result); - } - - void Matrix::Divide(Matrix matrix1, float divider, out Matrix& result) - { - float num = 1 / divider; + void Matrix::Divide(Matrix matrix1, Matrix matrix2, out Matrix& result) + { + Matrix inverse = Matrix::Invert(matrix2); + Matrix::Multiply(matrix1, inverse, result); + } + + void Matrix::Divide(Matrix matrix1, float divider, out Matrix& result) + { + float num = 1 / divider; result.M11 = matrix1.M11 * num; result.M12 = matrix1.M12 * num; result.M13 = matrix1.M13 * num; @@ -941,24 +971,24 @@ namespace XFX result.M42 = matrix1.M42 * num; result.M43 = matrix1.M43 * num; result.M44 = matrix1.M44 * num; - } - - Matrix Matrix::Divide(Matrix matrix1, float divider) - { - Matrix result; - Divide(matrix1, divider, result); - return result; - } - - bool Matrix::Equals(Object const * const obj) const - { - return is(this, obj) ? *this == *(Matrix*)obj : false; } - bool Matrix::Equals(const Matrix other) const - { - return (*this == other); - } + Matrix Matrix::Divide(Matrix matrix1, float divider) + { + Matrix result; + Divide(matrix1, divider, result); + return result; + } + + bool Matrix::Equals(Object const * const obj) const + { + return is(this, obj) ? *this == *(Matrix *)obj : false; + } + + bool Matrix::Equals(const Matrix other) const + { + return (*this == other); + } int Matrix::GetHashCode() const { @@ -966,14 +996,14 @@ namespace XFX (int)M31 ^ (int)M32 ^ (int)M33 ^ (int)M34 ^ (int)M41 ^ (int)M42 ^ (int)M43 ^ (int)M44); } - int Matrix::GetType() + const Type& Matrix::GetType() { - // TODO: implement + return MatrixTypeInfo; } - - void Matrix::Invert(Matrix matrix, out Matrix& result) - { - float num5 = matrix.M11; + + void Matrix::Invert(Matrix matrix, out Matrix& result) + { + float num5 = matrix.M11; float num4 = matrix.M12; float num3 = matrix.M13; float num2 = matrix.M14; @@ -1028,7 +1058,7 @@ namespace XFX result.M24 = (((num5 * num29) - (num3 * num26)) + (num2 * num25)) * num; result.M34 = -(((num5 * num28) - (num4 * num26)) + (num2 * num24)) * num; result.M44 = (((num5 * num27) - (num4 * num25)) + (num3 * num24)) * num; - } + } Matrix Matrix::Invert(Matrix matrix) { @@ -1064,15 +1094,15 @@ namespace XFX } Matrix Matrix::Multiply(Matrix matrix1, Matrix matrix2) - { - Matrix result; - Multiply(matrix1, matrix2, result); - return result; - } + { + Matrix result; + Multiply(matrix1, matrix2, result); + return result; + } - void Matrix::Multiply(Matrix matrix1, Matrix matrix2, out Matrix& result) - { - float num16 = (((matrix1.M11 * matrix2.M11) + (matrix1.M12 * matrix2.M21)) + (matrix1.M13 * matrix2.M31)) + (matrix1.M14 * matrix2.M41); + void Matrix::Multiply(Matrix matrix1, Matrix matrix2, out Matrix& result) + { + float num16 = (((matrix1.M11 * matrix2.M11) + (matrix1.M12 * matrix2.M21)) + (matrix1.M13 * matrix2.M31)) + (matrix1.M14 * matrix2.M41); float num15 = (((matrix1.M11 * matrix2.M12) + (matrix1.M12 * matrix2.M22)) + (matrix1.M13 * matrix2.M32)) + (matrix1.M14 * matrix2.M42); float num14 = (((matrix1.M11 * matrix2.M13) + (matrix1.M12 * matrix2.M23)) + (matrix1.M13 * matrix2.M33)) + (matrix1.M14 * matrix2.M43); float num13 = (((matrix1.M11 * matrix2.M14) + (matrix1.M12 * matrix2.M24)) + (matrix1.M13 * matrix2.M34)) + (matrix1.M14 * matrix2.M44); @@ -1104,18 +1134,18 @@ namespace XFX result.M42 = num3; result.M43 = num2; result.M44 = num; - } + } Matrix Matrix::Multiply(Matrix matrix1, float scaleFactor) - { - Matrix result; + { + Matrix result; Multiply(matrix1, scaleFactor, result); return result; - } - - void Matrix::Multiply(Matrix matrix1, float scaleFactor, out Matrix& result) - { - float num = scaleFactor; + } + + void Matrix::Multiply(Matrix matrix1, float scaleFactor, out Matrix& result) + { + float num = scaleFactor; result.M11 = matrix1.M11 * num; result.M12 = matrix1.M12 * num; result.M13 = matrix1.M13 * num; @@ -1132,7 +1162,7 @@ namespace XFX result.M42 = matrix1.M42 * num; result.M43 = matrix1.M43 * num; result.M44 = matrix1.M44 * num; - } + } Matrix Matrix::Negate(Matrix matrix) { @@ -1202,7 +1232,7 @@ namespace XFX { Matrix ret; Transpose(matrix, ret); - return ret; + return ret; } void Matrix::Transpose(Matrix matrix, out Matrix& result) @@ -1224,11 +1254,11 @@ namespace XFX result.M43 = matrix.M34; result.M44 = matrix.M44; } - - Matrix Matrix::operator+(const Matrix& other) - { + + Matrix Matrix::operator+(const Matrix& other) + { return Add(*this, other); - } + } Matrix Matrix::operator /(const Matrix& other) { diff --git a/src/libXFX/Plane.cpp b/src/libXFX/Plane.cpp index e879e7e..d806d3b 100644 --- a/src/libXFX/Plane.cpp +++ b/src/libXFX/Plane.cpp @@ -157,12 +157,16 @@ namespace XFX float dot = (Normal.X * max.X) + (Normal.Y * max.Y) + (Normal.Z * max.Z); if(dot + D > 0.0f) + { result = PlaneIntersectionType::Front; + } dot = (Normal.X * min.X) + (Normal.Y * min.Y) + (Normal.Z * min.Z); if(dot + D < 0.0f) + { result = PlaneIntersectionType::Back; + } result = PlaneIntersectionType::Intersecting; } @@ -179,10 +183,14 @@ namespace XFX float dot = (sphere.Center.X * Normal.X) + (sphere.Center.Y * Normal.Y) + (sphere.Center.Z * Normal.Z) + D; if(dot > sphere.Radius) + { result = PlaneIntersectionType::Front; + } if(dot < -sphere.Radius) + { result = PlaneIntersectionType::Back; + } result = PlaneIntersectionType::Intersecting; } diff --git a/src/libXFX/Vector2.cpp b/src/libXFX/Vector2.cpp index e9d0df0..ebc6d2d 100644 --- a/src/libXFX/Vector2.cpp +++ b/src/libXFX/Vector2.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -42,29 +43,26 @@ namespace XFX { const Vector2 Vector2::One = Vector2(1, 1); const Vector2 Vector2::Zero = Vector2(0, 0); + const Type Vector2TypeInfo("Vector2", "XFX::Vector2", TypeCode::Object); Vector2::Vector2(const float x, const float y) + : X(x), Y(y) { - X = x; - Y = y; } Vector2::Vector2(const float value) + : X(value), Y(value) { - X = value; - Y = value; } - + Vector2::Vector2(const Vector2 &obj) + : X(obj.X), Y(obj.Y) { - X = obj.X; - Y = obj.Y; } - + Vector2::Vector2() + : X(0), Y(0) { - X = 0; - Y = 0; } Vector2 Vector2::Add(const Vector2 value1, const Vector2 value2) @@ -112,40 +110,40 @@ namespace XFX result.X = MathHelper::Clamp(value.X, min.X, max.X); result.Y = MathHelper::Clamp(value.Y, min.Y, max.Y); } - + float Vector2::Distance(const Vector2 value1, const Vector2 value2) { - float x = value1.X - value2.X; - float y = value1.Y - value2.Y; - return Math::Sqrt((x * x) + (y * y)); + float x = value1.X - value2.X; + float y = value1.Y - value2.Y; + return Math::Sqrt((x * x) + (y * y)); } - + void Vector2::Distance(const Vector2& value1, const Vector2& value2, out float& result) { - float x = value1.X - value2.X; - float y = value1.Y - value2.Y; - result = Math::Sqrt((x * x) + (y * y)); + float x = value1.X - value2.X; + float y = value1.Y - value2.Y; + result = Math::Sqrt((x * x) + (y * y)); } - + float Vector2::DistanceSquared(const Vector2 value1, const Vector2 value2) { - float x = value1.X - value2.X; - float y = value1.Y - value2.Y; - return (x * x) + (y * y); + float x = value1.X - value2.X; + float y = value1.Y - value2.Y; + return (x * x) + (y * y); } - + void Vector2::DistanceSquared(const Vector2& value1, const Vector2& value2, out float& result) { - float x = value1.X - value2.X; - float y = value1.Y - value2.Y; - result = (x * x) + (y * y); + float x = value1.X - value2.X; + float y = value1.Y - value2.Y; + result = (x * x) + (y * y); } - + float Vector2::Dot(const Vector2 value1, const Vector2 value2) { - return (value1.X * value2.X + value1.Y * value2.Y); + return (value1.X * value2.X + value1.Y * value2.Y); } - + void Vector2::Dot(const Vector2& value1, const Vector2& value2, out float& result) { result = (value1.X * value2.X + value1.Y * value2.Y); @@ -160,65 +158,65 @@ namespace XFX { return (*this == other); } - + int Vector2::GetHashCode() const { return (int)X ^ (int)Y; } - int Vector2::GetType() + const Type& Vector2::GetType() { - // TODO: implement + return Vector2TypeInfo; } - + Vector2 Vector2::Hermite(const Vector2 value1, const Vector2 tangent1, const Vector2 value2, const Vector2 tangent2, const float amount) { return Vector2(MathHelper::Hermite(value1.X,tangent1.X,value2.X,tangent2.X,amount), MathHelper::Hermite(value1.Y,tangent1.Y,value2.Y,tangent2.Y,amount)); } - + void Vector2::Hermite(const Vector2& value1, const Vector2& tangent1, const Vector2& value2, const Vector2& tangent2, const float amount, out Vector2& result) { result.X = MathHelper::Hermite(value1.X,tangent1.X,value2.X,tangent2.X,amount); result.Y = MathHelper::Hermite(value1.Y,tangent1.Y,value2.Y,tangent2.Y,amount); } - + float Vector2::Length() const { return Math::Sqrt((X * X) + (Y * Y)); } - + float Vector2::LengthSquared() const { return (X * X) + (Y * Y); } - + Vector2 Vector2::Lerp(const Vector2 value1, const Vector2 value2, const float amount) { return Vector2(MathHelper::Lerp(value1.X, value2.X, amount), MathHelper::Lerp(value1.Y, value2.Y, amount)); } - + void Vector2::Lerp(const Vector2& value1, const Vector2& value2, const float amount, out Vector2& result) { result.X = MathHelper::Lerp(value1.X, value2.X, amount); result.Y = MathHelper::Lerp(value1.Y, value2.Y, amount); } - + Vector2 Vector2::Max(const Vector2 value1, const Vector2 value2) { return Vector2((value1.X > value2.X) ? value1.X : value2.X,(value1.Y > value2.Y) ? value1.Y : value2.Y); } - + void Vector2::Max(const Vector2& value1, const Vector2& value2, out Vector2& result) { result.X = (value1.X > value2.X) ? value1.X : value2.X; result.Y = (value1.Y > value2.Y) ? value1.Y : value2.Y; } - + Vector2 Vector2::Min(const Vector2 value1, const Vector2 value2) { return Vector2((value1.X < value2.X) ? value1.X : value2.X, (value1.Y < value2.Y) ? value1.Y : value2.Y); } - + void Vector2::Min(const Vector2& value1, const Vector2& value2, out Vector2& result) { result.X = (value1.X < value2.X) ? value1.X : value2.X; @@ -257,34 +255,42 @@ namespace XFX result.X = -value.X; result.Y = -value.Y; } - + void Vector2::Normalize() { float length = Length(); - if(length == 0) - return; - float num = 1 / length; - X *= num; - Y *= num; + + if(length == 0) + { + return; + } + + float num = 1 / length; + X *= num; + Y *= num; } - + Vector2 Vector2::Normalize(const Vector2 value) { Vector2 result; Normalize(value, result); - return result; + return result; } - + void Vector2::Normalize(const Vector2& value, out Vector2& result) { - float length = value.Length(); - if (length == 0) - return; - float num = 1 / length; - result.X = value.X * num; - result.Y = value.Y * num; + float length = value.Length(); + + if (length == 0) + { + return; + } + + float num = 1 / length; + result.X = value.X * num; + result.Y = value.Y * num; } - + Vector2 Vector2::Reflect(const Vector2 vector, const Vector2 normal) { Vector2 result; @@ -298,14 +304,14 @@ namespace XFX result.X = vector.X - normal.X * dp; result.Y = vector.Y - normal.Y * dp; } - + Vector2 Vector2::SmoothStep(const Vector2 value1, const Vector2 value2, const float amount) { Vector2 result; SmoothStep(value1, value2, amount, result); return result; } - + void Vector2::SmoothStep(const Vector2& value1, const Vector2& value2, const float amount, out Vector2& result) { result.X = MathHelper::SmoothStep(value1.X, value2.X, amount); @@ -318,7 +324,7 @@ namespace XFX Subtract(value1, value2, result); return result; } - + void Vector2::Subtract(const Vector2& value1, const Vector2& value2, out Vector2& result) { result.X = value1.X - value2.X; @@ -329,90 +335,90 @@ namespace XFX { return String::Format("{X:%f Y:%f}", X, Y); } - + Vector2 Vector2::Transform(const Vector2 position, const Matrix matrix) { - Vector2 result; + Vector2 result; Transform(position, matrix, result); - return result; + return result; } - + void Vector2::Transform(const Vector2& position, const Matrix& matrix, out Vector2& result) { - Vector4 vector; - - vector.X = (vector.X * matrix.M11) + (vector.Y * matrix.M21) + matrix.M41; - vector.Y = (vector.X * matrix.M12) + (vector.Y * matrix.M22) + matrix.M42; - vector.Z = (vector.X * matrix.M13) + (vector.Y * matrix.M23) + matrix.M43; - vector.W = 1 / ((vector.X * matrix.M14) + (vector.Y * matrix.M24) + matrix.M44); - - result.X = vector.X * vector.W; - result.Y = vector.Y * vector.W; + Vector4 vector; + + vector.X = (vector.X * matrix.M11) + (vector.Y * matrix.M21) + matrix.M41; + vector.Y = (vector.X * matrix.M12) + (vector.Y * matrix.M22) + matrix.M42; + vector.Z = (vector.X * matrix.M13) + (vector.Y * matrix.M23) + matrix.M43; + vector.W = 1 / ((vector.X * matrix.M14) + (vector.Y * matrix.M24) + matrix.M44); + + result.X = vector.X * vector.W; + result.Y = vector.Y * vector.W; } - + Vector2 Vector2::Transform(const Vector2 position, const Quaternion rotation) { Vector2 result; Transform(position, rotation, result); return result; } - + void Vector2::Transform(const Vector2& position, const Quaternion& rotation, out Vector2& result) { Quaternion quat = Quaternion(position.X, position.Y, 0, 0), i, t; Quaternion::Inverse(rotation, i); Quaternion::Multiply(rotation, quat, t); Quaternion::Multiply(t, i, quat); - + result.X = quat.X; result.Y = quat.Y; } - + void Vector2::Transform(const Vector2 sourceArray[], const int sourceIndex, const Matrix& matrix, Vector2 destinationArray[], const int destinationIndex, const int length) { sassert(sourceArray != null, "sourceArray cannot be null."); sassert(destinationArray != null, "destinationArray cannot be null."); - + for(int i = sourceIndex, j = destinationIndex; i < (sourceIndex + length); i++, j++) { TransformNormal(sourceArray[i], matrix, destinationArray[j]); } } - + void Vector2::Transform(const Vector2 sourceArray[], const int sourceIndex, const Quaternion& rotation, Vector2 destinationArray[], const int destinationIndex, const int length) { sassert(sourceArray != null, String::Format("sourceArray; %s", FrameworkResources::ArgumentNull_Generic)); sassert(destinationArray != null, String::Format("destinationArray; %s", FrameworkResources::ArgumentNull_Generic)); - + for(int i = sourceIndex, j = destinationIndex; i < (sourceIndex + length); i++, j++) { Transform(sourceArray[i], rotation, destinationArray[j]); } } - + Vector2 Vector2::TransformNormal(const Vector2 normal, const Matrix matrix) { - Vector2 result; - result.X = (normal.X * matrix.M11) + (normal.Y * matrix.M21); - result.Y = (normal.X * matrix.M12) + (normal.Y * matrix.M22); - - return result; + Vector2 result; + result.X = (normal.X * matrix.M11) + (normal.Y * matrix.M21); + result.Y = (normal.X * matrix.M12) + (normal.Y * matrix.M22); + + return result; } - + void Vector2::TransformNormal(const Vector2& normal, const Matrix& matrix, out Vector2& result) { - result.X = (normal.X * matrix.M11) + (normal.Y * matrix.M21); - result.Y = (normal.X * matrix.M12) + (normal.Y * matrix.M22); + result.X = (normal.X * matrix.M11) + (normal.Y * matrix.M21); + result.Y = (normal.X * matrix.M12) + (normal.Y * matrix.M22); } - + void Vector2::TransformNormal(const Vector2 sourceArray[], const int sourceIndex, const Matrix& matrix, Vector2 destinationArray[], const int destinationIndex, const int length) { sassert(sourceArray != null, String::Format("sourceArray; %s", FrameworkResources::ArgumentNull_Generic)); sassert(destinationArray != null, String::Format("destinationArray; %s", FrameworkResources::ArgumentNull_Generic)); - + for(int i = sourceIndex, j = destinationIndex; i < (sourceIndex + length); i++, j++) { TransformNormal(sourceArray[i], matrix, destinationArray[j]); @@ -423,17 +429,17 @@ namespace XFX { return Vector2(X + other.X, Y + other.Y); } - + Vector2 Vector2::operator/(const float divider) const { return Vector2(X / divider, Y / divider); } - + Vector2 Vector2::operator/(const Vector2& other) const { return Vector2(X / other.X, Y / other.Y); } - + bool Vector2::operator==(const Vector2& other) const { return ((X == other.X) && (Y == other.Y)); @@ -448,7 +454,7 @@ namespace XFX { return Vector2(X * scaleFactor, Y * scaleFactor); } - + Vector2 Vector2::operator*(const Vector2& other) const { return Vector2(X * other.X, Y * other.Y); @@ -458,7 +464,7 @@ namespace XFX { return Vector2(X - other.X, Y - other.Y); } - + Vector2 Vector2::operator-() const { return Vector2(-X, -Y); diff --git a/src/libXFX/Vector3.cpp b/src/libXFX/Vector3.cpp index 14f629e..f3187f5 100644 --- a/src/libXFX/Vector3.cpp +++ b/src/libXFX/Vector3.cpp @@ -34,13 +34,14 @@ #include #include #include +#include #include using namespace System; namespace XFX -{ +{ const Vector3 Vector3::Backward = Vector3(0, 0, 1); const Vector3 Vector3::Down = Vector3(0, -1, 0); const Vector3 Vector3::Forward = Vector3(0, 0, -1); @@ -52,17 +53,18 @@ namespace XFX const Vector3 Vector3::UnitZ = Vector3(0, 0, 1); const Vector3 Vector3::Up = Vector3(0, 1, 0); const Vector3 Vector3::Zero = Vector3(0, 0, 0); - + const Type Vector3TypeInfo("Vector3", "XFX::Vector3", TypeCode::Object); + Vector3::Vector3(float value) : X(value), Y(value), Z(value) { } - + Vector3::Vector3(float x, float y, float z) : X(x), Y(y), Z(z) { } - + Vector3::Vector3(Vector2 value, float z) : X(value.X), Y(value.Y), Z(z) { @@ -89,7 +91,7 @@ namespace XFX result.Y = value1.Y + value2.Y; result.Z = value1.Z + value2.Z; } - + Vector3 Vector3::Baricentric(Vector3 value1, Vector3 value2, Vector3 value3, float amount1, float amount2) { Vector3 result; @@ -98,14 +100,14 @@ namespace XFX result.X = MathHelper::Baricentric(value1.X, value2.X, value3.X, amount1, amount2); return result; } - + void Vector3::Baricentric(Vector3 value1, Vector3 value2, Vector3 value3, float amount1, float amount2, out Vector3& result) { result.X = MathHelper::Baricentric(value1.X, value2.X, value3.X, amount1, amount2); result.Z = MathHelper::Baricentric(value1.X, value2.X, value3.X, amount1, amount2); result.X = MathHelper::Baricentric(value1.X, value2.X, value3.X, amount1, amount2); } - + Vector3 Vector3::CatmullRom(Vector3 value1, Vector3 value2, Vector3 value3, Vector3 value4, float amount) { Vector3 result; @@ -114,14 +116,14 @@ namespace XFX result.Z = MathHelper::CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount); return result; } - + void Vector3::CatmullRom(Vector3 value1, Vector3 value2, Vector3 value3, Vector3 value4, float amount, out Vector3& result) { result.X = MathHelper::CatmullRom(value1.X, value2.X, value3.X, value4.X, amount); result.Y = MathHelper::CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount); result.Z = MathHelper::CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount); } - + Vector3 Vector3::Clamp(Vector3 value1, Vector3 min, Vector3 max) { Vector3 result; @@ -130,14 +132,14 @@ namespace XFX result.Z = MathHelper::Clamp(value1.Z, min.Z, max.Z); return result; } - + void Vector3::Clamp(Vector3 value1, Vector3 min, Vector3 max, out Vector3& result) { result.X = MathHelper::Clamp(value1.X, min.X, max.X); result.Y = MathHelper::Clamp(value1.Y, min.Y, max.Y); result.Z = MathHelper::Clamp(value1.Z, min.Z, max.Z); } - + Vector3 Vector3::Cross(Vector3 vector1, Vector3 vector2) { Vector3 result; @@ -146,62 +148,62 @@ namespace XFX result.Z = vector1.X * vector2.Y - vector1.Y * vector2.X; return result; } - + void Vector3::Cross(Vector3 vector1, Vector3 vector2, out Vector3& result) { result.X = vector1.Y * vector2.Z - vector1.Z * vector2.Y; result.Y = vector1.Z * vector2.X - vector1.X * vector2.Z; result.Z = vector1.X * vector2.Y - vector1.Y * vector2.X; } - + float Vector3::Distance(Vector3 value1, Vector3 value2) { float x = value1.X - value2.X; - float y = value1.Y - value2.Y; - float z = value1.Z - value2.Z; - + float y = value1.Y - value2.Y; + float z = value1.Z - value2.Z; + return Math::Sqrt((x*x) + (y*y) + (z*z)); } - + void Vector3::Distance(Vector3 value1, Vector3 value2, out float& result) { float x = value1.X - value2.X; - float y = value1.Y - value2.Y; - float z = value1.Z - value2.Z; - + float y = value1.Y - value2.Y; + float z = value1.Z - value2.Z; + result = Math::Sqrt((x*x) + (y*y) + (z*z)); } - + float Vector3::DistanceSquared(Vector3 value1, Vector3 value2) { float x = value1.X - value2.X; - float y = value1.Y - value2.Y; - float z = value1.Z - value2.Z; - - return (x*x) + (y*y) + (z*z); + float y = value1.Y - value2.Y; + float z = value1.Z - value2.Z; + + return (x*x) + (y*y) + (z*z); } - + void Vector3::DistanceSquared(Vector3 value1, Vector3 value2, out float& result) { float x = value1.X - value2.X; - float y = value1.Y - value2.Y; - float z = value1.Z - value2.Z; - + float y = value1.Y - value2.Y; + float z = value1.Z - value2.Z; + result = (x*x) + (y*y) + (z*z); } - + Vector3 Vector3::Divide(Vector3 value1, float value2) { return Vector3(value1.X / value2, value1.Y / value2, value1.Z / value2); } - + void Vector3::Divide(Vector3 value1, float value2, out Vector3& result) { result.X = value1.X / value2; result.Y = value1.Y / value2; result.Z = value1.Z / value2; } - + Vector3 Vector3::Divide(Vector3 value1, Vector3 value2) { Vector3 result; @@ -210,19 +212,19 @@ namespace XFX result.Z = value1.Z / value2.Z; return result; } - + void Vector3::Divide(Vector3 value1, Vector3 value2, out Vector3& result) { result.X = value1.X / value2.X; result.Y = value1.Y / value2.Y; result.Z = value1.Z / value2.Z; } - + float Vector3::Dot(Vector3 value1, Vector3 value2) { return (value1.X * value2.X + value1.Y * value2.Y + value1.Z * value2.Z); } - + void Vector3::Dot(Vector3 value1, Vector3 value2, out float& result) { result = (value1.X * value2.X + value1.Y * value2.Y + value1.Z * value2.Z); @@ -237,237 +239,249 @@ namespace XFX { return (*this == other); } - + int Vector3::GetHashCode() const { return ((int)X ^ (int)Y ^ (int)Z); } - int Vector3::GetType() + const Type& Vector3::GetType() { - // TODO: implement + return Vector3TypeInfo; } - + Vector3 Vector3::Hermite(Vector3 value1, Vector3 tangent1, Vector3 value2, Vector3 tangent2, float amount) { Vector3 result; Hermite(value1, tangent1, value2, tangent2, amount, result); return result; } - + void Vector3::Hermite(Vector3 value1, Vector3 tangent1, Vector3 value2, Vector3 tangent2, float amount, out Vector3& result) { result.X = MathHelper::Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount); result.Y = MathHelper::Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount); result.Z = MathHelper::Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount); } - + float Vector3::Length() const { return Math::Sqrt((X * X) + (Y * Y) + (Z * Z)); } - + float Vector3::LengthSquared() const { return (X * X) + (Y * Y) + (Z * Z); } - + Vector3 Vector3::Lerp(Vector3 value1, Vector3 value2, float amount) { Vector3 result; Lerp(value1, value2, amount, result); return result; } - + void Vector3::Lerp(Vector3 value1, Vector3 value2, float amount, out Vector3& result) { result.X = MathHelper::Lerp(value1.X, value2.X, amount); result.Y = MathHelper::Lerp(value1.Y, value2.Y, amount); result.Z = MathHelper::Lerp(value1.Z, value2.Z, amount); } - + Vector3 Vector3::Max(Vector3 value1, Vector3 value2) { Vector3 result; Max(value1, value2, result); return result; } - + void Vector3::Max(Vector3 value1, Vector3 value2, out Vector3& result) { result.X = (value1.X > value2.X) ? value1.X : value2.X; result.Y = (value1.Y > value2.Y) ? value1.Y : value2.Y; result.Z = (value1.Z > value2.Z) ? value1.Z : value2.Z; } - + Vector3 Vector3::Min(Vector3 value1, Vector3 value2) { Vector3 result; Min(value1, value2, result); return result; } - + void Vector3::Min(Vector3 value1, Vector3 value2, out Vector3& result) { result.X = (value1.X < value2.X) ? value1.X : value2.X; result.Y = (value1.Y < value2.Y) ? value1.Y : value2.Y; result.Z = (value1.Z < value2.Z) ? value1.Z : value2.Z; } - + Vector3 Vector3::Multiply(Vector3 value1, float scaleFactor) { Vector3 result; Multiply(value1, scaleFactor, result); return result; } - + void Vector3::Multiply(Vector3 value1, float scaleFactor, out Vector3& result) { result.X = value1.X * scaleFactor; result.Y = value1.Y * scaleFactor; result.Z = value1.Z * scaleFactor; } - + Vector3 Vector3::Multiply(Vector3 value1, Vector3 value2) { Vector3 result; Multiply(value1, value2, result); return result; } - + void Vector3::Multiply(Vector3 value1, Vector3 value2, out Vector3& result) { result.X = value1.X * value2.X; result.Y = value1.Y * value2.Y; result.Z = value1.Z * value2.Z; } - + Vector3 Vector3::Negate(Vector3 value) { Vector3 result; Negate(value, result); return result; } - + void Vector3::Negate(Vector3 value, out Vector3& result) { result.X = -value.X; result.Y = -value.Y; result.Z = -value.Z; } - + void Vector3::Normalize() { - float length = Length(); - if(length == 0) - return; - float num = 1 / length; - X *= num; - Y *= num; - Z *= num; + float length = Length(); + + if(length == 0) + { + return; + } + + float num = 1 / length; + X *= num; + Y *= num; + Z *= num; } - + Vector3 Vector3::Normalize(Vector3 value) { Vector3 result; - float length = value.Length(); - if(length == 0) - return Vector3::Zero; - float num = 1 / length; - result.X *= num; - result.Y *= num; - result.Z *= num; - return result; + float length = value.Length(); + + if(length == 0) + { + return Vector3::Zero; + } + + float num = 1 / length; + result.X *= num; + result.Y *= num; + result.Z *= num; + return result; } - + void Vector3::Normalize(Vector3 value, out Vector3& result) { - float length = value.Length(); - if(length == 0) - return; - float num = 1 / length; - result.X *= num; - result.Y *= num; - result.Z *= num; + float length = value.Length(); + + if(length == 0) + { + return; + } + + float num = 1 / length; + result.X *= num; + result.Y *= num; + result.Z *= num; } - + Vector3 Vector3::Reflect(Vector3 vector, Vector3 normal) { - Vector3 result; - Reflect(vector, normal, result); - return result; + Vector3 result; + Reflect(vector, normal, result); + return result; } - + void Vector3::Reflect(Vector3 vector, Vector3 normal, out Vector3& result) { float dot = ((vector.X * normal.X) + (vector.Y * normal.Y)) + (vector.Z * normal.Z); - - result.X = vector.X - ((2.0f * dot) * normal.X); - result.Y = vector.Y - ((2.0f * dot) * normal.Y); - result.Z = vector.Z - ((2.0f * dot) * normal.Z); - } - - Vector3 Vector3::SmoothStep(Vector3 value1, Vector3 value2, float amount) - { - Vector3 result; - SmoothStep(value1, value2, amount, result); - return result; - } - - void Vector3::SmoothStep(Vector3 value1, Vector3 value2, float amount, out Vector3& result) - { - result.X = MathHelper::SmoothStep(value1.X, value2.X, amount); - result.Y = MathHelper::SmoothStep(value1.Y, value2.Y, amount); - result.Z = MathHelper::SmoothStep(value1.Z, value2.Z, amount); - } - - Vector3 Vector3::Subtract(Vector3 value1, Vector3 value2) - { - Vector3 result; - Subtract(value1, value2, result); - return result; - } - - void Vector3::Subtract(Vector3 value1, Vector3 value2, out Vector3& result) + + result.X = vector.X - ((2.0f * dot) * normal.X); + result.Y = vector.Y - ((2.0f * dot) * normal.Y); + result.Z = vector.Z - ((2.0f * dot) * normal.Z); + } + + Vector3 Vector3::SmoothStep(Vector3 value1, Vector3 value2, float amount) + { + Vector3 result; + SmoothStep(value1, value2, amount, result); + return result; + } + + void Vector3::SmoothStep(Vector3 value1, Vector3 value2, float amount, out Vector3& result) + { + result.X = MathHelper::SmoothStep(value1.X, value2.X, amount); + result.Y = MathHelper::SmoothStep(value1.Y, value2.Y, amount); + result.Z = MathHelper::SmoothStep(value1.Z, value2.Z, amount); + } + + Vector3 Vector3::Subtract(Vector3 value1, Vector3 value2) + { + Vector3 result; + Subtract(value1, value2, result); + return result; + } + + void Vector3::Subtract(Vector3 value1, Vector3 value2, out Vector3& result) { result.X = value1.X - value2.X; - result.Y = value1.Y - value2.Y; - result.Z = value1.Z - value2.Z; + result.Y = value1.Y - value2.Y; + result.Z = value1.Z - value2.Z; } const String Vector3::ToString() const { return String::Format("{X:%f Y:%f Z:%f}", X, Y, Z); } - + Vector3 Vector3::Transform(Vector3 position, Matrix matrix) { Vector3 result; Transform(position, matrix, result); return result; } - + void Vector3::Transform(Vector3 position, Matrix matrix, out Vector3& result) { - Vector4 vector; - - vector.X = (((vector.X * matrix.M11) + (vector.Y * matrix.M21)) + (vector.Z * matrix.M31)) + matrix.M41; - vector.Y = (((vector.X * matrix.M12) + (vector.Y * matrix.M22)) + (vector.Z * matrix.M32)) + matrix.M42; - vector.Z = (((vector.X * matrix.M13) + (vector.Y * matrix.M23)) + (vector.Z * matrix.M33)) + matrix.M43; - vector.W = 1 / ((((vector.X * matrix.M14) + (vector.Y * matrix.M24)) + (vector.Z * matrix.M34)) + matrix.M44); - - result.X = vector.X * vector.W; - result.Y = vector.Y * vector.W; - result.Z = vector.Z * vector.W; + Vector4 vector; + + vector.X = (((vector.X * matrix.M11) + (vector.Y * matrix.M21)) + (vector.Z * matrix.M31)) + matrix.M41; + vector.Y = (((vector.X * matrix.M12) + (vector.Y * matrix.M22)) + (vector.Z * matrix.M32)) + matrix.M42; + vector.Z = (((vector.X * matrix.M13) + (vector.Y * matrix.M23)) + (vector.Z * matrix.M33)) + matrix.M43; + vector.W = 1 / ((((vector.X * matrix.M14) + (vector.Y * matrix.M24)) + (vector.Z * matrix.M34)) + matrix.M44); + + result.X = vector.X * vector.W; + result.Y = vector.Y * vector.W; + result.Z = vector.Z * vector.W; } - + Vector3 Vector3::Transform(Vector3 position, Quaternion rotation) { Vector3 result; Transform(position, rotation, result); return result; } - + void Vector3::Transform(Vector3 position, Quaternion rotation, out Vector3& result) { Vector3 xyz = Vector3(rotation.X,rotation.Y, rotation.Z), temp, temp2; @@ -478,7 +492,7 @@ namespace XFX Multiply(temp, 2, temp); Add(position, temp, result); } - + void Vector3::Transform(const Vector3 sourceArray[], const int sourceIndex, const Matrix matrix, Vector3 destinationArray[], const int destinationIndex, const int length) { sassert(sourceArray != null, String::Format("sourceArray; %s", FrameworkResources::ArgumentNull_Generic)); @@ -490,7 +504,7 @@ namespace XFX Transform(sourceArray[i], matrix, destinationArray[j]); } } - + void Vector3::Transform(const Vector3 sourceArray[], const int sourceIndex, const Quaternion rotation, Vector3 destinationArray[], const int destinationIndex, const int length) { sassert(sourceArray != null, String::Format("sourceArray; %s", FrameworkResources::ArgumentNull_Generic)); @@ -502,19 +516,19 @@ namespace XFX Transform(sourceArray[i], rotation, destinationArray[j]); } } - + Vector3 Vector3::TransformNormal(Vector3 normal, Matrix matrix) { Vector3 result; TransformNormal(normal, matrix, result); - return result; + return result; } - + void Vector3::TransformNormal(Vector3 normal, Matrix matrix, out Vector3& result) { result.X = ((normal.X * matrix.M11) + (normal.Y * matrix.M21)) + (normal.Z * matrix.M31); - result.Y = ((normal.X * matrix.M12) + (normal.Y * matrix.M22)) + (normal.Z * matrix.M32); - result.Z = ((normal.X * matrix.M13) + (normal.Y * matrix.M23)) + (normal.Z * matrix.M33); + result.Y = ((normal.X * matrix.M12) + (normal.Y * matrix.M22)) + (normal.Z * matrix.M32); + result.Z = ((normal.X * matrix.M13) + (normal.Y * matrix.M23)) + (normal.Z * matrix.M33); } void Vector3::TransformNormal(const Vector3 sourceArray[], const int sourceIndex, const Matrix matrix, Vector3 destinationArray[], const int destinationIndex, const int length) @@ -528,7 +542,7 @@ namespace XFX TransformNormal(sourceArray[i], matrix, destinationArray[j]); } } - + Vector3 Vector3::operator+(const Vector3& other) { Vector3 result; @@ -537,7 +551,7 @@ namespace XFX result.Z = Z + other.Z; return result; } - + Vector3 Vector3::operator/(const float divider) { Vector3 result; @@ -546,7 +560,7 @@ namespace XFX result.Z = Z / divider; return result; } - + Vector3 Vector3::operator/(const Vector3& other) { Vector3 result; @@ -555,17 +569,17 @@ namespace XFX result.Z = Z / other.Z; return result; } - + bool Vector3::operator==(const Vector3& other) const { return Equals(other); } - + bool Vector3::operator!=(const Vector3& other) const { return !Equals(other); } - + Vector3 Vector3::operator*(const float scaleFactor) { Vector3 result; @@ -574,7 +588,7 @@ namespace XFX result.Z = Z * scaleFactor; return result; } - + Vector3 Vector3::operator*(const Vector3& other) { Vector3 result; @@ -583,12 +597,12 @@ namespace XFX result.Z = Z * other.Z; return result; } - + Vector3 Vector3::operator-(const Vector3& other) { return Vector3(X -= other.X, Y -= other.Y, Z -= other.Z); } - + Vector3 Vector3::operator-() { return Vector3(-X, -Y, -Z); diff --git a/src/libXFX/Vector4.cpp b/src/libXFX/Vector4.cpp index 290676f..c01ed45 100644 --- a/src/libXFX/Vector4.cpp +++ b/src/libXFX/Vector4.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -46,6 +47,7 @@ namespace XFX const Vector4 Vector4::UnitY = Vector4(0,1,0,0); const Vector4 Vector4::UnitZ = Vector4(0,0,1,0); const Vector4 Vector4::Zero = Vector4(0,0,0,0); + const Type Vector4TypeInfo("Vector4", "XFX::Vector4", TypeCode::Object); Vector4::Vector4(float value) : X(value), Y(value), Z(value), W(value) @@ -66,12 +68,12 @@ namespace XFX : X(value.X), Y(value.Y), Z(value.Z), W(w) { } - + Vector4::Vector4(const Vector4 &obj) : X(obj.X), Y(obj.Y), Z(obj.Z), W(obj.W) { } - + Vector4::Vector4() : X(0), Y(0), Z(0), W(0) { @@ -91,22 +93,22 @@ namespace XFX result.Y = vector1.Y + vector2.Y; result.Z = vector1.Z + vector2.Z; } - + Vector4 Vector4::Baricentric(Vector4 value1, Vector4 value2, Vector4 value3, float amount1, float amount2) { Vector4 result; Baricentric(value1, value2, value3, amount1, amount2, result); return result; } - + void Vector4::Baricentric(Vector4 value1, Vector4 value2, Vector4 value3, float amount1, float amount2, out Vector4& result) { - result.X = MathHelper::Baricentric(value1.X, value2.X, value3.X, amount1, amount2); - result.Y = MathHelper::Baricentric(value1.Y, value2.Y, value3.Y, amount1, amount2); - result.Z = MathHelper::Baricentric(value1.Z, value2.Z, value3.Z, amount1, amount2); - result.W = MathHelper::Baricentric(value1.W, value2.W, value3.W, amount1, amount2); + result.X = MathHelper::Baricentric(value1.X, value2.X, value3.X, amount1, amount2); + result.Y = MathHelper::Baricentric(value1.Y, value2.Y, value3.Y, amount1, amount2); + result.Z = MathHelper::Baricentric(value1.Z, value2.Z, value3.Z, amount1, amount2); + result.W = MathHelper::Baricentric(value1.W, value2.W, value3.W, amount1, amount2); } - + Vector4 Vector4::CatmullRom(Vector4 value1, Vector4 value2, Vector4 value3, Vector4 value4, float amount) { Vector4 result; @@ -116,7 +118,7 @@ namespace XFX result.W = MathHelper::CatmullRom(value1.W, value2.W, value3.W, value4.W, amount); return result; } - + void Vector4::CatmullRom(Vector4 value1, Vector4 value2, Vector4 value3, Vector4 value4, float amount, out Vector4& result) { result.X = MathHelper::CatmullRom(value1.X, value2.X, value3.X, value4.X, amount); @@ -124,49 +126,49 @@ namespace XFX result.Z = MathHelper::CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount); result.W = MathHelper::CatmullRom(value1.W, value2.W, value3.W, value4.W, amount); } - + Vector4 Vector4::Clamp(Vector4 value1, Vector4 min, Vector4 max) { Vector4 result; Clamp(value1, min, max, result); return result; } - + void Vector4::Clamp(Vector4 value1, Vector4 min, Vector4 max, out Vector4& result) { - result = Vector4( - MathHelper::Clamp(value1.X, min.X, max.X), - MathHelper::Clamp(value1.Y, min.Y, max.Y), - MathHelper::Clamp(value1.Z, min.Z, max.Z), - MathHelper::Clamp(value1.W, min.W, max.W)); - } + result = Vector4( + MathHelper::Clamp(value1.X, min.X, max.X), + MathHelper::Clamp(value1.Y, min.Y, max.Y), + MathHelper::Clamp(value1.Z, min.Z, max.Z), + MathHelper::Clamp(value1.W, min.W, max.W)); + } - float Vector4::Distance(Vector4 value1, Vector4 value2) - { - return Math::Sqrt(DistanceSquared(value1, value2)); - } - - void Vector4::Distance(Vector4 value1, Vector4 value2, out float& result) - { - result = Math::Sqrt(DistanceSquared(value1, value2)); - } - - float Vector4::DistanceSquared(Vector4 value1, Vector4 value2) - { - float result = (value1.W - value2.W) * (value1.W - value2.W) + - (value1.X - value2.X) * (value1.X - value2.X) + - (value1.Y - value2.Y) * (value1.Y - value2.Y) + - (value1.Z - value2.Z) * (value1.Z - value2.Z); - return result; - } - - void Vector4::DistanceSquared(Vector4 value1, Vector4 value2, out float& result) - { - result = (value1.W - value2.W) * (value1.W - value2.W) + - (value1.X - value2.X) * (value1.X - value2.X) + - (value1.Y - value2.Y) * (value1.Y - value2.Y) + - (value1.Z - value2.Z) * (value1.Z - value2.Z); - } + float Vector4::Distance(Vector4 value1, Vector4 value2) + { + return Math::Sqrt(DistanceSquared(value1, value2)); + } + + void Vector4::Distance(Vector4 value1, Vector4 value2, out float& result) + { + result = Math::Sqrt(DistanceSquared(value1, value2)); + } + + float Vector4::DistanceSquared(Vector4 value1, Vector4 value2) + { + float result = (value1.W - value2.W) * (value1.W - value2.W) + + (value1.X - value2.X) * (value1.X - value2.X) + + (value1.Y - value2.Y) * (value1.Y - value2.Y) + + (value1.Z - value2.Z) * (value1.Z - value2.Z); + return result; + } + + void Vector4::DistanceSquared(Vector4 value1, Vector4 value2, out float& result) + { + result = (value1.W - value2.W) * (value1.W - value2.W) + + (value1.X - value2.X) * (value1.X - value2.X) + + (value1.Y - value2.Y) * (value1.Y - value2.Y) + + (value1.Z - value2.Z) * (value1.Z - value2.Z); + } Vector4 Vector4::Divide(Vector4 value, float scale) { @@ -209,153 +211,157 @@ namespace XFX return ((int)X ^ (int)Y ^ (int)Z ^ (int)W); } - int Vector4::GetType() + const Type& Vector4::GetType() { - // TODO: implement + return Vector4TypeInfo; } - - Vector4 Vector4::Hermite(Vector4 value1, Vector4 tangent1, Vector4 value2, Vector4 tangent2, float amount) - { - Vector4 result; - Hermite(value1, tangent1, value2, tangent2, amount, result); - return result; - } - - void Vector4::Hermite(Vector4 value1, Vector4 tangent1, Vector4 value2, Vector4 tangent2, float amount, out Vector4& result) - { - result.W = MathHelper::Hermite(value1.W, tangent1.W, value2.W, tangent2.W, amount); - result.X = MathHelper::Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount); - result.Y = MathHelper::Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount); - result.Z = MathHelper::Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount); - } - - float Vector4::Length() - { - return Math::Sqrt((X * X) + (Y * Y) + (Z * Z) + (W * W)); - } - - float Vector4::LengthSquared() - { - return (X * X) + (Y * Y) + (Z * Z) + (W * W); - } - - Vector4 Vector4::Lerp(Vector4 value1, Vector4 value2, float amount) - { - Vector4 result; - Lerp(value1, value2, amount, result); - return result; - } - - void Vector4::Lerp(Vector4 value1, Vector4 value2, float amount, out Vector4& result) - { + + Vector4 Vector4::Hermite(Vector4 value1, Vector4 tangent1, Vector4 value2, Vector4 tangent2, float amount) + { + Vector4 result; + Hermite(value1, tangent1, value2, tangent2, amount, result); + return result; + } + + void Vector4::Hermite(Vector4 value1, Vector4 tangent1, Vector4 value2, Vector4 tangent2, float amount, out Vector4& result) + { + result.W = MathHelper::Hermite(value1.W, tangent1.W, value2.W, tangent2.W, amount); + result.X = MathHelper::Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount); + result.Y = MathHelper::Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount); + result.Z = MathHelper::Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount); + } + + float Vector4::Length() + { + return Math::Sqrt((X * X) + (Y * Y) + (Z * Z) + (W * W)); + } + + float Vector4::LengthSquared() + { + return (X * X) + (Y * Y) + (Z * Z) + (W * W); + } + + Vector4 Vector4::Lerp(Vector4 value1, Vector4 value2, float amount) + { + Vector4 result; + Lerp(value1, value2, amount, result); + return result; + } + + void Vector4::Lerp(Vector4 value1, Vector4 value2, float amount, out Vector4& result) + { result.X = MathHelper::Lerp(value1.X, value2.X, amount); result.Y = MathHelper::Lerp(value1.Y, value2.Y, amount); result.Z = MathHelper::Lerp(value1.Z, value2.Z, amount); - result.W = MathHelper::Lerp(value1.W, value2.W, amount); - } - - Vector4 Vector4::Max(Vector4 value1, Vector4 value2) - { - Vector4 result; - Max(value1, value2, result); - return result; - } - - void Vector4::Max(Vector4 value1, Vector4 value2, out Vector4& result) - { + result.W = MathHelper::Lerp(value1.W, value2.W, amount); + } + + Vector4 Vector4::Max(Vector4 value1, Vector4 value2) + { + Vector4 result; + Max(value1, value2, result); + return result; + } + + void Vector4::Max(Vector4 value1, Vector4 value2, out Vector4& result) + { result.X = MathHelper::Max(value1.X, value2.X); result.Y = MathHelper::Max(value1.Y, value2.Y); result.Z = MathHelper::Max(value1.Z, value2.Z); - result.W = MathHelper::Max(value1.W, value2.W); - } - - Vector4 Vector4::Min(Vector4 value1, Vector4 value2) - { - Vector4 result; - Min(value1, value2, result); - return result; - } - - void Vector4::Min(Vector4 value1, Vector4 value2, out Vector4& result) - { - result = Vector4( - MathHelper::Min(value1.X, value2.X), - MathHelper::Min(value1.Y, value2.Y), - MathHelper::Min(value1.Z, value2.Z), - MathHelper::Min(value1.W, value2.W)); - } - - Vector4 Vector4::Multiply(Vector4 value1, float scaleFactor) - { - Vector4 result; - Multiply(value1, scaleFactor, result); - return result; - } - - void Vector4::Multiply(Vector4 value1, float scaleFactor, out Vector4& result) - { + result.W = MathHelper::Max(value1.W, value2.W); + } + + Vector4 Vector4::Min(Vector4 value1, Vector4 value2) + { + Vector4 result; + Min(value1, value2, result); + return result; + } + + void Vector4::Min(Vector4 value1, Vector4 value2, out Vector4& result) + { + result = Vector4( + MathHelper::Min(value1.X, value2.X), + MathHelper::Min(value1.Y, value2.Y), + MathHelper::Min(value1.Z, value2.Z), + MathHelper::Min(value1.W, value2.W)); + } + + Vector4 Vector4::Multiply(Vector4 value1, float scaleFactor) + { + Vector4 result; + Multiply(value1, scaleFactor, result); + return result; + } + + void Vector4::Multiply(Vector4 value1, float scaleFactor, out Vector4& result) + { result.X = value1.X * scaleFactor; - result.Y = value1.Y * scaleFactor; - result.Z = value1.Z * scaleFactor; - result.W = value1.W * scaleFactor; - } - - Vector4 Vector4::Multiply(Vector4 value1, Vector4 value2) - { - Vector4 result; - Multiply(value1, value2, result); - return result; - } - - void Vector4::Multiply(Vector4 value1, Vector4 value2, out Vector4& result) - { - result.W = value1.W * value2.W; - result.X = value1.X * value2.X; - result.Y = value1.Y * value2.Y; - result.Z = value1.Z * value2.Z; - } - - Vector4 Vector4::Negate(Vector4 value) - { - Vector4 result; + result.Y = value1.Y * scaleFactor; + result.Z = value1.Z * scaleFactor; + result.W = value1.W * scaleFactor; + } + + Vector4 Vector4::Multiply(Vector4 value1, Vector4 value2) + { + Vector4 result; + Multiply(value1, value2, result); + return result; + } + + void Vector4::Multiply(Vector4 value1, Vector4 value2, out Vector4& result) + { + result.W = value1.W * value2.W; + result.X = value1.X * value2.X; + result.Y = value1.Y * value2.Y; + result.Z = value1.Z * value2.Z; + } + + Vector4 Vector4::Negate(Vector4 value) + { + Vector4 result; Negate(value, result); return result; - } - - void Vector4::Negate(Vector4 value, out Vector4& result) - { - result = Vector4(-value.X, -value.Y, -value.Z, -value.W); - } - - void Vector4::Normalize() - { - float length = Length(); - if( length == 0 ) - return; - float num = 1 / length; - X *= num; - Y *= num; - Z *= num; - W *= num; - } - - Vector4 Vector4::Normalize(Vector4 value) - { - Normalize(value, value); - return value; - } - - void Vector4::Normalize(Vector4 value, out Vector4& result) - { - float factor; - float length = value.Length(); - factor = 1.0f / length; - - result.W = value.W * factor; - result.X = value.X * factor; - result.Y = value.Y * factor; - result.Z = value.Z * factor; - } + } + + void Vector4::Negate(Vector4 value, out Vector4& result) + { + result = Vector4(-value.X, -value.Y, -value.Z, -value.W); + } + + void Vector4::Normalize() + { + float length = Length(); + + if(length == 0) + { + return; + } + + float num = 1 / length; + X *= num; + Y *= num; + Z *= num; + W *= num; + } + + Vector4 Vector4::Normalize(Vector4 value) + { + Normalize(value, value); + return value; + } + + void Vector4::Normalize(Vector4 value, out Vector4& result) + { + float factor; + float length = value.Length(); + factor = 1.0f / length; + + result.W = value.W * factor; + result.X = value.X * factor; + result.Y = value.Y * factor; + result.Z = value.Z * factor; + } Vector4 Vector4::SmoothStep(Vector4 value1, Vector4 value2, float amount) { @@ -363,7 +369,7 @@ namespace XFX MathHelper::SmoothStep(value1.X, value2.X, amount), MathHelper::SmoothStep(value1.Y, value2.Y, amount), MathHelper::SmoothStep(value1.Z, value2.Z, amount), - MathHelper::SmoothStep(value1.W, value2.W, amount)); + MathHelper::SmoothStep(value1.W, value2.W, amount)); } void Vector4::SmoothStep(Vector4 value1, Vector4 value2, float amount, out Vector4& result) @@ -372,7 +378,7 @@ namespace XFX MathHelper::SmoothStep(value1.X, value2.X, amount), MathHelper::SmoothStep(value1.Y, value2.Y, amount), MathHelper::SmoothStep(value1.Z, value2.Z, amount), - MathHelper::SmoothStep(value1.W, value2.W, amount)); + MathHelper::SmoothStep(value1.W, value2.W, amount)); } Vector4 Vector4::Subtract(Vector4 vector1, Vector4 vector2) @@ -464,7 +470,7 @@ namespace XFX result.X = ((vector.X * ((1.0f - yy) - zz)) + (vector.Y * (xy - wz))) + (vector.Z * (xz + wy)); result.Y = ((vector.X * (xy + wz)) + (vector.Y * ((1.0f - xx) - zz))) + (vector.Z * (yz - wx)); result.Z = ((vector.X * (xz - wy)) + (vector.Y * (yz + wx))) + (vector.Z * ((1.0f - xx) - yy)); - result.W = 1.0f; + result.W = 1.0f; } Vector4 Vector4::Transform(Vector2 vector, Quaternion rotation) @@ -549,7 +555,7 @@ namespace XFX W -= other.W; return *this; } - + Vector4 Vector4::operator -() { return Vector4(-X, -Y, -Z, -W);