scimesh 0.3.4
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
scimesh::Rasterizer Struct Reference

Low-level triangle rasterizer with depth buffering and lighting. More...

#include <rasterizer.h>

Collaboration diagram for scimesh::Rasterizer:

Public Member Functions

 Rasterizer (int w, int h)
 Construct a rasterizer for the given output size.
 
void clear (float clear_depth=1.0f)
 Clear the depth and normal buffers.
 
float fog_depth_from_ndc (float z_ndc) const
 Convert a depth-buffer value to a distance in world units.
 
void set_blend_mode (bool enabled)
 Enable or disable alpha blending.
 
void rasterize_triangle (const Vec3 &screen_v0, const Color &color0, const Vec3 &normal0, const Vec2 &uv0, const Vec3 &screen_v1, const Color &color1, const Vec3 &normal1, const Vec2 &uv1, const Vec3 &screen_v2, const Color &color2, const Vec3 &normal2, const Vec2 &uv2, bool backface_culling, bool smooth_shading, const Vec3 &light_direction, bool wireframe, const Color &wireframe_color, Image &output)
 Rasterize a single triangle into the output image.
 
void rasterize_point (float screen_x, float screen_y, float depth, float radius, const Color &color, const Vec3 &normal, const Vec3 &light_direction, Image &output)
 Rasterize a single point (filled circle) into the output image.
 
void rasterize_line (const Vec3 &screen_v0, const Color &color0, const Vec3 &screen_v1, const Color &color1, float width, bool lit, const Vec3 &normal, const Vec3 &light_direction, Image &output)
 Rasterize a line segment with a screen-space width.
 
void apply_ssao (Image &output, float z_near, float z_far)
 Apply screen-space ambient occlusion to the output image.
 

Public Attributes

Image * active_texture = nullptr
 Optional texture image for textured meshes.
 
Output dimensions
int width = 0
 Output image width in pixels.
 
int height = 0
 Output image height in pixels.
 
Depth and normal buffers
std::vector< float > z_buffer
 Z-buffer (depth buffer), one float per pixel.
 
std::vector< Vec3 > normal_buffer
 Normal buffer, one Vec3 per pixel.
 
Blending
bool blend_mode = false
 Enable alpha blending for transparent triangles (default: false).
 
Specular lighting
Color specular_color = Color(0.0f, 0.0f, 0.0f, 0.0f)
 Specular highlight color.
 
float shininess = 0.0f
 Shininess exponent (Phong model).
 
Lights
std::vector< Light > lights
 Light sources for Blinn-Phong shading.
 
float ambient = 0.3f
 Ambient light level (0.0–1.0, default: 0.3).
 
Post-processing
float contrast = 1.0f
 Contrast adjustment (1.0 = no change).
 
Fog
bool fog_enabled = false
 Enable depth fog (default: false).
 
float fog_start = 0.0f
 Distance where fog begins (default: 0).
 
float fog_end = 1.0f
 Distance where fog is fully opaque (default: 1).
 
FogSpace fog_space = FogSpace::WORLD
 The space (and unit) of fog_start / fog_end (default: FogSpace::WORLD).
 
float z_near = 0.1f
 Near plane distance of the current projection (default: 0.1).
 
float z_far = 10000.0f
 Far plane distance of the current projection (default: 10000.0).
 
bool orthographic = false
 Whether the current projection is orthographic (default: false).
 
Color fog_color = TRANSPARENT_BLACK
 The fog color (what distant objects blend into).
 
SSAO (Screen-Space Ambient Occlusion)
bool ssao_enabled = false
 Enable SSAO (default: false).
 
float ssao_radius = 16.0f
 SSAO sample radius in pixels (default: 16).
 
float ssao_intensity = 0.8f
 SSAO darkening intensity (0.0–1.0, default: 0.8).
 

Detailed Description

Low-level triangle rasterizer with depth buffering and lighting.

The Rasterizer is the heart of the rendering pipeline. It takes triangles in screen space (after all transforms have been applied) and fills in the corresponding pixels, applying:

  • Z-buffering (depth testing): only the closest surface at each pixel is kept.
  • Blinn-Phong shading: per-pixel lighting with ambient, diffuse, and specular components.
  • Texture mapping: bilinear sampling from an optional texture image.
  • Fog: linear depth fog for atmospheric effects.
  • SSAO: screen-space ambient occlusion for crevice darkening.

