1
0
Fork 0
2018-soft-3d-renderer/include/vec.h

112 lines
1.6 KiB
C
Raw Normal View History

#ifndef VEC_H
// 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) {}
union
{
float e[4];
struct
{
float x, y, z, w;
};
};
};
// OPERATORS
// -v
2018-09-19 05:33:52 +00:00
inline Vector operator-(Vector const &v)
{
Vector result;
result.x = -v.x;
result.y = -v.y;
result.z = -v.z;
return result;
}
2018-09-20 02:15:53 +00:00
// v1 - v2
inline Vector operator-(Vector const &v1, Vector const &v2)
{
Vector result;
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
result.z = v1.z - v2.z;
return result;
}
2018-09-11 02:51:59 +00:00
// v1 + v2
2018-09-19 05:33:52 +00:00
inline Vector operator+(Vector const &v1, Vector const &v2)
2018-09-11 02:51:59 +00:00
{
Vector result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
result.z = v1.z + v2.z;
return result;
}
// v1 += v2
2018-09-19 05:33:52 +00:00
inline Vector &operator+=(Vector &v1, Vector const &v2)
2018-09-11 02:51:59 +00:00
{
v1 = v1 + v2;
return v1;
}
2018-09-19 03:01:03 +00:00
// f * v
2018-09-19 05:33:52 +00:00
inline Vector operator*(float f, Vector const &v)
2018-09-19 03:01:03 +00:00
{
Vector result;
result.x = v.x * f;
result.y = v.y * f;
result.z = v.z * f;
return result;
}
2018-09-11 02:51:59 +00:00
// v / f
2018-09-19 05:33:52 +00:00
inline Vector operator/(Vector const &v, float f)
2018-09-11 02:51:59 +00:00
{
Vector result;
float inverse = 1.0f / f;
result.x = v.x * inverse;
result.y = v.y * inverse;
result.z = v.z * inverse;
return result;
}
// v /= f
inline Vector &operator/=(Vector &v, float f)
{
v = v / f;
return v;
}
2018-09-19 05:33:52 +00:00
// PUBLIC FUNCTIONS
void VectorNormalize(Vector &v);
float VectorLength(Vector &v);
float VectorDot(Vector &v1, Vector &v2);
Vector VectorCross(Vector &v1, Vector &v2);
#define VEC_H
#endif