1
0
Fork 0

Clean up structs and operators

This commit is contained in:
Austin Morlan 2018-09-18 22:33:52 -07:00
parent b3991a4651
commit 84d096c246
Signed by: austin
GPG Key ID: FD6B27654AF5E348
10 changed files with 125 additions and 110 deletions

View File

@ -40,11 +40,12 @@ WARNINGS_OFF=-Wno-missing-braces -Wno-gnu-anonymous-struct -Wno-old-style-cast\
CFLAGS=$(D) $(O) -std=c++11 $(WARNINGS_ON) $(WARNINGS_OFF) -I$(INCLUDE_DIR) CFLAGS=$(D) $(O) -std=c++11 $(WARNINGS_ON) $(WARNINGS_OFF) -I$(INCLUDE_DIR)
LIBS=-lSDL2 LIBS=-lSDL2
_HEADERS = camera.h color.h engine.h geometry.h light.h loader.h platform.h point.h\ _HEADERS = camera.h color.h engine.h geometry.h light.h loader.h matrix.h\
transform.h util.h vec.h platform.h point.h transform.h util.h vec.h
HEADERS = $(patsubst %,$(INCLUDE_DIR)/%,$(_HEADERS)) HEADERS = $(patsubst %,$(INCLUDE_DIR)/%,$(_HEADERS))
_OBJS = engine.o geometry.o light.o loader.o main.o platform.o transform.o _OBJS = color.o engine.o geometry.o light.o loader.o main.o platform.o transform.o\
vec.o
OBJS = $(patsubst %,$(BUILD_DIR)/%,$(_OBJS)) OBJS = $(patsubst %,$(BUILD_DIR)/%,$(_OBJS))
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADERS) $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADERS)

View File

