1
0
Fork 0
2018-soft-3d-renderer/include/geometry.h

90 lines
1.8 KiB
C++

#ifndef GEOMETRY_H
#include "color.h"
#include "point.h"
#include <cstdint>
#include <vector>
// STRUCTURES
struct Material
{
ColorF32 kAmbient;
ColorF32 kDiffuse;
};
struct Face
{
unsigned int vertIndex[3];
ColorU32 color;
};
struct Mesh
{
inline Mesh(void)
{
position.x = 0.0f;
position.y = 0.0f;
position.z = 0.0f;
rotation[0] = 0.0f;
rotation[1] = 0.0f;
rotation[2] = 0.0f;
scale = 1.0f;
}
inline void CullBackfaces(Point camPosition)
{
culledFaces.clear();
for (size_t f = 0; f < faces.size(); ++f)
{
unsigned int v0 = faces[f].vertIndex[0];
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 normal = Vector::Cross(v01, v02);
// Invert for Blender-compatibility
normal = -normal;
// Eye vector to viewport
Vector view = camPosition - vertsTransformed[v0];
float dot = Vector::Dot(normal, view);
if (dot < EPSILON_E3)
{
culledFaces.push_back(faces[f]);
}
}
}
Point position;
float rotation[3];
float scale;
std::vector<Point> verts;
std::vector<Point> vertsTransformed;
std::vector<Face> faces;
std::vector<Face> culledFaces;
Material material;
};
// PUBLIC FUNCTIONS
void FillTriangle(
uint32_t *buffer, int width,
float *zbuffer, uint32_t color,
float x0, float y0, float z0,
float x1, float y1, float z1,
float x2, float y2, float z2);
#define GEOMETRY_H
#endif