Drawing
Face culling and winding order
In a closed mesh, about half the triangles face away from the camera and are hidden by the other half. Face culling throws them away before they are rasterized. The GPU decides which side a triangle shows from the order of its corners on screen. This page explains that test, the conventions around it, and the cases that reverse it. The last part shows how RASM does it.
In short
- A triangle's winding is the order of its three corners on screen: counter-clockwise or clockwise. Seen from behind, the same triangle has the opposite winding.
- The GPU finds the winding from the sign of the triangle's signed area in screen space, after projection.
- The application says which winding is the front (glTF and OpenGL use counter-clockwise), and which faces to cull: none, back or front.
- A mirroring transform (negative determinant) reverses the winding of every triangle. So does showing the image upside down.
- Double-sided surfaces (leaves, cloth, glass panes) are not culled, and their back side is lit with the normal reversed.
1Why cull back faces
Every triangle of a closed mesh (a box, a character, a car body) has an outside. Its normal points out. From any camera position, the triangles whose normals point away from the camera are behind the ones that point toward it. Drawing them only costs time: the depth test hides them anyway.
Back-face culling is only correct for closed meshes seen from outside:
Closed mesh
Every back face has a front face before it. Nothing is lost.
Open surface
A single wall or a leaf has nothing behind its back side. Culling makes it vanish when seen from behind.
2Winding: the order of the corners
A triangle is stored as three indices into the vertex list, in a fixed order: 0, 1, 2. Look at the triangle and follow the corners in that order. You go either counter-clockwise or clockwise. Walk around to its back and look again: the same order now goes the other way.
It comes from the index order and from where the camera is. Tools write the indices so that the outside is the front.
3The test: the signed area
After the vertex shader, the divide by w and the viewport transform, each corner has a 2D screen position (xi, yi). The shoelace formula gives the area of the triangle with a sign:
a = ½ · [ (x0·y1 − x1·y0) + (x1·y2 − x2·y1) + (x2·y0 − x0·y2) ]
= ½ · ((x1 − x0)·(y2 − y0) − (x2 − x0)·(y1 − y0))
With y pointing up, the sign is the winding:
a > 0
Counter-clockwise.
a < 0
Clockwise.
a = 0
A line: never drawn.
The screen-space test gives the same answer as the 3D question "does the normal point toward the eye?", that is (p0 − eye) · n < 0 for a front face, where n is the cross product of two edges. Projection keeps the side of the eye a plane is seen from.
4Conventions: which winding is front
The GPU has two settings: which winding counts as front, and which faces to cull.
| API or format | Front-face setting | Default front |
|---|---|---|
| glTF 2.0, OpenGL tradition | — / glFrontFace | counter-clockwise |
| Direct3D 11 and 12 | FrontCounterClockwise in the rasterizer state | clockwise (the flag is false) |
| Metal | front-facing winding on the render encoder | clockwise |
| Vulkan | frontFace in the pipeline, or dynamic state (core since 1.3) | no default: the pipeline must say |
The four cull modes, on the closed mesh of section 1 (seen from above, camera below, thick lines are drawn):
none
Drops nothing.
Used for: double-sided surfaces, 2D and UI, debug views.
back
Drops faces with the "back" winding.
Used for: almost every closed, opaque mesh.
front
Drops faces with the "front" winding.
Used for: mirrored meshes (section 5), inside-out volumes such as a sky box seen from inside, some shadow-map techniques.
front and back
Drops every triangle.
Used for: rare: points and lines still draw.
Try the whole test on one triangle. The picture computes the signed area of section 3 from the three corners, then applies the two settings.
A model exported clockwise into a counter-clockwise engine appears inside out, with its far inner walls visible and its near outer walls missing: the "front" card above, when "back" was meant.
5What reverses the winding
A mirroring transform
A mirror (a reflection) turns a counter-clockwise triangle into a clockwise one. A scale of −1 along one axis is a mirror, and so is any matrix whose 3×3 part has a negative determinant.
det ≥ 0 → cull back. det < 0 → cull front (or keep back culling and swap the front-face setting). For T · R · S, det = sx · sy · sz: a rotation has det = +1.
Two mirrors cancel:
scale (1, 1, 1)
det = +1. Counter-clockwise, as modeled.
scale (−1, 1, 1)
det = −1. One mirror: clockwise.
scale (−1, −1, 1)
det = +1. Two mirrors: a 180° rotation, counter-clockwise again.
Try it. The front face is counter-clockwise and the camera looks at the outside of the triangle.
Mirrors are common: a level designer reuses a left door as a right door with a negative scale, and glTF files may contain nodes with negative scale. Because the determinant is a product, a hierarchy is mirrored when an odd number of its matrices are.
An upside-down image
Showing an image upside down is also a mirror (across the horizontal axis). This is what happens when Y-up clip coordinates reach Vulkan, whose clip space is Y-down, without a fix: every winding reverses.
Y-up clip space on Vulkan, no fix
The image is upside down and every winding is reversed.
With a fix
Both usual fixes, a negative viewport height or a flipped projection, give an upright image and keep the windings as authored (Coordinate spaces).
6Double-sided surfaces
A leaf, a flag or a sheet of paper is one layer of triangles. It must be visible from both sides, so its material turns culling off. The back side then needs care in lighting: its normal still points out of the front.
Back face lit with n
The back is dark, or lit from the wrong side.
Back face lit with −n
The light falls on the side being looked at.
Under a mirroring transform the "front-facing" flag reports the authored front as "back". A shader that also handles mirrors must combine the flag with the sign of the determinant.
7Where culling sits in the pipeline
They may be shared with front-facing triangles. To skip vertex work too, engines cull whole clusters of triangles earlier, with a cone that bounds their normals (cluster cone culling).
8Worked example
The triangle of section 3, drawn with Vulkan into an 800 × 600 image with back-face culling and a counter-clockwise front face. Vulkan computes the area in framebuffer coordinates (rows go down) with a minus sign in front: a = −½ · Σ (xi · yi+1 − xi+1 · yi). A positive a is counter-clockwise.
1. In Y-up clip space
This is how the projection matrix produced it (clip space with Y up, as in OpenGL-style math).
2. With a negative viewport height
The flip of the viewport and the minus sign of Vulkan's formula cancel: Y-up counter-clockwise stays counter-clockwise.
3. Without the flip
This is why a Y-up projection on Vulkan needs a fix, or a clockwise front face.
4. A mirrored object
The determinant decides the cull mode per object, before drawing. Nothing about the triangle itself changes.
9Pitfalls
- A model looks inside out
- Its winding convention differs from the engine's, or it has a mirroring transform and the cull mode was not flipped.
- Everything vanishes on Vulkan
- A Y-up projection without a Y flip, with a counter-clockwise front face.
- A wall or a leaf disappears from one side
- An open surface without a double-sided material.
- The back of a double-sided surface is dark or lit from the wrong side
- Its normal was not reversed for back faces.
- Lighting is wrong only on mirrored double-sided objects
- The front-facing flag used without the determinant's sign.
- Holes in some models only
- Mixed winding inside one mesh; it needs fixing in the tool, or a double-sided material.
10In RASM
RASM uses glTF's convention: counter-clockwise is front. The cull mode is chosen per instance on the CPU from the material and the determinant, and the draw list is grouped so that each group of equal modes is one indirect draw. Because the Vulkan backend flips Y with a negative viewport height, clip space and the screen agree on the winding (D8).
CullMode: NONE, FRONT, BACK. The one BLEND run is always NONE. GPU culling keeps each run's visible draws at the start of its range (GPU-driven rendering).Where RASM follows the textbook, and where it differs
| Part | This page | RASM |
|---|---|---|
| Front face | a convention | Counter-clockwise (VK_FRONT_FACE_COUNTER_CLOCKWISE in createGraphicsPipeline), like glTF. Imported data is not mirrored (D8). |
| Y flip | keeps the winding if done right | A negative viewport height in VulkanCommandBuffer::setViewport, so Y-up counter-clockwise stays counter-clockwise on screen. |
| Cull mode | a pipeline setting | Dynamic state (VK_DYNAMIC_STATE_CULL_MODE, core since Vulkan 1.3). Pipelines are created with VK_CULL_MODE_NONE; RenderContext::setCullMode sets it while recording. |
| Mirrors | cull front, or swap the front face | Cull front: getCullMode returns FRONT when scale.x * scale.y * scale.z * det(mat3(subMesh.transform)) < 0. The front-face setting never changes. This is the full determinant of the instance's world matrix, because the entity's rotation has determinant 1. A sub-mesh that a glTF clip can mirror (a scale that reaches 0 or less on its node, a node above it or one of its joints) gets NONE, because its determinant can change sign on the GPU from frame to frame (animationCanMirror); the shader's handedness comes from the posed matrix, so its lighting stays right. |
| Double-sided | no culling, reversed normal on the back | NONE when the material's doubleSided is set (glTF doubleSided, or createMaterial's last argument). fragMain sets backFace = isFrontFace == (handedness < 0), and the PBR shader then uses −n. |
| Mirror + front-facing flag | combine with the determinant | Done: vertMain passes handedness, the sign of determinant(modelMat3), to the fragment shader. |
| Granularity | per object | Per instance row (one sub-mesh of one entity). updateDrawModes recomputes it, with the material's alpha mode, after updateTransform and after a material change; a changed mode rebuilds the draw lists of the entity's scenes. A see-through (BLEND) material always gets NONE, so its draws stay in one sorted run; it drops its back faces in the shader instead (transparency). |
| Draw calls | one state per draw | Draws are sorted into at most seven runs (three cull modes for opaque and for cut-out draws, one see-through run), one indirect draw per run, instead of one draw per object. |
| Other passes | — | beginPass sets NONE, drawSceneView restores NONE after its runs, and the stats overlay draws with NONE. |
| Cluster cone culling | section 7 | Not done. Culling on the GPU works per draw (frustum, occlusion), not per cluster of triangles. |
Where it lives in code
| What | Where |
|---|---|
| The modes | CullMode in include/rasm/types.h |
| Choosing a mode | getCullMode, Renderer::updateDrawModes in src/renderer/gpuScene.cpp |
| Runs in the draw list | Renderer::rebuildDrawList in src/renderer/gpuScene.cpp; DrawRun in src/renderer/gpuScene.h; MAX_DRAW_RUNS in src/renderer/uberMaterialTypes.h |
| Drawing the runs | Renderer::drawSceneView in src/renderer/gpuScene.cpp |
| Front face, dynamic cull mode | VulkanDevice::createGraphicsPipeline in src/rhi/vulkan/vulkanDevice.cpp |
| Setting the mode, the Y flip | VulkanCommandBuffer::setCullMode, VulkanCommandBuffer::setViewport in src/rhi/vulkan/vulkanCommandBuffer.cpp |
| Double-sided flag | Material in include/rasm/material.h; read from glTF in src/assets/material.cpp |
| Back-face lighting | vertMain, fragMain in shaders/passes/rasm.slang; the normal flip in shaders/lib/pbr.slang |
- The counter-clockwise front face and the negative-height viewport are a pair. Change the Y flip, and the winding rule changes with it.
getCullModeand the shader'shandednessmust agree: both come from the sign of the world matrix's determinant.- Every pipeline takes its cull mode from the command buffer. A pass that draws must set it (
beginPasssetsNONE); code that changes it putsNONEback. - Anything that changes a transform or a material's
doubleSidedoralphaModemust go throughupdateDrawModes, or a draw stays in the wrong run. - The draw list has one run per alpha mode and cull mode: adding a mode means changing
MAX_DRAW_RUNSon both the C++ and the Slang side (Struct layout).
Further reading
- Tomas Akenine-Möller and others, Real-Time Rendering, 4th edition: section 19.3, Backface Culling.
- The Vulkan specification: "Basic Polygon Rasterization" (the signed area and frontFace).
- The glTF 2.0 specification: "Double-sided" materials and the rule for negative-determinant node transforms.
- Christer Ericson, Real-Time Collision Detection (2004): orientation tests and signed areas.