Usage

You typically don't create a Rasterizer directly; it is managed internally by Renderer. However, for advanced use:

Rasterizer rast(800, 600);
rast.lights.push_back(Light{Vec3(0,0,1), Color(1,1,1), 1.0f, true});
rast.ambient = 0.4f;
rast.clear(); // clear depth and normal buffers
// ... feed triangles via rasterize_triangle() ...
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
An RGBA color with floating-point components.
Definition types.h:88
A light source for the Blinn-Phong shading model.
Definition types.h:179
Low-level triangle rasterizer with depth buffering and lighting.
Definition rasterizer.h:45
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
See also
Renderer, Mesh, RenderOptions

Definition at line 45 of file rasterizer.h.

Constructor & Destructor Documentation

◆ Rasterizer()

scimesh::Rasterizer::Rasterizer ( int  w,
int  h 
)

Construct a rasterizer for the given output size.

Allocates depth and normal buffers.

Parameters
wOutput width in pixels.
hOutput height in pixels.

Definition at line 12 of file rasterizer.cpp.

Member Function Documentation

◆ apply_ssao()

void scimesh::Rasterizer::apply_ssao ( Image &  output,
float  z_near,
float  z_far 
)

Apply screen-space ambient occlusion to the output image.

Uses the depth and normal buffers to darken crevices and corners. Must be called after all triangles have been rasterized.

Parameters
[in,out]outputThe image to modify.

Apply screen-space ambient occlusion to the output image.

Uses the depth and normal buffers filled by rasterize_triangle() to darken creases and contact areas. Requires ssao_enabled.

The depth buffer holds normalized device depth ([-1, 1], see ndc_to_screen()); the matching camera planes have to be passed here so that distances in world units can be recovered (the same inversion that world-space fog uses). Set orthographic when the scene was rendered with a parallel projection.

Parameters
[in,out]outputThe rendered image to darken.
z_nearNear plane distance of the camera.
z_farFar plane distance of the camera.
See also
fog_depth_from_ndc(), ssao_enabled

Definition at line 410 of file rasterizer.cpp.

◆ clear()

void scimesh::Rasterizer::clear ( float  clear_depth = 1.0f)

Clear the depth and normal buffers.

Must be called at the start of each frame.

Parameters
clear_depthInitial depth value (default: 1.0 = farthest).

Definition at line 15 of file rasterizer.cpp.

◆ fog_depth_from_ndc()

float scimesh::Rasterizer::fog_depth_from_ndc ( float  z_ndc) const

Convert a depth-buffer value to a distance in world units.

Used for world-space fog (FogSpace::WORLD): the rasterizer only has the normalized device depth of a fragment, so the corresponding distance from the camera is recovered with the inverse of the projection (perspective or orthographic, selected by orthographic):

  • perspective: d = (2*n*f) / (f + n - z_ndc * (f - n))
  • orthographic: d = (z_ndc * (f - n) + f + n) / 2

where n = z_near and f = z_far. Both formulas are exact, because the depth buffer stores z_ndc for both projection types.

Parameters
z_ndcDepth-buffer value (normalized device depth, [-1, 1]; -1 = near plane, +1 = far plane).
Returns
Distance from the camera in world units, measured along the viewing direction.
See also
FogSpace, fog_space

Definition at line 49 of file rasterizer.cpp.

◆ rasterize_line()

void scimesh::Rasterizer::rasterize_line ( const Vec3 &  screen_v0,
const Color &  color0,
const Vec3 &  screen_v1,
const Color &  color1,
float  width,
bool  lit,
const Vec3 &  normal,
const Vec3 &  light_direction,
Image &  output 
)

Rasterize a line segment with a screen-space width.

The segment is drawn as a series of stamps along its dominant screen axis (DDA), with linearly interpolated color and depth. This is the counterpart of rasterize_point() for line layers and is used by the renderer for LineLayer geometry, which has no world-space thickness.

Depth testing, fog, contrast and alpha blending behave exactly like for triangles and points, so lines can be occluded by meshes and vice versa. Zero-length segments degenerate to a single stamp (a dot).

