axmol/core/2d/Light.cpp

180 lines
5.1 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axmol.dev/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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/Light.h"
#include "2d/Scene.h"
2019-11-23 20:27:39 +08:00
namespace ax
{
2019-11-23 20:27:39 +08:00
void BaseLight::setIntensity(float intensity)
{
2022-07-16 10:43:05 +08:00
AX_ASSERT(intensity >= 0);
2019-11-23 20:27:39 +08:00
_intensity = intensity;
}
void BaseLight::onEnter()
{
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);
2019-11-23 20:27:39 +08:00
if (iter == lights.end())
lights.emplace_back(this);
2019-11-23 20:27:39 +08:00
}
Node::onEnter();
}
void BaseLight::onExit()
{
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);
2019-11-23 20:27:39 +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)
2019-11-23 20:27:39 +08:00
{
float projLen = sqrt(direction.x * direction.x + direction.z * direction.z);
2022-07-16 10:43:05 +08:00
float rotY = AX_RADIANS_TO_DEGREES(atan2f(-direction.x, -direction.z));
float rotX = -AX_RADIANS_TO_DEGREES(atan2f(-direction.y, projLen));
2019-11-23 20:27:39 +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() {}
2019-11-23 20:27:39 +08:00
////////////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
DirectionLight* DirectionLight::create(const Vec3& direction, const Color3B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto light = new DirectionLight();
2019-11-23 20:27:39 +08:00
light->setRotationFromDirection(direction);
light->setColor(color);
light->autorelease();
return light;
}
2021-12-25 10:04:45 +08:00
void DirectionLight::setDirection(const Vec3& dir)
2019-11-23 20:27:39 +08:00
{
setRotationFromDirection(dir);
}
Vec3 DirectionLight::getDirection() const
{
Mat4 mat = getNodeToParentTransform();
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
}
Vec3 DirectionLight::getDirectionInWorld() const
{
Mat4 mat = getNodeToWorldTransform();
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
}
2021-12-25 10:04:45 +08:00
DirectionLight::DirectionLight() {}
DirectionLight::~DirectionLight() {}
2019-11-23 20:27:39 +08:00
//////////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
PointLight* PointLight::create(const Vec3& position, const Color3B& color, float range)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto light = new PointLight();
2019-11-23 20:27:39 +08:00
light->setPosition3D(position);
light->setColor(color);
light->_range = range;
light->autorelease();
return light;
}
2021-12-25 10:04:45 +08:00
PointLight::PointLight() {}
PointLight::~PointLight() {}
2019-11-23 20:27:39 +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)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto light = new SpotLight();
2019-11-23 20:27:39 +08:00
light->setRotationFromDirection(direction);
light->setPosition3D(position);
light->setColor(color);
light->setInnerAngle(innerAngle);
light->setOuterAngle(outerAngle);
light->_range = range;
light->autorelease();
return light;
}
2021-12-25 10:04:45 +08:00
void SpotLight::setDirection(const Vec3& dir)
2019-11-23 20:27:39 +08:00
{
setRotationFromDirection(dir);
}
Vec3 SpotLight::getDirection() const
{
Mat4 mat = getNodeToParentTransform();
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
}
Vec3 SpotLight::getDirectionInWorld() const
{
Mat4 mat = getNodeToWorldTransform();
return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
}
void SpotLight::setInnerAngle(float angle)
{
2021-12-25 10:04:45 +08:00
_innerAngle = angle;
2019-11-23 20:27:39 +08:00
_cosInnerAngle = cosf(angle);
}
void SpotLight::setOuterAngle(float angle)
{
2021-12-25 10:04:45 +08:00
_outerAngle = angle;
2019-11-23 20:27:39 +08:00
_cosOuterAngle = cosf(angle);
}
2021-12-25 10:04:45 +08:00
SpotLight::SpotLight() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
SpotLight::~SpotLight() {}
2019-11-23 20:27:39 +08:00
/////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
AmbientLight* AmbientLight::create(const Color3B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto light = new AmbientLight();
2019-11-23 20:27:39 +08:00
light->setColor(color);
light->autorelease();
return light;
}
2021-12-25 10:04:45 +08:00
AmbientLight::AmbientLight() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
AmbientLight::~AmbientLight() {}
2019-11-23 20:27:39 +08:00
}