diff --git a/Makefile b/Makefile index b3adeb4..f8ca7bd 100644 --- a/Makefile +++ b/Makefile @@ -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) LIBS=-lSDL2 -_HEADERS = camera.h color.h engine.h geometry.h light.h loader.h platform.h point.h\ - transform.h util.h vec.h +_HEADERS = camera.h color.h engine.h geometry.h light.h loader.h matrix.h\ + platform.h point.h transform.h util.h vec.h 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)) $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADERS) diff --git a/include/color.h b/include/color.h index 70cf388..88b31c9 100644 --- a/include/color.h +++ b/include/color.h @@ -4,6 +4,7 @@ #include +// 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 diff --git a/include/matrix.h b/include/matrix.h index 8d12db7..592fa9d 100644 --- a/include/matrix.h +++ b/include/matrix.h @@ -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; diff --git a/include/point.h b/include/point.h index 2f4725e..abf579f 100644 --- a/include/point.h +++ b/include/point.h @@ -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; diff --git a/include/vec.h b/include/vec.h index bd56eda..6213cdd 100644 --- a/include/vec.h +++ b/include/vec.h @@ -1,8 +1,5 @@ #ifndef VEC_H -#include "util.h" -#include - // 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 diff --git a/src/color.cpp b/src/color.cpp new file mode 100644 index 0000000..a87fdea --- /dev/null +++ b/src/color.cpp @@ -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; +} + diff --git a/src/engine.cpp b/src/engine.cpp index cb19ae6..090fc4b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -68,8 +68,8 @@ int Engine_Init(char *objFilename, char *mtlFilename) // Mesh configuration - mesh.position.z = 125; - mesh.position.y = -125; + mesh.position.z = 50; + mesh.position.y = -75; mesh.scale = 1.0f; @@ -213,7 +213,7 @@ static void ComputeNormals(void) Vector v01 = p1 - 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 localVerts.data[v0].normal += normal; @@ -231,7 +231,7 @@ static void ComputeNormals(void) { // Compute the average normal for this vertex localVerts.data[v].normal /= vertexNormalCount[v]; - localVerts.data[v].normal.Normalize(); + VectorNormalize(localVerts.data[v].normal); } } } diff --git a/src/geometry.cpp b/src/geometry.cpp index 5bcc193..d8ad1a1 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -58,12 +58,12 @@ void ClipAndCull( // Calculate the face's normal (inverted for Blender-compatibility) Vector v01 = p1 - p0; Vector v02 = p2 - p0; - Vector normal = -Vector::Cross(v01, v02); + Vector normal = -VectorCross(v01, v02); // Eye vector to viewport Vector view = camPosition - p0; - float dot = Vector::Dot(normal, view); + float dot = VectorDot(normal, view); // Not a backface; add it to the list @@ -100,9 +100,9 @@ void RenderMesh( // Constants for this triangle used for barycentric calculations Vector v01 = p1 - p0; Vector v02 = p2 - p0; - float dot0101 = Vector::Dot(v01, v01); - float dot0102 = Vector::Dot(v01, v02); - float dot0202 = Vector::Dot(v02, v02); + float dot0101 = VectorDot(v01, v01); + float dot0102 = VectorDot(v01, v02); + float dot0202 = VectorDot(v02, v02); // 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 Point p(x, y, 1.0f); Vector v0P = p - p0; - float dot0P01 = Vector::Dot(v0P, v01); - float dot0P02 = Vector::Dot(v0P, v02); + float dot0P01 = VectorDot(v0P, v01); + float dot0P02 = VectorDot(v0P, v02); float denomInv = 1.0f / ((dot0101 * dot0202) - (dot0102 * dot0102)); float barycenter[3]; barycenter[1] = (dot0202 * dot0P01 - dot0102 * dot0P02) * denomInv; diff --git a/src/light.cpp b/src/light.cpp index a19ee1f..afb7fe4 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -16,8 +16,8 @@ static ColorF32 ComputeDiffuseLight( ColorF32 ComputeLight(Vertex &vert, Material &material, Light &light) { // Distance from light to vertex - Vector d = light.position - vert.point; - float distance = d.Length(); + Vector direction = light.position - vert.point; + float distance = VectorLength(direction); // 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 Vector direction = lightPosition - vertPosition; - direction.Normalize(); + VectorNormalize(direction); - float dot = Vector::Dot(vertNormal, direction); + float dot = VectorDot(vertNormal, direction); ColorF32 result = surfaceReflectivity * lightIntensity * dot; - return result; } diff --git a/src/vec.cpp b/src/vec.cpp new file mode 100644 index 0000000..866e109 --- /dev/null +++ b/src/vec.cpp @@ -0,0 +1,58 @@ +#include "vec.h" +#include "util.h" +#include + + +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; +}