|
scimesh 0.3.4
Headless CPU-only 3D software renderer for scientific mesh visualization
|
Low-level triangle rasterizer with depth buffering and lighting. More...
#include <rasterizer.h>

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). | |
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:
You typically don't create a Rasterizer directly; it is managed internally by Renderer. However, for advanced use:
Definition at line 45 of file rasterizer.h.
Construct a rasterizer for the given output size.
Allocates depth and normal buffers.
| w | Output width in pixels. |
| h | Output height in pixels. |
Definition at line 12 of file rasterizer.cpp.
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.
| [in,out] | output | The 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.
| [in,out] | output | The rendered image to darken. |
| z_near | Near plane distance of the camera. | |
| z_far | Far plane distance of the camera. |
Definition at line 410 of file rasterizer.cpp.
Clear the depth and normal buffers.
Must be called at the start of each frame.
| clear_depth | Initial depth value (default: 1.0 = farthest). |
Definition at line 15 of file rasterizer.cpp.
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):
d = (2*n*f) / (f + n - z_ndc * (f - n))d = (z_ndc * (f - n) + f + n) / 2where n = z_near and f = z_far. Both formulas are exact, because the depth buffer stores z_ndc for both projection types.
| z_ndc | Depth-buffer value (normalized device depth, [-1, 1]; -1 = near plane, +1 = far plane). |
Definition at line 49 of file rasterizer.cpp.
| 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).
| screen_v0,screen_v1 | Screen-space endpoints (x, y, depth). | |
| color0,color1 | Colors at the two endpoints. | |
| width | Line width in device pixels (the caller scales by the supersampling factor, exactly like the radius of rasterize_point()). | |
| lit | Whether to shade the line; false draws the flat color (the default for lines). | |
| normal | Surface normal, used only when lit is true. | |
| light_direction | Light direction, used only when lit is true. | |
| [in,out] | output | The output image. |
Definition at line 349 of file rasterizer.cpp.
| 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.
| screen_x,screen_y | Screen-space center position. | |
| depth | Depth value for z-buffering. | |
| radius | Circle radius in screen pixels. | |
| color | Point color. | |
| normal | Surface normal (for lighting). | |
| light_direction | Light direction for shading. | |
| [in,out] | output | The output image. |
Definition at line 328 of file rasterizer.cpp.
| 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:
smooth_shading is false)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.
| screen_v0,screen_v1,screen_v2 | Screen-space vertex positions. | |
| color0,color1,color2 | Per-vertex colors. | |
| normal0,normal1,normal2 | Per-vertex normals (view space). | |
| uv0,uv1,uv2 | Per-vertex texture coordinates. | |
| backface_culling | Whether to cull backfaces. | |
| smooth_shading | Whether to interpolate normals. | |
| light_direction | Light direction for shading. | |
| wireframe | Whether to draw wireframe edges. | |
| wireframe_color | Color for wireframe edges. | |
| [in,out] | output | The output image to draw into. |
Definition at line 162 of file rasterizer.cpp.
Enable or disable alpha blending.
| enabled | If true, transparent fragments blend with the existing pixel color. |
Definition at line 218 of file rasterizer.h.
Optional texture image for textured meshes.
Set this before rasterizing textured triangles. The triangle's UV coordinates are used to sample this image.
Definition at line 331 of file rasterizer.h.
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.
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.
| float scimesh::Rasterizer::contrast = 1.0f |
Contrast adjustment (1.0 = no change).
Definition at line 120 of file rasterizer.h.
| Color scimesh::Rasterizer::fog_color = TRANSPARENT_BLACK |
The fog color (what distant objects blend into).
Definition at line 161 of file rasterizer.h.
Enable depth fog (default: false).
Definition at line 128 of file rasterizer.h.
| 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.
| 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.
Definition at line 144 of file rasterizer.h.
| 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.
| int scimesh::Rasterizer::height = 0 |
Output image height in pixels.
Definition at line 53 of file rasterizer.h.
| std::vector<Light> scimesh::Rasterizer::lights |
Light sources for Blinn-Phong shading.
If empty, a default directional light from +Z is used.
Definition at line 107 of file rasterizer.h.
| 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.
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.
| 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 highlight color.
Set to a non-transparent color to enable specular highlights.
Definition at line 91 of file rasterizer.h.
Enable SSAO (default: false).
Definition at line 169 of file rasterizer.h.
SSAO darkening intensity (0.0–1.0, default: 0.8).
Definition at line 175 of file rasterizer.h.
| float scimesh::Rasterizer::ssao_radius = 16.0f |
SSAO sample radius in pixels (default: 16).
Definition at line 172 of file rasterizer.h.
| int scimesh::Rasterizer::width = 0 |
Output image width in pixels.
Definition at line 50 of file rasterizer.h.
| 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.
| 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.
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.