axmol/core/2d/CCLight.cpp

179 lines
5.1 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "2d/CCLight.h"
2014-08-15 14:51:23 +08:00
#include "2d/CCScene.h"
2014-08-15 10:32:07 +08:00
NS_AX_BEGIN
2014-08-15 10:32:07 +08:00
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)
{
2021-12-25 10:04:45 +08:00
auto& lights = scene->_lights;
auto iter = std::find(lights.begin(), lights.end(), this);
2014-09-17 17:59:10 +08:00
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)
{
2021-12-25 10:04:45 +08:00
auto& lights = scene->_lights;
auto iter = std::find(lights.begin(), lights.end(), this);
2014-09-17 17:59:10 +08:00
if (iter != lights.end())
lights.erase(iter);
}
Node::onExit();
}
2021-12-25 10:04:45 +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);
2021-12-25 10:04:45 +08:00
float rotY = CC_RADIANS_TO_DEGREES(atan2f(-direction.x, -direction.z));
float rotX = -CC_RADIANS_TO_DEGREES(atan2f(-direction.y, projLen));
2014-09-17 18:58:35 +08:00
setRotation3D(Vec3(rotX, rotY, 0.0f));
}
2021-12-25 10:04:45 +08:00
BaseLight::BaseLight() : _intensity(1.0f), _lightFlag(LightFlag::LIGHT0), _enabled(true) {}
BaseLight::~BaseLight() {}
2014-09-17 17:59:10 +08:00
////////////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
DirectionLight* DirectionLight::create(const Vec3& direction, const Color3B& color)
2014-09-17 17:59:10 +08:00
{
2021-12-08 00:11:53 +08:00
auto light = new 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
}
2021-12-25 10:04:45 +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
}
Vec3 DirectionLight::getDirection() const
2014-09-17 17:59:10 +08:00
{
Mat4 mat = getNodeToParentTransform();
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
2014-09-17 17:59:10 +08:00
}
Vec3 DirectionLight::getDirectionInWorld() const
2014-09-17 17:59:10 +08:00
{
Mat4 mat = getNodeToWorldTransform();
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
2014-09-17 17:59:10 +08:00
}
2021-12-25 10:04:45 +08:00
DirectionLight::DirectionLight() {}
DirectionLight::~DirectionLight() {}
2014-09-17 17:59:10 +08:00
//////////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
PointLight* PointLight::create(const Vec3& position, const Color3B& color, float range)
2014-09-17 17:59:10 +08:00
{
2021-12-08 00:11:53 +08:00
auto light = new 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
}
2021-12-25 10:04:45 +08:00
PointLight::PointLight() {}
PointLight::~PointLight() {}
2014-09-17 17:59:10 +08:00
//////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +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
{
2021-12-08 00:11:53 +08:00
auto light = new 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
}
2021-12-25 10:04:45 +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
}
Vec3 SpotLight::getDirection() const
2014-09-17 17:59:10 +08:00
{
2014-09-17 18:58:35 +08:00
Mat4 mat = getNodeToParentTransform();
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
2014-09-17 17:59:10 +08:00
}
Vec3 SpotLight::getDirectionInWorld() const
2014-09-17 17:59:10 +08:00
{
2014-09-17 18:58:35 +08:00
Mat4 mat = getNodeToWorldTransform();
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
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
{
2021-12-25 10:04:45 +08:00
_innerAngle = angle;
2014-09-17 18:58:35 +08:00
_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
{
2021-12-25 10:04:45 +08:00
_outerAngle = angle;
_cosOuterAngle = cosf(angle);
2014-09-17 17:59:10 +08:00
}
2021-12-25 10:04:45 +08:00
SpotLight::SpotLight() {}
2021-12-25 10:04:45 +08:00
SpotLight::~SpotLight() {}
2014-09-18 16:38:35 +08:00
/////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
AmbientLight* AmbientLight::create(const Color3B& color)
{
2021-12-08 00:11:53 +08:00
auto light = new AmbientLight();
light->setColor(color);
light->autorelease();
return light;
}
2021-12-25 10:04:45 +08:00
AmbientLight::AmbientLight() {}
2021-12-25 10:04:45 +08:00
AmbientLight::~AmbientLight() {}
2014-08-19 15:51:30 +08:00
NS_AX_END