![]() |
CUGL 4.0
Cornell University Game Library
|
#include <CULight.h>
Public Member Functions | |
| Light (void) | |
| ~Light () | |
| virtual void | dispose () |
| virtual bool | init (Uint32 rays=DEFAULT_RAYS) |
| virtual bool | initWithReach (float reach, Uint32 rays=DEFAULT_RAYS) |
| virtual bool | initWithPosition (const Vec2 &pos, Uint32 rays=DEFAULT_RAYS) |
| virtual bool | initWithPositionReach (const Vec2 &pos, float reach, Uint32 rays=DEFAULT_RAYS) |
| void | setRayNum (Uint32 rays) |
| int | getRayNum () |
| const Vec2 & | getPosition () const |
| float | getX () const |
| float | getY () const |
| void | setPosition (float x, float y) |
| void | setPosition (const Vec2 &position) |
| float | getReach () const |
| void | setReach (float dist) |
| float | getAngle () const |
| void | setAngle (float angle) |
| Color4 | getColor () const |
| void | setColor (Color4 color) |
| void | setColor (float r, float g, float b, float a) |
| float | getGamma () const |
| void | setGamma (float gamma) |
| bool | isActive () const |
| void | setActive (bool active) |
| bool | isXray () const |
| void | setXray (bool xray) |
| bool | isStaticLight () const |
| void | setStaticLight (bool staticLight) |
| bool | isSoft () const |
| void | setSoft (bool soft) |
| float | getSoftness () const |
| void | setSoftness (float factor) |
| bool | getDebug () const |
| void | setDebug (bool flag) |
| bool | contains (float x, float y) |
| virtual bool | contains (const Vec2 &p) |
| const graphics::Mesh< LightVertex > & | getLightMesh () const |
| const graphics::Mesh< graphics::SpriteVertex > & | getDebugMesh () const |
| const Affine2 | getGraphicsTransform () const |
| void | attachBody (b2Body *body) |
| void | attachBody (b2Body *body, const Vec2 &offset) |
| void | attachBody (b2Body *body, const Vec2 &offset, float radians) |
| float | getPhysicsUnits () const |
| void | setPhysicsUnits (float units) |
| bool | doesIgnoreAttachedBody () const |
| void | setIgnoreAttachedBody (bool flag) |
| const b2Filter & | getContactFilter () const |
| void | setContactFilter (const b2Filter &filter) |
| virtual void | detachWorld (const ObstacleWorld *world) override |
| void | update (b2World *world, const Rect &bounds) |
| virtual void | detachWorld (const ObstacleWorld *world) |
Static Public Member Functions | |
| static bool | filters (const b2Filter &filter, b2Fixture *fixture) |
| static const b2Filter & | getGlobalContactFilter () |
| static void | setGlobalContactFilter (const b2Filter &filter) |
Static Public Attributes | |
| static const int | MIN_RAYS |
| static int | DEFAULT_RAYS |
| static float | DEFAULT_REACH |
| static float | DEFAULT_SOFTNESS |
Protected Member Functions | |
| virtual void | initRays ()=0 |
| void | resetRays () |
| virtual void | updateMesh ()=0 |
Protected Attributes | |
| Uint32 | _rayNum |
| Color4 | _color |
| float | _gamma |
| Vec2 | _position |
| float | _angle |
| float | _reach |
| float | _shadowFactor |
| bool | _active |
| bool | _dirty |
| bool | _soft |
| bool | _xray |
| bool | _staticLight |
| b2Body * | _body |
| Vec2 | _bodyOffset |
| float | _bodyAngle |
| float | _units |
| bool | _ignoreBody |
| b2Filter | _contactFilter |
| std::vector< Vec2 > | _lightRays |
| std::vector< Vec2 > | _endPoints |
| std::vector< Vec2 > | _directions |
| std::vector< float > | _fractions |
| LightRaycast | _raycast |
| bool | _debug |
| graphics::Mesh< LightVertex > | _lightMesh |
| graphics::Mesh< graphics::SpriteVertex > | _debugMesh |
Static Protected Attributes | |
| static b2Filter | _globalFilter |
This is an abstract class for all light objects.
This is a fairly minimal class that exists primarily for polymorphic reasons. As a general rule, you will want one of the subclasses, such as PointLight, ConeLight, or DirectionalLight.
While a light can be attached to a box2d body, it does not own the body. That body should be constructed elsewhere such as in an Obstacle object. This class should use the WorldObject interface to be notified when the underlying world has been released.
Note that lights have no methods for rendering. That is done via the classes LightBatch. The methods getLightMesh and getDebugMesh extract meshes to be used in LightBatch or graphics::SpriteBatch, respectively.
As an abstract class there are no static constructors.
| cugl::physics2::lights::Light::Light | ( | void | ) |
Creates a light with no mesh or drawing information.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.
|
inline |
Deletes this light, disposing all resources.
|
inline |
Attaches the light to the specified the body
When attached, the light will automatically follow this body unless stated otherwise. Note that the body rotation angle is taken into account for the light offset and direction calculations.
Note that lights do not own box2d bodies. The body should created (and deleted) separately, either manually or part of an Obstacle object.
| body | The body to attach this light to |
|
inline |
Attaches the light to the specified body with the relative offset
When attached, the light will automatically follow this body. Note that the body rotation angle is taken into account for the light offset and direction calculations.
Note that lights do not own box2d bodies. The body should created (and deleted) separately, either manually or part of an Obstacle object.
| body | The body to attach this light to |
| offset | The relative offset in world coordinates |
| void cugl::physics2::lights::Light::attachBody | ( | b2Body * | body, |
| const Vec2 & | offset, | ||
| float | radians | ||
| ) |
Attaches the light to the specified body with the relative offset and direction
When attached, the light will automatically follow this body unless stated otherwise. Note that the body rotation angle is taken into account for the light offset and direction calculations.
Note that lights do not own box2d bodies. The body should created (and deleted) separately, either manually or part of an Obstacle object.
| body | The body to attach this light to |
| offset | The relative offset in world coordinates |
| radians | The angle offset offset in radians |
|
inlinevirtual |
Returns true if given point is inside of this light area
| p | the point in world coordinates |
Reimplemented in cugl::physics2::lights::ChainLight, cugl::physics2::lights::ConeLight, cugl::physics2::lights::DirectionalLight, and cugl::physics2::lights::PointLight.
|
inline |
Returns true if given point is inside of this light area
| x | the horizontal position of point in world coordinates |
| y | the vertical position of point in world coordinates |
|
inlineoverridevirtual |
Detatches the given obstacle world from this object.
This method ensures safe clean-up.
| world | The world to detatch. |
Reimplemented from cugl::physics2::WorldObject.
|
virtual |
Deletes the light, freeing all resources.
You must reinitialize the light to use it.
Reimplemented in cugl::physics2::lights::ChainLight.
|
inline |
Returns true if the attached body fixtures will be ignored during raycasting
If the value is true, all the fixtures of attached body will be ignored and will not create any shadows for this light. By default, this value is set to false.
|
static |
Returns true if the given filter omits the given fixture.
This method is used to eliminate any fixtures that would be filtered out during raycasting.
| filter | The filter to apply |
| fixture | The fixture to test |
|
inline |
Returns the angle of the light in radians
For lights with direction, the angle is typically the "facing" of the light.
|
inline |
Returns the current color of this light
The RGB values set the color while the alpha value sets the intensity. Note that you can also use colorless (e.g Color4#BLACK light with shadows. By default this is a light grey color.
|
inline |
Returns the contact filter for this light
In raycasting, this filter is combined with getGlobalContactFilter to remove any fixtures that should be ignored.
|
inline |
Returns true if debugging is enabled.
If this attribute is true, getDebugMesh will return a non-empty mesh for debugging.
|
inline |
Returns the debug mesh for this light.
This mesh is a wireframe that can be passed to graphics::SpriteBatch for drawing, or used in a scene2::MeshNode. The coordinates of the mesh are in the light coordinate space, meaning that they the origin is the light position. They are scaled by the getPhysicsUnits. This means that you should use getGraphicsTransform to draw this mesh to a sprite batch.
Note that if getDebug is not true, this mesh will be empty.
|
inline |
Returns the gamma correction factor for this light.
This value is used to adjust the light color in the LightBatch#draw method.
|
inlinestatic |
Returns the contact filter for all lights.
In raycasting, this filter is combined with getContactFilter to remove any fixtures that should be ignored.
| const Affine2 cugl::physics2::lights::Light::getGraphicsTransform | ( | ) | const |
Returns a transform for displaying graphics attached to this light.
Obstacles are generally used to transform a 2d graphics object on the screen. This includes getDebugMesh. Passing this transform to a graphics::SpriteBatch will display the object in its proper location.
|
inline |
Returns the light mesh for this light.
This light mesh can be passed to a LightBatch for drawing. Vertices in a light mesh are in absolute worlds coordinates and do not need to be transformed before sending them to the light batch.
|
inline |
Returns the physics units for this light.
Physics units are the number of pixels per box2d unit. These values are used to create the debug mesh.
|
inline |
Returns the position of the light
All positions are measured in box2d world coordinates. If this light has an attached box2d body, that body will determine this position.
|
inline |
Returns the number of rays set for this light
|
inline |
Returns the light reach
The reach is the distance that the light extends from its position.
NOTE: The minimum value should be capped to 0.1f meter. In addition, this value does not include any gamma correction.
|
inline |
Returns the softness value for the beam tips.
This value is multiplied by the reach of the light to get the length of the soft shadows. The default value is 0.1f
|
inline |
Returns the horizontal position of the light
All positions are measured in box2d world coordinates. If this light has an attached box2d body, that body will determine this position.
|
inline |
Returns the vertical starting position of light
All positions are measured in box2d world coordinates. If this light has an attached box2d body, that body will determine this position.
|
inlinevirtual |
Initializes a new light with the given number of rays.
All attributes will have their default values. The light will be placed at the world origin, and will have the default reach. The light color will be a transluscent light grey.
The number of rays cannot be less than MIN_RAYS. More rays make the light look more realistic but will decrease performance.
| rays | The number of rays |
Reimplemented in cugl::physics2::lights::DirectionalLight.
|
protectedpure virtual |
Creates the ray template and associated data structures
This method should be called after any action that would alter the ray template. Such examples include changing the reach or the number of rays.
This method is specific to each light type
Implemented in cugl::physics2::lights::ChainLight, cugl::physics2::lights::ConeLight, cugl::physics2::lights::DirectionalLight, and cugl::physics2::lights::PointLight.
|
inlinevirtual |
Initializes a new light with the given number of rays and position
All other attributes will have their default values. The light will have the default reach. The light color will be a transluscent light grey.
The number of rays cannot be less than MIN_RAYS. More rays make the light look more realistic but will decrease performance.
| pos | The light position |
| rays | The number of rays |
Reimplemented in cugl::physics2::lights::DirectionalLight.
|
virtual |
Initializes a new light with the given number of rays, position, and reach
All other attributes will have their default values. The light color will be a transluscent light grey.
The number of rays cannot be less than MIN_RAYS. More rays make the light look more realistic but will decrease performance.
| pos | The light position |
| reach | The light reach |
| rays | The number of rays |
Reimplemented in cugl::physics2::lights::DirectionalLight.
|
inlinevirtual |
Initializes a new light with the given number of rays and reach
All other attributes will have their default values. The light will be placed at the world origin. The light color will be a transluscent light grey.
The number of rays cannot be less than MIN_RAYS. More rays make the light look more realistic but will decrease performance.
| reach | The light reach |
| rays | The number of rays |
Reimplemented in cugl::physics2::lights::DirectionalLight.
|
inline |
Returns true if this light is active.
An enabled light will respond to calls to update and LightMap#render.
|
inline |
Returns true if the tips of the light beams are soft
This value is true by default.
|
inline |
Returns true if this light is static
Static lights do not get any automatic updates, but any changes to one of its parameters will update it. Static lights are useful for lights that you want to collide with static geometry but ignore all the dynamic objects. This can reduce the CPU burden of the light by about 90%.
|
inline |
Returns true if this light beams go through obstacles
Enabling this will allow beams to go through obstacles that reduce CPU burden of light about 70%. Use the combination of x-ray and non x-ray lights wisely,
|
protected |
Resets the rays to the current orientation.
This extends all rays to their full lemgth. It should be called just before ray casting.
|
inline |
Enables/disables this light.
An enabled light will respond to calls to update() and LightMap#render.
| active | Whether to enable this light |
|
inline |
Sets the angle of the light in radians
For lights with direction, the angle is typically the "facing" of the light.
| angle | The angle in radians |
|
inline |
Sets the light color
The RGB values set the color while the alpha value sets the intensity. Note that you can also use colorless (e.g Color4#BLACK light with shadows. By default this is a light grey color.
Note that this color will not be committed to mesh until a call to update.
| color | The light color |
|
inline |
Sets the light color
The RGB values set the color while the alpha value sets the intensity. Note that you can also use colorless (e.g Color4#BLACK light with shadows. By default this is a light grey color.
Note that this color will not be committed to mesh until a call to update.
| r | the red component |
| g | the green component |
| b | the blue component |
| a | the shadow intensity |
|
inline |
Sets the contact filter for this light
In raycasting, this filter is combined with getGlobalContactFilter to remove any fixtures that should be ignored.
| filter | The contact filter |
|
inline |
Sets whether debugging is enabled.
If this attribute is true, getDebugMesh will return a non-empty mesh for debugging.
| flag | Whether debugging should be enabled. |
|
inline |
Sets the gamma correction factor for this light.
This value is used to adjust the light color in the LightBatch#draw method.
| gamma | the gamma correction factor for this light. |
|
inlinestatic |
Sets the contact filter for all lights.
In raycasting, this filter is combined with getContactFilter to remove any fixtures that should be ignored.
| filter | The contact filter |
|
inline |
Sets if the attached body fixtures should be ignored during raycasting
If the value is true, all the fixtures of attached body will be ignored and will not create any shadows for this light. By default, this value is set to false.
| flag | Whether to ignore the attached body. |
|
inline |
Sets the physics units for this light.
Physics units are the number of pixels per box2d unit. These values are used to create the debug mesh.
| units | The physics units for this light |
|
inline |
Sets the light starting position
All positions are measured in box2d world coordinates. If this light has an attached box2d body, that body will determine this position. In particular, setting this value is only relevant if there is no attached body. Otherwise this value will be overwritten at the next call to update.
| position | The light position |
|
inline |
Sets the light starting position
All positions are measured in box2d world coordinates. If this light has an attached box2d body, that body will determine this position. In particular, setting this value is only relevant if there is no attached body. Otherwise this value will be overwritten at the next call to update.
| x | The x-coordinate of the light |
| y | The y-coordinate of the light |
| void cugl::physics2::lights::Light::setRayNum | ( | Uint32 | rays | ) |
Sets the number of rays for this light.
This is an internal method to update the associated mesh.
| rays | The number of rays for this light |
|
inline |
Sets the light reach
The reach is the distance that the light extends from its position.
NOTE: The minimum value should be capped to 0.1f meter. In addition, this value does not include any gamma correction.
| dist | The light reach |
|
inline |
Enables/disables softness on tips of the light beams
This value is true by default.
| soft | Whether to enable soft tips |
|
inline |
Sets the softness value for the beam tips.
This value is multiplied by the reach of the light to get the length of the soft shadows. The default value is 0.1f
| factor | The softness value for the beam tips. |
|
inline |
Enables/disables light static behavior
Static lights do not get any automatic updates, but any changes to one of its parameters will update it. Static lights are useful for lights that you want to collide with static geometry but ignore all the dynamic objects. This can reduce the CPU burden of the light by about 90%.
| staticLight | Whether to make this light static |
|
inline |
Enables/disables x-ray beams for this light
Enabling this will allow beams to go through obstacles that reduce CPU burden of light about 70%. Use the combination of x-ray and non x-ray lights wisely,
| xray | Whether to enable x-ray beams |
| void cugl::physics2::lights::Light::update | ( | b2World * | world, |
| const Rect & | bounds | ||
| ) |
Updates this light according to box2d.
The bounds are used for culling. Rays outside of the bounds are not processed and are set to 0.
| world | The box2d world |
| bounds | The world bounds |
|
protectedpure virtual |
Updates the mesh(es) associated with this light.
This will always update the light mesh. It will only update the debug mesh if getDebug is true.
Implemented in cugl::physics2::lights::ChainLight, cugl::physics2::lights::ConeLight, cugl::physics2::lights::DirectionalLight, and cugl::physics2::lights::PointLight.
|
protected |
Whether this light is active (inactive lights do not update or render)
|
protected |
The current light angle in radians
|
protected |
The attached body (if any)
|
protected |
The angle offset of the light from the body
|
protected |
The positional offset from the body
|
protected |
The light color
|
protected |
The light specific filter
|
protected |
Whether to compute the debug mesh
|
protected |
The mesh for debugging (generated on demand)
|
protected |
The normalized direction of each ray
|
protected |
Whether this light requires an update
|
protected |
The current ray endpoints
|
protected |
The fraction of each ray that is illuminated
|
protected |
The gamma correction factor
|
staticprotected |
The global filter for all lights
|
protected |
Whether this light ignores the fixtures of the attached body
|
protected |
The mesh for rendering the light
|
protected |
The unmodified light rays for this light
|
protected |
The current light position
|
protected |
The raycasting function for this light
|
protected |
The number of light rays
|
protected |
The light reach (e.g. how far the light extends)
|
protected |
The percentage of the reach to make as soft shadows
|
protected |
Whether this light has soft tips
|
protected |
Whether this light ignores dynamic game elements
|
protected |
The physics units for this light
|
protected |
Whether this light shines through obstacles
|
static |
The default number of rays to use
|
static |
The default reach for a light (in box2d units)
|
static |
The default shadow softness (in box2d units)
|
static |
The minimum supported number of rays