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

113 lines
2.1 KiB
C
Raw Normal View History

2018-09-05 02:50:27 +00:00
#ifndef GEOMETRY_H
#include "color.h"
#include "engine.h"
2018-09-05 02:50:27 +00:00
#include "point.h"
#include <cstdint>
2018-09-05 02:50:27 +00:00
#include <vector>
// STRUCTURES
struct Material
{
ColorF32 kAmbient;
ColorF32 kDiffuse;
};
2018-09-05 02:50:27 +00:00
struct Face
{
unsigned int vertIndex[3];
2018-09-11 02:51:59 +00:00
Vector normal;
ColorF32 color;
};
struct Vertex
{
Point point;
Vector normal;
ColorF32 color;
};
struct MeshRenderData
{
std::vector<Vertex> vertsTransformed;
std::vector<Face> culledFaces;
bool smooth;
2018-09-05 02:50:27 +00:00
};
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)
{
2018-09-11 02:51:59 +00:00
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];
2018-09-11 02:51:59 +00:00
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);
2018-09-11 02:51:59 +00:00
// Store normal for flat shading
faces[f].normal = normal;
faces[f].normal.Normalize();
// Invert for Blender-compatibility
normal = -normal;
// Eye vector to viewport
2018-09-11 02:51:59 +00:00
Vector view = camPosition - renderData.vertsTransformed[v0].point;
float dot = Vector::Dot(normal, view);
if (dot < EPSILON_E3)
{
2018-09-11 02:51:59 +00:00
renderData.culledFaces.push_back(faces[f]);
}
}
}
2018-09-11 02:51:59 +00:00
Point position;
float rotation[3];
float scale;
2018-09-11 02:51:59 +00:00
std::vector<Vertex> verts;
2018-09-05 02:50:27 +00:00
std::vector<Face> faces;
Material material;
2018-09-11 02:51:59 +00:00
MeshRenderData renderData;
2018-09-05 02:50:27 +00:00
};
// PUBLIC FUNCTIONS
2018-09-11 02:51:59 +00:00
void RenderMesh(
Engine_Buffer &buffer, MeshRenderData &mesh);
2018-09-05 02:50:27 +00:00
#define GEOMETRY_H
#endif