1
0
Fork 0

Convert hot-loop functions to be inline

master
Austin Morlan 4 years ago
parent 0203beb367
commit ab9675508a
Signed by: austin
GPG Key ID: FD6B27654AF5E348
  1. 4
      Makefile
  2. 19
      include/color.h
  3. 60
      include/vec.h
  4. 20
      src/color.cpp
  5. 4
      src/engine.cpp
  6. 58
      src/vec.cpp

@ -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
HEADERS = $(patsubst %,$(INCLUDE_DIR)/%,$(_HEADERS))
_OBJS = color.o engine.o light.o loader.o main.o platform.o render.o\
transform.o vec.o
_OBJS = engine.o light.o loader.o main.o platform.o render.o\
transform.o
OBJS = $(patsubst %,$(BUILD_DIR)/%,$(_OBJS))
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADERS)

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

@ -1,5 +1,8 @@
#ifndef VEC_H
#include "util.h"
#include <cmath>
// STRUCTURE
struct Vector
@ -100,10 +103,59 @@ 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);
inline 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;
}
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

@ -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;
}

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

@ -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;
}
Loading…
Cancel
Save