Clean up structs and operators
This commit is contained in:
10 files changed
+125
-110
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
+4
-4
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -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;
|
||||
|
||||
+4
-5
@@ -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;
|
||||
}
|
||||
|
||||
+58
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user