axmol/cocos/base/CCLight.cpp

192 lines
4.0 KiB
C++
Raw Normal View History

2014-09-28 16:02:12 +08:00
#include "base/CCLight.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-28 16:02:12 +08:00
void BaseLight::setIntensity(float intensity)
2014-09-17 17:59:10 +08:00
{
CC_ASSERT(intensity >= 0);
_intensity = intensity;
}
2014-09-28 16:02:12 +08:00
void BaseLight::onEnter()
2014-09-17 17:59:10 +08:00
{
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();
}
2014-09-28 16:02:12 +08:00
void BaseLight::onExit()
2014-09-17 17:59:10 +08:00
{
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-28 16:02:12 +08:00
void BaseLight::setRotationFromDirection( const Vec3 &direction )
2014-09-17 18:58:35 +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));
setRotation3D(Vec3(rotX, rotY, 0.0f));
}
2014-09-28 16:02:12 +08:00
BaseLight::BaseLight()
2014-09-17 17:59:10 +08:00
: _intensity(1.0f)
, _enabled(true)
, _lightFlag(LightFlag::LIGHT0)
{
}
2014-09-28 16:02:12 +08:00
BaseLight::~BaseLight()
2014-09-17 17:59:10 +08:00
{
}
////////////////////////////////////////////////////////////////////
2014-09-28 16:02:12 +08:00
DirectionLight* DirectionLight::create(const Vec3 &direction, const Color3B &color)
2014-09-17 17:59:10 +08:00
{
2014-09-28 16:02:12 +08:00
auto light = new (std::nothrow) DirectionLight();
2014-09-17 18:58:35 +08:00
light->setRotationFromDirection(direction);
light->setColor(color);
light->autorelease();
return light;
2014-09-17 17:59:10 +08:00
}
2014-09-28 16:02:12 +08:00
void DirectionLight::setDirection(const Vec3 &dir)
2014-09-17 17:59:10 +08:00
{
2014-09-17 18:58:35 +08:00
setRotationFromDirection(dir);
2014-09-17 17:59:10 +08:00
}
2014-09-28 16:02:12 +08:00
const Vec3& DirectionLight::getDirection() const
2014-09-17 17:59:10 +08:00
{
static Vec3 dir;
Mat4 mat = getNodeToParentTransform();
dir.set(-mat.m[8], -mat.m[9], -mat.m[10]);
return dir;
}
2014-09-28 16:02:12 +08:00
const Vec3& DirectionLight::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;
}
2014-09-28 16:02:12 +08:00
DirectionLight::DirectionLight()
2014-09-17 17:59:10 +08:00
{
}
2014-09-28 16:02:12 +08:00
DirectionLight::~DirectionLight()
2014-09-17 17:59:10 +08:00
{
}
//////////////////////////////////////////////////////////////////
2014-09-28 16:02:12 +08:00
PointLight* PointLight::create(const Vec3 &position, const Color3B &color, float range)
2014-09-17 17:59:10 +08:00
{
2014-09-28 16:02:12 +08:00
auto light = new (std::nothrow) PointLight();
2014-09-17 18:58:35 +08:00
light->setPosition3D(position);
light->setColor(color);
light->_range = range;
light->autorelease();
return light;
2014-09-17 17:59:10 +08:00
}
2014-09-28 16:02:12 +08:00
PointLight::PointLight()
2014-09-17 17:59:10 +08:00
{
}
2014-09-28 16:02:12 +08:00
PointLight::~PointLight()
2014-09-17 17:59:10 +08:00
{
}
//////////////////////////////////////////////////////////////
2014-09-28 16:02:12 +08:00
SpotLight* SpotLight::create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range)
2014-09-17 17:59:10 +08:00
{
2014-09-28 16:02:12 +08:00
auto light = new (std::nothrow) SpotLight();
2014-09-17 18:58:35 +08:00
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
}
2014-09-28 16:02:12 +08:00
void SpotLight::setDirection(const Vec3 &dir)
2014-09-17 17:59:10 +08:00
{
2014-09-17 18:58:35 +08:00
setRotationFromDirection(dir);
2014-09-17 17:59:10 +08:00
}
2014-09-28 16:02:12 +08:00
const Vec3& SpotLight::getDirection() const
2014-09-17 17:59:10 +08:00
{
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 16:02:12 +08:00
const Vec3& SpotLight::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
}
2014-09-28 16:02:12 +08:00
void SpotLight::setInnerAngle(float angle)
2014-09-17 17:59:10 +08:00
{
2014-09-17 18:58:35 +08:00
_innerAngle = angle;
_cosInnerAngle = cosf(angle);
2014-09-17 17:59:10 +08:00
}
2014-09-28 16:02:12 +08:00
void SpotLight::setOuterAngle(float angle)
2014-09-17 17:59:10 +08:00
{
2014-09-17 18:58:35 +08:00
_outerAngle = angle;
_cosInnerAngle = cosf(angle);
2014-09-17 17:59:10 +08:00
}
2014-09-28 16:02:12 +08:00
SpotLight::SpotLight()
2014-08-15 10:32:07 +08:00
{
}
2014-09-28 16:02:12 +08:00
SpotLight::~SpotLight()
{
}
2014-09-18 16:38:35 +08:00
/////////////////////////////////////////////////////////////
2014-09-28 16:02:12 +08:00
AmbientLight* AmbientLight::create( const Color3B &color )
{
2014-09-28 16:02:12 +08:00
auto light = new (std::nothrow) AmbientLight();
light->setColor(color);
light->autorelease();
return light;
}
2014-09-28 16:02:12 +08:00
AmbientLight::AmbientLight()
2014-08-15 14:51:23 +08:00
{
}
2014-09-28 16:02:12 +08:00
AmbientLight::~AmbientLight()
2014-08-15 14:51:23 +08:00
{
2014-08-19 15:51:30 +08:00
}
NS_CC_END