Darkplaces lighting/Lighting model

From Blood Wiki
(Difference between revisions)
Jump to: navigation, search
m
 
(31 intermediate revisions by one user not shown)
Line 4: Line 4:
  
 
Lighting are breaked into the passes:
 
Lighting are breaked into the passes:
# Base pass (lightmap/lightgrid/vertex lighting)
+
# Static lighting (lightmap or lightgrid or vertex lighting)
 
# Dynamic light 1
 
# Dynamic light 1
 
# Dynamic light 2
 
# Dynamic light 2
Line 12: Line 12:
 
# Fog (optional)
 
# Fog (optional)
  
Basically Darkplaces lighting model involves this data to be processed for each pixel:
+
Data used for each light pass:
* Vertex normal : values between vertices of pixel's triangle are interpolated by [http://en.wikipedia.org/wiki/Gouraud_shading Gouraud shading]
+
* Vertex normal : values between vertices of pixel's triangle are interpolated using [http://en.wikipedia.org/wiki/Gouraud_shading Gouraud shading]
 
* Color Map : base texture (best way to use [[diffuse]] texture)
 
* Color Map : base texture (best way to use [[diffuse]] texture)
 
* Normal Map : texture storing additional surface curvature (optionally, alpha storing height)
 
* Normal Map : texture storing additional surface curvature (optionally, alpha storing height)
* Gloss Map : texture is used for specular and containing gloss color (RGB) and exponent mod (alpha)
+
* Gloss Map : texture used for specular reflection. Contains gloss color (RGB) and optional exponent multiplier (alpha)
 
* Glow Map : texture's local luminance texture, RGB additive blended texture that entirely ignores shading
 
* Glow Map : texture's local luminance texture, RGB additive blended texture that entirely ignores shading
 
* Light Vector : vector of light direction (either calculated for realtime light or got from deluxemap [[lightmap]] component)
 
* Light Vector : vector of light direction (either calculated for realtime light or got from deluxemap [[lightmap]] component)
 
* Eye Vector : being computed for each pixel, this vector is used for specular calculation.
 
* Eye Vector : being computed for each pixel, this vector is used for specular calculation.
 +
 +
This gallery illustrates how lighting features changes the quality:
 +
<gallery>
 +
File:Darkplaces lightexample flat.jpg|Attenuation-only 'flat' lighting
 +
File:Darkplaces lightexample shadows.jpg|+ Shadows
 +
File:Darkplaces lightexample shading.jpg|+ Shading
 +
File:Darkplaces lightexample specular.jpg|+ Specular
 +
File:Darkplaces lightexample bouncegrid.jpg|+ Photonmapping
 +
</gallery>
  
 
==Attenuation==
 
==Attenuation==
Line 26: Line 35:
 
{{design|Spotlights and projected dynamic lights could be simulated with cubemap filters.}}
 
{{design|Spotlights and projected dynamic lights could be simulated with cubemap filters.}}
  
Static lights that is used for lightmap calculations are opposite. They have a variety of options for controlling attenuation (this options is only used in Q3map2 LIGHT stage):
+
Static lights can have a variety of options for controlling attenuation (this options is only used in Q3map2 LIGHT stage):
 
* target and radius : simulating spot lights with cone falloff
 
* target and radius : simulating spot lights with cone falloff
 
* _sun flag - infinite distance lights, constant direction
 
* _sun flag - infinite distance lights, constant direction
Line 37: Line 46:
  
 
Rough shading algorithm explanation:
 
Rough shading algorithm explanation:
  <font color=green>// Surface's vertex normals are always supplied, texture normalmap is forced to default RGB '128 128 255' if not found</font>
+
  <font color=green>// Surface's vertex normals are always supplied, texture normalmap is forced to default RGB '0.5 0.5 1' (same as surface normal) if not found</font>
 
  PixelNormal = Surface.VertexNormal + Texture.NormalMap;
 
  PixelNormal = Surface.VertexNormal + Texture.NormalMap;
 
  <font color=green>// LightSource are only defined for dynamic lights</font>
 
  <font color=green>// LightSource are only defined for dynamic lights</font>
 
  <font color=green>// LightmapGlobal.AmbientLight is r_ambient cvar</font>
 
  <font color=green>// LightmapGlobal.AmbientLight is r_ambient cvar</font>
  <font color=green>// Material.AmbientLight is set by '''dprtlightambient''' matrial keyword and only applicable to dynamic lights</font>
