ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Triangle2D.cpp
Go to the documentation of this file.
1#include "Triangle2D.h"
2
4
5Triangle2D::Triangle2D(const Vector2D& p1, const Vector2D& p2, const Vector2D& p3) {
6 p1X = p1.X;
7 p1Y = p1.Y;
8 p2X = p2.X;
9 p2Y = p2.Y;
10 p3X = p3.X;
11 p3Y = p3.Y;
12
13 v0X = p2X - p1X;
14 v0Y = p2Y - p1Y;
15 v1X = p3X - p1X;
16 v1Y = p3Y - p1Y;
17
18 denominator = 1.0f / (v0X * v1Y - v1X * v0Y);
19}
20
21Triangle2D::Triangle2D(const Quaternion& lookDirection, Transform* camT, Triangle3D* t, Material* material) {
22 this->material = material;
23
24 if (t->hasUV) {
25 this->p1UV = t->p1UV;
26 this->p2UV = t->p2UV;
27 this->p3UV = t->p3UV;
28
29 this->hasUV = true;
30 }
31
32 //Untranslate, unrotate, unscale
33 Vector3D p1 = camT->GetRotation().Multiply(lookDirection).UnrotateVector(*t->p1 - camT->GetPosition()) * camT->GetScale();
34 Vector3D p2 = camT->GetRotation().Multiply(lookDirection).UnrotateVector(*t->p2 - camT->GetPosition()) * camT->GetScale();
35 Vector3D p3 = camT->GetRotation().Multiply(lookDirection).UnrotateVector(*t->p3 - camT->GetPosition()) * camT->GetScale();
36
37 averageDepth = (p1.Z + p2.Z + p3.Z) / 3.0f;
38
39 p1X = p1.X;
40 p1Y = p1.Y;
41 p2X = p2.X;
42 p2Y = p2.Y;
43 p3X = p3.X;
44 p3Y = p3.Y;
45
46 v0X = p2X - p1X;
47 v0Y = p2Y - p1Y;
48 v1X = p3X - p1X;
49 v1Y = p3Y - p1Y;
50
51 denominator = 1.0f / (v0X * v1Y - v1X * v0Y);
52
53 normal = t->Normal();
54
55 t3p1 = t->p1;
56 t3p2 = t->p2;
57 t3p3 = t->p3;
58
59 //for quadtree / min/max dimensions
60 if (p1X < p2X){
61 min.X = (p1X < p3X ? p1X : p3X);
62 max.X = (p2X > p3X ? p2X : p3X);
63 }
64 else{
65 min.X = (p2X < p3X ? p2X : p3X);
66 max.X = (p1X > p3X ? p1X : p3X);
67 }
68
69 if (p1Y < p2Y){
70 min.Y = (p1Y < p3Y ? p1Y : p3Y);
71 max.Y = (p2Y > p3Y ? p2Y : p3Y);
72 }
73 else{
74 min.Y = (p2Y < p3Y ? p2Y : p3Y);
75 max.Y = (p1Y > p3Y ? p1Y : p3Y);
76 }
77}
78
80 averageDepth = (t->p1->Z + t->p2->Z + t->p3->Z) / 3.0f;
81
82 p1X = t->p1->X;
83 p1Y = t->p1->Y;
84 p2X = t->p2->X;
85 p2Y = t->p2->Y;
86 p3X = t->p3->X;
87 p3Y = t->p3->Y;
88
89 v0X = p2X - p1X;
90 v0Y = p2Y - p1Y;
91 v1X = p3X - p1X;
92 v1Y = p3Y - p1Y;
93
94 denominator = 1.0f / (v0X * v1Y - v1X * v0Y);
95
96 t3p1 = t->p1;
97 t3p2 = t->p2;
98 t3p3 = t->p3;
99
100 //for quadtree / min/max dimensions
101 if (p1X < p2X){
102 min.X = (p1X < p3X ? p1X : p3X);
103 max.X = (p2X > p3X ? p2X : p3X);
104 }
105 else{
106 min.X = (p2X < p3X ? p2X : p3X);
107 max.X = (p1X > p3X ? p1X : p3X);
108 }
109
110 if (p1Y < p2Y){
111 min.Y = (p1Y < p3Y ? p1Y : p3Y);
112 max.Y = (p2Y > p3Y ? p2Y : p3Y);
113 }
114 else{
115 min.Y = (p2Y < p3Y ? p2Y : p3Y);
116 max.Y = (p1Y > p3Y ? p1Y : p3Y);
117 }
118}
119
123
127
131
135
136bool Triangle2D::DidIntersect(const float& x, const float& y, float& u, float& v, float& w) {
137 float v2lX = x - p1X;
138 float v2lY = y - p1Y;
139
140 v = (v2lX * v1Y - v1X * v2lY) * denominator;
141 w = (v0X * v2lY - v2lX * v0Y) * denominator;
142
143 if (v < 0.0f || w < 0.0f || v > 1.0f || w > 1.0f) return false;
144
145 u = 1.0f - v - w;
146
147 return u >= 0.0f;
148}
149
151 return bbox->Overlaps(min, max);
152}
153
154//Separating Axis Theorem (SAT) algorithm
156 if (!(bbox->GetMinimum().X < Mathematics::Max(p1X, p2X, p3X) && bbox->GetMaximum().X > Mathematics::Min(p1X, p2X, p3X))) { return false; }
157 if (!(bbox->GetMinimum().Y < Mathematics::Max(p1Y, p2Y, p3Y) && bbox->GetMaximum().Y > Mathematics::Min(p1Y, p2Y, p3Y))) { return false; }
158
159 Vector2D axes[] = {
160 {-1.0f * (p2Y - p1Y), p2X - p1X},
161 {-1.0f * (p3Y - p1Y), p3X - p1X},
162 {-1.0f * (p3Y - p2Y), (p3X - p2X)}
163 };
164
165 Vector2D c = Vector2D((bbox->GetMinimum().X + bbox->GetMaximum().X) * 0.5f, (bbox->GetMinimum().Y + bbox->GetMaximum().Y) * 0.5f);
166 Vector2D e = Vector2D((bbox->GetMaximum().X - bbox->GetMinimum().X) * 0.5f, (bbox->GetMaximum().Y - bbox->GetMinimum().Y) * 0.5f);
167
168 float p0 = axes[0].X * (p1X - c.X) + axes[0].Y * (p1Y - c.Y);
169 float p2 = axes[0].X * (p3X - c.X) + axes[0].Y * (p3Y - c.Y);
170
171 float r = e.X * fabsf(axes[0].X) + e.Y * fabsf(axes[0].Y);
172
173 if (Mathematics::Max(-1.0f * Mathematics::Max(p0, p2), Mathematics::Min(p0, p2)) > r)
174 return false;
175
176 p0 = axes[1].X * (p1X - c.X) + axes[1].Y * (p1Y - c.Y);
177 float p1 = axes[1].X * (p2X - c.X) + axes[1].Y * (p2Y - c.Y);
178
179 r = e.X * fabsf(axes[1].X) + e.Y * fabsf(axes[1].Y);
180
181 if (Mathematics::Max(-1.0f * Mathematics::Max(p0, p1), Mathematics::Min(p0, p1)) > r)
182 return false;
183
184 p0 = axes[2].X * (p1X - c.X) + axes[2].Y * (p1Y - c.Y);
185 p1 = axes[2].X * (p2X - c.X) + axes[2].Y * (p2Y - c.Y);
186
187 r = e.X * fabsf(axes[2].X) + e.Y * fabsf(axes[2].Y);
188
189 if (Mathematics::Max(-1.0f * Mathematics::Max(p0, p1), Mathematics::Min(p0, p1)) > r)
190 return false;
191
192 return true;
193}
194
196 return Vector2D(p1X, p1Y).ToString() + " " + Vector2D(p2X, p2Y).ToString() + " " + Vector2D(p3X, p3Y).ToString();
197}
Defines the Triangle2D class for representing and manipulating 2D triangles.
Represents a 2D axis-aligned bounding box.
bool Overlaps(BoundingBox2D *bb)
Checks if this bounding box overlaps with another BoundingBox2D.
Vector2D GetMinimum()
Gets the minimum corner of the bounding box.
Vector2D GetMaximum()
Gets the maximum corner of the bounding box.
Abstract base class for rendering materials.
Definition Material.h:27
static T Min(T value1, T value2)
Returns the minimum of two values.
static T Max(T value1, T value2)
Returns the maximum of two values.
A mathematical construct representing a rotation in 3D space.
Definition Quaternion.h:30
Vector2D UnrotateVector(const Vector2D &coordinate) const
Applies the inverse of this quaternion's rotation to a 2D vector.
Quaternion Multiply(const Quaternion &quaternion) const
Multiplies (composes) this quaternion with another (order matters).
Represents a 3D transformation including position, rotation, and scale.
Definition Transform.h:22
Quaternion GetRotation()
Gets the current rotation of the object.
Definition Transform.cpp:61
Vector3D GetPosition()
Gets the position of the object.
Definition Transform.cpp:69
Vector3D GetScale()
Gets the scale of the object.
Definition Transform.cpp:77
String ToString()
Converts the triangle's data to a string representation.
float p3X
Definition Triangle2D.h:28
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
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
const Vector2D * p2UV
Pointer to the UV coordinates of the second vertex.
Definition Triangle3D.h:29
Vector3D * p1
Pointer to the first vertex of the triangle.
Definition Triangle3D.h:24
Vector3D * Normal()
Calculates and returns the normal of the triangle.
Vector3D * p3
Pointer to the third vertex of the triangle.
Definition Triangle3D.h:26
bool hasUV
Indicates whether the triangle has UV mapping.
Definition Triangle3D.h:36
Vector3D * p2
Pointer to the second vertex of the triangle.
Definition Triangle3D.h:25
const Vector2D * p3UV
Pointer to the UV coordinates of the third vertex.
Definition Triangle3D.h:30
const Vector2D * p1UV
Pointer to the UV coordinates of the first vertex.
Definition Triangle3D.h:28
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
String ToString() const
Converts the vector to a string representation.
Definition Vector2D.cpp:160
float X
The X-component of the 2D vector.
Definition Vector2D.h:29
float Y
The Y-component of the 2D vector.
Definition Vector2D.h:30
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
float Z
The Z-component of the 3D vector.
Definition Vector3D.h:30
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29