![]() |
vkmEngine 1.0.15
A C++ game engine · vkmengine.com
|
How a surface is lit: five light types (Directional, Point, Spot, and the Rect and Disk area lights) as data-only Light components, evaluated in the forward PBR shader against a Forward+ cluster grid; shadows from a shared atlas; and the ambient from the sky, reflection probes and an irradiance volume. Where each of these runs in the frame is rendering.md.
Light and its fields are documented in src/engine/ecs/component/render/light.h. Position comes from the entity's Transform; direction is the entity's forward, its rotated -Z. A spot shines along it; an area light emits from the opposite face, along -direction (both faces when twoSided).
sourceRadius is the light's size - a directional light's angular radius in radians, a point or spot's emitter radius in metres. It sizes both the highlight and the shadow's penumbra; zero is a point source with hard shadows.
flicker wavers a point, spot or area light as a fire does, by the world's clock (flickerWave). It scales the light as it is shaded, LightData::wave, never intensity: gameplay reads and the scene saves the steady value, and a capture takes the light steady, so the probes and the irradiance volume, which rebake when a light changes, do not rebake for it.
The key light is findKeyLight: the lowest-slot enabled directional light. The sun a shadow is drawn for is lowestSlotDirectionalCaster, the lowest-slot directional light that casts - not always the key light.
The forward fragment shader (shaders/forward/pbr/, BRDF terms in shaders/brdf.glsl):
The irradiance volume and the reflection probes are pictures of the scene (GLSceneCapture), so whatever lights the captured surfaces is what they pass on: a wall lit by the open sky would light every probe in a sealed room through the plaster. So a captured surface takes its ambient from the volume wherever the volume covers it, and from the sky only outside it:
A volume filling a room wants its box out to the middle of the walls and a blendDistance shorter than their half-thickness; longer, the room's surfaces near a face take part of the sky.
Rect and Disk lights (shaders/area_light.glsl, shaders/form_factor.glsl) cast no shadow.
intensity is point-equivalent: radiance is intensity / area, so far away an area light lights like a point light of the same intensity. radius only windows the light to zero.
Two depth stores, their counts in engine_config.h and the largest tile's edge RenderSettings::shadowResolution:
GLShadowPass draws them. Casters come from the whole scene, not the camera's visible set, culled per tile and face on the thread pool against the light's frustum (GLShadowData::cullCasters). A tile or cube face is kept between frames when its batch's signature (matrix and casters, ShadowCasterBatch::signature) matches, so a still view redraws no shadow; the fixed casters are signed all at once by RenderObjects::fixedSerial, the moving ones each, and a posed caster leaves a batch unsigned. A replaced world or a shader reload forgets every tile (forgetHeld). An alpha-masked or see-through transparent caster cuts its shadow by its alpha (shadow/depth_masked, GLMaterial::cutsItsShadow).
Bias. Every light reads shadowBias and shadowNormalBias in texels of its own map at the receiver, scaled by the light's angle to the surface (biasSin, biasSlide in shaders/shadows.glsl). One pair of values holds for every light type, range and cascade, so the defaults rarely need touching (engine.md).
Soft shadows. Sun, spot and point lights cast percentage-closer soft shadows sized by sourceRadius: a blocker search, a penumbra from the blocker distance, and a fixed Vogel disk of hardware compares. The disk is not rotated per pixel - that is grain only a temporal filter removes, and there is none. A source of zero, or RenderSettings::softShadows off, gives hard shadows. A penumbra too wide for its cascade is read from a coarser one; a spot or point map has none, and stops at SOFT_SPOT_REACH.
Cascades. shadowDistance sets how far the sun's cascades reach (spot and point use radius); the shadow fades out toward its reach (SHADOW_FADE). Cascades blend across a band at each split (CASCADE_BLEND). The split is logarithmic, anchored at a fixed world distance (CASCADE_NEAR) rather than the camera's near plane, so raising shadowDistance costs the far field density, not the foreground sharpness.
The atlas is read both compared and as raw depth (blocker search, the ShadowAtlas view), so the comparison lives on a sampler object: GLShadowAtlas::bind2D and bind2DRaw bind the two.
GLScreenShadowPass finds in the depth buffer what the cascades are too coarse for - a foot on the floor, the crease under a ledge - for the cascaded sun alone (GLShadowData::sunLight). It is Bend Studio's method (shaders/screen_shadow, under its Apache-2.0 notice): each pixel marches toward the light the pixels a contact's reach spans at its depth (CONTACT_REACH), at most SAMPLE_COUNT (engine.md).
It is a contact shadow, not a replacement for the cascades. The viewmodel band casts and takes nothing; an EyeOnly mesh, which the shadow maps never see, does cast here. The result goes into the AO target's alpha; the forward pass multiplies the sun's visibility by it for the opaque bucket, and decals use it for the sun on the surface under them. screenShadows turns it off.
GLIBLBaker (called from GLBackend::render, not a pass) keeps the IBL set showing the scene's sky: an environment cube (what an HDR sky's skybox and the captures draw; shading never reads it), an irradiance cube, a prefilter cube (one roughness per mip) and the BRDF LUT (integrated once at startup). An HDR sky (Environment.sky.hdrPath) bakes when the path changes; the procedural sky when a sky parameter changes or the sun or moon moves past SkyParams::SAME_DIRECTION. A scene with no sky, or an HDR that fails to load, bakes nothing - it does not keep the last scene's sky.
Following the sun. A sun that only drifted (within SkyParams::DRIFT) is re-baked a step a frame into a second set of cubes, swapped in when complete (GLIBL), more steps a frame while it runs behind (GLIBLBaker::MAX_CATCH_UP_STEPS); the sky and the key light never trail. Anything bigger bakes whole at once.
The procedural sky is Hillaire's sky and atmosphere, from four tables in GLAtmosphere. Transmittance and multiple scattering depend on the air alone and are redrawn when it changes (GLIBLBaker::bakeAir); the sky-view table and the aerial-perspective volume follow the camera and sun and are computed every frame by the Atmosphere pass. Surfaces take the aerial perspective with the fog (rendering.md), scaled by sky.aerialPerspective.
One sun lights the scene and the sky. The sky's sun is Atmosphere::solarIlluminance, from sky.lightColor and sky.lightIntensity, and the sky's radiance is that times the atmosphere's integral with no constant between them, so a brighter sun brightens the sky with it. sky.intensity scales the skybox, the ambient and the air's light.
The sun's place is authored on the Environment (sunElevation, sunAzimuth), because the sky must work with or without a directional light. While the sky is procedural, SkySystem writes the key light's rotation, colour and intensity every frame - by day Atmosphere::sunlight, which reddens at a low sun; by night the night.moonlight* pair. Author those on the environment, not the Light: an edit to the light is overwritten next frame (editor.md). Shadow settings stay the light's.
Night. Below the horizon a skyglow and a moon lobe take over across NightSkySettings::TWILIGHT_DEGREES either side of it (shaders/sky.glsl, shared by the bake and the skybox), and the key light fades across the same band. The moon sits opposite the sun, tilted by moonTilt. Stars are drawn by the skybox only; the prefiltered cubes would smear them.
The view frustum is diced into CLUSTER_X x CLUSTER_Y screen tiles by CLUSTER_Z exponential depth slices. The ClusterCull pass lists the lights touching each cluster (a point as its sphere, a spot as its cone), and the forward pass shades a pixel against its cluster's list rather than every light - which is why MAX_LIGHTS can be generous. A list holds at most MAX_LIGHTS_PER_CLUSTER; lights past it are dropped, which shows as a cut, and the Light Clusters view draws such a cluster magenta. The cull is one invocation per cluster testing every light, so finer clusters cost the cull; coarser ones put more lights in each list, nearer the cap.
Cross-cutting limits live in src/engine/core/engine_config.h (Config::), and GLSL cannot read a C++ header. GLBackend::shaderConstants writes them out as GLSL from the C++ constants themselves, and Vkm::GL::setShaderPrelude puts that text under the #version of every stage, graphics and compute. So a shader just uses MAX_LIGHTS, with nothing to include and nothing to keep in step. The same carries the bindings (rendering.md), the render-mode and tonemap enums, and constants such as the twilight band and the atmosphere's geometry and table sizes.
Never re-define a prelude constant in a shader - GLSL rejects the redefinition, which is what keeps a shader from holding its own copy. Config::NUM_CASCADES and Config::SHADOW_NEAR are not in the prelude; the shader reads them from the shadow block.