-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon.h
95 lines (76 loc) · 3.74 KB
/
polygon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include <glm/glm.hpp>
#include <vector>
#include <QString>
#include <QImage>
#include <QColor>
// A Vertex is a point in space that defines one corner of a polygon.
// Each Vertex has several attributes that determine how they contribute to the
// appearance of their Polygon, such as coloration.
struct Vertex
{
glm::vec4 m_pos; // The position of the vertex. In hw02, this is in pixel space.
glm::vec3 m_color; // The color of the vertex. X corresponds to Red, Y corresponds to Green, and Z corresponds to Blue.
glm::vec4 m_normal; // The surface normal of the vertex (not yet used)
glm::vec2 m_uv; // The texture coordinates of the vertex (not yet used)
Vertex(glm::vec4 p, glm::vec3 c, glm::vec4 n, glm::vec2 u)
: m_pos(p), m_color(c), m_normal(n), m_uv(u)
{}
};
// Each Polygon can be decomposed into triangles that fill its area.
struct Triangle
{
// The indices of the Vertices that make up this triangle.
// The indices correspond to the std::vector of Vertices stored in the Polygon
// which stores this Triangle
unsigned int m_indices[3];
};
class Polygon
{
public:
// TODO: Populate this list of triangles in Triangulate()
std::vector<Triangle> m_tris;
// The list of Vertices that define this polygon. This is already filled by the Polygon constructor.
std::vector<Vertex> m_verts;
// The name of this polygon, primarily to help you debug
QString m_name;
// The image that can be read to determine pixel color when used in conjunction with UV coordinates
// Not used until homework 3.
QImage* mp_texture;
// The image that can be read to determine surface normal offset when used in conjunction with UV coordinates
// Not used until homework 3
QImage* mp_normalMap;
// Polygon class constructors
Polygon(const QString& name, const std::vector<glm::vec4>& pos, const std::vector<glm::vec3> &col);
Polygon(const QString& name, int sides, glm::vec3 color, glm::vec4 pos, float rot, glm::vec4 scale);
Polygon(const QString& name);
Polygon();
Polygon(const Polygon& p);
~Polygon();
// TODO: Complete the body of Triangulate() in polygon.cpp
// Creates a set of triangles that, when combined, fill the area of this convex polygon.
void Triangulate();
// Copies the input QImage into this Polygon's texture
void SetTexture(QImage*);
// Copies the input QImage into this Polygon's normal map
void SetNormalMap(QImage*);
// Various getter, setter, and adder functions
void AddVertex(const Vertex&);
void AddTriangle(const Triangle&);
void ClearTriangles();
Triangle& TriAt(unsigned int);
Triangle TriAt(unsigned int) const;
Vertex& VertAt(unsigned int);
Vertex VertAt(unsigned int) const;
glm::vec3 barycentricInterpolate(const Triangle& t, glm::vec2 coord) const;
float depthInterpolate(const Triangle& t, glm::vec2 coord) const;
float persInterpolate(std::array<glm::vec4, 3>& triangleCoords, std::array<float, 3> zCoords, glm::vec2 coord) const;
glm::vec2 interpolateUV(std::array<glm::vec4, 3>& triangleCoords, std::array<glm::vec2, 3> uv, glm::vec2 coord) const;
glm::vec4 interpolateNormals(std::array<glm::vec4, 3>& triangleCoords, std::array<glm::vec4, 3> normals, glm::vec2 coord) const;
private:
float triArea(const Vertex& v1, const Vertex& v2, const float v3x, const float v3y) const;
float subTriArea(glm::vec2 a, glm::vec2 b, glm::vec2 c) const;
};
// Returns the color of the pixel in the image at the specified texture coordinates.
// Returns white if the image is a null pointer
glm::vec3 GetImageColor(const glm::vec2 &uv_coord, const QImage* const image);