代码:
enum class LightType// 光线类型
{
DIRECTIONAL = 0,
POINT = 1,
SPOT = 2,
AMBIENT = 3,
};
enum class LightFlag//光线标识
{
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,
LIGHT10 = 1 << 10,
LIGHT11 = 1 << 11,
LIGHT12 = 1 << 12,
LIGHT13 = 1 << 13,
LIGHT14 = 1 << 14,
LIGHT15 = 1 << 15,
};
/**
@js NA
*/
// 基础光
class CC_DLL BaseLight : public Node
{
public:
/**
* Get the light type,light type MUST be one of LightType::DIRECTIONAL ,
* LightType::POINT, LightType::SPOT, LightType::AMBIENT.
*/
// 得到光线的类型
virtual LightType getLightType() const = 0;
/** intensity getter and setter */
// 得到光的强度
float getIntensity() const { return _intensity; }
// 设置光的强度
void setIntensity(float intensity);
/**light flag getter and setter*/
// 得到光的标识
LightFlag getLightFlag() const { return _lightFlag; }
// 设置光的标识
void setLightFlag(LightFlag flag) { _lightFlag = flag; }
/**
* light enabled getter and setter.
*/
// 设置光线是否可用
void setEnabled(bool enabled) { _enabled = enabled; }
// 光线是否可用
bool isEnabled() const { return _enabled; }
//override
virtual void onEnter() override;
virtual void onExit() override;
CC_CONSTRUCTOR_ACCESS:
BaseLight();
virtual ~BaseLight();
protected:
void setRotationFromDirection( const Vec3 &direction );
protected:
float _intensity;
LightFlag _lightFlag;
bool _enabled;
};
/**
@js NA
*/
// 平行光
class CC_DLL DirectionLight : public BaseLight
{
public:
/**
* Creates a direction light.
* @param direction The light's direction
* @param color The light's color.
*
* @return The new direction light.
*/
// 创建一个平行光
static DirectionLight* create(const Vec3 &direction, const Color3B &color);
//get light type
// 得到光的类型
virtual LightType getLightType() const override { return LightType::DIRECTIONAL; }
/**
* Sets the Direction in parent.
*
* @param dir The Direction in parent.
*/
// 设置光的方向
void setDirection(const Vec3 &dir);
/**
* Returns the Direction in parent.
*/
// 得到光的方向
Vec3 getDirection() const;
/**
* Returns direction in world.
*/
// 得到光在世界坐标中的方向
Vec3 getDirectionInWorld() const;
CC_CONSTRUCTOR_ACCESS:
DirectionLight();
virtual ~DirectionLight();
};
/**
@js NA
*/
// 点光源
class CC_DLL PointLight : public BaseLight
{
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.
*/
// 创建一个点光源
static PointLight* create(const Vec3 &position, const Color3B &color, float range);
//get light type
// 得到光的类型
virtual LightType getLightType() const override { return LightType::POINT; }
/** get or set range */
// 得到光的范围
float getRange() const { return _range; }
// 设置光的范围
void setRange(float range) { _range = range; }
CC_CONSTRUCTOR_ACCESS:
PointLight();
virtual ~PointLight();
protected:
float _range;
};
/**
@js NA
*/
// 聚光灯
class CC_DLL SpotLight : public BaseLight
{
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.
*/
// 创建一个聚光灯
static SpotLight* create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range);
//get light type
// 得到光的类型
virtual LightType getLightType() const override { return LightType::SPOT; }
/**
* Sets the Direction in parent.
*
* @param dir The Direction in parent.
*/
// 设置光的方向
void setDirection(const Vec3 &dir);
/**
* Returns the Direction in parent.
*/
// 得到光的方向
Vec3 getDirection() const;
/**
* Returns direction in world.
*/
// 得到光在世界坐标中的方向
Vec3 getDirectionInWorld() const;
/**
* Sets the range of point or spot light.
*
* @param range The range of point or spot light.
*/
// 设置光的范围
void setRange(float range) { _range = range; }
/**
* 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);
/**
* Returns the inner angle the spot light (in radians).
*/
// 得到光的内部角度
float getInnerAngle() const { return _innerAngle; }
/** get cos innerAngle */
// 得到内部角度的cos(余弦)值
float getCosInnerAngle() const { return _cosInnerAngle; }
/**
* Sets the outer angle of a spot light (in radians).
*
* @param outerAngle The angle of spot light (in radians).
*/
// 设置光的外部角度
void setOuterAngle(float angle);
/**
* Returns the outer angle of the spot light (in radians).
*/
// 得到光的外部角度
float getOuterAngle() const { return _outerAngle; }
/** get cos outAngle */
// 得到外部角度的cos(余弦值)
float getCosOuterAngle() const { return _cosOuterAngle; }
CC_CONSTRUCTOR_ACCESS:
SpotLight();
virtual ~SpotLight();
protected:
float _range;
float _innerAngle;
float _cosInnerAngle;
float _outerAngle;
float _cosOuterAngle;
};
/**
@js NA
*/
// 环境光
class CC_DLL AmbientLight : public BaseLight
{
public:
/**
* Creates a ambient light.
* @param color The light's color.
*
* @return The new ambient light.
*/
// 创建一个环境光
static AmbientLight* create(const Color3B &color);
//get light type
// 得到光的类型
virtual LightType getLightType() const override { return LightType::AMBIENT; }
CC_CONSTRUCTOR_ACCESS:
AmbientLight();
virtual ~AmbientLight();
};