88 lines
1.7 KiB
C++
88 lines
1.7 KiB
C++
#ifndef GEOMETRY_H
|
|
|
|
#include "color.h"
|
|
#include "engine.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(
|
|
Engine_Buffer &buffer, ColorU32 &color,
|
|
Point &p0, Point &p1, Point &p2);
|
|
|
|
|
|
#define GEOMETRY_H
|
|
#endif
|
|
|