scimesh 0.3.2
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.
 
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 apply_ssao (Image &output, float z_near, float z_far)
 Apply screen-space ambient occlusion to the output image.
 

Public Attributes

Imageactive_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< Vec3normal_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< Lightlights
 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.
 
float fog_end = 1.0f
 Distance where fog is fully opaque.
 
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
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.
z_nearNear plane distance.
z_farFar plane distance.

Definition at line 206 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.

◆ 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 183 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)
Parameters
screen_v0,screen_v1,screen_v2Screen-space vertex positions.
color0,color1,color2Per-vertex colors.
normal0,normal1,normal2Per-vertex normals.
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 78 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 174 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 240 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 137 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.

Definition at line 134 of file rasterizer.h.

◆ fog_start

float scimesh::Rasterizer::fog_start = 0.0f

Distance where fog begins.

Definition at line 131 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.

◆ 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 145 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 151 of file rasterizer.h.

◆ ssao_radius

float scimesh::Rasterizer::ssao_radius = 16.0f

SSAO sample radius in pixels (default: 16).

Definition at line 148 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.


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