ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
asw::lighting::LightMap Class Reference

A screen sized texture of light, multiplied over the scene. More...

#include <lighting.h>

Collaboration diagram for asw::lighting::LightMap:
[legend]

Classes

struct  Glow
 
struct  Tiles
 

Public Member Functions

void set_ambient (const asw::Color &color)
 Set the colour of unlit areas. Black is pitch dark, white is fully lit.
 
asw::Color get_ambient () const
 Get the colour of unlit areas.
 
void set_camera (const asw::Camera *camera)
 Take positions in world space and convert them with a camera. The camera must outlive the light map, or be cleared first.
 
void set_shadow_mode (ShadowMode mode)
 Set how occluders block light.
 
ShadowMode get_shadow_mode () const
 Get how occluders block light.
 
void update (float dt)
 Advance flicker and pulse. Call once per update.
 
void clear ()
 Remove the lights, glows and tiles added this frame.
 
void add (const Light &light)
 Add a light for this frame.
 
void add_glow (const asw::Texture &texture, const asw::Quad< float > &dest, const asw::Color &tint=asw::Color(255, 255, 255))
 Add a texture that gives off light, such as lava or a lamp sprite. Its colours are added to the light map.
 
void add_tiles (const TileLight &tile_light, const asw::Vec2< float > &position={ })
 Add tile lighting for this frame. The grid must outlive the next render() or draw().
 
void add_occluder (const asw::Polygonf &polygon)
 Add a polygon that blocks light.
 
void add_occluder (const asw::Quad< float > &rect)
 Add a rectangle that blocks light.
 
void clear_occluders ()
 Remove all occluders.
 
const std::vector< asw::Polygonf > & get_occluders () const
 Get the occluders.
 
void draw ()
 Draw the light map and multiply it over the current render target.
 
const asw::Texture & render ()
 Render the light map without drawing it, to draw it yourself. The render target is the same afterwards as before.
 

Private Member Functions

asw::Vec2< float > to_screen (const asw::Vec2< float > &point) const
 
asw::Quad< float > to_screen (const asw::Quad< float > &rect) const
 
const asw::Texture & gradient (asw::Falloff falloff)
 
void prepare_occluders ()
 
bool has_blockers (const asw::Quad< float > &reach) const
 
void draw_light (const Light &light)
 
void draw_shadows (const asw::Vec2< float > &center, const asw::Quad< float > &reach)
 

Private Attributes

asw::Color ambient { 0, 0, 0 }
 
const asw::Camera * camera { nullptr }
 
ShadowMode shadow_mode { ShadowMode::None }
 
double time { 0.0 }
 
std::vector< Light > lights
 
std::vector< Glow > glows
 
std::vector< Tiles > tiles
 
std::vector< asw::Polygonf > occluders
 
std::vector< asw::Polygonf > screen_occluders
 
std::vector< asw::Quad< float > > screen_bounds
 
std::vector< float > screen_winding
 
asw::Polygonf seen
 
std::vector< SDL_Vertex > shadow_vertices
 
std::vector< int > shadow_indices
 
asw::Texture map
 
asw::Texture scratch
 
asw::Vec2< int > map_size
 
std::vector< std::pair< asw::Falloff, asw::Texture > > gradients
 

Detailed Description

A screen sized texture of light, multiplied over the scene.

Each frame: clear(), then add lights, glows and tiles, then draw the scene and call draw(). Occluders stay until clear_occluders(), so walls only need adding once.

Definition at line 204 of file lighting.h.

Member Function Documentation

◆ add()

void asw::lighting::LightMap::add ( const Light & light)

Add a light for this frame.

Definition at line 377 of file lighting.cpp.

◆ add_glow()

void asw::lighting::LightMap::add_glow ( const asw::Texture & texture,
const asw::Quad< float > & dest,
const asw::Color & tint = asw::Color(255, 255, 255) )

Add a texture that gives off light, such as lava or a lamp sprite. Its colours are added to the light map.

Parameters
textureThe texture.
destWhere to draw it.
tintColour to multiply the texture by.

Definition at line 382 of file lighting.cpp.

◆ add_occluder() [1/2]

void asw::lighting::LightMap::add_occluder ( const asw::Polygonf & polygon)

Add a polygon that blocks light.

Definition at line 394 of file lighting.cpp.

◆ add_occluder() [2/2]

void asw::lighting::LightMap::add_occluder ( const asw::Quad< float > & rect)

Add a rectangle that blocks light.

Definition at line 399 of file lighting.cpp.

◆ add_tiles()

void asw::lighting::LightMap::add_tiles ( const TileLight & tile_light,
const asw::Vec2< float > & position = { } )

Add tile lighting for this frame. The grid must outlive the next render() or draw().

Parameters
tile_lightThe grid, after compute().
positionWhere the top left of the grid is.

Definition at line 388 of file lighting.cpp.

◆ clear()

void asw::lighting::LightMap::clear ( )

Remove the lights, glows and tiles added this frame.

Definition at line 370 of file lighting.cpp.

◆ clear_occluders()

void asw::lighting::LightMap::clear_occluders ( )

Remove all occluders.

Definition at line 406 of file lighting.cpp.

◆ draw()

void asw::lighting::LightMap::draw ( )

Draw the light map and multiply it over the current render target.

Definition at line 685 of file lighting.cpp.

◆ draw_light()