Parameters
screen_v0,screen_v1Screen-space endpoints (x, y, depth).
color0,color1Colors at the two endpoints.
widthLine width in device pixels (the caller scales by the supersampling factor, exactly like the radius of rasterize_point()).
litWhether to shade the line; false draws the flat color (the default for lines).
normalSurface normal, used only when lit is true.
light_directionLight direction, used only when lit is true.
[in,out]outputThe output image.

Definition at line 349 of file rasterizer.cpp.

◆ rasterize_point()

void scimesh::Rasterizer::rasterize_point ( float  screen_x,
float  screen_y,
float  depth,
float  radius,
const Color &  color,
const Vec3 &  normal,
const Vec3 &  light_direction,
Image &  output 
)

Rasterize a single point (filled circle) into the output image.

The point is drawn as a circle of the given radius (in screen pixels). Depth testing is performed at the circle center.

Parameters
screen_x,screen_yScreen-space center position.
depthDepth value for z-buffering.
radiusCircle radius in screen pixels.
colorPoint color.
normalSurface normal (for lighting).
light_directionLight direction for shading.
[in,out]outputThe output image.

Definition at line 328 of file rasterizer.cpp.

◆ rasterize_triangle()

void scimesh::Rasterizer::rasterize_triangle ( const Vec3 &  screen_v0,
const Color &  color0,
const Vec3 &  normal0,
const Vec2 &  uv0,
const Vec3 &  screen_v1,
const Color &  color1,
const Vec3 &  normal1,
const Vec2 &  uv1,
const Vec3 &  screen_v2,
const Color &  color2,
const Vec3 &  normal2,
const Vec2 &  uv2,
bool  backface_culling,
bool  smooth_shading,
const Vec3 &  light_direction,
bool  wireframe,
const Color &  wireframe_color,
Image &  output 
)

Rasterize a single triangle into the output image.

The triangle vertices are in screen space (pixel coordinates with depth). This function performs:

  • Bounding-box culling
  • Barycentric interpolation of color, normal, UV, and depth
  • Z-buffer depth testing
  • Backface culling (if enabled)
  • Per-pixel shading (or flat shading if smooth_shading is false)
  • Wireframe edge drawing (if enabled)

Triangles are shaded two-sided: a fragment whose back side is visible (positive screen-space area) and whose normal points away from the camera (normal.z < 0, i.e. the normals are expected in view space) is lit with the normal flipped towards the viewer. Coincident front/back face pairs therefore shade identically, independently of which of the two wins the depth test.

Parameters
screen_v0,screen_v1,screen_v2Screen-space vertex positions.
color0,color1,color2Per-vertex colors.
normal0,normal1,normal2Per-vertex normals (view space).
uv0,uv1,uv2Per-vertex texture coordinates.
backface_cullingWhether to cull backfaces.
smooth_shadingWhether to interpolate normals.
light_directionLight direction for shading.
wireframeWhether to draw wireframe edges.
wireframe_colorColor for wireframe edges.
[in,out]outputThe output image to draw into.

Definition at line 162 of file rasterizer.cpp.

◆ set_blend_mode()

void scimesh::Rasterizer::set_blend_mode ( bool  enabled)
inline

Enable or disable alpha blending.

Parameters
enabledIf true, transparent fragments blend with the existing pixel color.

Definition at line 218 of file rasterizer.h.

Member Data Documentation

◆ active_texture

Image* scimesh::Rasterizer::active_texture = nullptr

Optional texture image for textured meshes.

Set this before rasterizing textured triangles. The triangle's UV coordinates are used to sample this image.

See also
Image, Mesh::texture

Definition at line 331 of file rasterizer.h.

◆ ambient

float scimesh::Rasterizer::ambient = 0.3f

Ambient light level (0.0–1.0, default: 0.3).

Uniform fill light that prevents completely black shadows.

Definition at line 112 of file rasterizer.h.

◆ blend_mode

bool scimesh::Rasterizer::blend_mode = false

Enable alpha blending for transparent triangles (default: false).

When enabled, transparent fragments blend with the existing pixel color instead of overwriting it. When disabled, only fully opaque fragments are drawn (alpha is ignored after the depth test).

