Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

2D triangle rasterizer

A triangle is defined by three sets of floating-point coordinates A pixel is defined as the product of two half-open sets [x,x+1)*[y,y+1) Thus any point is specifically inside one pixel

The aim of this rasterizer is to produce a VERY GOOD rasterization which is suitable for multiple purposes; thus, speed is less of a factor. Formally, instead of producing the set of pixels whose centers lie within the triangle, we produce a set of pixels which overlap the triangle AT ALL and, for each pixel, we produce a set of vertices which define the convex intersection of the pixel square and the triangle. This allows us to calculate the "correct" centroid-sampled position, calculate atlas masks, etc.

To ensure proper seaming between adjacent triangles, all edges are scan-converted from top to bottom.

Note: pixel coordinates are in usual "screen space" i.e. all coordinates positive, (0,0) is top-left

Classes

NameDescription
Geo::RastFragment

stores a fragment of a triangle i.e. a triangle clipped to a pixel box

Geo::RastTri

records details of the input triangle

Geo::RastVert

stores a rasterization vertex, including barycentric coordinates

Functions

NameDescription
GetArea()

get signed area of fragment polygon; it's flipped if area < 0

GetBarycentricCoords(float, float, float &, float &)

Gets the barycentric coords of the given point (x, y).

GetBarycentricCoords(float, float, float &, float &)

get barycentric coordinates of a point in the pixel

GetCentroid(RastVert &)

get centroid of fragment polygon (average of vertex positions)

IsPtInPoly(float, float)

is a given point in the fragment polygon?

RasterizeTriangle(float, float, float, float, float, float, RasterizationClient, void *)

rasterize a triangle

RasterizeWindingAgnosticTriangle(float, float, float, float, float, float, RasterizationClient, void *)

rasterize a triangle regardless of winding order

RastTri(float, float, float, float, float, float)

Constructor.

RastVert()

Constructors.

RastVert(float, float, float, float)

Constructors.

Variables

NameDescription
float m_dX2

x2-x1, y2-y1

float m_dX3

x3-x1, y3-y1

float m_dY2

x2-x1, y2-y1

float m_dY3

x3-x1, y3-y1

Geo::u32 m_NumPts

number of points in fragment polygon

const RastVert * m_Points

fragment polygon points; coordinates are in [0,1]x[0,1] i.e.

const RastTri * m_Tri

reference to triangle that owns this fragment

float m_U

Barycentric coordinates.

float m_V

Barycentric coordinates.

float m_X

Vertex position.

Geo::s32 m_X

integer coordinates of (top-left of) pixel

float m_X1

x1, y1

float m_Y

Vertex position.

Geo::s32 m_Y

integer coordinates of (top-left of) pixel

float m_Y1

x1, y1

Typedefs

NameDescription
void(* RasterizationClient)(const RastFragment *fragment, void *user)

callback function for rasterization


Anchor
a732ca24ecf5dfa9681975b332067fd64
a732ca24ecf5dfa9681975b332067fd64

float Geo::RastFragment::GetArea

...

public: float GetArea() const

...

get signed area of fragment polygon; it's flipped if area < 0


Anchor
a11602daf89f124258458c9e9b212e696
a11602daf89f124258458c9e9b212e696

void Geo::RastTri::GetBarycentricCoords

...

public: void GetBarycentricCoords
(
    float x,
    float y,
    float & u,
    float & v
) const

...

Gets the barycentric coords of the given point (x, y).


Anchor
a08b44a83beca802facdb85d4acd0554b
a08b44a83beca802facdb85d4acd0554b

void Geo::RastFragment::GetBarycentricCoords

...

public: void GetBarycentricCoords
(
    float x,
    float y,
    float & u,
    float & v
) const

...

get barycentric coordinates of a point in the pixel


Anchor
a19cfa9fbee24386df0449437de91559d
a19cfa9fbee24386df0449437de91559d

void Geo::RastFragment::GetCentroid

...

public: void GetCentroid
(
    RastVert & out
) const

...

get centroid of fragment polygon (average of vertex positions)


Anchor
a47d2a5bbe1dc7a8197fbb6aef9c0e6c3
a47d2a5bbe1dc7a8197fbb6aef9c0e6c3

bool Geo::RastFragment::IsPtInPoly

...

public: bool IsPtInPoly
(
    float x,
    float y
) const

...

is a given point in the fragment polygon?


Anchor
abc580bbd3b111fddd941b1be0ca4e6dc
abc580bbd3b111fddd941b1be0ca4e6dc

void Geo::RasterizeTriangle

...

public: void RasterizeTriangle
(
    float x1,
    float y1,
    float x2,
    float y2,
    float x3,
    float y3,
    RasterizationClient emitter,
    void * user
)

...

rasterize a triangle


Anchor
a61df9d8b888597df1c53e4e6d7a0705c
a61df9d8b888597df1c53e4e6d7a0705c

void Geo::RasterizeWindingAgnosticTriangle

...

public: void RasterizeWindingAgnosticTriangle
(
    float x1,
    float y1,
    float x2,
    float y2,
    float x3,
    float y3,
    RasterizationClient emitter,
    void * user
)

...

rasterize a triangle regardless of winding order


Anchor
ac1183ee52feb8d04bb7b2e6b9e750634
ac1183ee52feb8d04bb7b2e6b9e750634

Geo::RastTri::RastTri

...

public: RastTri
(
    float x1,
    float y1,
    float x2,
    float y2,
    float x3,
    float y3
)

...

Constructor.


Anchor
a6e7f547fdaf03c2ec2ba3035a399b5db
a6e7f547fdaf03c2ec2ba3035a399b5db

Geo::RastVert::RastVert

...

public: RastVert()

...

Constructors.


Anchor
ad41d85ad8c3087beaf1af62a28e63d3e
ad41d85ad8c3087beaf1af62a28e63d3e

Geo::RastVert::RastVert

...

public: RastVert
(
    float x,
    float y,
    float u,
    float v
)

...

Constructors.