void asw::lighting::LightMap::draw_light ( const Light & light)
private

Definition at line 529 of file lighting.cpp.

◆ draw_shadows()

void asw::lighting::LightMap::draw_shadows ( const asw::Vec2< float > & center,
const asw::Quad< float > & reach )
private

Definition at line 472 of file lighting.cpp.

◆ get_ambient()

asw::Color asw::lighting::LightMap::get_ambient ( ) const

Get the colour of unlit areas.

Definition at line 345 of file lighting.cpp.

◆ get_occluders()

const std::vector< asw::Polygonf > & asw::lighting::LightMap::get_occluders ( ) const

Get the occluders.

Definition at line 411 of file lighting.cpp.

◆ get_shadow_mode()

asw::lighting::ShadowMode asw::lighting::LightMap::get_shadow_mode ( ) const

Get how occluders block light.

Definition at line 360 of file lighting.cpp.

◆ gradient()

const asw::Texture & asw::lighting::LightMap::gradient ( asw::Falloff falloff)
private

Definition at line 426 of file lighting.cpp.

◆ has_blockers()

bool asw::lighting::LightMap::has_blockers ( const asw::Quad< float > & reach) const
private

Definition at line 460 of file lighting.cpp.

◆ prepare_occluders()

void asw::lighting::LightMap::prepare_occluders ( )
private

Definition at line 443 of file lighting.cpp.

◆ render()

const asw::Texture & asw::lighting::LightMap::render ( )

Render the light map without drawing it, to draw it yourself. The render target is the same afterwards as before.

Returns
The light map texture, at the logical screen size.

Definition at line 617 of file lighting.cpp.

◆ set_ambient()

void asw::lighting::LightMap::set_ambient ( const asw::Color & color)

Set the colour of unlit areas. Black is pitch dark, white is fully lit.

Definition at line 340 of file lighting.cpp.

◆ set_camera()

void asw::lighting::LightMap::set_camera ( const asw::Camera * camera)

Take positions in world space and convert them with a camera. The camera must outlive the light map, or be cleared first.

Definition at line 350 of file lighting.cpp.

◆ set_shadow_mode()

void asw::lighting::LightMap::set_shadow_mode ( ShadowMode mode)

Set how occluders block light.

Definition at line 355 of file lighting.cpp.

◆ to_screen() [1/2]

asw::Quad< float > asw::lighting::LightMap::to_screen ( const asw::Quad< float > & rect) const
private

Definition at line 421 of file lighting.cpp.

◆ to_screen() [2/2]

asw::Vec2< float > asw::lighting::LightMap::to_screen ( const asw::Vec2< float > & point) const
private

Definition at line 416 of file lighting.cpp.

◆ update()

void asw::lighting::LightMap::update ( float dt)

Advance flicker and pulse. Call once per update.

Parameters
dtThe time in seconds since the last update.

Definition at line 365 of file lighting.cpp.

Member Data Documentation

◆ ambient

asw::Color asw::lighting::LightMap::ambient { 0, 0, 0 }
private

Definition at line 308 of file lighting.h.

◆ camera

const asw::Camera* asw::lighting::LightMap::camera { nullptr }
private

Definition at line 309 of file lighting.h.

◆ glows

std::vector<Glow> asw::lighting::LightMap::glows
private

Definition at line 316 of file lighting.h.

◆ gradients

std::vector<std::pair<asw::Falloff, asw::Texture> > asw::lighting::LightMap::gradients
private

Definition at line 334 of file lighting.h.

◆ lights

std::vector<Light> asw::lighting::LightMap::lights
private

Definition at line 315 of file lighting.h.

◆ map

asw::Texture asw::lighting::LightMap::map
private

Definition at line 331 of file lighting.h.

◆ map_size

asw::Vec2<int> asw::lighting::LightMap::map_size
private

Definition at line 333 of file lighting.h.

◆ occluders

std::vector<asw::Polygonf> asw::lighting::LightMap::occluders
private

Definition at line 318 of file lighting.h.

◆ scratch

asw::Texture asw::lighting::LightMap::scratch
private

Definition at line 332 of file lighting.h.

◆ screen_bounds

std::vector<asw::Quad<float> > asw::lighting::LightMap::screen_bounds
private

Definition at line 323 of file lighting.h.

◆ screen_occluders

std::vector<asw::Polygonf> asw::lighting::LightMap::screen_occluders
private

Definition at line 322 of file lighting.h.

◆ screen_winding

std::vector<float> asw::lighting::LightMap::screen_winding
private

Definition at line 324 of file lighting.h.

◆ seen

asw::Polygonf asw::lighting::LightMap::seen
private

Definition at line 327 of file lighting.h.

◆ shadow_indices

std::vector<int> asw::lighting::LightMap::shadow_indices
private

Definition at line 329 of file lighting.h.

◆ shadow_mode

ShadowMode asw::lighting::LightMap::shadow_mode { ShadowMode::None }
private

Definition at line 310 of file lighting.h.

◆ shadow_vertices

std::vector<SDL_Vertex> asw::lighting::LightMap::shadow_vertices
private

Definition at line 328 of file lighting.h.

◆ tiles

std::vector<Tiles> asw::lighting::LightMap::tiles
private

Definition at line 317 of file lighting.h.

◆ time

double asw::lighting::LightMap::time { 0.0 }
private

Definition at line 313 of file lighting.h.


The documentation for this class was generated from the following files: