| TGX 1.1.4 A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.

Loading...
Searching...
No Matches
Macros | Functions
Rasterizer.h File Reference

3D triangle rasterizer function. More...

`#include "ShaderParams.h"`

Go to the source code of this file.

## Macros
#define | TGX_RASTERIZE_SUBPIXEL_BITS (6)
| Sub-pixel precision bits. More...

## Functions
TGX_INLINE void | tgx::minmax3_i32 (const int32_t a, const int32_t b, const int32_t c, int32_t &mn, int32_t &mx)
TGX_INLINE int32_t | tgx::tgx_floor_div_subpixel_i32 (const int32_t x)
TGX_INLINE int32_t | tgx::tgx_ceil_div_subpixel_i32 (const int32_t x)
template<auto shader_function, typename RASTERIZER_PARAMS >
TGX_RASTERIZE_TRIANGLE_INLINE void | tgx::rasterizeTriangle (const int LX, const int LY, const RasterizerVec4 &V0, const RasterizerVec4 &V1, const RasterizerVec4 &V2, const int32_t offset_x, const int32_t offset_y, const RASTERIZER_PARAMS &data)
| Fast triangle rasterizer for 3D graphics: More...

## Detailed Description

3D triangle rasterizer function.

## Macro Definition Documentation

## ◆ TGX_RASTERIZE_SUBPIXEL_BITS

#define TGX_RASTERIZE_SUBPIXEL_BITS (6)

Sub-pixel precision bits.

Value should range between 1 and 8. Larger value provide greater resolution and smoother animation but at the expense of the maximum viewport size:

subpixel bits | max viewport size LX*LY
--- | ---
8 | 2048 x 2048
6 | 4096 x 4096
4 | 8192 x 8192
2 | 16384 x 16384

## Function Documentation

## ◆ rasterizeTriangle()

template<auto shader_function, typename RASTERIZER_PARAMS >

TGX_RASTERIZE_TRIANGLE_INLINE void tgx::rasterizeTriangle | ( | const int | LX,
|  | const int | LY,
|  | const RasterizerVec4 & | V0,
|  | const RasterizerVec4 & | V1,
|  | const RasterizerVec4 & | V2,
|  | const int32_t | offset_x,
|  | const int32_t | offset_y,
|  | const RASTERIZER_PARAMS & | data
| ) |  |
| inline

Fast triangle rasterizer for 3D graphics:

Features

- Pixel perfect rasterization with adjustable subpixels from 2 to 8 bits (set with `TGX_RASTERIZE_SUBPIXEL_BITS`).
- Top-left rule to prevent drawing pixels twice.
- Tile rasterization: large viewport can be splitted in multiple sub-images.
- templated shader functions so to implement: z-buffer testing, shading, texturing...

Template Parameters

  shader_function | the shader function to call.
  RASTERIZER_PARAMS | the type of the object containing the 'uniform' data (ie data not specific to a vertex).

Parameters

  LX,LY | Viewport size. The image itself may be smaller than the viewport and an offset may be specified so it is possible to draw the whole viewport in 'tile" mode be calling this method several times with different offsets. The maximum viewport size depend on `TGX_RASTERIZE_SUBPIXEL_BITS`
  V0,V1,V2 | Normalized coordinates of the vertices of the triangle (x,y,z,w) where, 'a la opengl' the viewport is mapped to [-1, 1]^2. These vectors also optionally contain the 'varying' parameters associated with each vertex, namely the texture coords and the color associated with each vertex (when applicable) that are used by the shader function.
  offset_x,offset_y | Offset of this image inside the viewport. So the image corresponds to to the box `[offset_x, offset_x + im.width[x[offset_x, offset_x + im.height[` and only the intersection of this box with the viewport box `[0, LX[x[0, LY[` is drawn onto the image.
  data | 'Uniform' parameters (depending on the rasterization type).

Remarks

- Maximum viewport size depending on `TGX_RASTERIZE_SUBPIXEL_BITS`:
  | subpixel bits | max viewport size LX*LY |
  |---------------|-------------------------|
  | 8 | 2048 x 2048 |
  | 6 | 4096 x 4096 |
  | 4 | 8192 x 8192 |
  | 2 | 16384 x 16384 |
- The `(x,y)` coordinates of the vertices `V0,V1,V2` do not need to be inside the viewport `[-1,1]^2` and yet the triangle will still be perfectly rasterized provided that they are not 'too far away'. This 'too far away' correspond to the maximum viewport size according to the chosen sub-pixel precision (for instance, [-2,2]^2 will work any viewport at most 1024x1024 when using 8 bits precision).
- Color are passed in RGBf format irrespectively of the image color type to improve quality and simplify handling of different image types.