ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Triangle2D Class Reference

Represents a 2D triangle with support for UV mapping, depth, and intersection testing. More...

#include <Triangle2D.h>

Collaboration diagram for Triangle2D:

Public Member Functions

 Triangle2D ()
 Default constructor.
 
 Triangle2D (const Vector2D &p1, const Vector2D &p2, const Vector2D &p3)
 Constructs a triangle from 2D vertices.
 
 Triangle2D (const Quaternion &lookDirection, Transform *camT, Triangle3D *t, Material *material)
 Constructs a triangle from 3D data with a camera transformation and material.
 
 Triangle2D (Triangle3D *t)
 Constructs a triangle from a 3D triangle.
 
Vector2D GetP1 ()
 Gets the first vertex as a 2D point.
 
Vector2D GetP2 ()
 Gets the second vertex as a 2D point.
 
Vector2D GetP3 ()
 Gets the third vertex as a 2D point.
 
MaterialGetMaterial ()
 Gets the material assigned to the triangle.
 
bool DidIntersect (const float &x, const float &y, float &u, float &v, float &w)
 Checks if a point intersects the triangle using barycentric coordinates.
 
bool DidIntersect (BoundingBox2D *bbox)
 Checks if the triangle intersects a bounding box.
 
bool DidIntersectSAT (BoundingBox2D *bbox)
 Checks if the triangle intersects a bounding box using the Separating Axis Theorem (SAT).
 
String ToString ()
 Converts the triangle's data to a string representation.
 

Public Attributes

float denominator = 0.0f
 Precomputed denominator for barycentric coordinate calculations.
 
float p1X
 
float p1Y
 
float p2X
 
float p2Y
 
float p3X
 
float p3Y
 Coordinates of the triangle's vertices.
 
float v0X
 
float v0Y
 
float v1X
 
float v1Y
 
float v2X
 
float v2Y
 Edge vectors for barycentric coordinate calculations.
 
Vector2D min
 
Vector2D max
 Minimum and maximum bounds of the triangle.
 
Vector3Dnormal
 Normal vector of the triangle (if available).
 
Materialmaterial
 Material assigned to the triangle.
 
Vector3Dt3p1
 Pointer to the first vertex in 3D space.
 
Vector3Dt3p2
 Pointer to the second vertex in 3D space.
 
Vector3Dt3p3
 Pointer to the third vertex in 3D space.
 
const Vector2Dp1UV
 UV coordinates of the first vertex.
 
const Vector2Dp2UV
 UV coordinates of the second vertex.
 
const Vector2Dp3UV
 UV coordinates of the third vertex.
 
bool hasUV = false
 Indicates whether the triangle has UV mapping.
 
float averageDepth = 0.0f
 Average depth of the triangle for rendering.
 

Detailed Description

Represents a 2D triangle with support for UV mapping, depth, and intersection testing.

Definition at line 25 of file Triangle2D.h.

Constructor & Destructor Documentation

◆ Triangle2D() [1/4]

Default constructor.

Definition at line 3 of file Triangle2D.cpp.

◆ Triangle2D() [2/4]

Triangle2D ( const Vector2D p1,
const Vector2D p2,
const Vector2D p3 
)

Constructs a triangle from 2D vertices.

Parameters
p1First vertex.
p2Second vertex.
p3Third vertex.

Definition at line 5 of file Triangle2D.cpp.

References denominator, p1X, p1Y, p2X, p2Y, p3X, p3Y, v0X, v0Y, v1X, v1Y, Vector2D::X, and Vector2D::Y.

◆ Triangle2D() [3/4]

Triangle2D ( const Quaternion lookDirection,
Transform camT,
Triangle3D t,
Material material 
)

Constructs a triangle from 3D data with a camera transformation and material.

Parameters
lookDirectionCamera look direction as a quaternion.
camTCamera transform.
tSource 3D triangle.
materialMaterial to assign to the triangle.

Definition at line 21 of file Triangle2D.cpp.

