Skip to content

Commit fb43f04

Browse files
committed
reorganize vertex and face class
1 parent 32b0663 commit fb43f04

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

include/face.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include "vertex.hpp"
4+
#include "material.hpp"
5+
6+
#include <array>
7+
8+
namespace simple_renderer {
9+
10+
class Face {
11+
public:
12+
// Default constructor
13+
Face() = default;
14+
// Copy constructor
15+
Face(const Face& face) = default;
16+
Face& operator=(const Face& face) = default;
17+
// Move constructor
18+
Face(Face&& face) = default;
19+
Face& operator=(Face&& face) = default;
20+
// Destructor
21+
~Face() = default;
22+
23+
explicit Face(const Vertex& v0, const Vertex& v1, const Vertex& v2, Material material)
24+
: vertices_{v0, v1, v2}, material_(std::move(material)) {
25+
calculateNormal();
26+
}
27+
28+
void transform(const glm::mat4 &tran) {
29+
vertices_[0].transform(tran);
30+
vertices_[1].transform(tran);
31+
vertices_[2].transform(tran);
32+
}
33+
34+
const std::array<Vertex, 3>& vertices() const { return vertices_; }
35+
const Vertex& vertex(size_t index) const { return vertices_[index]; }
36+
const glm::vec3& normal() const { return normal_; }
37+
const Material& material() const { return material_; }
38+
39+
private:
40+
std::array<Vertex, 3> vertices_;
41+
glm::vec3 normal_;
42+
Material material_;
43+
44+
void calculateNormal() {
45+
glm::vec3 edge1 = glm::vec3(vertices_[1].position()) - glm::vec3(vertices_[0].position());
46+
glm::vec3 edge2 = glm::vec3(vertices_[2].position()) - glm::vec3(vertices_[0].position());
47+
normal_ = glm::normalize(glm::cross(edge1, edge2));
48+
}
49+
};
50+
51+
}

0 commit comments

Comments
 (0)