113 lines
2.1 KiB
C++
113 lines
2.1 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];
|
|
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
|
|
{
|
|
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)
|
|
{
|
|
renderData.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 =
|
|
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 - renderData.vertsTransformed[v0].point;
|
|
|
|
float dot = Vector::Dot(normal, view);
|
|
|
|
if (dot < EPSILON_E3)
|
|
{
|
|
renderData.culledFaces.push_back(faces[f]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Point position;
|
|
float rotation[3];
|
|
float scale;
|
|
|
|
std::vector<Vertex> verts;
|
|
std::vector<Face> faces;
|
|
Material material;
|
|
|
|
MeshRenderData renderData;
|
|
};
|
|
|
|
|
|
// PUBLIC FUNCTIONS
|
|
void RenderMesh(
|
|
Engine_Buffer &buffer, MeshRenderData &mesh);
|
|
|
|
|
|
#define GEOMETRY_H
|
|
#endif
|
|
|