+
  <font color=green>// Material.AmbientLight is set by '''dprtlightambient''' material keyword and only applicable to dynamic lights</font>
 
  AmbientLight = LightSource.AmbientLight + LightmapGlobal.AmbientLight + Material.AmbientLight;
 
  AmbientLight = LightSource.AmbientLight + LightmapGlobal.AmbientLight + Material.AmbientLight;
  <font color=green>// Since LightSource are only defined for dynamic lights, for all static lighting it defaults to 1</font>
+
  if (LightSource)
DiffuseLight = LightSource.DiffuseLight;
+
    DiffuseLight = LightSource.DiffuseLight;
  Shading = dotproduct(PixelNormal, LightVector) * DiffuseLight + AmbientLightAmount;
+
else
 +
    DiffuseLight = 1; <font color=green>// Static lighting</font>
 +
  Shading = dotproduct(PixelNormal, LightVector) * DiffuseLight + AmbientLight;
  
 
{{cvar|r_shadow_usenormalmap|Enables use of normalmap texture for lighting. Setting this to to will make only vertex normals to be used.}}
 
{{cvar|r_shadow_usenormalmap|Enables use of normalmap texture for lighting. Setting this to to will make only vertex normals to be used.}}
Line 53: Line 64:
 
{{cvar|r_ambient|Amount of ambient light to be added to all surfaces. Makes level brighter.}}
 
{{cvar|r_ambient|Amount of ambient light to be added to all surfaces. Makes level brighter.}}
  
 +
{{grammar}}
 
==Specular==
 
==Specular==
Specular are optional and could be tweaked per-material.  
+
Specular (or gloss) is the mirror-like reflection of light from a surface, in which light from a single incoming direction (a ray) is reflected into a single outgoing direction. Many well-known surfaces (metal, plastic, wood) is recognized by specularity they have.
  
 
Specular offers two parameters to mess with:
 
Specular offers two parameters to mess with:
* Specular exponent : intensity of specular effect, this could be very high or very low. Basically its just a modifier to gloss texture.
+
* Specular multiplier: intensity of specular effect, this could be very high or very low. It is multiplier to gloss texture.
* Specular power : how 'sharp' gloss is, high values are used to make texture to be plastic-like, while lower ones are suitable for matte surfaces. Basically its just a modifier to gloss texture's alpha channel (which is forced to 1 if not supplied).
+
* Specular exponent: how 'sharp' gloss is, high values are used to make texture to be plastic-like, while lower ones are suitable for matte surfaces. Basically its just a modifier to gloss texture's alpha channel (which is forced to 1 if not supplied).
  
 
Gloss may be forced (see r_shadow_gloss 2 below), in this case texture, if missing its own gloss map, gets a white image for gloss map and parameters from a cvars.
 
Gloss may be forced (see r_shadow_gloss 2 below), in this case texture, if missing its own gloss map, gets a white image for gloss map and parameters from a cvars.
Line 68: Line 80:
 
  <font color=green>// Global.GlossExponent is controlled by r_shadow_glossexponent or r_shadow_glossexponent2 if gloss is forced</font>
 
  <font color=green>// Global.GlossExponent is controlled by r_shadow_glossexponent or r_shadow_glossexponent2 if gloss is forced</font>
 
  <font color=green>// Material.GlossExponentModis set by '''dpglossexponentmod''' material keyword</font>
 
  <font color=green>// Material.GlossExponentModis set by '''dpglossexponentmod''' material keyword</font>
  SpecularExponent = Texture.GlossMap.Alpha * Global.GlossExponent * Material.GlossExponentMod;
+
  if (Texture.GlossMap.Alpha)
 +
    SpecularExponent = Texture.GlossMap.Alpha * Global.GlossExponent * Material.GlossExponentMod;
 +