References averageDepth, denominator, Transform::GetPosition(), Transform::GetRotation(), Transform::GetScale(), hasUV, Triangle3D::hasUV, material, max, min, Quaternion::Multiply(), normal, Triangle3D::Normal(), Triangle3D::p1, p1UV, Triangle3D::p1UV, p1X, p1Y, Triangle3D::p2, p2UV, Triangle3D::p2UV, p2X, p2Y, Triangle3D::p3, p3UV, Triangle3D::p3UV, p3X, p3Y, t3p1, t3p2, t3p3, Quaternion::UnrotateVector(), v0X, v0Y, v1X, v1Y, Vector2D::X, Vector3D::X, Vector2D::Y, Vector3D::Y, and Vector3D::Z.

◆ Triangle2D() [4/4]

Constructs a triangle from a 3D triangle.

Parameters
tSource 3D triangle.

Definition at line 79 of file Triangle2D.cpp.

References averageDepth, denominator, max, min, Triangle3D::p1, p1X, p1Y, Triangle3D::p2, p2X, p2Y, Triangle3D::p3, p3X, p3Y, t3p1, t3p2, t3p3, v0X, v0Y, v1X, v1Y, Vector2D::X, Vector3D::X, Vector2D::Y, Vector3D::Y, and Vector3D::Z.

Member Function Documentation

◆ DidIntersect() [1/2]

bool DidIntersect ( BoundingBox2D bbox)

Checks if the triangle intersects a bounding box.

Parameters
bboxBounding box to test against.
Returns
True if the triangle intersects the bounding box, otherwise false.

Definition at line 150 of file Triangle2D.cpp.

References max, min, and BoundingBox2D::Overlaps().

◆ DidIntersect() [2/2]

bool DidIntersect ( const float x,
const float y,
float u,
float v,
float w 
)

Checks if a point intersects the triangle using barycentric coordinates.

Parameters
xX-coordinate of the point.
yY-coordinate of the point.
uOutput barycentric coordinate u.
vOutput barycentric coordinate v.
wOutput barycentric coordinate w.
Returns
True if the point intersects the triangle, otherwise false.

Definition at line 136 of file Triangle2D.cpp.

References denominator, p1X, p1Y, v0X, v0Y, v1X, and v1Y.

Referenced by Node::Insert().

◆ DidIntersectSAT()

bool DidIntersectSAT ( BoundingBox2D bbox)

Checks if the triangle intersects a bounding box using the Separating Axis Theorem (SAT).

Parameters
bboxBounding box to test against.
Returns
True if the triangle intersects the bounding box, otherwise false.

Definition at line 155 of file Triangle2D.cpp.

References BoundingBox2D::GetMaximum(), BoundingBox2D::GetMinimum(), Mathematics::Max(), Mathematics::Min(), p1X, p1Y, p2X, p2Y, p3X, p3Y, Vector2D::X, and Vector2D::Y.

◆ GetMaterial()

Material * GetMaterial ( )

Gets the material assigned to the triangle.

Returns
Pointer to the material.

Definition at line 132 of file Triangle2D.cpp.

References material.

Referenced by Rasterizer::CheckRasterPixel(), and Rasterizer::CheckRasterPixelAccel().

◆ GetP1()

Vector2D GetP1 ( )

Gets the first vertex as a 2D point.

Returns
First vertex.

Definition at line 120 of file Triangle2D.cpp.

References p1X, and p1Y.

◆ GetP2()

Vector2D GetP2 ( )

Gets the second vertex as a 2D point.

Returns
Second vertex.

Definition at line 124 of file Triangle2D.cpp.

References p2X, and p2Y.

◆ GetP3()

Vector2D GetP3 ( )

Gets the third vertex as a 2D point.

Returns
Third vertex.

Definition at line 128 of file Triangle2D.cpp.

References p3X, and p3Y.

◆ ToString()

String ToString ( )

Converts the triangle's data to a string representation.

Returns
String representation of the triangle.

Definition at line 195 of file Triangle2D.cpp.

References p1X, p1Y, p2X, p2Y, p3X, p3Y, and Vector2D::ToString().

Member Data Documentation

◆ averageDepth

float averageDepth = 0.0f

Average depth of the triangle for rendering.

Definition at line 44 of file Triangle2D.h.

