axmol/core/2d/CCLight.h

293 lines
7.2 KiB
C
Raw Normal View History

2014-09-17 17:59:10 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2014-09-17 17:59:10 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2014-09-17 17:59:10 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2014-09-28 16:02:12 +08:00
#ifndef __CCLIGHT_H__
#define __CCLIGHT_H__
2014-09-17 17:59:10 +08:00
#include "2d/CCNode.h"
NS_AX_BEGIN
2014-09-17 17:59:10 +08:00
enum class LightType
{
DIRECTIONAL = 0,
2021-12-25 10:04:45 +08:00
POINT = 1,
SPOT = 2,
AMBIENT = 3,
2014-09-17 17:59:10 +08:00
};
enum class LightFlag
{
2021-12-25 10:04:45 +08:00
LIGHT0 = 1,
LIGHT1 = 1 << 1,
LIGHT2 = 1 << 2,
LIGHT3 = 1 << 3,
LIGHT4 = 1 << 4,
LIGHT5 = 1 << 5,
LIGHT6 = 1 << 6,
LIGHT7 = 1 << 7,
LIGHT8 = 1 << 8,
LIGHT9 = 1 << 9,
2014-09-17 17:59:10 +08:00
LIGHT10 = 1 << 10,
LIGHT11 = 1 << 11,
LIGHT12 = 1 << 12,
LIGHT13 = 1 << 13,
LIGHT14 = 1 << 14,
LIGHT15 = 1 << 15,
};
2015-03-19 19:54:46 +08:00
/**
@js NA
*/
2022-07-15 19:17:01 +08:00
class AX_DLL BaseLight : public Node
2014-09-17 17:59:10 +08:00
{
public:
2015-03-20 17:20:22 +08:00
/**
* Get the light type,light type MUST be one of LightType::DIRECTIONAL ,
* LightType::POINT, LightType::SPOT, LightType::AMBIENT.
*/
2014-09-26 14:13:26 +08:00
virtual LightType getLightType() const = 0;
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/** intensity getter and setter */
float getIntensity() const { return _intensity; }
void setIntensity(float intensity);
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**light flag getter and setter*/
LightFlag getLightFlag() const { return _lightFlag; }
void setLightFlag(LightFlag flag) { _lightFlag = flag; }
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* light enabled getter and setter.
*/
void setEnabled(bool enabled) { _enabled = enabled; }
bool isEnabled() const { return _enabled; }
2021-12-25 10:04:45 +08:00
// override
2014-09-17 17:59:10 +08:00
virtual void onEnter() override;
virtual void onExit() override;
2021-12-25 10:04:45 +08:00
BaseLight();
2014-09-28 16:02:12 +08:00
virtual ~BaseLight();
2021-12-25 10:04:45 +08:00
2014-09-17 18:58:35 +08:00
protected:
2021-12-25 10:04:45 +08:00
void setRotationFromDirection(const Vec3& direction);
2014-09-17 17:59:10 +08:00
protected:
2021-12-25 10:04:45 +08:00
float _intensity;
LightFlag _lightFlag;
bool _enabled;
2014-09-17 17:59:10 +08:00
};
2015-03-19 19:54:46 +08:00
/**
@js NA
*/
2022-07-15 19:17:01 +08:00
class AX_DLL DirectionLight : public BaseLight
2014-09-17 17:59:10 +08:00
{
public:
/**
* Creates a direction light.
* @param direction The light's direction
* @param color The light's color.
*
* @return The new direction light.
*/
2021-12-25 10:04:45 +08:00
static DirectionLight* create(const Vec3& direction, const Color3B& color);
// get light type
2014-09-26 14:13:26 +08:00
virtual LightType getLightType() const override { return LightType::DIRECTIONAL; }
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Sets the Direction in parent.
*
* @param dir The Direction in parent.
*/
2021-12-25 10:04:45 +08:00
void setDirection(const Vec3& dir);
2014-09-17 17:59:10 +08:00
/**
* Returns the Direction in parent.
*/
Vec3 getDirection() const;
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Returns direction in world.
*/
Vec3 getDirectionInWorld() const;
2021-12-25 10:04:45 +08:00
DirectionLight();
2014-09-28 16:02:12 +08:00
virtual ~DirectionLight();
2014-09-17 17:59:10 +08:00
};
2015-03-19 19:54:46 +08:00
/**
@js NA
*/
2022-07-15 19:17:01 +08:00
class AX_DLL PointLight : public BaseLight
2014-09-17 17:59:10 +08:00
{
public:
/**
* Creates a point light.
* @param position The light's position
* @param color The light's color.
* @param range The light's range.
*
* @return The new point light.
*/
2021-12-25 10:04:45 +08:00
static PointLight* create(const Vec3& position, const Color3B& color, float range);
// get light type
2014-09-26 14:13:26 +08:00
virtual LightType getLightType() const override { return LightType::POINT; }
2021-12-25 10:04:45 +08:00
2014-09-17 18:58:35 +08:00
/** get or set range */
float getRange() const { return _range; }
void setRange(float range) { _range = range; }
2021-12-25 10:04:45 +08:00
PointLight();
2014-09-28 16:02:12 +08:00
virtual ~PointLight();
2021-12-25 10:04:45 +08:00
2014-09-17 18:58:35 +08:00
protected:
float _range;
2014-09-17 17:59:10 +08:00
};
2015-03-19 19:54:46 +08:00
/**
@js NA
*/
2022-07-15 19:17:01 +08:00
class AX_DLL SpotLight : public BaseLight
2014-09-17 17:59:10 +08:00
{
public:
/**
* Creates a spot light.
* @param direction The light's direction
* @param position The light's position
* @param color The light's color.
* @param innerAngle The light's inner angle (in radians).
* @param outerAngle The light's outer angle (in radians).
* @param range The light's range.
*
* @return The new spot light.
*/
2021-12-25 10:04:45 +08:00
static SpotLight* create(const Vec3& direction,
const Vec3& position,
const Color3B& color,
float innerAngle,
float outerAngle,
float range);
// get light type
2014-09-26 14:13:26 +08:00
virtual LightType getLightType() const override { return LightType::SPOT; }
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Sets the Direction in parent.
*
* @param dir The Direction in parent.
*/
2021-12-25 10:04:45 +08:00
void setDirection(const Vec3& dir);
2014-09-17 17:59:10 +08:00
/**
* Returns the Direction in parent.
*/
Vec3 getDirection() const;
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Returns direction in world.
*/
Vec3 getDirectionInWorld() const;
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Sets the range of point or spot light.
*
* @param range The range of point or spot light.
*/
void setRange(float range) { _range = range; }
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Returns the range of point or spot light.
*
* @return The range of the point or spot light.
*/
float getRange() const { return _range; }
/**
* Sets the inner angle of a spot light (in radians).
*
* @param angle The angle of spot light (in radians).
*/
void setInnerAngle(float angle);
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Returns the inner angle the spot light (in radians).
*/
float getInnerAngle() const { return _innerAngle; }
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/** get cos innerAngle */
float getCosInnerAngle() const { return _cosInnerAngle; }
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Sets the outer angle of a spot light (in radians).
*
* @param outerAngle The angle of spot light (in radians).
*/
void setOuterAngle(float outerAngle);
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/**
* Returns the outer angle of the spot light (in radians).
*/
float getOuterAngle() const { return _outerAngle; }
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
/** get cos outAngle */
2014-10-09 16:16:02 +08:00
float getCosOuterAngle() const { return _cosOuterAngle; }
2021-12-25 10:04:45 +08:00
SpotLight();
2014-09-28 16:02:12 +08:00
virtual ~SpotLight();
2021-12-25 10:04:45 +08:00
2014-09-17 17:59:10 +08:00
protected:
float _range;
float _innerAngle;
float _cosInnerAngle;
float _outerAngle;
float _cosOuterAngle;
};
2015-03-19 19:54:46 +08:00
/**
@js NA
*/
2022-07-15 19:17:01 +08:00
class AX_DLL AmbientLight : public BaseLight
2014-09-17 17:59:10 +08:00
{
public:
/**
* Creates a ambient light.
* @param color The light's color.
*
* @return The new ambient light.
*/
2021-12-25 10:04:45 +08:00
static AmbientLight* create(const Color3B& color);
// get light type
2014-09-26 14:13:26 +08:00
virtual LightType getLightType() const override { return LightType::AMBIENT; }
2021-12-25 10:04:45 +08:00
AmbientLight();
2014-09-28 16:02:12 +08:00
virtual ~AmbientLight();
2014-09-17 17:59:10 +08:00
};
NS_AX_END
2014-09-17 17:59:10 +08:00
#endif