else
 +
    SpecularExponent = Global.GlossExponent * Material.GlossExponentMod;
 
  <font color=green>// this is rough specular calculation</font>
 
  <font color=green>// this is rough specular calculation</font>
 
  <font color=green>// optionally, engine can use real reflection map to get specular normal (see r_shadow_glossexact below)</font>
 
  <font color=green>// optionally, engine can use real reflection map to get specular normal (see r_shadow_glossexact below)</font>
 
  SpecularNormal = PixelNormal + EyeVector;
 
  SpecularNormal = PixelNormal + EyeVector;
 
  Specular = SpecularColor * power(dotproduct(PixelNormal, SpecularNormal), SpecularExponent)
 
  Specular = SpecularColor * power(dotproduct(PixelNormal, SpecularNormal), SpecularExponent)
 +
{{important|Forced gloss (gloss 2) which is used if texture's gloss map is missed, are only used on outside map to simulate wet surfaces effect.}}
  
 
{{cvar|r_shadow_glossintensity|Global intensity for specular calculations, default is 1.}}
 
{{cvar|r_shadow_glossintensity|Global intensity for specular calculations, default is 1.}}
Line 81: Line 97:
  
 
==Shadows==
 
==Shadows==
Shadows are most valuable part of realtime lighting. They are increasing scene depth so it looks more realistic. Like with materials, power has a price: shadows are quite complex render task, many lights casting many shadows may decrease rendering speed significantly.  
+
Shadows are most valuable part of realtime lighting. They are increasing scene depth so it looks more realistic.  
  
{{important|Map designer should plan his map with this limitation in mind - there should be no situation of many lights being seen from a certain point, or user will experience a game slowdown.}}
+
{{important|Shadows are quite complex render task, many lights casting many shadows may decrease rendering speed significantly. Map designer should plan his map with this limitation in mind - there should be no situation of many lights being seen from a certain point, or user will experience a game slowdown.}}
  
Darkplaces supports two realtime shadowing techniques.
+
Darkplaces supports two realtime shadowing techniques: stencil shadow volumes and shadow mapping.
  
===Stencil shadows===
+
===Stencil shadow volumes===
 
[http://en.wikipedia.org/wiki/Shadow_volume Stencil shadow volumes] is a base shadow rendering method in Darkplaces.  
 
[http://en.wikipedia.org/wiki/Shadow_volume Stencil shadow volumes] is a base shadow rendering method in Darkplaces.  
  
Line 97: Line 113:
 
{{cvar|r_showshadows|Show areas covered by shadow volumes. Useful for finding out why some areas of the map render slowly <nowiki>(bright blue = lots of passes, slow)</nowiki>. Only matters if using shadow volumes.}}
 
{{cvar|r_showshadows|Show areas covered by shadow volumes. Useful for finding out why some areas of the map render slowly <nowiki>(bright blue = lots of passes, slow)</nowiki>. Only matters if using shadow volumes.}}
  
===Shadowmapping===
+
===Shadow mapping===
Since 2010 [http://en.wikipedia.org/wiki/Shadow_mapping shadow mapping] was implemented by [http://sauerbraten.org/ Eihrul].  
+
In 2010 darkplaces got [http://en.wikipedia.org/wiki/Shadow_mapping shadow mapping] implemented by [http://sauerbraten.org/ Eihrul].  
  
 
Shadowmapping have a number of advantages over shadow volume rendering and is considered to replace it:
 
Shadowmapping have a number of advantages over shadow volume rendering and is considered to replace it:
Line 105: Line 121:
 
* Takes less CPU time (as construction of shadow volumes is not required)
 
* Takes less CPU time (as construction of shadow volumes is not required)
 
* Distance-based LOD (far lights rendered with lower shadowmap resolution)
 
* Distance-based LOD (far lights rendered with lower shadowmap resolution)
* Shadow edges are not sharp
 
  
 
{{hidden begin|Console variables}}
 
{{hidden begin|Console variables}}
{{cvar|r_shadow_shadowmapping|Enables shadowmapping}}
+
{{cvar|r_shadow_shadowmapping|Enables shadow mapping}}
 
{{cvar|r_shadow_shadowmapping|Shadowmap [[bias]] parameter (this is multiplied by nearclip * 1024 / lodsize}}
 
{{cvar|r_shadow_shadowmapping|Shadowmap [[bias]] parameter (this is multiplied by nearclip * 1024 / lodsize}}
 
{{cvar|r_shadow_shadowmapping_bordersize|Shadowmap size bias for filtering}}
 
{{cvar|r_shadow_shadowmapping_bordersize|Shadowmap size bias for filtering}}
Line 116: Line 131:
 
{{cvar|r_shadow_shadowmapping_minsize|Shadowmap size limit}}
 
{{cvar|r_shadow_shadowmapping_minsize|Shadowmap size limit}}
 
{{cvar|r_shadow_shadowmapping_nearclip|Shadowmap near clip in world units. Increasing this will make shadow rendering to be more precise (as more bits goes to middle range), at the cost of the small non-shadowed zone around light}}
 
{{cvar|r_shadow_shadowmapping_nearclip|Shadowmap near clip in world units. Increasing this will make shadow rendering to be more precise (as more bits goes to middle range), at the cost of the small non-shadowed zone around light}}
{{cvar|r_shadow_shadowmapping_polygonfactor|Slope-dependent shadowmapping bias}}
+
{{cvar|r_shadow_shadowmapping_polygonfactor|Slope-dependent shadow mapping bias}}
{{cvar|r_shadow_shadowmapping_polygonoffset|Constant shadowmapping bias}}
+
{{cvar|r_shadow_shadowmapping_polygonoffset|Constant shadow mapping bias}}
 
{{cvar|r_shadow_shadowmapping_precision|Makes shadowmaps have a maximum resolution of this number of pixels per light source radius unit such that, for example, at precision 0.5 a light with radius 200 will have a maximum resolution of 100 pixels}}
 
{{cvar|r_shadow_shadowmapping_precision|Makes shadowmaps have a maximum resolution of this number of pixels per light source radius unit such that, for example, at precision 0.5 a light with radius 200 will have a maximum resolution of 100 pixels}}
 
{{cvar|r_shadow_shadowmapping_useshadowsampler|whether to use sampler2DShadow if available}}
 
{{cvar|r_shadow_shadowmapping_useshadowsampler|whether to use sampler2DShadow if available}}
Line 130: Line 145:
 
'''Fake light'''
 
'''Fake light'''
 
:A developer-only mode which is forced on non-lit maps (ones which was compiled and didnt get LIGHT phase). This is infinite realtime light that is cast from eye position.
 
:A developer-only mode which is forced on non-lit maps (ones which was compiled and didnt get LIGHT phase). This is infinite realtime light that is cast from eye position.
 +
'''Sun light'''
 +
:A similar to fake light technique to render sun lighting on outdoor surfaces, Used for sunset and sun dawn effects on outdoor maps.
 
'''Cel shading'''
 
'''Cel shading'''
 
:Altered shading for default lighting model.
 
:Altered shading for default lighting model.
 +
:{{cvar|r_celshading|Enable cel shading (alternate diffuse math)}}
 
'''Lightmap'''
 
'''Lightmap'''
 
:Lightmap with no deluxemap applied (just an old lightmap with no per-pixel lighting effects)
 
:Lightmap with no deluxemap applied (just an old lightmap with no per-pixel lighting effects)
Line 137: Line 155:
 
:No lighting applied (all textures at their full brightness)
 
:No lighting applied (all textures at their full brightness)
  
{{navigation footer|Lighting methods}}
+
{{navigation footer|Special effects}}
 +
{{finished}}

Latest revision as of 12:50, 15 February 2013

Darkplaces lighting | previous chapter: Introduction

Contents

Lighting visualized. Blue is shadow volumes, bright orange is zones being processed for dynamic realtime lighting.

Darkplaces uses a variation of Phong shading as a default lighting model. It is based on per-pixel lighting - a technique of computing lighting equations for each pixel independently. Renderer contains lots of tricks to allow shading to be fast and flexible. Default model is used on both GL2.0 rendering paths and pre-shader rendering paths (GL1.4), although not all lighting features are possible on pre-shader fixed rendering pipeline.

Lighting are breaked into the passes:

  1. Static lighting (lightmap or lightgrid or vertex lighting)
  2. Dynamic light 1
  3. Dynamic light 2
  4. ...
  5. Dynamic light X
  6. Photon Mapping (optional)
  7. Fog (optional)

Data used for each light pass:

  • Vertex normal : values between vertices of pixel's triangle are interpolated using Gouraud shading
  • Color Map : base texture (best way to use diffuse texture)
  • Normal Map : texture storing additional surface curvature (optionally, alpha storing height)
  • Gloss Map : texture used for specular reflection. Contains gloss color (RGB) and optional exponent multiplier (alpha)
  • Glow Map : texture's local luminance texture, RGB additive blended texture that entirely ignores shading
  • Light Vector : vector of light direction (either calculated for realtime light or got from deluxemap lightmap component)
  • Eye Vector : being computed for each pixel, this vector is used for specular calculation.

This gallery illustrates how lighting features changes the quality:

[edit] Attenuation

Dynamic lights in Darkplaces engine are omnidirectional, using linear falloff.

 Spotlights and projected dynamic lights could be simulated with cubemap filters.

Static lights can have a variety of options for controlling attenuation (this options is only used in Q3map2 LIGHT stage):

  • target and radius : simulating spot lights with cone falloff
  • _sun flag - infinite distance lights, constant direction
  • _deviance and _samples - non-point lights

[edit] Shading

Shading is done by using dotproduct of light vector and pixel normal. Some ambient lighting could be added for a more smooth result.

Important: Lightmaps by default dont have shading since they lack light direction data. In order to make lightmaps to use whole shading cycle, map compiler tool should be configured to make deluxemaps (second lightmap set which store per-pixel light directions)

Rough shading algorithm explanation:

// Surface's vertex normals are always supplied, texture normalmap is forced to default RGB '0.5 0.5 1' (same as surface normal) if not found
PixelNormal = Surface.VertexNormal + Texture.NormalMap;
// LightSource are only defined for dynamic lights
// LightmapGlobal.AmbientLight is r_ambient cvar
// Material.AmbientLight is set by dprtlightambient material keyword and only applicable to dynamic lights
AmbientLight = LightSource.AmbientLight + LightmapGlobal.AmbientLight + Material.AmbientLight;
if (LightSource)
    DiffuseLight = LightSource.DiffuseLight;
else
    DiffuseLight = 1; // Static lighting
Shading = dotproduct(PixelNormal, LightVector) * DiffuseLight + AmbientLight;
 r_shadow_usenormalmap : Enables use of normalmap texture for lighting. Setting this to to will make only vertex normals to be used.
 r_shadow_bumpscale_basetexture : Forces normalmap to be automatically calculated from color texture by using it as bumpmap, value controls magnitude. Requires r_restart.
 r_shadow_bumpscale_bumpmap : Normalmap can be supplied as _bump texture, in this case engine converts it to normalmap on load, values is magnitude. Requires r_restart.
 r_glsl_deluxemapping : Use full shading on lightmapped surfaces. A value of 2 forces deluxemap shading even if surface have no one.
 r_ambient : Amount of ambient light to be added to all surfaces. Makes level brighter.

[edit] Specular

Specular (or gloss) is the mirror-like reflection of light from a surface, in which light from a single incoming direction (a ray) is reflected into a single outgoing direction. Many well-known surfaces (metal, plastic, wood) is recognized by specularity they have.

Specular offers two parameters to mess with:

  • Specular multiplier: intensity of specular effect, this could be very high or very low. It is multiplier to gloss texture.
  • Specular exponent: how 'sharp' gloss is, high values are used to make texture to be plastic-like, while lower ones are suitable for matte surfaces. Basically its just a modifier to gloss texture's alpha channel (which is forced to 1 if not supplied).

Gloss may be forced (see r_shadow_gloss 2 below), in this case texture, if missing its own gloss map, gets a white image for gloss map and parameters from a cvars.

Rough specular algorithm explanation:

// Global.GlossIntensity is controlled by r_shadow_glossintensity or r_shadow_glossintensity2 if gloss is forced
// Material.GlossIntensityMod is set by dpglossintensitymod material keyword
SpecularColor = Texture.GlossMap * Global.GlossIntensity * Material.GlossIntensityMod;
// Global.GlossExponent is controlled by r_shadow_glossexponent or r_shadow_glossexponent2 if gloss is forced
// Material.GlossExponentModis set by dpglossexponentmod material keyword
if (Texture.GlossMap.Alpha)
    SpecularExponent = Texture.GlossMap.Alpha * Global.GlossExponent * Material.GlossExponentMod;
else
    SpecularExponent = Global.GlossExponent * Material.GlossExponentMod;
// this is rough specular calculation
// optionally, engine can use real reflection map to get specular normal (see r_shadow_glossexact below)
SpecularNormal = PixelNormal + EyeVector;
Specular = SpecularColor * power(dotproduct(PixelNormal, SpecularNormal), SpecularExponent)
Important: Forced gloss (gloss 2) which is used if texture's gloss map is missed, are only used on outside map to simulate wet surfaces effect.
 r_shadow_glossintensity : Global intensity for specular calculations, default is 1.
 r_shadow_gloss2intensity : Global intensity for specular calculations applied for forced-gloss surfaces, default is 1.
 r_shadow_glossexponent : Global gloss exponent used as a base in shader calculations.
 r_shadow_gloss2exponent : Same one used for forced-gloss surfaces.
 r_shadow_glossexact : Use real reflection math for gloss calculation. This is slower and little more correct.

[edit] Shadows

Shadows are most valuable part of realtime lighting. They are increasing scene depth so it looks more realistic.

Important: Shadows are quite complex render task, many lights casting many shadows may decrease rendering speed significantly. Map designer should plan his map with this limitation in mind - there should be no situation of many lights being seen from a certain point, or user will experience a game slowdown.

Darkplaces supports two realtime shadowing techniques: stencil shadow volumes and shadow mapping.

[edit] Stencil shadow volumes

Stencil shadow volumes is a base shadow rendering method in Darkplaces.

This technique is well known for it's shadows not having penumbra. Many other restrictions (high fillrate hit, bad scalability) make this method to be used only on a few games (such as Doom 3).

 r_shadow_polygonfactor : how much to enlarge shadow volume polygons when rendering (should be 0!)
 r_shadow_polygonoffset : how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)
 r_shadow_frontsidecasting : whether to cast shadows from illuminated triangles (front side of model) or unlit triangles (back side of model)
 r_showshadows : Show areas covered by shadow volumes. Useful for finding out why some areas of the map render slowly (bright blue = lots of passes, slow). Only matters if using shadow volumes.

[edit] Shadow mapping

In 2010 darkplaces got shadow mapping implemented by Eihrul.

Shadowmapping have a number of advantages over shadow volume rendering and is considered to replace it:

  • Penumbra
  • Fast to render (especially on complex area maps)
  • Takes less CPU time (as construction of shadow volumes is not required)
  • Distance-based LOD (far lights rendered with lower shadowmap resolution)

[edit] Other light models

Darkplaces includes other lighting models which are used eventually:

Fake light

A developer-only mode which is forced on non-lit maps (ones which was compiled and didnt get LIGHT phase). This is infinite realtime light that is cast from eye position.

Sun light

A similar to fake light technique to render sun lighting on outdoor surfaces, Used for sunset and sun dawn effects on outdoor maps.

Cel shading

Altered shading for default lighting model.
 r_celshading : Enable cel shading (alternate diffuse math)

Lightmap

Lightmap with no deluxemap applied (just an old lightmap with no per-pixel lighting effects)

Fullbright

No lighting applied (all textures at their full brightness)



Next chapter: Special effects
Back to index | Top of the page

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox