Drawing
Vector lines, polygons and points
A map is mostly vector data: roads and rivers are lines, lakes and parks are polygons, shops are points. Their shape comes from the data, but their look comes from a style: a road is 7 pixels wide at any zoom. This page shows how to draw them on the GPU: lines spread from their center line in the vertex shader, joins and caps, smooth edges from the distance to them, polygons with holes, points, the order they are drawn in, how they lie on terrain, and what culling must do for widths measured in pixels. The last part shows how RASM does it.
In short
- A line's vertices keep its center line, and an offset in half widths. The vertex shader multiplies the offset by the style's width, so a style changes without a rebuild.
- Where two segments meet, a join fills the gap (miter, bevel or round); the ends get a cap (butt, square or round).
- Each fragment knows its distance to the edge. One pixel's change of it (
fwidth) gives a smooth edge, at any width and angle. - A line is never drawn under one pixel: a thinner one keeps one pixel and fades by its coverage, so it neither vanishes nor shimmers.
- A width is in pixels or in metres, spread across the screen or in the ground's plane.
- Polygons with holes are cut into triangles by ear clipping. Points are quads with a round edge.
- Layers are drawn in a set order, over the ground by a small depth bias, or over everything.
- On terrain, features are cut on the terrain's grid, so each piece lies flat on one terrain triangle.
- Culling must pad a feature's bounds by its width in pixels at its distance, or wide lines vanish at the screen's edge.
1The problem
A GPU draws triangles. A road is a list of points, and its width is a number of pixels on screen: as the camera zooms out, the road keeps its 7 pixels while the world shrinks. So the triangles of a line depend on the camera. Rebuilding them on the CPU every frame is slow; building them once in world units makes them too thin far away and too wide near.
Triangles in world units
The width follows the perspective: a far road is thinner than a pixel and breaks up.
A center line and offsets
The data is built once; the vertex shader spreads it by the width every frame.
2A line as a center line and offsets
Each segment of the line becomes a quad of four vertices. All four sit on the center line, two at each end. Each also has an offset (x along the segment, y across it), in half widths: −1 on one side, +1 on the other. The vertex shader puts the vertex at anchor + (x · along + y · across) · half width.
3Joins and caps
Two quads that meet at an angle leave a wedge open on the outer side of the turn. A join fills it. The ends of a line get a cap.
Miter
The outer edges meet in a point. A sharp turn makes a long spike, so past a limit it becomes a bevel.
Bevel
The two outer corners joined by one triangle.
Round
A fan of triangles around the joint; its edge is round to the pixel.
4Smooth edges, and never thinner than a pixel
Each vertex hands its offset to the fragments, interpolated. A fragment's distance to the edge is |y| on the body, the length of (x, y) in a round join or cap, the larger of |x| and |y| in a square cap. The geometry reaches one pixel past the edge, and the fragment covers what is inside:
coverage = saturate((1 − distance) / fwidth(distance) + 0.5)
- distance
- in half widths: 1 at the edge
- fwidth
- how much the distance changes over one pixel: the smooth band is one pixel wide at any width, distance and angle
A line of 0.3 pixels, drawn as it is
Only the pixels whose centers it crosses are hit: dashes that crawl and shimmer.
One pixel, faded to 0.3
Every pixel along it gets a little: a faint, steady line. MSAA and TAA then have nothing to fight.
5Pixels or metres; the screen or the ground
Pixels
A road of a map style: the same width at any zoom. The world width is the pixel width times the pixel's size at that depth.
Metres
A river 40 m wide, as it is in the world. Far away it reaches the one-pixel minimum and fades.
Across the screen
The offset is turned to the screen: the line never foreshortens. For 3D paths, routes, markers.
In the ground's plane
The band lies on the ground: a road draped on hills. Seen edge-on it gets thinner, down to the minimum.
6Polygons with holes
A lake is an outline with islands inside, each a ring of points. Ear clipping cuts it into triangles: it joins each hole to the outline by a bridge, so the shape is one ring, then cuts off "ears", triangles made of three neighbour points with no other point inside, until nothing is left. A polygon's own edges are triangle edges, so MSAA or TAA smooths them.
7Points and markers
A point is a quad around its anchor, its four corners at offsets (±1, ±1). The fragment's distance is the length of its offset, so the disc is round and smooth; a ring along its edge is the same distance test at 1 − ring width. An icon is the same quad with texture coordinates from an atlas.
8Draw order and depth
Map layers overlap on purpose: a road over a park over the ground. All lie in the same plane, so the depth test cannot order them. They are blended with their smooth edges, in a fixed order of layers, each one a little nearer than the ground by a depth bias. An overlay (a route, a selection) is drawn at the near plane, over everything.
A marker at its anchor's depth
A quad that faces the screen stands up from the ground: below its anchor the ground is nearer and hides it.
The ground's depth under each corner
Each corner takes the depth of the ground under its pixel, through the ground plane at the anchor, when that is nearer.
9Draping on terrain
A road on hilly terrain must follow the drawn ground exactly, or it sinks into a hill or floats over a valley. The terrain is drawn as two triangles per cell, each flat. So cut every segment where it crosses a grid line or a cell's diagonal, and lift each new point by the terrain's own rule: then each piece lies flat on one terrain triangle. Polygons are cut the same way.
That holds for the line's middle. A road also has a width, and the shader spreads it in the plane of the triangle under the middle. Where the road is wider than a cell, its sides reach over the next triangles, and where the ground bends up beside the road (a valley's floor, the inside of a turn), it rises above that plane and covers the road's edges in wedges. So a road whose width is known in metres is draped whole: build its outline on the flat grid, at its full width, with its joins and caps, then cut those triangles on the grid like a polygon and lift every corner. Each corner keeps where it is across the road (its offset, for the smooth edge and the outline) and along it (for the dashes).
The smooth edge still reaches a pixel past the road. The shader moves each corner out by that pixel in the plane of its own triangle, and draws the road nearer by as much as it moved, so the pixel does not sink into a slope.
10Culling a width in pixels
A line's bounding sphere is built from its center line. A line 40 pixels wide whose center line is just off the screen still covers 20 pixels of it. A pixel is larger far away, so the pad grows with depth: pad the sphere by the widest offset in pixels times the size of a pixel at the sphere's far side, for each view. The occlusion test then uses the padded sphere too.
11Motion for TAA
TAA moves each pixel's history by the motion of what is in it. A smooth edge is part line, part ground: the line writes its anchor's motion scaled by its coverage, and the blend keeps the rest of the motion behind it. So a line over the sky, the ground or a moving object reprojects without a smear.
12Pitfalls
- Thin lines break into crawling dashes
- They are thinner than a pixel. Keep one pixel and fade by the coverage.
- Wide lines vanish at the screen's edge
- Culling uses the center line's bounds. Pad by the width in pixels at the far side, per view.
- Markers and roads are cut in half on a tilted map
- A quad facing the screen sinks into the ground below its anchor. Give each corner the depth of the ground under it.
- A see-through line is darker at its joints
- Its quads and joins overlap on the inner side of a turn and blend twice. Draw see-through lines opaque, or as one layer with a lower alpha.
- Roads sink into hills or float over valleys
- The line is not cut on the terrain's grid, or the terrain is drawn from other heights than the ones used to drape.
- A wide road on hills is shredded at its edges
- Only its middle is draped; its width is flat and the ground beside it rises above it. Drape the whole width: cut the road's outline on the grid, like a polygon.
- Layers flicker against each other
- Same depth, no order. Draw layers in a fixed order, blended, with a depth bias against the ground only.
- Mesh simplification ruins the lines
- LODs and meshlets are for meshes whose vertices stay where they are. Build vector meshes without them.
13In RASM
A layer of vector features is an ordinary entity: a mesh built by the vector builders (include/rasm/vector.h), drawn with a MaterialType::VECTOR material whose VectorMaterial is the style. So it goes through the GPU culling, the Transparent pass's sort, TAA and MSAA like any blended mesh, and is a TileStreamer tile's content like any other. Labels are vector features too: a fourth kind of vertex, the glyph, with its own page, Text and labels.
RASM_EXPORT_VIEW) sizes given in pixels are multiplied by SceneData::pixelScale (getPixelScale), its ExportDesc::pixelScale, which also divides the LOD's pixels per unit; in every other view it is 1 and the shaders do no work for it. Sizes in metres do not change.P30's fix: buildMesh and createMesh(MeshBuild&&), for any mesh).// From examples/25_vector_map/main.cpp: a road layer, then a tile's roads built on a server thread.
rasm::MaterialHandle roads = makeLayer(engine, {.color = glm::vec4(1.0f, 0.75f, 0.25f, 1.0f), .width = 7.0f, .outlineColor = glm::vec4(0.55f, 0.32f, 0.08f, 1.0f), .outlineWidth = 1.5f, .drawOrder = -10});
// ... on a server thread:
rasm::VectorGeometry geometry;
rasm::addVectorLines(geometry, origin, points.data(), sizes.data(), static_cast<uint32_t>(sizes.size()), {.join = rasm::LineJoin::ROUND, .cap = rasm::LineCap::ROUND});
data.layers[ROADS] = rasm::buildVectorMesh(std::move(geometry));
// ... on the main thread, when the tile arrives:
const rasm::MeshHandle mesh = engine.createMesh(std::move(data.layers[ROADS]));
const rasm::EntityHandle entity = engine.createEntity("layer", mesh, roads, rasm::Transform{origin});
Where RASM follows this page, and where it differs
| Topic | This page | RASM |
|---|---|---|
| The vertex | anchor and offset | Vertex: pos the anchor, normal the ground's up, tangent the direction the offset is measured from (w: the feature id's bits, exact for any 32-bit id), uv[0] the offset, uv[1] the distance along the line and, at a join, its turn, or an icon's coordinates, uv[2] the kind and the distance metric (shaders/lib/vector.slang). |
| Joins, caps | miter, bevel, round; butt, square, round | LineShape: LineJoin, LineCap, miterLimit up to MAX_VECTOR_MITER (2 half widths). Each miter triangle is in its own segment's frame, so the distance across is exact on both sides; a bevel is measured round from the joint. A join's vertices carry the turn on the ground: across the screen the shader turns each offset to the screen's angle (getScreenJoinOffset), so the join meets the next segment under any camera. |
| Smooth edges | distance and fwidth | The geometry reaches one pixel past the edge; getVectorCoverage. The minimum is one pixel, faded below (fade). In the ground's plane, a band seen edge-on under half a pixel widens to half a pixel and fades. |
| Style | width, units, plane | VectorMaterial: color, width, widthUnit (PIXELS, METRES), facing (SCREEN, GROUND), dashLength, gapLength, outlineColor, outlineWidth, drawOrder, depth (TESTED, OVERLAY). Dashes in pixels follow the pixel's size at each vertex, so along a line that runs into the distance they are close, not exact. |
| Polygons | ear clipping | earcut (mapbox/earcut.hpp, vcpkg earcut-hpp, header-only), in the ground's plane through the origin, or across the tile when draped. No smooth edges of their own: MSAA or TAA. |
| Order and depth | layers in order, a bias, overlays | Vectors are always in the blend run. The Transparent pass sorts by (VECTOR_ORDER_SCENE + drawOrder, then far to near); the scene's see-through draws sort at order 0. A tested feature takes the depth of a point moved toward the camera by 1/4096 of its distance (VECTOR_DEPTH_BIAS), so the bias holds for perspective, orthographic and finite-far cameras; a screen-facing corner takes the ground's point under it when that is nearer (getGroundPoint); a line or point whose width is in pixels is drawn nearer again by its half width in the world, since the ground beside its anchor may rise above the plane it spreads in; an overlay, and a label whose anchor the Label Depth pass found in view (Text and labels), is at the near plane and never occlusion culled. The order holds while the GPU blend sort runs: with frozen culling the blend run is drawn in list order. |
| Draping | cut on the grid | VectorPlacement::drapeTile and drapeHeights: cut at x = k, z = k and x + z = k (the grid's split), lifted by getTerrainPosition, with the triangle's normal as the ground's up. |
| A road's whole width | the outline cut on the grid | LineShape::drapeWidth (metres, 0 by default): a draped line is built that wide on the flat grid by the same joins and caps (pushLine), then each triangle is cut on the grid and each corner lifted (pushDrapedLine, on a lattice of 2-20 cells, so pieces that meet lift a point the same). What reaches past the tile is cut at its edge: give each tile the lines within half the width of it, and the neighbour draws the rest. Its vertices are VECTOR_KIND_BAND: pos on the ground, uv[0] the offset interpolated in its triangle, uv[1] the distance along and the built half width. The shader moves a corner only by its smooth edge's pixel (or up to a pixel's width when the band is thinner), in its own ground's plane, and draws it nearer by that pixel's move (not by the widening of a band seen edge-on); it draws the built width whatever the material's width, widthUnit and facing (the outline and the dashes keep the material's unit), so a new width takes a rebuild. Bug B35: a road wider than a cell, draped by its middle only, was covered at its edges where the ground bends up. |
| Culling | pad per view | getWorldSphere adds getVectorPadding for a VECTOR material: 2 half widths and 3 pixels at the sphere's far side (in metres for a metre width), in the view's own pixel size, so the frustum test, the Hi-Z test, the blend sort and the culling debug view all see it. Shadow maps leave vectors out (VIEW_DEPTH_ONLY); vector draws are drawn whole, never by meshlets. |
| Data lines | a color per point | addVectorLines' colors or scalars, one per point: every vertex a point makes takes its value (VectorGeometry::colors, the mesh's vertex colors); lines without values beside them are white. With VectorMaterial::colorMode SCALAR the vertex carries the scalar, the line interpolates it, and the pixel maps it through colormap and range (getVectorColor in rasm.slang; Values through a colormap). A draped band takes no values. |
| Motion | the anchor's, by coverage | toVectorOutput writes the anchor's motion times alpha; the premultiplied blend keeps the rest of what is behind. |
25_vector_map after the first 30, 1500 frames, two views (TAA and 4× MSAA)Release, headless, RTX 3060Where it lives in code
| What | Where |
|---|---|
| The builders | addVectorLines, addVectorPolygons, addVectorPoints, buildVectorMesh, VectorGeometry, VectorPlacement, LineShape in include/rasm/vector.h; src/scene/vector.cpp |
| The style | VectorMaterial, VectorUnit, VectorFacing, VectorDepth in include/rasm/material.h; gfx::VectorData in MaterialData, and a data line's colormap and range in its gfx::PointData |
| Meshes off the main thread | buildMesh, MeshBuild, MeshBuildOptions (include/rasm/mesh.h), Renderer::createMesh(MeshBuild&&) in src/assets/mesh.cpp |
| Shaders | shaders/lib/vector.slang (getVectorVertex, shadeVector, getVectorPadding, getBlendOrder); rasm.slang; frustumCulling.slang, cull.slang, sort.slang |
| The C API (1.5) | rasm_build_mesh, rasm_create_mesh_from_build, rasm_vector_*, rasm_vector_material in src/c/cVector.cpp |
| Tests | tests/vectorTests.cpp (offsets, joins, caps, sides, holes, draping on the drawn ground, a road draped across its width, points); image tests vectors, vectors_msaa4, vectors_taa (with a line just off screen that culling's pad keeps), draped, draped_msaa4, draped_taa (a road draped across its width and one in pixels on hills) |
| Examples | examples/25_vector_map (a city's vector tiles, --culling for the culling debug view); examples/24_map_tiles (roads and lakes draped on the terrain); examples/26_picking (a road draped across its width, whose edges it picks from above; --fly, --no-aa) |
src/scene/vector.cppandshaders/lib/vector.slangagree on the vertex: the kinds, the metrics, the across directioncross(along, up).- No offset reaches past
MAX_VECTOR_MITERhalf widths: culling's pad counts on it. - Vector meshes have no LODs and no meshlets (
buildVectorMesh). - Every pass that draws a
VECTORmaterial spreads its vertices, the pick pass too (Picking): it compilesvertMainwithRASM_PICKand writes the feature id. The feature id's bits are intangent.w: copy them, never compute with them. - A vertex behind the camera takes the sizes of one at the near plane (
getSafeW): a line that crosses the near plane keeps a bounded width.
See-through lines blend twice where their pieces overlap (D45). Dashes in pixels are close, not exact, on a line that runs into the distance. Decoding vector tile files (MVT) is the application's.
Further reading
- Mapbox, Drawing Antialiased Lines with OpenGL (2015): lines as extruded vertices, joins, and the distance to the edge.
- Matt DesLauriers, Drawing Lines is Hard (2015): the ways to draw lines on the GPU and their trade-offs.
- David Eberly, Triangulation by Ear Clipping (2002); mapbox/earcut.hpp.
- Cesium, Robust Polyline Rendering with WebGL and ground primitives: lines on terrain.