@ -4,6 +4,7 @@
#include <cstdint> #include <cstdint>
// STRUCTURES
struct ColorU32 struct ColorU32
{ {
union union
@ -25,7 +26,7 @@ struct ColorF32
// OPERATORS // OPERATORS
// c1 + c2 // c1 + c2
inline ColorF32 operator+(ColorF32 c1, ColorF32 c2) inline ColorF32 operator+(ColorF32 const &c1, ColorF32 const &c2)
{ {
ColorF32 result; ColorF32 result;
@ -38,7 +39,7 @@ inline ColorF32 operator+(ColorF32 c1, ColorF32 c2)
} }
// c1 += c2 // c1 += c2
inline ColorF32 &operator+=(ColorF32 &c1, ColorF32 c2) inline ColorF32 &operator+=(ColorF32 &c1, ColorF32 const &c2)
{ {
c1 = c1 + c2; c1 = c1 + c2;
@ -46,7 +47,7 @@ inline ColorF32 &operator+=(ColorF32 &c1, ColorF32 c2)
} }
// c * f // c * f
inline ColorF32 operator*(ColorF32 c, float f) inline ColorF32 operator*(ColorF32 const &c, float f)
{ {
ColorF32 result; ColorF32 result;
@ -59,7 +60,7 @@ inline ColorF32 operator*(ColorF32 c, float f)
} }
// f * c // f * c
inline ColorF32 operator*(float f, ColorF32 c) inline ColorF32 operator*(float f, ColorF32 const &c)
{ {
ColorF32 result; ColorF32 result;
@ -69,7 +70,7 @@ inline ColorF32 operator*(float f, ColorF32 c)
} }
// c1 * c2 // c1 * c2
inline ColorF32 operator*(ColorF32 c1, ColorF32 c2) inline ColorF32 operator*(ColorF32 const &c1, ColorF32 const &c2)
{ {
ColorF32 result; ColorF32 result;
@ -82,7 +83,7 @@ inline ColorF32 operator*(ColorF32 c1, ColorF32 c2)
} }
// c / f // c / f
inline ColorF32 operator/(ColorF32 c, float f) inline ColorF32 operator/(ColorF32 const &c, float f)
{ {
ColorF32 result; ColorF32 result;
@ -105,22 +106,8 @@ inline ColorF32 &operator/=(ColorF32 &c, float f)
} }
inline void ScaleColor(ColorF32 &c) // PUBLIC FUNCTIONS
{ 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;
}
#define COLOR_H #define COLOR_H

View File

@ -32,7 +32,7 @@ struct Matrix
// OPERATORS // OPERATORS
// m1 * m2 // m1 * m2
inline Matrix operator*(Matrix m1, Matrix m2) inline Matrix operator*(Matrix const &m1, Matrix const &m2)
{ {
Matrix result; Matrix result;
@ -55,7 +55,7 @@ inline Matrix operator*(Matrix m1, Matrix m2)
} }
// v * m // v * m
inline Point operator*(Point v, Matrix m) inline Point operator*(Point const &v, Matrix const &m)
{ {
Point result; Point result;
@ -75,7 +75,7 @@ inline Point operator*(Point v, Matrix m)
} }
// v *=m // v *=m
inline Point &operator*=(Point &v, Matrix m) inline Point &operator*=(Point &v, Matrix const &m)
{ {
v = v * m; v = v * m;
@ -83,7 +83,7 @@ inline Point &operator*=(Point &v, Matrix m)
} }
// v * m // v * m
inline Vector operator*(Vector v, Matrix m) inline Vector operator*(Vector const &v, Matrix const &m)
{ {
Vector result; Vector result;

View File

@ -23,7 +23,7 @@ struct Point
// OPERATORS // OPERATORS
// p / f // p / f
inline Point operator/(Point p, float f) inline Point operator/(Point const &p, float f)
{ {
Point result; Point result;
@ -45,7 +45,7 @@ inline Point &operator/=(Point &p, float f)
} }
// p1 - p2 // p1 - p2
inline Vector operator-(Point p1, Point p2) inline Vector operator-(Point const &p1, Point const &p2)
{ {
Vector result; Vector result;
@ -57,7 +57,7 @@ inline Vector operator-(Point p1, Point p2)
} }
// -p // -p
inline Point operator-(Point p) inline Point operator-(Point const &p)
{ {
Point result; Point result;

View File

@ -1,8 +1,5 @@
#ifndef VEC_H #ifndef VEC_H
#include "util.h"
#include <cmath>
// STRUCTURE // STRUCTURE
struct Vector struct Vector
@ -10,60 +7,6 @@ struct Vector
inline Vector(void) : x(0), y(0), z(0), w(0) {} 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 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 union
{ {
float e[4]; float e[4];
@ -78,7 +21,7 @@ struct Vector
// OPERATORS // OPERATORS
// -v // -v
inline Vector operator-(Vector v) inline Vector operator-(Vector const &v)
{ {
Vector result; Vector result;
@ -90,7 +33,7 @@ inline Vector operator-(Vector v)
} }
// v1 + v2 // v1 + v2
inline Vector operator+(Vector v1, Vector v2) inline Vector operator+(Vector const &v1, Vector const &v2)
{ {
Vector result; Vector result;
@ -102,7 +45,7 @@ inline Vector operator+(Vector v1, Vector v2)
} }
// v1 += v2 // v1 += v2
inline Vector &operator+=(Vector &v1, Vector v2) inline Vector &operator+=(Vector &v1, Vector const &v2)
{ {
v1 = v1 + v2; v1 = v1 + v2;
@ -110,7 +53,7 @@ inline Vector &operator+=(Vector &v1, Vector v2)
} }
// f * v // f * v
inline Vector operator*(float f, Vector &v) inline Vector operator*(float f, Vector const &v)
{ {
Vector result; Vector result;
@ -122,7 +65,7 @@ inline Vector operator*(float f, Vector &v)
} }
// v / f // v / f
inline Vector operator/(Vector v, float f) inline Vector operator/(Vector const &v, float f)
{ {
Vector result; 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 #define VEC_H
#endif #endif

20
src/color.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "color.h"
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;
}

View File

@ -68,8 +68,8 @@ int Engine_Init(char *objFilename, char *mtlFilename)
// Mesh configuration // Mesh configuration
mesh.position.z = 125; mesh.position.z = 50;
mesh.position.y = -125; mesh.position.y = -75;
mesh.scale = 1.0f; mesh.scale = 1.0f;
@ -213,7 +213,7 @@ static void ComputeNormals(void)
Vector v01 = p1 - p0; Vector v01 = p1 - p0;
Vector v02 = p2 - p0; Vector v02 = p2 - p0;
Vector normal = Vector::Cross(v01, v02); Vector normal = VectorCross(v01, v02);
// Add each vertex's normal to the sum for future averaging // Add each vertex's normal to the sum for future averaging
localVerts.data[v0].normal += normal; localVerts.data[v0].normal += normal;
@ -231,7 +231,7 @@ static void ComputeNormals(void)
{ {
// Compute the average normal for this vertex // Compute the average normal for this vertex
localVerts.data[v].normal /= vertexNormalCount[v]; localVerts.data[v].normal /= vertexNormalCount[v];
localVerts.data[v].normal.Normalize(); VectorNormalize(localVerts.data[v].normal);
} }
} }
} }

View File

@ -58,12 +58,12 @@ void ClipAndCull(
// Calculate the face's normal (inverted for Blender-compatibility) // Calculate the face's normal (inverted for Blender-compatibility)
Vector v01 = p1 - p0; Vector v01 = p1 - p0;
Vector v02 = p2 - p0; Vector v02 = p2 - p0;
Vector normal = -Vector::Cross(v01, v02); Vector normal = -VectorCross(v01, v02);
// Eye vector to viewport // Eye vector to viewport
Vector view = camPosition - p0; Vector view = camPosition - p0;
float dot = Vector::Dot(normal, view); float dot = VectorDot(normal, view);
// Not a backface; add it to the list // Not a backface; add it to the list
@ -100,9 +100,9 @@ void RenderMesh(
// Constants for this triangle used for barycentric calculations // Constants for this triangle used for barycentric calculations
Vector v01 = p1 - p0; Vector v01 = p1 - p0;
Vector v02 = p2 - p0; Vector v02 = p2 - p0;
float dot0101 = Vector::Dot(v01, v01); float dot0101 = VectorDot(v01, v01);
float dot0102 = Vector::Dot(v01, v02); float dot0102 = VectorDot(v01, v02);
float dot0202 = Vector::Dot(v02, v02); float dot0202 = VectorDot(v02, v02);
// Iterate over the bounding box and determine if each point is in the triangle // Iterate over the bounding box and determine if each point is in the triangle
@ -113,8 +113,8 @@ void RenderMesh(
// Calculate the barycentric coordinate of this point // Calculate the barycentric coordinate of this point
Point p(x, y, 1.0f); Point p(x, y, 1.0f);
Vector v0P = p - p0; Vector v0P = p - p0;
float dot0P01 = Vector::Dot(v0P, v01); float dot0P01 = VectorDot(v0P, v01);
float dot0P02 = Vector::Dot(v0P, v02); float dot0P02 = VectorDot(v0P, v02);
float denomInv = 1.0f / ((dot0101 * dot0202) - (dot0102 * dot0102)); float denomInv = 1.0f / ((dot0101 * dot0202) - (dot0102 * dot0102));
float barycenter[3]; float barycenter[3];
barycenter[1] = (dot0202 * dot0P01 - dot0102 * dot0P02) * denomInv; barycenter[1] = (dot0202 * dot0P01 - dot0102 * dot0P02) * denomInv;

View File

@ -16,8 +16,8 @@ static ColorF32 ComputeDiffuseLight(
ColorF32 ComputeLight(Vertex &vert, Material &material, Light &light) ColorF32 ComputeLight(Vertex &vert, Material &material, Light &light)
{ {
// Distance from light to vertex // Distance from light to vertex
Vector d = light.position - vert.point; Vector direction = light.position - vert.point;
float distance = d.Length(); float distance = VectorLength(direction);
// Intensity is a function of the falloff factors and the distance // Intensity is a function of the falloff factors and the distance
@ -66,13 +66,12 @@ static ColorF32 ComputeDiffuseLight(
{ {
// Light is a function of the position of the light source and the surface // Light is a function of the position of the light source and the surface
Vector direction = lightPosition - vertPosition; Vector direction = lightPosition - vertPosition;
direction.Normalize(); VectorNormalize(direction);
float dot = Vector::Dot(vertNormal, direction); float dot = VectorDot(vertNormal, direction);
ColorF32 result = surfaceReflectivity * lightIntensity * dot; ColorF32 result = surfaceReflectivity * lightIntensity * dot;
return result; return result;
} }

58
src/vec.cpp Normal file
View File

@ -0,0 +1,58 @@
#include "vec.h"
#include "util.h"
#include <cmath>
void VectorNormalize(Vector &v)
{
float length = VectorLength(v);
// zero length
if (length == 0.0f)
{
v.x = 0.0f;
v.y = 0.0f;
v.z = 0.0f;
}
else
{
float lengthInv = 1.0f / length;
v.x *= lengthInv;
v.y *= lengthInv;
v.z *= lengthInv;
}
}
float VectorLength(Vector &v)
{
float result = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
// zero length
if (result < EPSILON_E3)
{
result = 0.0f;
}
return result;
}
float VectorDot(Vector &v1, Vector &v2)
{
float result;
result = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
return result;
}
Vector VectorCross(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;
}