2014-09-17 17:59:10 +08:00
|
|
|
#include "CCLight3D.h"
|
2014-08-15 14:51:23 +08:00
|
|
|
#include "2d/CCScene.h"
|
2014-08-15 10:32:07 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-09-17 17:59:10 +08:00
|
|
|
void BaseLight3D::setIntensity(float intensity)
|
|
|
|
{
|
|
|
|
CC_ASSERT(intensity >= 0);
|
|
|
|
_intensity = intensity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseLight3D::onEnter()
|
|
|
|
{
|
|
|
|
auto scene = getScene();
|
|
|
|
if (scene)
|
|
|
|
{
|
|
|
|
auto &lights = scene->_lights;
|
|
|
|
auto iter = std::find(lights.begin(), lights.end(), this);
|
|
|
|
if (iter == lights.end())
|
|
|
|
lights.push_back(this);
|
|
|
|
}
|
|
|
|
Node::onEnter();
|
|
|
|
}
|
|
|
|
void BaseLight3D::onExit()
|
|
|
|
{
|
|
|
|
auto scene = getScene();
|
|
|
|
if (scene)
|
|
|
|
{
|
|
|
|
auto &lights = scene->_lights;
|
|
|
|
auto iter = std::find(lights.begin(), lights.end(), this);
|
|
|
|
if (iter != lights.end())
|
|
|
|
lights.erase(iter);
|
|
|
|
}
|
|
|
|
Node::onExit();
|
|
|
|
}
|
|
|
|
|
2014-09-17 18:58:35 +08:00
|
|
|
void BaseLight3D::setRotationFromDirection( const Vec3 &direction )
|
|
|
|
{
|
|
|
|
float projLen = sqrt(direction.x * direction.x + direction.z * direction.z);
|
|
|
|
float rotY = CC_RADIANS_TO_DEGREES(atan2f(-direction.x, -direction.z));
|
|
|
|
float rotX = -CC_RADIANS_TO_DEGREES(atan2f(-direction.y, projLen));
|
|
|
|
setRotation3D(Vec3(rotX, rotY, 0.0f));
|
|
|
|
}
|
|
|
|
|
2014-09-17 17:59:10 +08:00
|
|
|
BaseLight3D::BaseLight3D()
|
|
|
|
: _intensity(1.0f)
|
|
|
|
, _enabled(true)
|
|
|
|
, _lightFlag(LightFlag::LIGHT0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
BaseLight3D::~BaseLight3D()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
DirectionLight3D* DirectionLight3D::create(const Vec3 &direction, const Color3B &color)
|
|
|
|
{
|
2014-09-17 18:58:35 +08:00
|
|
|
auto light = new (std::nothrow) DirectionLight3D();
|
|
|
|
light->setRotationFromDirection(direction);
|
|
|
|
light->setColor(color);
|
|
|
|
light->autorelease();
|
|
|
|
return light;
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DirectionLight3D::setDirection(const Vec3 &dir)
|
|
|
|
{
|
2014-09-17 18:58:35 +08:00
|
|
|
setRotationFromDirection(dir);
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
const Vec3& DirectionLight3D::getDirection() const
|
|
|
|
{
|
|
|
|
static Vec3 dir;
|
|
|
|
Mat4 mat = getNodeToParentTransform();
|
|
|
|
dir.set(-mat.m[8], -mat.m[9], -mat.m[10]);
|
|
|
|
return dir;
|
|
|
|
}
|
2014-09-28 11:03:18 +08:00
|
|
|
const Vec3& DirectionLight3D::getDirectionInWorld() const
|
2014-09-17 17:59:10 +08:00
|
|
|
{
|
2014-09-28 11:03:18 +08:00
|
|
|
static Vec3 dir;
|
2014-09-17 17:59:10 +08:00
|
|
|
Mat4 mat = getNodeToWorldTransform();
|
|
|
|
dir.set(-mat.m[8], -mat.m[9], -mat.m[10]);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
DirectionLight3D::DirectionLight3D()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
DirectionLight3D::~DirectionLight3D()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
PointLight3D* PointLight3D::create(const Vec3 &position, const Color3B &color, float range)
|
|
|
|
{
|
2014-09-17 18:58:35 +08:00
|
|
|
auto light = new (std::nothrow) PointLight3D();
|
|
|
|
light->setPosition3D(position);
|
|
|
|
light->setColor(color);
|
|
|
|
light->_range = range;
|
|
|
|
light->autorelease();
|
|
|
|
return light;
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PointLight3D::PointLight3D()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
PointLight3D::~PointLight3D()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
SpotLight3D* SpotLight3D::create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range)
|
|
|
|
{
|
2014-09-17 18:58:35 +08:00
|
|
|
auto light = new (std::nothrow) SpotLight3D();
|
|
|
|
light->setRotationFromDirection(direction);
|
|
|
|
light->setPosition3D(position);
|
|
|
|
light->setColor(color);
|
|
|
|
light->setInnerAngle(innerAngle);
|
|
|
|
light->setOuterAngle(outerAngle);
|
|
|
|
light->_range = range;
|
|
|
|
light->autorelease();
|
|
|
|
return light;
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpotLight3D::setDirection(const Vec3 &dir)
|
|
|
|
{
|
2014-09-17 18:58:35 +08:00
|
|
|
setRotationFromDirection(dir);
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vec3& SpotLight3D::getDirection() const
|
|
|
|
{
|
2014-09-17 18:58:35 +08:00
|
|
|
static Vec3 dir;
|
|
|
|
Mat4 mat = getNodeToParentTransform();
|
|
|
|
dir.set(-mat.m[8], -mat.m[9], -mat.m[10]);
|
|
|
|
return dir;
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:03:18 +08:00
|
|
|
const Vec3& SpotLight3D::getDirectionInWorld() const
|
2014-09-17 17:59:10 +08:00
|
|
|
{
|
2014-09-28 11:03:18 +08:00
|
|
|
static Vec3 dir;
|
2014-09-17 18:58:35 +08:00
|
|
|
Mat4 mat = getNodeToWorldTransform();
|
|
|
|
dir.set(-mat.m[8], -mat.m[9], -mat.m[10]);
|
|
|
|
return dir;
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpotLight3D::setInnerAngle(float angle)
|
|
|
|
{
|
2014-09-17 18:58:35 +08:00
|
|
|
_innerAngle = angle;
|
|
|
|
_cosInnerAngle = cosf(angle);
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpotLight3D::setOuterAngle(float angle)
|
|
|
|
{
|
2014-09-17 18:58:35 +08:00
|
|
|
_outerAngle = angle;
|
|
|
|
_cosInnerAngle = cosf(angle);
|
2014-09-17 17:59:10 +08:00
|
|
|
}
|
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
SpotLight3D::SpotLight3D()
|
2014-08-15 10:32:07 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
SpotLight3D::~SpotLight3D()
|
2014-08-20 16:20:22 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
AmbientLight3D* AmbientLight3D::create( const Color3B &color )
|
2014-08-20 16:20:22 +08:00
|
|
|
{
|
2014-09-18 16:38:35 +08:00
|
|
|
auto light = new (std::nothrow) AmbientLight3D();
|
2014-08-20 16:20:22 +08:00
|
|
|
light->setColor(color);
|
|
|
|
light->autorelease();
|
|
|
|
return light;
|
|
|
|
}
|
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
AmbientLight3D::AmbientLight3D()
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
AmbientLight3D::~AmbientLight3D()
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
|
|
|
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
NS_CC_END
|