Referenced by Rasterizer::CheckRasterPixel(), Rasterizer::CheckRasterPixelAccel(), Triangle2D(), and Triangle2D().

◆ denominator

float denominator = 0.0f

Precomputed denominator for barycentric coordinate calculations.

Definition at line 27 of file Triangle2D.h.

Referenced by Rasterizer::CheckRasterPixelAccel(), DidIntersect(), Triangle2D(), Triangle2D(), and Triangle2D().

◆ hasUV

bool hasUV = false

Indicates whether the triangle has UV mapping.

Definition at line 43 of file Triangle2D.h.

Referenced by Triangle2D().

◆ material

Material* material

Material assigned to the triangle.

Definition at line 33 of file Triangle2D.h.

Referenced by GetMaterial(), and Triangle2D().

◆ max

Vector2D max

Minimum and maximum bounds of the triangle.

Definition at line 30 of file Triangle2D.h.

Referenced by DidIntersect(), Triangle2D(), and Triangle2D().

◆ min

Vector2D min

Definition at line 30 of file Triangle2D.h.

Referenced by DidIntersect(), Triangle2D(), and Triangle2D().

◆ normal

Vector3D* normal

Normal vector of the triangle (if available).

Definition at line 32 of file Triangle2D.h.

Referenced by Triangle2D().

◆ p1UV

const Vector2D* p1UV

UV coordinates of the first vertex.

Definition at line 39 of file Triangle2D.h.

Referenced by Rasterizer::CheckRasterPixel(), Rasterizer::CheckRasterPixelAccel(), and Triangle2D().

◆ p1X

◆ p1Y

◆ p2UV

const Vector2D* p2UV

UV coordinates of the second vertex.

Definition at line 40 of file Triangle2D.h.

Referenced by Rasterizer::CheckRasterPixel(), Rasterizer::CheckRasterPixelAccel(), and Triangle2D().

◆ p2X

float p2X

Definition at line 28 of file Triangle2D.h.

Referenced by DidIntersectSAT(), GetP2(), ToString(), Triangle2D(), Triangle2D(), and Triangle2D().

◆ p2Y

float p2Y

Definition at line 28 of file Triangle2D.h.

Referenced by DidIntersectSAT(), GetP2(), ToString(), Triangle2D(), Triangle2D(), and Triangle2D().

◆ p3UV

const Vector2D* p3UV

UV coordinates of the third vertex.

Definition at line 41 of file Triangle2D.h.

Referenced by Rasterizer::CheckRasterPixel(), Rasterizer::CheckRasterPixelAccel(), and Triangle2D().

◆ p3X

float p3X

Definition at line 28 of file Triangle2D.h.

Referenced by DidIntersectSAT(), GetP3(), ToString(), Triangle2D(), Triangle2D(), and Triangle2D().

◆ p3Y

float p3Y

Coordinates of the triangle's vertices.

Definition at line 28 of file Triangle2D.h.

Referenced by DidIntersectSAT(), GetP3(), ToString(), Triangle2D(), Triangle2D(), and Triangle2D().

◆ t3p1

Vector3D* t3p1

Pointer to the first vertex in 3D space.

Definition at line 35 of file Triangle2D.h.

Referenced by Rasterizer::CheckRasterPixel(), Rasterizer::CheckRasterPixelAccel(), Triangle2D(), and Triangle2D().

◆ t3p2

Vector3D* t3p2

Pointer to the second vertex in 3D space.

Definition at line 36 of file Triangle2D.h.

Referenced by Triangle2D(), and Triangle2D().

◆ t3p3

Vector3D* t3p3

Pointer to the third vertex in 3D space.

Definition at line 37 of file Triangle2D.h.

Referenced by Rasterizer::CheckRasterPixel(), Rasterizer::CheckRasterPixelAccel(), Triangle2D(), and Triangle2D().

◆ v0X

◆ v0Y

◆ v1X

◆ v1Y

◆ v2X

float v2X

Definition at line 29 of file Triangle2D.h.

◆ v2Y

float v2Y

Edge vectors for barycentric coordinate calculations.

Definition at line 29 of file Triangle2D.h.


The documentation for this class was generated from the following files: