raisin::Light
class · #include <rayrai/Light.hpp>
Simple light with optional shadow map support.
Supports all LightType models and owns the shadow-map framebuffer and depth textures used by the renderer. GL resources are created lazily by the shadow pre-pass methods (which need a current OpenGL context) and released in the destructor when the creating context is current; otherwise the ids are dropped without deletion. Non-copyable and non-movable.
Constructors & destructor
Light
Light()Default constructor creates a directional light.
No GL resources are created here; the shadow framebuffer is allocated on first use, so construction does not need a GL context.
Light(const Light &) = deleteLight(Light &&) = delete~Light
~Light()Public member functions
operator=
Light &operator=(const Light &) = deleteLight &operator=(Light &&) = deletesetAsDirectional
void setAsDirectional(
const glm::vec3 &dir,
const glm::vec3 &diffColor={0.85f, 0.85f, 0.85f})Configure as a directional light.
Parameters
| Parameter | Description |
|---|---|
dir | Direction pointing from the light toward the scene (normalized here). |
diffColor | Diffuse color (linear RGB). |
setAsPoint
void setAsPoint(
const glm::vec3 &pos,
const glm::vec3 &diffColor={0.85f, 0.85f, 0.85f})Configure as a point light.
Parameters
| Parameter | Description |
|---|---|
pos | Light position in world coordinates. |
diffColor | Diffuse color (linear RGB). |
setSpotAngles
void setSpotAngles(float innerDeg, float outerDeg)Set the spotlight cone half-angles in degrees.
Convenience wrapper around the raw cosine fields. The inner cone is the fully-lit region; intensity falls off smoothly between innerDeg and outerDeg. Pass any values; they are clamped so that innerDeg <= outerDeg.
Parameters
| Parameter | Description |
|---|---|
innerDeg | Half-angle in degrees where falloff begins. |
outerDeg | Half-angle in degrees where light reaches zero. |
setRange
void setRange(float rangeMeters)Set the attenuation of a point, spot or area light from its range in metres.
Sets the inverse-quadratic coefficients 1, 4.5/range and 75/range^2: full intensity at the source, about 1% (1/80.5) at the range, and still decreasing beyond it (it never reaches zero, and the range does not limit which objects the light shades). Use this instead of writing constant/linear/quadratic by hand. radius, the source size, is not changed.
Parameters
| Parameter | Description |
|---|---|
rangeMeters | Distance at which the light contribution falls to about 1%. |
ensureShadowFramebuffer
bool ensureShadowFramebuffer()Create or resize the shadow framebuffer and 2D depth texture.
Requires a current OpenGL context. Resources created in a different context are discarded and recreated.
Returns Always true.
Exceptions
| Exception | Description |
|---|---|
std::runtime_error | (via the framebuffer completeness check) if the framebuffer is incomplete; resources are discarded in that case. |
shadowPrePass
bool shadowPrePass()Prepare for 2D shadow depth rendering.
Ensures the framebuffer, updates lightSpaceMatrix_, binds the FBO, sets the viewport, clears depth, enables polygon offset and disables face culling (restored by shadowPostPass()). Requires a current GL context.
Returns Always true; failures throw as in ensureShadowFramebuffer().
shadowPrePassInto
bool shadowPrePassInto(unsigned int framebuffer, bool clearDepth)Prepare 2D shadow depth rendering into another framebuffer.
Same as shadowPrePass() but binds framebuffer, whose depth attachment must match the allocated shadow map size, and clears depth only when clearDepth is set. Call ensureShadowFramebuffer() first.
Parameters
| Parameter | Description |
|---|---|
framebuffer | Framebuffer to render into. |
clearDepth | Clear the depth attachment before rendering. |
Returns Always true.
updateShadowMatrix
void updateShadowMatrix(int snapResolution)Recompute lightSpaceMatrix_ (used by standalone shadow maps and atlas tiles).
Directional lights use an orthographic box (see setShadowOrtho()) around the shadow center; spot lights a perspective frustum fitted to the outer cone; area lights a 150 degree frustum; point lights a 90 degree frustum.
Parameters
| Parameter | Description |
|---|---|
snapResolution | Shadow-map resolution in texels used to snap directional shadows to the texel grid (reduces shimmering). |
pointShadowPrePassFace
bool pointShadowPrePassFace(int face)Prepare one face of a point-light cubemap shadow render.
Updates lightSpaceMatrix_ and the face's entry in pointLightSpaceMatrices_, then binds that cubemap face for depth rendering. Requires a current GL context.
Parameters
| Parameter | Description |
|---|---|
face | Cubemap face index in OpenGL order [0, 5] (clamped). |
Returns Always true; failures throw std::runtime_error.
shadowPostPass
void shadowPostPass()Restore the caller's face-cull state, disable polygon offset and bind the default framebuffer after shadow rendering.
setShadowResolution
void setShadowResolution(int res)Set shadow-map resolution.
Releases existing shadow resources when the value changes; they are recreated at the new size on the next pre-pass.
Parameters
| Parameter | Description |
|---|---|
res | Shadow map size in pixels (min 64; also capped at the driver's maximum texture size on allocation). |
setShadowsEnabled
void setShadowsEnabled(bool e)Enable or disable shadow sampling.
Parameters
| Parameter | Description |
|---|---|
e | True to enable shadows. |
setShadowParams
void setShadowParams(float bias, float strength, float pcfRadius)Set shadow bias and filtering parameters.
Parameters
| Parameter | Description |
|---|---|
bias | Depth bias to reduce acne. |
strength | Shadow darkening strength. |
pcfRadius | PCF filter radius in shadow-map texels. |
setShadowOrtho
void setShadowOrtho(float halfSize, float zNear, float zFar)Configure the shadow projection volume.
Parameters
| Parameter | Description |
|---|---|
halfSize | Half-size of the orthographic box (directional lights only), in metres. |
zNear | Near plane in metres (all light types). |
zFar | Far plane in metres (all light types). |
setShadowCenter
void setShadowCenter(const glm::vec3 ¢er)Set the center point used for directional shadow projection.
Parameters
| Parameter | Description |
|---|---|
center | World-space center. |
setShadowPosition
void setShadowPosition(const glm::vec3 &pos, bool enable=true)Override the light position used for directional shadow projection.
Parameters
| Parameter | Description |
|---|---|
pos | World-space light position. |
enable | True to use the custom position, false to revert to auto position. |
Public attributes
Color terms
Linear RGB multipliers (not clamped; values above 1 brighten). The ambient term is added without distance attenuation.
ambient
glm::vec3 ambientConstant term added regardless of surface orientation.
diffuse
glm::vec3 diffuseLambertian term.
specular
glm::vec3 specularHighlight term.
Attenuation
Inverse-quadratic falloff for point, spot and area lights: intensity / (constant + linear*r + quadratic*r^2) where r is in metres (never less than radius). Ignored for directional lights. Use setRange() for a more ergonomic alternative.
constant
float constantConstant denominator term.
linear
float linearCoefficient of r (1/m).
quadratic
float quadraticCoefficient of r^2 (1/m^2).
Projector (light cookie)
Optional 2D texture that tints the light; not applied to point lights.
projectorMap
unsigned int projectorMapGL 2D texture id; 0 disables. Not owned by the light.
projectorStrength
float projectorStrengthBlend toward the projector color, [0, 1]; 0 disables.
projectorUvScaleOffset
glm::vec4 projectorUvScaleOffsetProjector UV transform: uv * xy + zw.
Camera-distance fade
When enabled, non-directional lights fade out smoothly over [distanceFadeBegin, distanceFadeBegin + distanceFadeLength] of camera distance.
distanceFadeEnabled
bool distanceFadeEnabledEnable the camera-distance fade.
distanceFadeBegin
float distanceFadeBeginMetres from camera.
distanceFadeShadow
float distanceFadeShadowMetres from camera at which the shadow starts fading.
distanceFadeLength
float distanceFadeLengthMetres of fade band.
Internal shadow state
Public for the renderer; prefer the setters above.
shadowFbo_
unsigned int shadowFbo_Shadow framebuffer id (shared by the 2D and cubemap passes).
shadowDepthTex_
unsigned int shadowDepthTex_2D shadow depth texture (directional/spot/area).
shadowCubeDepthTex_
unsigned int shadowCubeDepthTex_Point-light cubemap depth texture.
shadowRes_
int shadowRes_Requested shadow-map resolution (set via setShadowResolution()).
shadowAllocatedRes_
int shadowAllocatedRes_Allocated 2D depth texture size; 0 when unallocated.
shadowCubeAllocatedRes_
int shadowCubeAllocatedRes_Allocated cubemap face size; 0 when unallocated.
lightSpaceMatrix_
glm::mat4 lightSpaceMatrix_World-to-light-clip matrix from the last updateShadowMatrix()/pre-pass.
pointLightSpaceMatrices_
std::array<glm::mat4, 6> pointLightSpaceMatrices_Per-face matrices from pointShadowPrePassFace().
shadowsEnabled_
bool shadowsEnabled_Set via setShadowsEnabled().
shadowBias_
float shadowBias_Set via setShadowParams().
shadowStrength_
float shadowStrength_Set via setShadowParams().
shadowPCFRadius_
float shadowPCFRadius_Set via setShadowParams() (texels).
shadowOrthoHalfSize_
float shadowOrthoHalfSize_Set via setShadowOrtho() (m).
shadowNear_
float shadowNear_Set via setShadowOrtho() (m).
shadowFar_
float shadowFar_Set via setShadowOrtho() (m).
shadowCenter_
glm::vec3 shadowCenter_Set via setShadowCenter().
shadowPosition_
glm::vec3 shadowPosition_Set via setShadowPosition().
shadowUseCustomPosition_
bool shadowUseCustomPosition_Set via setShadowPosition().
type
LightType typeActive light type.
position
glm::vec3 positionWorld-space light position (point, spot and area lights).
direction
glm::vec3 directionDirection the light travels, i.e. pointing from the light toward the scene (directional, spot and area lights). Need not be unit length.
spotInnerCos
float spotInnerCosCosine of the inner spotlight cone half-angle (use setSpotAngles() to set in degrees).
spotOuterCos
float spotOuterCosCosine of the outer spotlight cone half-angle (use setSpotAngles() to set in degrees).
radius
float radiusLight source size in metres. It is not a cutoff: the attenuation distance is clamped to at least this value (area lights use a quarter of it, min 0.1 m), and for area lights it also softens specular highlights.
areaSize
glm::vec2 areaSizeArea-light width (along areaRight) and height (along areaUp) in metres.
areaRight
glm::vec3 areaRightArea-light width axis in world space (normalized by the shader).
areaUp
glm::vec3 areaUpArea-light height axis in world space (normalized by the shader).
negative
bool negativeSubtract this light's contribution instead of adding it.
temperatureEnabled
bool temperatureEnabledTint the color terms by temperatureKelvin (normalized to D65).
temperatureKelvin
float temperatureKelvinKelvin.