1
0
Fork 0

Convert hot-loop functions to be inline

This commit is contained in:
Austin Morlan 2018-09-20 20:20:41 -07:00
parent 0203beb367
commit ab9675508a
Signed by: austin
GPG Key ID: FD6B27654AF5E348
6 changed files with 78 additions and 87 deletions

View File

@ -44,8 +44,8 @@ _HEADERS = camera.h color.h engine.h geometry.h light.h loader.h matrix.h\
platform.h point.h render.h transform.h util.h vec.h platform.h point.h render.h transform.h util.h vec.h
HEADERS = $(patsubst %,$(INCLUDE_DIR)/%,$(_HEADERS)) HEADERS = $(patsubst %,$(INCLUDE_DIR)/%,$(_HEADERS))
_OBJS = color.o engine.o light.o loader.o main.o platform.o render.o\ _OBJS = engine.o light.o loader.o main.o platform.o render.o\
transform.o vec.o transform.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

@ -24,6 +24,8 @@ struct ColorF32
}; };
// OPERATORS // OPERATORS
// c1 + c2 // c1 + c2
inline ColorF32 operator+(ColorF32 const &c1, ColorF32 const &c2) inline ColorF32 operator+(ColorF32 const &c1, ColorF32 const &c2)
@ -107,7 +109,22 @@ inline ColorF32 &operator/=(ColorF32 &c, float f)
// PUBLIC FUNCTIONS // PUBLIC FUNCTIONS
void ScaleColor(ColorF32 &c); 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;
}
#define COLOR_H #define COLOR_H

View File

@ -1,5 +1,8 @@
#ifndef VEC_H #ifndef VEC_H
#include "util.h"
#include <cmath>
// STRUCTURE // STRUCTURE
struct Vector struct Vector
@ -100,10 +103,59 @@ inline Vector &operator/=(Vector &v, float f)
// PUBLIC FUNCTIONS // PUBLIC FUNCTIONS
void VectorNormalize(Vector &v); inline float VectorLength(Vector &v)
float VectorLength(Vector &v); {
float VectorDot(Vector &v1, Vector &v2); float result = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
Vector VectorCross(Vector &v1, Vector &v2);
// zero length
if (result < EPSILON_E3)
{
result = 0.0f;
}
return result;
}
inline 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;
}
}
inline 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;
}
inline float VectorDot(Vector &v1, Vector &v2)
{
float result;
result = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
return result;
}
#define VEC_H #define VEC_H

View File

@ -1,20 +0,0 @@
#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

@ -64,8 +64,8 @@ int EngineInit(char *objFilename, char *mtlFilename)
// Mesh configuration // Mesh configuration
mesh.position.z = 125; mesh.position.z = 200;
mesh.position.y = -125; mesh.position.y = -100;
mesh.scale = 1.0f; mesh.scale = 1.0f;

View File

@ -1,58 +0,0 @@
#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;
}