ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Triangle2D.h
Go to the documentation of this file.
1/**
2 * @file Triangle2D.h
3 * @brief Defines the Triangle2D class for representing and manipulating 2D triangles.
4 *
5 * The Triangle2D class provides functionality to define, transform, and test intersections
6 * with 2D triangles. It includes support for UV mapping, depth calculations, and material assignment.
7 *
8 * @date 22/12/2024
9 * @version 1.0
10 * @author Coela Can't
11 */
12
13#pragma once
14
19#include "Triangle3D.h"
20
21/**
22 * @class Triangle2D
23 * @brief Represents a 2D triangle with support for UV mapping, depth, and intersection testing.
24 */
26public:
27 float denominator = 0.0f; ///< Precomputed denominator for barycentric coordinate calculations.
28 float p1X, p1Y, p2X, p2Y, p3X, p3Y; ///< Coordinates of the triangle's vertices.
29 float v0X, v0Y, v1X, v1Y, v2X, v2Y; ///< Edge vectors for barycentric coordinate calculations.
30 Vector2D min, max; ///< Minimum and maximum bounds of the triangle.
31
32 Vector3D* normal; ///< Normal vector of the triangle (if available).
33 Material* material; ///< Material assigned to the triangle.
34
35 Vector3D* t3p1; ///< Pointer to the first vertex in 3D space.
36 Vector3D* t3p2; ///< Pointer to the second vertex in 3D space.
37 Vector3D* t3p3; ///< Pointer to the third vertex in 3D space.
38
39 const Vector2D* p1UV; ///< UV coordinates of the first vertex.
40 const Vector2D* p2UV; ///< UV coordinates of the second vertex.
41 const Vector2D* p3UV; ///< UV coordinates of the third vertex.
42
43 bool hasUV = false; ///< Indicates whether the triangle has UV mapping.
44 float averageDepth = 0.0f; ///< Average depth of the triangle for rendering.
45
46 /**
47 * @brief Default constructor.
48 */
49 Triangle2D();
50
51 /**
52 * @brief Constructs a triangle from 2D vertices.
53 * @param p1 First vertex.
54 * @param p2 Second vertex.
55 * @param p3 Third vertex.
56 */
57 Triangle2D(const Vector2D& p1, const Vector2D& p2, const Vector2D& p3);
58
59 /**
60 * @brief Constructs a triangle from 3D data with a camera transformation and material.
61 * @param lookDirection Camera look direction as a quaternion.
62 * @param camT Camera transform.
63 * @param t Source 3D triangle.
64 * @param material Material to assign to the triangle.
65 */
66 Triangle2D(const Quaternion& lookDirection, Transform* camT, Triangle3D* t, Material* material);
67
68 /**
69 * @brief Constructs a triangle from a 3D triangle.
70 * @param t Source 3D triangle.
71 */
73
74 /**
75 * @brief Gets the first vertex as a 2D point.
76 * @return First vertex.
77 */
79
80 /**
81 * @brief Gets the second vertex as a 2D point.
82 * @return Second vertex.
83 */
85
86 /**
87 * @brief Gets the third vertex as a 2D point.
88 * @return Third vertex.
89 */
91
92 /**
93 * @brief Gets the material assigned to the triangle.
94 * @return Pointer to the material.
95 */
97
98 /**
99 * @brief Checks if a point intersects the triangle using barycentric coordinates.
100 * @param x X-coordinate of the point.
101 * @param y Y-coordinate of the point.
102 * @param u Output barycentric coordinate u.
103 * @param v Output barycentric coordinate v.
104 * @param w Output barycentric coordinate w.
105 * @return True if the point intersects the triangle, otherwise false.
106 */
107 bool DidIntersect(const float& x, const float& y, float& u, float& v, float& w);
108
109 /**
110 * @brief Checks if the triangle intersects a bounding box.
111 * @param bbox Bounding box to test against.
112 * @return True if the triangle intersects the bounding box, otherwise false.
113 */
114 bool DidIntersect(BoundingBox2D* bbox);
115
116 /**
117 * @brief Checks if the triangle intersects a bounding box using the Separating Axis Theorem (SAT).
118 * @param bbox Bounding box to test against.
119 * @return True if the triangle intersects the bounding box, otherwise false.
120 */
121 bool DidIntersectSAT(BoundingBox2D* bbox);
122
123 /**
124 * @brief Converts the triangle's data to a string representation.
125 * @return String representation of the triangle.
126 */
127 String ToString();
128};
Defines the BoundingBox2D class for managing 2D bounding boxes.
Base class for materials defining shading and rendering behavior.
Defines the Transform class for managing position, rotation, and scale of objects.
Defines the Triangle3D class for representing and manipulating 3D triangles.
Defines a 2D vector and various related operations.
Represents a 2D axis-aligned bounding box.
Abstract base class for rendering materials.
Definition Material.h:27
A mathematical construct representing a rotation in 3D space.
Definition Quaternion.h:30
Represents a 3D transformation including position, rotation, and scale.
Definition Transform.h:22
String ToString()
Converts the triangle's data to a string representation.
float p3X
Definition Triangle2D.h:28
float v2Y
Edge vectors for barycentric coordinate calculations.
Definition Triangle2D.h:29
float p2Y
Definition Triangle2D.h:28
bool DidIntersectSAT(BoundingBox2D *bbox)
Checks if the triangle intersects a bounding box using the Separating Axis Theorem (SAT).
Vector2D min
Definition Triangle2D.h:30
Vector3D * normal
Normal vector of the triangle (if available).
Definition Triangle2D.h:32
float p1X
Definition Triangle2D.h:28
float v0X
Definition Triangle2D.h:29
Material * GetMaterial()
Gets the material assigned to the triangle.
const Vector2D * p2UV
UV coordinates of the second vertex.
Definition Triangle2D.h:40
Vector2D GetP2()
Gets the second vertex as a 2D point.
float p2X
Definition Triangle2D.h:28
float v2X
Definition Triangle2D.h:29
Vector3D * t3p1
Pointer to the first vertex in 3D space.
Definition Triangle2D.h:35
Triangle2D()
Default constructor.
Definition Triangle2D.cpp:3
Vector2D max
Minimum and maximum bounds of the triangle.
Definition Triangle2D.h:30
float v0Y
Definition Triangle2D.h:29
bool hasUV
Indicates whether the triangle has UV mapping.
Definition Triangle2D.h:43
bool DidIntersect(const float &x, const float &y, float &u, float &v, float &w)
Checks if a point intersects the triangle using barycentric coordinates.
float averageDepth
Average depth of the triangle for rendering.
Definition Triangle2D.h:44
float denominator
Precomputed denominator for barycentric coordinate calculations.
Definition Triangle2D.h:27
Vector3D * t3p3
Pointer to the third vertex in 3D space.
Definition Triangle2D.h:37
float v1X
Definition Triangle2D.h:29
Vector2D GetP1()
Gets the first vertex as a 2D point.
const Vector2D * p3UV
UV coordinates of the third vertex.
Definition Triangle2D.h:41
float v1Y
Definition Triangle2D.h:29
Vector2D GetP3()
Gets the third vertex as a 2D point.
Vector3D * t3p2
Pointer to the second vertex in 3D space.
Definition Triangle2D.h:36
const Vector2D * p1UV
UV coordinates of the first vertex.
Definition Triangle2D.h:39
Material * material
Material assigned to the triangle.
Definition Triangle2D.h:33
float p1Y
Definition Triangle2D.h:28
float p3Y
Coordinates of the triangle's vertices.
Definition Triangle2D.h:28
Represents a 3D triangle with support for UV mapping and ray intersection testing.
Definition Triangle3D.h:22
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26