83 lines
1.3 KiB
C
83 lines
1.3 KiB
C
|
#ifndef VEC_H
|
||
|
|
||
|
#include "util.h"
|
||
|
#include <cmath>
|
||
|
|
||
|
|
||
|
// STRUCTURE
|
||
|
struct Vector
|
||
|
{
|
||
|
inline Vector(void) : x(0), y(0), z(0), w(0) {}
|
||
|
inline Vector(float x, float y, float z) : x(x), y(y), z(z), w(0) {}
|
||
|
|
||
|
inline void Normalize(void)
|
||
|
{
|
||
|
float length = sqrtf(x*x + y*y + z*z);
|
||
|
|
||
|
// zero length
|
||
|
if (length < EPSILON_E3)
|
||
|
{
|
||
|
x = 0.0f;
|
||
|
y = 0.0f;
|
||
|
z = 0.0f;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
float lengthInv = 1.0f / length;
|
||
|
|
||
|
x *= lengthInv;
|
||
|
y *= lengthInv;
|
||
|
z *= lengthInv;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
inline static float Dot(Vector v1, Vector v2)
|
||
|
{
|
||
|
float result;
|
||
|
|
||
|
result = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
inline static Vector Cross(Vector v1, Vector v2)
|
||
|
{
|
||
|
Vector result;
|
||
|
|
||
|
result.x = (v1.y * v2.z) - (v1.z * v2.y);
|
||
|
result.y = (v1.z * v2.x) - (v1.x * v2.z);
|
||
|
result.z = (v1.x * v2.y) - (v1.y * v2.x);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
union
|
||
|
{
|
||
|
float e[4];
|
||
|
|
||
|
struct
|
||
|
{
|
||
|
float x, y, z, w;
|
||
|
};
|
||
|
};
|
||
|
};
|
||
|
|
||
|
|
||
|
// OPERATORS
|
||
|
// -v
|
||
|
inline Vector operator-(Vector v)
|
||
|
{
|
||
|
Vector result;
|
||
|
|
||
|
result.x = -v.x;
|
||
|
result.y = -v.y;
|
||
|
result.z = -v.z;
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
|
#define VEC_H
|
||
|
#endif
|
||
|
|