Clean up structs and operators

This commit is contained in:
austin committed 2020-07-01 17:43:01 -07:00
1 parent b3991a4651
commit 84d096c246
10 files changed
+125 -110

No files matched your search

+9 -22
View File
@@ -4,6 +4,7 @@
#include <cstdint>
// STRUCTURES
struct ColorU32
{
union
@@ -25,7 +26,7 @@ struct ColorF32
// OPERATORS
// c1 + c2
inline ColorF32 operator+(ColorF32 c1, ColorF32 c2)
inline ColorF32 operator+(ColorF32 const &c1, ColorF32 const &c2)
{
ColorF32 result;
@@ -38,7 +39,7 @@ inline ColorF32 operator+(ColorF32 c1, ColorF32 c2)
}
// c1 += c2
inline ColorF32 &operator+=(ColorF32 &c1, ColorF32 c2)
inline ColorF32 &operator+=(ColorF32 &c1, ColorF32 const &c2)
{
c1 = c1 + c2;
@@ -46,7 +47,7 @@ inline ColorF32 &operator+=(ColorF32 &c1, ColorF32 c2)
}
// c * f
inline ColorF32 operator*(ColorF32 c, float f)
inline ColorF32 operator*(ColorF32 const &c, float f)
{
ColorF32 result;
@@ -59,7 +60,7 @@ inline ColorF32 operator*(ColorF32 c, float f)
}
// f * c
inline ColorF32 operator*(float f, ColorF32 c)
inline ColorF32 operator*(float f, ColorF32 const &c)
{
ColorF32 result;
@@ -69,7 +70,7 @@ inline ColorF32 operator*(float f, ColorF32 c)
}
// c1 * c2
inline ColorF32 operator*(ColorF32 c1, ColorF32 c2)
inline ColorF32 operator*(ColorF32 const &c1, ColorF32 const &c2)
{
ColorF32 result;
@@ -82,7 +83,7 @@ inline ColorF32 operator*(ColorF32 c1, ColorF32 c2)
}
// c / f
inline ColorF32 operator/(ColorF32 c, float f)
inline ColorF32 operator/(ColorF32 const &c, float f)
{
ColorF32 result;
@@ -105,22 +106,8 @@ inline ColorF32 &operator/=(ColorF32 &c, float f)
}
inline void ScaleColor(ColorF32 &c)
{
float blue = MAX(c.b, 0.0f);
float green = MAX(c.g, 0.0f);
float red = MAX(c.r, 0.0f);
float alpha = MAX(c.a, 0.0f);
float max = MAX(MAX(MAX(blue,green),red),1.0f);
ColorF32 scaled = {blue, green, red, alpha};
scaled /= max;
c.b = scaled.b;
c.g = scaled.g;
c.r = scaled.r;
c.a = scaled.a;
}
// PUBLIC FUNCTIONS
void ScaleColor(ColorF32 &c);
#define COLOR_H
+4 -4
View File
@@ -32,7 +32,7 @@ struct Matrix
// OPERATORS
// m1 * m2
inline Matrix operator*(Matrix m1, Matrix m2)
inline Matrix operator*(Matrix const &m1, Matrix const &m2)
{
Matrix result;
@@ -55,7 +55,7 @@ inline Matrix operator*(Matrix m1, Matrix m2)
}
// v * m
inline Point operator*(Point v, Matrix m)
inline Point operator*(Point const &v, Matrix const &m)
{
Point result;
@@ -75,7 +75,7 @@ inline Point operator*(Point v, Matrix m)
}
// v *=m
inline Point &operator*=(Point &v, Matrix m)
inline Point &operator*=(Point &v, Matrix const &m)
{
v = v * m;
@@ -83,7 +83,7 @@ inline Point &operator*=(Point &v, Matrix m)
}
// v * m
inline Vector operator*(Vector v, Matrix m)
inline Vector operator*(Vector const &v, Matrix const &m)
{
Vector result;
+3 -3
View File
@@ -23,7 +23,7 @@ struct Point
// OPERATORS
// p / f
inline Point operator/(Point p, float f)
inline Point operator/(Point const &p, float f)
{
Point result;
@@ -45,7 +45,7 @@ inline Point &operator/=(Point &p, float f)
}
// p1 - p2
inline Vector operator-(Point p1, Point p2)
inline Vector operator-(Point const &p1, Point const &p2)
{
Vector result;
@@ -57,7 +57,7 @@ inline Vector operator-(Point p1, Point p2)
}
// -p
inline Point operator-(Point p)
inline Point operator-(Point const &p)
{
Point result;
+12 -62
View File
@@ -1,8 +1,5 @@
#ifndef VEC_H
#include "util.h"
#include <cmath>
// STRUCTURE
struct Vector
@@ -10,60 +7,6 @@ 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 = Length();
// zero length
if (length == 0.0f)
{
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
else
{
float lengthInv = 1.0f / length;
x *= lengthInv;
y *= lengthInv;
z *= lengthInv;
}
}
inline float Length(void)
{
float result = sqrtf(x*x + y*y + z*z);
// zero length
if (result < EPSILON_E3)
{
result = 0.0f;
}
return result;
}
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];
@@ -78,7 +21,7 @@ struct Vector
// OPERATORS
// -v
inline Vector operator-(Vector v)
inline Vector operator-(Vector const &v)
{
Vector result;
@@ -90,7 +33,7 @@ inline Vector operator-(Vector v)
}
// v1 + v2
inline Vector operator+(Vector v1, Vector v2)
inline Vector operator+(Vector const &v1, Vector const &v2)
{
Vector result;
@@ -102,7 +45,7 @@ inline Vector operator+(Vector v1, Vector v2)
}
// v1 += v2
inline Vector &operator+=(Vector &v1, Vector v2)
inline Vector &operator+=(Vector &v1, Vector const &v2)
{
v1 = v1 + v2;
@@ -110,7 +53,7 @@ inline Vector &operator+=(Vector &v1, Vector v2)
}
// f * v
inline Vector operator*(float f, Vector &v)
inline Vector operator*(float f, Vector const &v)
{
Vector result;
@@ -122,7 +65,7 @@ inline Vector operator*(float f, Vector &v)
}
// v / f
inline Vector operator/(Vector v, float f)
inline Vector operator/(Vector const &v, float f)
{
Vector result;
@@ -144,6 +87,13 @@ inline Vector &operator/=(Vector &v, float f)
}
// 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