Skip to main content

Module raster

Module raster 

Source
Expand description

A software rasterizer for 3D model thumbnails, as Eagle makes them at import: the model is drawn in the viewer’s home view on a 1080 square with a transparent background, the empty border is cropped away and 40 transparent pixels are added on every side.

The view is the viewer’s: Y up, the eye in direction (-1.5, 2, 3) from the centre of the bounding sphere, 45 degrees of vertical field of view, at the distance where the sphere fills the view. Lighting is geom::shade with the light at the eye.

Triangles are filled with fixed-point edge functions (a consistent tie-break rule, so shared edges leave neither cracks nor double hits) into a 2x supersampled z-buffer, which is then box-filtered down with premultiplied alpha so the silhouette is antialiased.

Textured meshes sample their picture per pixel (texture::Texture::sample, bilinear in linear light) at perspective-correct coordinates; pixels below a texture’s alpha cutoff are left out. Normal, bump and emissive maps are read at the same coordinates (geom::normal_mapped, geom::bumped).

See-through meshes are drawn after the rest, farthest first, each one’s back faces and then its front faces, and write depth as they go: what Eagle’s viewer (three.js) and the app’s shader do.

Triangles with any vertex nearer than the near plane are skipped rather than clipped: the fitted camera keeps the whole bounding sphere in front of the eye (the near plane is at most d - 2r), so nothing is lost.

Constants§

CANVAS
Side of the square Eagle renders models on.
PADDING
Transparent border added around the cropped render.

Functions§

render
Draws model on a transparent size x size image.
render_view
Draws meshes as the viewer shows them: a w x h picture through the camera matrix vp (projection times view, clip depth 0..1), lit from to_eye (unit, towards the camera), meshes without a colour in default_color, on a transparent background. The viewer’s snapshot. render_view_lines adds the viewer’s lines.
render_view_lines
render_view with line segments drawn over the faces, each in its colour, about a pixel wide and hidden where a face is in front of them (the edges, grid and axes the viewer shows).
thumbnail
Eagle’s import thumbnail of the 3D file at path: the render on a CANVAS square, trimmed and padded by PADDING. None when the file can’t be read as a model or shows nothing.
trim_and_pad
Crops img to the bounding box of its pixels with alpha above 0 and adds pad transparent pixels on every side; None when nothing is visible.