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();
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseLight3D::BaseLight3D()
|
|
|
|
: _intensity(1.0f)
|
|
|
|
, _enabled(true)
|
|
|
|
, _lightFlag(LightFlag::LIGHT0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
BaseLight3D::~BaseLight3D()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
DirectionLight3D* DirectionLight3D::create(const Vec3 &direction, const Color3B &color)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectionLight3D::setDirection(const Vec3 &dir)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
const Vec3& DirectionLight3D::getDirection() const
|
|
|
|
{
|
|
|
|
static Vec3 dir;
|
|
|
|
Mat4 mat = getNodeToParentTransform();
|
|
|
|
dir.set(-mat.m[8], -mat.m[9], -mat.m[10]);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
const Vec3& DirectionLight3D::getDirectionInWorld() const
|
|
|
|
{
|
|
|
|
static Vec3 dir;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PointLight3D::PointLight3D()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
PointLight3D::~PointLight3D()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
SpotLight3D* SpotLight3D::create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotLight3D::setDirection(const Vec3 &dir)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec3& SpotLight3D::getDirection() const
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec3& SpotLight3D::getDirectionInWorld() const
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotLight3D::setRange(float range)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
float SpotLight3D::getRange()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotLight3D::setInnerAngle(float angle)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
float SpotLight3D::getInnerAngle()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotLight3D::setOuterAngle(float angle)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
float SpotLight3D::getOuterAngle()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
2014-08-20 16:20:22 +08:00
|
|
|
Light3D::Light3D()
|
|
|
|
: _isEnabled(true)
|
|
|
|
, _range(0.0)
|
|
|
|
, _innerAngle(0.0)
|
|
|
|
, _outerAngle(0.0)
|
2014-08-15 10:32:07 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
Light3D::~Light3D()
|
2014-08-15 10:32:07 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-11 12:05:49 +08:00
|
|
|
Light3D* Light3D::createDirectionalLight( const Vec3 &direction, const Color3B &color )
|
2014-08-20 16:20:22 +08:00
|
|
|
{
|
|
|
|
Light3D *light = new Light3D;
|
2014-09-11 15:46:32 +08:00
|
|
|
light->_lightType = Light3D::LightType::DIRECTIONAL;
|
2014-08-20 16:20:22 +08:00
|
|
|
light->calculateRotation(direction);
|
|
|
|
light->setColor(color);
|
|
|
|
light->autorelease();
|
|
|
|
return light;
|
|
|
|
}
|
|
|
|
|
2014-09-11 12:05:49 +08:00
|
|
|
Light3D* Light3D::createPointLight( const Vec3 &position, const Color3B &color, float range )
|
2014-08-20 16:20:22 +08:00
|
|
|
{
|
|
|
|
Light3D *light = new Light3D;
|
2014-09-11 15:46:32 +08:00
|
|
|
light->_lightType = Light3D::LightType::POINT;
|
2014-08-20 16:20:22 +08:00
|
|
|
light->setPosition3D(position);
|
|
|
|
light->setColor(color);
|
|
|
|
light->_range = range;
|
|
|
|
light->autorelease();
|
|
|
|
return light;
|
|
|
|
}
|
|
|
|
|
2014-09-11 12:05:49 +08:00
|
|
|
Light3D* Light3D::createSpotLight( const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range )
|
2014-08-20 16:20:22 +08:00
|
|
|
{
|
|
|
|
Light3D *light = new Light3D;
|
2014-09-11 15:46:32 +08:00
|
|
|
light->_lightType = Light3D::LightType::SPOT;
|
2014-08-20 16:20:22 +08:00
|
|
|
light->calculateRotation(direction);
|
|
|
|
light->setPosition3D(position);
|
|
|
|
light->setColor(color);
|
|
|
|
light->_innerAngle = innerAngle;
|
|
|
|
light->_outerAngle = outerAngle;
|
|
|
|
light->_range = range;
|
|
|
|
light->autorelease();
|
|
|
|
return light;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
void Light3D::setLightType( LightType lightType )
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
_lightType = lightType;
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
|
|
|
|
Light3D::LightType Light3D::getLightType()
|
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
return _lightType;
|
2014-08-15 19:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Light3D::setRange( float range )
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
_range = range;
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
float Light3D::getRange()
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
return _range;
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
void Light3D::setInnerAngle( float angle )
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
_innerAngle = angle;
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
float Light3D::getInnerAngle()
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
return _innerAngle;
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
void Light3D::setOuterAngle( float angle )
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
_outerAngle = angle;
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
float Light3D::getOuterAngle()
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
return _outerAngle;
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
void Light3D::onEnter()
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
auto scene = getScene();
|
|
|
|
if (scene)
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
auto &lights = scene->_lights;
|
2014-08-15 19:15:14 +08:00
|
|
|
auto iter = std::find(lights.begin(), lights.end(), this);
|
|
|
|
if (iter == lights.end())
|
|
|
|
lights.push_back(this);
|
|
|
|
}
|
2014-08-25 13:58:15 +08:00
|
|
|
Node::onEnter();
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
void Light3D::onExit()
|
2014-08-15 14:51:23 +08:00
|
|
|
{
|
2014-08-15 19:15:14 +08:00
|
|
|
auto scene = getScene();
|
|
|
|
if (scene)
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
auto &lights = scene->_lights;
|
2014-08-15 19:15:14 +08:00
|
|
|
auto iter = std::find(lights.begin(), lights.end(), this);
|
|
|
|
if (iter != lights.end())
|
|
|
|
lights.erase(iter);
|
|
|
|
}
|
2014-08-25 13:58:15 +08:00
|
|
|
Node::onExit();
|
2014-08-15 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 12:05:49 +08:00
|
|
|
void Light3D::setEnabled( bool enabled )
|
2014-08-19 13:54:30 +08:00
|
|
|
{
|
2014-09-11 12:05:49 +08:00
|
|
|
_isEnabled = enabled;
|
2014-08-19 13:54:30 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 16:20:22 +08:00
|
|
|
bool Light3D::getEnabled()
|
2014-08-19 13:54:30 +08:00
|
|
|
{
|
2014-08-20 16:20:22 +08:00
|
|
|
return _isEnabled;
|
2014-08-19 13:54:30 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 16:20:22 +08:00
|
|
|
void Light3D::calculateRotation( const Vec3 &direction )
|
2014-08-19 13:54:30 +08:00
|
|
|
{
|
2014-08-20 16:41:45 +08:00
|
|
|
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));
|
2014-08-20 16:20:22 +08:00
|
|
|
setRotation3D(Vec3(rotX, rotY, 0.0f));
|
2014-08-19 13:54:30 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 16:20:22 +08:00
|
|
|
void Light3D::setDirection( const Vec3 &dir )
|
2014-08-19 15:51:30 +08:00
|
|
|
{
|
2014-08-20 16:20:22 +08:00
|
|
|
calculateRotation(dir);
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 16:20:22 +08:00
|
|
|
Vec3 Light3D::getDirection() const
|
2014-08-19 15:51:30 +08:00
|
|
|
{
|
2014-08-20 16:20:22 +08:00
|
|
|
Mat4 mat = getNodeToParentTransform();
|
2014-09-11 12:05:49 +08:00
|
|
|
// mat.m[12] = mat.m[13] = mat.m[14] = 0.0f;
|
|
|
|
// mat.inverse();
|
|
|
|
// mat.transpose();
|
|
|
|
//
|
|
|
|
// Vec3 dir;
|
|
|
|
// mat.transformVector(0.0f, 0.0f, -1.0f, 0.0f, &dir);
|
|
|
|
//
|
|
|
|
// return dir;
|
|
|
|
|
|
|
|
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
|
2014-08-20 16:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vec3 Light3D::getWorldDirection() const
|
|
|
|
{
|
|
|
|
Mat4 mat = getNodeToWorldTransform();
|
2014-09-11 12:05:49 +08:00
|
|
|
// mat.m[12] = mat.m[13] = mat.m[14] = 0.0f;
|
|
|
|
// mat.inverse();
|
|
|
|
// mat.transpose();
|
|
|
|
//
|
|
|
|
// Vec3 dir;
|
|
|
|
// mat.transformVector(0.0f, 0.0f, -1.0f, 0.0f, &dir);
|
|
|
|
//
|
|
|
|
// return dir;
|
|
|
|
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:15:14 +08:00
|
|
|
NS_CC_END
|