00001 00008 #include <cmath> 00009 #include <cassert> 00010 00011 #include "PS2Math.h" 00012 00013 using namespace PS2Math; 00014 00019 Vector4::Vector4(float x, float y, float z, float w) { 00020 00021 this->x = x; 00022 this->y = y; 00023 this->z = z; 00024 this->w = w; 00025 } 00026 00032 Vector4 &Vector4::Omogenize() { 00033 00034 x /= w; 00035 y /= w; 00036 z /= w; 00037 w = 1; 00038 00039 return *this; 00040 } 00041 00046 Vector4 &Vector4::Normalize() { 00047 00048 float Module = w / sqrt(x*x + y*y + z*z); 00049 x *= Module; 00050 y *= Module; 00051 z *= Module; 00052 00053 return *this; 00054 } 00055 00063 Vector4 &Vector4::OmogenizeWtoZ() { 00064 00065 x /= w; 00066 y /= w; 00067 z = w; 00068 w = 1; 00069 00070 return *this; 00071 } 00072 00077 float &Vector4::operator[](int Index) { 00078 00079 switch(Index) { 00080 00081 case 0: 00082 return x; 00083 case 1: 00084 return y; 00085 case 2: 00086 return z; 00087 case 3: 00088 return w; 00089 } 00090 00091 assert(false); 00092 return x; 00093 } 00094 00097 const float Vector4::operator[](int Index) const { 00098 00099 switch(Index) { 00100 00101 case 0: 00102 return x; 00103 case 1: 00104 return y; 00105 case 2: 00106 return z; 00107 case 3: 00108 return w; 00109 } 00110 00111 assert(false); 00112 return x; 00113 } 00114 00119 std::ostream &PS2Math::operator<<(std::ostream &outStream, const Vector4 &V) { 00120 00121 outStream << "[" << V[0] << "\t" << V[1] << "\t" << 00122 V[2] << "\t" << V[3] << "]\n"; 00123 00124 return outStream; 00125 } 00126 00130 const Vector4 PS2Math::operator-(const Vector4 &V) { 00131 00132 return Vector4(-V.x, -V.y, -V.z, V.w); 00133 } 00134 00137 const Vector4 PS2Math::operator*(const Vector4 &V1, const Vector4 &V2) { 00138 00139 return Vector4(V1.y * V2.z - V1.z * V2.y, 00140 V1.z * V2.x - V1.x * V2.z, 00141 V1.x * V2.y - V1.y * V2.x, 00142 V1.w * V2.w); 00143 } 00144 00147 const Vector4 PS2Math::operator-(const Vector4 &V1, const Vector4 &V2) { 00148 00149 return V1 + -V2; 00150 } 00151 00154 const Vector4 PS2Math::operator+(const Vector4 &V1, const Vector4 &V2) { 00155 00156 return Vector4(V2.w * V1.x + V1.w * V2.x, 00157 V2.w * V1.y + V1.w * V2.y, 00158 V2.w * V1.z + V1.w * V2.z, 00159 V2.w * V1.w); 00160 } 00161 00164 float PS2Math::Dot(const Vector4 &V1, const Vector4 &V2) { 00165 00166 return (V1.x * V2.x + V1.y * V2.y + V1.z * V2.z) / (V1.w * V2.w); 00167 } 00168 00183 float PS2Math::Vector4::ConvertToFixedPoint(int Vertex[]) { 00184 00185 float divw; 00186 00187 divw = 1.0f / w; 00188 Vertex[0] = (int) (x * divw) << 4; 00189 Vertex[1] = (int) (y * divw) << 4; 00190 Vertex[2] = (int) (z * divw); 00191 Vertex[3] = 1; 00192 00193 return divw; 00194 }