UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Raster.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 Raster.h: Generic triangle rasterization code.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
10
11//
12// FTriangleRasterizer - A generic 2d triangle rasterizer which linearly interpolates vertex parameters and calls a virtual function for each pixel.
13//
14
15template<class RasterPolicyType> class FTriangleRasterizer : public RasterPolicyType
16{
17public:
18
19 typedef typename RasterPolicyType::InterpolantType InterpolantType;
20
21 void DrawTriangle(const InterpolantType& I0, const InterpolantType& I1, const InterpolantType& I2, const FVector2D& P0, const FVector2D& P1, const FVector2D& P2, bool BackFacing)
22 {
24 FVector2f Points[3] = { FVector2f(P0), FVector2f(P1), FVector2f(P2) };
25
26 // Find the top point.
27
28 if (Points[1].Y < Points[0].Y && Points[1].Y <= Points[2].Y)
29 {
30 Exchange(Points[0], Points[1]);
32 }
33 else if (Points[2].Y < Points[0].Y && Points[2].Y <= Points[1].Y)
34 {
35 Exchange(Points[0], Points[2]);
37 }
38
39 // Find the bottom point.
40
41 if (Points[1].Y > Points[2].Y)
42 {
43 Exchange(Points[2], Points[1]);
45 }
46
47 // Avoid any division by zero
48 float PointDiffY_1_0 = (Points[1].Y - Points[0].Y);
50 {
52 }
53 float PointDiffY_2_0 = (Points[2].Y - Points[0].Y);
55 {
57 }
58 float PointDiffY_2_1 = (Points[2].Y - Points[1].Y);
60 {
62 }
63
64 // Calculate the edge gradients.
65
66 float TopMinDiffX = (Points[1].X - Points[0].X) / PointDiffY_1_0,
67 TopMaxDiffX = (Points[2].X - Points[0].X) / PointDiffY_2_0;
70
71 float BottomMinDiffX = (Points[2].X - Points[1].X) / PointDiffY_2_1,
72 BottomMaxDiffX = (Points[2].X - Points[0].X) / PointDiffY_2_0;
75
76 DrawTriangleTrapezoid(
77 Interpolants[0],
79 Interpolants[0],
81 Points[0].X,
83 Points[0].X,
85 Points[0].Y,
86 Points[1].Y,
88 );
89
90 DrawTriangleTrapezoid(
91 Interpolants[1],
95 Points[1].X,
97 Points[0].X + TopMaxDiffX * PointDiffY_1_0,
99 Points[1].Y,
100 Points[2].Y,
102 );
103 }
104
105 FTriangleRasterizer(const RasterPolicyType& InRasterPolicy): RasterPolicyType(InRasterPolicy) {}
106
107private:
108
109 void DrawTriangleTrapezoid(
114 float TopMinX,
115 float DeltaMinX,
116 float TopMaxX,
117 float DeltaMaxX,
118 float InMinY,
119 float InMaxY,
120 bool BackFacing
121 )
122 {
123 int32 IntMinY = FMath::Clamp(FMath::CeilToInt(InMinY),RasterPolicyType::GetMinY(),RasterPolicyType::GetMaxY() + 1),
124 IntMaxY = FMath::Clamp(FMath::CeilToInt(InMaxY),RasterPolicyType::GetMinY(),RasterPolicyType::GetMaxY() + 1);
125
126 for(int32 IntY = IntMinY;IntY < IntMaxY;IntY++)
127 {
128 float Y = IntY - InMinY;
129 float LocalMinX = TopMinX + DeltaMinX * Y;
130 float LocalMaxX = TopMaxX + DeltaMaxX * Y;
133
134 if(LocalMinX > LocalMaxX)
135 {
138 }
139
140 if(LocalMaxX > LocalMinX)
141 {
142 int32 IntMinX = FMath::Clamp(FMath::CeilToInt(LocalMinX),RasterPolicyType::GetMinX(),RasterPolicyType::GetMaxX() + 1),
143 IntMaxX = FMath::Clamp(FMath::CeilToInt(LocalMaxX),RasterPolicyType::GetMinX(),RasterPolicyType::GetMaxX() + 1);
145
146 for(int32 X = IntMinX;X < IntMaxX;X++)
147 {
148 RasterPolicyType::ProcessPixel(X,IntY,MinInterpolant + DeltaInterpolant * (X - LocalMinX),BackFacing);
149 }
150 }
151 }
152 }
153};
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define X(Name, Desc)
Definition FormatStringSan.h:47
UE::Math::TVector2< float > FVector2f
Definition MathFwd.h:74
#define UE_SMALL_NUMBER
Definition UnrealMathUtility.h:130
UE_REWRITE constexpr void Exchange(T &A, T &B)
Definition UnrealTemplate.h:627
Definition Raster.h:16
FTriangleRasterizer(const RasterPolicyType &InRasterPolicy)
Definition Raster.h:105
void DrawTriangle(const InterpolantType &I0, const InterpolantType &I1, const InterpolantType &I2, const FVector2D &P0, const FVector2D &P1, const FVector2D &P2, bool BackFacing)
Definition Raster.h:21
RasterPolicyType::InterpolantType InterpolantType
Definition Raster.h:19
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
static UE_FORCEINLINE_HINT bool IsNearlyZero(float Value, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:407
T Y
Definition Vector2D.h:52
T X
Definition Vector2D.h:49