Definition at line 81 of file rasterizer.h.

◆ contrast

float scimesh::Rasterizer::contrast = 1.0f

Contrast adjustment (1.0 = no change).

Definition at line 120 of file rasterizer.h.

◆ fog_color

Color scimesh::Rasterizer::fog_color = TRANSPARENT_BLACK

The fog color (what distant objects blend into).

Definition at line 161 of file rasterizer.h.

◆ fog_enabled

bool scimesh::Rasterizer::fog_enabled = false

Enable depth fog (default: false).

Definition at line 128 of file rasterizer.h.

◆ fog_end

float scimesh::Rasterizer::fog_end = 1.0f

Distance where fog is fully opaque (default: 1).

Interpreted in the space given by fog_space.

Definition at line 138 of file rasterizer.h.

◆ fog_space

FogSpace scimesh::Rasterizer::fog_space = FogSpace::WORLD

The space (and unit) of fog_start / fog_end (default: FogSpace::WORLD).

If FogSpace::WORLD, the values are converted from the depth buffer using z_near, z_far and orthographic.

See also
FogSpace

Definition at line 144 of file rasterizer.h.

◆ fog_start

float scimesh::Rasterizer::fog_start = 0.0f

Distance where fog begins (default: 0).

Interpreted in the space given by fog_space.

Definition at line 133 of file rasterizer.h.

◆ height

int scimesh::Rasterizer::height = 0

Output image height in pixels.

Definition at line 53 of file rasterizer.h.

◆ lights

std::vector<Light> scimesh::Rasterizer::lights

Light sources for Blinn-Phong shading.

If empty, a default directional light from +Z is used.

See also
Light

Definition at line 107 of file rasterizer.h.

◆ normal_buffer

std::vector<Vec3> scimesh::Rasterizer::normal_buffer

Normal buffer, one Vec3 per pixel.

Stores the surface normal at each pixel for SSAO computations.

Definition at line 69 of file rasterizer.h.

◆ orthographic

bool scimesh::Rasterizer::orthographic = false

Whether the current projection is orthographic (default: false).

Affects the depth-buffer -> world-units conversion for world-space fog.

Definition at line 158 of file rasterizer.h.

◆ shininess

float scimesh::Rasterizer::shininess = 0.0f

Shininess exponent (Phong model).

Higher values = sharper highlights. Typical range: 16–128.

Definition at line 96 of file rasterizer.h.

◆ specular_color

Color scimesh::Rasterizer::specular_color = Color(0.0f, 0.0f, 0.0f, 0.0f)

Specular highlight color.

Set to a non-transparent color to enable specular highlights.

Definition at line 91 of file rasterizer.h.

◆ ssao_enabled

bool scimesh::Rasterizer::ssao_enabled = false

Enable SSAO (default: false).

Definition at line 169 of file rasterizer.h.

◆ ssao_intensity

float scimesh::Rasterizer::ssao_intensity = 0.8f

SSAO darkening intensity (0.0–1.0, default: 0.8).

Definition at line 175 of file rasterizer.h.

◆ ssao_radius

float scimesh::Rasterizer::ssao_radius = 16.0f

SSAO sample radius in pixels (default: 16).

Definition at line 172 of file rasterizer.h.

◆ width

int scimesh::Rasterizer::width = 0

Output image width in pixels.

Definition at line 50 of file rasterizer.h.

◆ z_buffer

std::vector<float> scimesh::Rasterizer::z_buffer

Z-buffer (depth buffer), one float per pixel.

Stores the depth of the closest surface at each pixel. A new triangle only overwrites a pixel if its depth is closer.

Definition at line 64 of file rasterizer.h.

◆ z_far

float scimesh::Rasterizer::z_far = 10000.0f

Far plane distance of the current projection (default: 10000.0).

Only used to convert depth-buffer values to world units for world-space fog.

Definition at line 154 of file rasterizer.h.

◆ z_near

float scimesh::Rasterizer::z_near = 0.1f

Near plane distance of the current projection (default: 0.1).

Only used to convert depth-buffer values to world units for world-space fog.

Definition at line 149 of file rasterizer.h.


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