Add Gouraud shading

This commit is contained in:
austin committed 2020-07-01 17:42:52 -07:00
1 parent bf5fabfdb2
commit 8b9e6b54c8
9 files changed
+285 -99

No files matched your search

+10
View File
@@ -70,6 +70,16 @@ inline ColorF32 operator*(ColorF32 c, float f)
return result;
}
// f * c
inline ColorF32 operator*(float f, ColorF32 c)
{
ColorF32 result;
result = c * f;
return result;
}
#define COLOR_H
#endif
+2 -1
View File
@@ -19,7 +19,8 @@ enum Engine_Input
ROTATE_Z_POS,
ROTATE_Z_NEG,
SCALE_UP,
SCALE_DOWN
SCALE_DOWN,
SHADING_TOGGLE
};
+37 -12
View File
@@ -17,7 +17,22 @@ struct Material
struct Face
{
unsigned int vertIndex[3];
ColorU32 color;
Vector normal;
ColorF32 color;
};
struct Vertex
{
Point point;
Vector normal;
ColorF32 color;
};
struct MeshRenderData
{
std::vector<Vertex> vertsTransformed;
std::vector<Face> culledFaces;
bool smooth;
};
struct Mesh
@@ -37,7 +52,7 @@ struct Mesh
inline void CullBackfaces(Point camPosition)
{
culledFaces.clear();
renderData.culledFaces.clear();
for (size_t f = 0; f < faces.size(); ++f)
{
@@ -45,41 +60,51 @@ struct Mesh
unsigned int v1 = faces[f].vertIndex[1];
unsigned int v2 = faces[f].vertIndex[2];
Vector v01 = vertsTransformed[v1] - vertsTransformed[v0];
Vector v02 = vertsTransformed[v2] - vertsTransformed[v0];
Vector v01 =
renderData.vertsTransformed[v1].point
- renderData.vertsTransformed[v0].point;
Vector v02 =
renderData.vertsTransformed[v2].point -
renderData.vertsTransformed[v0].point;
Vector normal = Vector::Cross(v01, v02);
// Store normal for flat shading
faces[f].normal = normal;
faces[f].normal.Normalize();
// Invert for Blender-compatibility
normal = -normal;
// Eye vector to viewport
Vector view = camPosition - vertsTransformed[v0];
Vector view = camPosition - renderData.vertsTransformed[v0].point;
float dot = Vector::Dot(normal, view);
if (dot < EPSILON_E3)
{
culledFaces.push_back(faces[f]);
renderData.culledFaces.push_back(faces[f]);
}
}
}
Point position;
float rotation[3];
float scale;
std::vector<Point> verts;
std::vector<Point> vertsTransformed;
std::vector<Vertex> verts;
std::vector<Face> faces;
std::vector<Face> culledFaces;
Material material;
MeshRenderData renderData;
};
// PUBLIC FUNCTIONS
void FillTriangle(
Engine_Buffer &buffer, ColorU32 &color,
Point &p0, Point &p1, Point &p2);
void RenderMesh(
Engine_Buffer &buffer, MeshRenderData &mesh);
#define GEOMETRY_H
+21
View File
@@ -74,6 +74,7 @@ inline Point operator*(Point v, Matrix m)
return result;
}
// v *=m
inline Point &operator*=(Point &v, Matrix m)
{
v = v * m;
@@ -81,6 +82,26 @@ inline Point &operator*=(Point &v, Matrix m)
return v;
}
// v * m
inline Vector operator*(Vector v, Matrix m)
{
Vector result;
for (int col = 0; col < 4; ++col)
{
float sum = 0.0;
for (int row = 0; row < 4; ++row)
{
sum += v.e[row] * m.e[row][col];
}
result.e[col] = sum;
}
return result;
}
#define MATRIX_H
#endif
+42
View File
@@ -76,6 +76,48 @@ inline Vector operator-(Vector v)
return result;
}
// v1 + v2
inline Vector operator+(Vector v1, Vector v2)
{
Vector result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
result.z = v1.z + v2.z;
return result;
}
// v1 += v2
inline Vector &operator+=(Vector &v1, Vector v2)
{
v1 = v1 + v2;
return v1;
}
// v / f
inline Vector operator/(Vector v, float f)
{
Vector result;
float inverse = 1.0f / f;
result.x = v.x * inverse;
result.y = v.y * inverse;
result.z = v.z * inverse;
return result;
}
// v /= f
inline Vector &operator/=(Vector &v, float f)
{
v = v / f;
return v;
}
#define VEC_H
#endif