axmol/cocos/3d/CCLight3D.cpp

331 lines
7.2 KiB
C++
Raw Normal View History

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;
}
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)
{
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
}
const Vec3& SpotLight3D::getDirectionInWorld() const
{
2014-09-17 18:58:35 +08:00
static Vec3 dir;
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
}
/////////////////////////////////////////////////////////////
Light3D::Light3D()
: _isEnabled(true)
, _range(0.0)
, _innerAngle(0.0)
, _outerAngle(0.0)
2014-08-15 10:32:07 +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 )
{
Light3D *light = new Light3D;
2014-09-11 15:46:32 +08:00
light->_lightType = Light3D::LightType::DIRECTIONAL;
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 )
{
Light3D *light = new Light3D;
2014-09-11 15:46:32 +08:00
light->_lightType = Light3D::LightType::POINT;
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 )
{
Light3D *light = new Light3D;
2014-09-11 15:46:32 +08:00
light->_lightType = Light3D::LightType::SPOT;
light->calculateRotation(direction);
light->setPosition3D(position);
light->setColor(color);
light->_innerAngle = innerAngle;
light->_outerAngle = outerAngle;
light->_range = range;
light->autorelease();
return light;
}
void Light3D::setLightType( LightType lightType )
2014-08-15 14:51:23 +08:00
{
_lightType = lightType;
2014-08-15 14:51:23 +08:00
}
Light3D::LightType Light3D::getLightType()
{
return _lightType;
}
void Light3D::setRange( float range )
2014-08-15 14:51:23 +08:00
{
_range = range;
2014-08-15 14:51:23 +08:00
}
float Light3D::getRange()
2014-08-15 14:51:23 +08:00
{
return _range;
2014-08-15 14:51:23 +08:00
}
void Light3D::setInnerAngle( float angle )
2014-08-15 14:51:23 +08:00
{
_innerAngle = angle;
2014-08-15 14:51:23 +08:00
}
float Light3D::getInnerAngle()
2014-08-15 14:51:23 +08:00
{
return _innerAngle;
2014-08-15 14:51:23 +08:00
}
void Light3D::setOuterAngle( float angle )
2014-08-15 14:51:23 +08:00
{
_outerAngle = angle;
2014-08-15 14:51:23 +08:00
}
float Light3D::getOuterAngle()
2014-08-15 14:51:23 +08:00
{
return _outerAngle;
2014-08-15 14:51:23 +08:00
}
void Light3D::onEnter()
2014-08-15 14:51:23 +08:00
{
2014-09-17 18:58:35 +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-08-15 14:51:23 +08:00
}
void Light3D::onExit()
2014-08-15 14:51:23 +08:00
{
2014-09-17 18:58:35 +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-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
}
bool Light3D::getEnabled()
2014-08-19 13:54:30 +08:00
{
return _isEnabled;
2014-08-19 13:54:30 +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));
setRotation3D(Vec3(rotX, rotY, 0.0f));
2014-08-19 13:54:30 +08:00
}
void Light3D::setDirection( const Vec3 &dir )
2014-08-19 15:51:30 +08:00
{
calculateRotation(dir);
2014-08-19 15:51:30 +08:00
}
Vec3 Light3D::getDirection() const
2014-08-19 15:51:30 +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]);
}
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
}
NS_CC_END