2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2019 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
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.
|
|
|
|
|
|
|
|
Code based GamePlay3D's Camera: http://gameplay3d.org
|
|
|
|
|
|
|
|
****************************************************************************/
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/Camera.h"
|
|
|
|
#include "2d/CameraBackgroundBrush.h"
|
|
|
|
#include "platform/GLView.h"
|
|
|
|
#include "2d/Scene.h"
|
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/QuadCommand.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Camera* Camera::_visitingCamera = nullptr;
|
|
|
|
Viewport Camera::_defaultViewport;
|
|
|
|
|
|
|
|
// start static methods
|
|
|
|
|
|
|
|
Camera* Camera::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Camera* camera = new Camera();
|
2022-08-11 10:52:38 +08:00
|
|
|
camera->initDefault();
|
2019-11-23 20:27:39 +08:00
|
|
|
camera->autorelease();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera* Camera::createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new Camera();
|
|
|
|
ret->initPerspective(fieldOfView, aspectRatio, nearPlane, farPlane);
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Camera* Camera::createOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new Camera();
|
|
|
|
ret->initOrthographic(zoomX, zoomY, nearPlane, farPlane);
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Camera* Camera::getDefaultCamera()
|
|
|
|
{
|
2022-07-01 12:17:08 +08:00
|
|
|
// camera nullptr scene init fix #690
|
|
|
|
|
2022-07-01 10:54:20 +08:00
|
|
|
auto scene = Director::getInstance()->getRunningScene();
|
2022-07-01 12:17:08 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(scene, "Scene is not done initializing, please use this->_defaultCamera instead.");
|
2022-07-01 12:17:08 +08:00
|
|
|
|
|
|
|
return scene->getDefaultCamera();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Viewport& Camera::getDefaultViewport()
|
|
|
|
{
|
|
|
|
return _defaultViewport;
|
|
|
|
}
|
|
|
|
void Camera::setDefaultViewport(const Viewport& vp)
|
|
|
|
{
|
|
|
|
_defaultViewport = vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Camera* Camera::getVisitingCamera()
|
|
|
|
{
|
|
|
|
return _visitingCamera;
|
|
|
|
}
|
|
|
|
|
|
|
|
// end static methods
|
|
|
|
|
|
|
|
Camera::Camera()
|
2022-06-25 10:15:31 +08:00
|
|
|
: _eyeZdistance(1)
|
|
|
|
, _zoomFactor(1)
|
|
|
|
, _nearPlane(-1024)
|
|
|
|
, _farPlane(1024)
|
|
|
|
, _zoomFactorNearPlane(10)
|
|
|
|
, _zoomFactorFarPlane(1024)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// minggo comment
|
|
|
|
// _frustum.setClipZ(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera::~Camera()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_clearBrush);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Mat4& Camera::getProjectionMatrix() const
|
|
|
|
{
|
|
|
|
return _projection;
|
|
|
|
}
|
|
|
|
const Mat4& Camera::getViewMatrix() const
|
|
|
|
{
|
|
|
|
Mat4 viewInv(getNodeToWorldTransform());
|
|
|
|
static int count = sizeof(float) * 16;
|
|
|
|
if (memcmp(viewInv.m, _viewInv.m, count) != 0)
|
|
|
|
{
|
|
|
|
_viewProjectionDirty = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_frustumDirty = true;
|
|
|
|
_viewInv = viewInv;
|
|
|
|
_view = viewInv.getInversed();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return _view;
|
|
|
|
}
|
|
|
|
void Camera::lookAt(const Vec3& lookAtPos, const Vec3& up)
|
|
|
|
{
|
|
|
|
Vec3 upv = up;
|
|
|
|
upv.normalize();
|
|
|
|
Vec3 zaxis;
|
|
|
|
Vec3::subtract(this->getPosition3D(), lookAtPos, &zaxis);
|
|
|
|
zaxis.normalize();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3 xaxis;
|
|
|
|
Vec3::cross(upv, zaxis, &xaxis);
|
|
|
|
xaxis.normalize();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3 yaxis;
|
|
|
|
Vec3::cross(zaxis, xaxis, &yaxis);
|
|
|
|
yaxis.normalize();
|
2021-12-25 10:04:45 +08:00
|
|
|
Mat4 rotation;
|
2022-06-25 10:15:31 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
rotation.m[0] = xaxis.x;
|
|
|
|
rotation.m[1] = xaxis.y;
|
|
|
|
rotation.m[2] = xaxis.z;
|
|
|
|
rotation.m[3] = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
rotation.m[4] = yaxis.x;
|
|
|
|
rotation.m[5] = yaxis.y;
|
|
|
|
rotation.m[6] = yaxis.z;
|
|
|
|
rotation.m[7] = 0;
|
2022-06-25 10:15:31 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
rotation.m[8] = zaxis.x;
|
|
|
|
rotation.m[9] = zaxis.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
rotation.m[10] = zaxis.z;
|
|
|
|
rotation.m[11] = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
Quaternion quaternion;
|
|
|
|
Quaternion::createFromRotationMatrix(rotation, &quaternion);
|
2019-11-23 20:27:39 +08:00
|
|
|
quaternion.normalize();
|
|
|
|
setRotationQuat(quaternion);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Mat4& Camera::getViewProjectionMatrix() const
|
|
|
|
{
|
|
|
|
getViewMatrix();
|
|
|
|
if (_viewProjectionDirty)
|
|
|
|
{
|
|
|
|
_viewProjectionDirty = false;
|
|
|
|
Mat4::multiply(_projection, _view, &_viewProjection);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return _viewProjection;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::setAdditionalProjection(const Mat4& mat)
|
|
|
|
{
|
|
|
|
_projection = mat * _projection;
|
|
|
|
getViewProjectionMatrix();
|
|
|
|
}
|
|
|
|
|
2022-08-11 10:52:38 +08:00
|
|
|
void Camera::initDefault()
|
2022-08-11 02:09:10 +08:00
|
|
|
{
|
|
|
|
auto& size = _director->getWinSize();
|
|
|
|
switch (_director->getProjection())
|
|
|
|
{
|
|
|
|
case Director::Projection::_2D:
|
|
|
|
{
|
|
|
|
_fieldOfView = 60.0F;
|
|
|
|
_nearPlane = -1024.0F;
|
|
|
|
_farPlane = 1024.0F;
|
2022-08-11 10:52:38 +08:00
|
|
|
initOrthographic(size.width, size.height, _nearPlane, _farPlane);
|
2022-08-11 02:09:10 +08:00
|
|
|
setPosition3D(Vec3(size.width / 2.0F, size.height / 2.0F, 0.f));
|
|
|
|
setRotation3D(Vec3(0.f, 0.f, 0.f));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Director::Projection::_3D:
|
|
|
|
{
|
|
|
|
float zeye = _director->getZEye();
|
|
|
|
_fieldOfView = 60.0F;
|
|
|
|
_nearPlane = 0.5F;
|
|
|
|
_farPlane = zeye + size.height / 2.0f;
|
2022-08-11 10:52:38 +08:00
|
|
|
initPerspective(_fieldOfView, (float)size.width / size.height, _nearPlane, _farPlane);
|
2022-08-11 02:09:10 +08:00
|
|
|
Vec3 eye(size.width / 2.0f, size.height / 2.0f, zeye), center(size.width / 2.0f, size.height / 2.0f, 0.0f),
|
|
|
|
up(0.0f, 1.0f, 0.0f);
|
|
|
|
setPosition3D(eye);
|
|
|
|
lookAt(center, up);
|
2022-08-11 10:52:38 +08:00
|
|
|
_eyeZdistance = eye.z;
|
2022-08-11 02:09:10 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 10:52:38 +08:00
|
|
|
setDepth(0);
|
2022-12-29 16:54:01 +08:00
|
|
|
|
|
|
|
if (_zoomFactor != 1.0F)
|
|
|
|
applyZoom();
|
2022-08-11 02:09:10 +08:00
|
|
|
}
|
|
|
|
|
2022-08-11 01:14:18 +08:00
|
|
|
void Camera::updateTransform()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-06-25 10:15:31 +08:00
|
|
|
auto& size = _director->getWinSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
// create default camera
|
2022-12-29 16:54:26 +08:00
|
|
|
switch (_director->getProjection())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-12-29 16:54:26 +08:00
|
|
|
case Director::Projection::_2D:
|
2022-08-11 01:14:18 +08:00
|
|
|
{
|
|
|
|
initOrthographic(size.width, size.height, _nearPlane, _farPlane);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-29 16:54:26 +08:00
|
|
|
case Director::Projection::_3D:
|
2022-08-11 01:14:18 +08:00
|
|
|
{
|
|
|
|
float zeye = _director->getZEye();
|
|
|
|
initPerspective(_fieldOfView, (float)size.width / size.height, _nearPlane, _farPlane);
|
|
|
|
Vec3 eye(size.width / 2.0f, size.height / 2.0f, zeye), center(size.width / 2.0f, size.height / 2.0f, 0.0f),
|
|
|
|
up(0.0f, 1.0f, 0.0f);
|
|
|
|
_eyeZdistance = eye.z;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera::initPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
|
|
|
|
{
|
2022-06-25 10:15:31 +08:00
|
|
|
_fieldOfView = fieldOfView;
|
|
|
|
_nearPlane = nearPlane;
|
|
|
|
_farPlane = farPlane;
|
|
|
|
|
|
|
|
if (_zoomFactorFarPlane == 1024)
|
|
|
|
_zoomFactorFarPlane = farPlane;
|
|
|
|
if (_zoomFactorNearPlane == 10)
|
|
|
|
_zoomFactorNearPlane = nearPlane;
|
|
|
|
|
2022-08-11 01:14:18 +08:00
|
|
|
Mat4::createPerspective(_fieldOfView, aspectRatio, _nearPlane, _farPlane, &_projection);
|
2019-11-23 20:27:39 +08:00
|
|
|
_viewProjectionDirty = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_frustumDirty = true;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera::initOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_zoom[0] = zoomX;
|
|
|
|
_zoom[1] = zoomY;
|
2019-11-23 20:27:39 +08:00
|
|
|
_nearPlane = nearPlane;
|
2021-12-25 10:04:45 +08:00
|
|
|
_farPlane = farPlane;
|
2022-06-25 10:15:31 +08:00
|
|
|
Mat4::createOrthographic(_zoom[0] * _zoomFactor, _zoom[1] * _zoomFactor, _nearPlane, _farPlane, &_projection);
|
2019-11-23 20:27:39 +08:00
|
|
|
_viewProjectionDirty = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_frustumDirty = true;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 Camera::project(const Vec3& src) const
|
|
|
|
{
|
|
|
|
Vec2 screenPos;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
auto viewport = _director->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec4 clipPos;
|
|
|
|
getViewProjectionMatrix().transformVector(Vec4(src.x, src.y, src.z, 1.0f), &clipPos);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(clipPos.w != 0.0f, "clipPos.w can't be 0.0f!");
|
2019-11-23 20:27:39 +08:00
|
|
|
float ndcX = clipPos.x / clipPos.w;
|
|
|
|
float ndcY = clipPos.y / clipPos.w;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
screenPos.x = (ndcX + 1.0f) * 0.5f * viewport.width;
|
|
|
|
screenPos.y = (1.0f - (ndcY + 1.0f) * 0.5f) * viewport.height;
|
|
|
|
return screenPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 Camera::projectGL(const Vec3& src) const
|
|
|
|
{
|
|
|
|
Vec2 screenPos;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
auto viewport = _director->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec4 clipPos;
|
|
|
|
getViewProjectionMatrix().transformVector(Vec4(src.x, src.y, src.z, 1.0f), &clipPos);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-06-25 10:15:31 +08:00
|
|
|
if (clipPos.w == 0.0f)
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("WARNING: Camera's clip position w is 0.0! a black screen should be expected.");
|
2022-06-25 10:15:31 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float ndcX = clipPos.x / clipPos.w;
|
|
|
|
float ndcY = clipPos.y / clipPos.w;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
screenPos.x = (ndcX + 1.0f) * 0.5f * viewport.width;
|
|
|
|
screenPos.y = (ndcY + 1.0f) * 0.5f * viewport.height;
|
|
|
|
return screenPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec3 Camera::unproject(const Vec3& src) const
|
|
|
|
{
|
|
|
|
Vec3 dst;
|
2021-04-22 22:01:47 +08:00
|
|
|
unproject(_director->getWinSize(), &src, &dst);
|
2019-11-23 20:27:39 +08:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec3 Camera::unprojectGL(const Vec3& src) const
|
|
|
|
{
|
|
|
|
Vec3 dst;
|
2021-04-22 22:01:47 +08:00
|
|
|
unprojectGL(_director->getWinSize(), &src, &dst);
|
2019-11-23 20:27:39 +08:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
void Camera::unproject(const Vec2& viewport, const Vec3* src, Vec3* dst) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(src && dst, "vec3 can not be null");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec4 screen(src->x / viewport.width, ((viewport.height - src->y)) / viewport.height, src->z, 1.0f);
|
|
|
|
screen.x = screen.x * 2.0f - 1.0f;
|
|
|
|
screen.y = screen.y * 2.0f - 1.0f;
|
|
|
|
screen.z = screen.z * 2.0f - 1.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
getViewProjectionMatrix().getInversed().transformVector(screen, &screen);
|
|
|
|
if (screen.w != 0.0f)
|
|
|
|
{
|
|
|
|
screen.x /= screen.w;
|
|
|
|
screen.y /= screen.w;
|
|
|
|
screen.z /= screen.w;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
dst->set(screen.x, screen.y, screen.z);
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
void Camera::unprojectGL(const Vec2& viewport, const Vec3* src, Vec3* dst) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(src && dst, "vec3 can not be null");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec4 screen(src->x / viewport.width, src->y / viewport.height, src->z, 1.0f);
|
|
|
|
screen.x = screen.x * 2.0f - 1.0f;
|
|
|
|
screen.y = screen.y * 2.0f - 1.0f;
|
|
|
|
screen.z = screen.z * 2.0f - 1.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
getViewProjectionMatrix().getInversed().transformVector(screen, &screen);
|
|
|
|
if (screen.w != 0.0f)
|
|
|
|
{
|
|
|
|
screen.x /= screen.w;
|
|
|
|
screen.y /= screen.w;
|
|
|
|
screen.z /= screen.w;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
dst->set(screen.x, screen.y, screen.z);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Camera::isVisibleInFrustum(const AABB* aabb) const
|
|
|
|
{
|
|
|
|
if (_frustumDirty)
|
|
|
|
{
|
|
|
|
_frustum.initFrustum(this);
|
|
|
|
_frustumDirty = false;
|
|
|
|
}
|
|
|
|
return !_frustum.isOutOfFrustum(*aabb);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
float Camera::getDepthInView(const Mat4& transform) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Mat4 camWorldMat = getNodeToWorldTransform();
|
|
|
|
const Mat4& viewMat = camWorldMat.getInversed();
|
|
|
|
float depth = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] +
|
|
|
|
viewMat.m[14]);
|
2019-11-23 20:27:39 +08:00
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::setDepth(int8_t depth)
|
|
|
|
{
|
|
|
|
if (_depth != depth)
|
|
|
|
{
|
|
|
|
_depth = depth;
|
|
|
|
if (_scene)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// notify scene that the camera order is dirty
|
2019-11-23 20:27:39 +08:00
|
|
|
_scene->setCameraOrderDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 10:15:31 +08:00
|
|
|
void Camera::setZoom(float factor)
|
|
|
|
{
|
|
|
|
_zoomFactor = factor;
|
|
|
|
applyZoom();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::applyZoom()
|
|
|
|
{
|
2022-08-11 01:38:59 +08:00
|
|
|
switch (_director->getProjection())
|
2022-06-25 10:15:31 +08:00
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
case ax::Director::Projection::_2D:
|
2022-06-25 10:15:31 +08:00
|
|
|
{
|
|
|
|
Mat4::createOrthographic(_zoom[0] * _zoomFactor, _zoom[1] * _zoomFactor, _nearPlane, _farPlane, &_projection);
|
|
|
|
break;
|
|
|
|
}
|
2022-08-08 18:02:17 +08:00
|
|
|
case ax::Director::Projection::_3D:
|
2022-06-25 10:15:31 +08:00
|
|
|
{
|
|
|
|
// Push the far plane farther the more we zoom out.
|
|
|
|
if (_zoomFactorFarPlane * _zoomFactor > _farPlane)
|
|
|
|
{
|
|
|
|
_farPlane = _zoomFactorFarPlane * _zoomFactor;
|
2022-08-11 01:14:18 +08:00
|
|
|
Camera::updateTransform();
|
2022-06-25 10:15:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push the near plane closer the more we zoom in.
|
|
|
|
if (_zoomFactorNearPlane * _zoomFactor < _nearPlane)
|
|
|
|
{
|
|
|
|
_nearPlane = _zoomFactorNearPlane * _zoomFactor;
|
2022-08-11 01:14:18 +08:00
|
|
|
Camera::updateTransform();
|
2022-06-25 10:15:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this->setPositionZ(_eyeZdistance * _zoomFactor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void Camera::onEnter()
|
|
|
|
{
|
|
|
|
if (_scene == nullptr)
|
|
|
|
{
|
|
|
|
auto scene = getScene();
|
|
|
|
if (scene)
|
|
|
|
{
|
|
|
|
setScene(scene);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Node::onEnter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::onExit()
|
|
|
|
{
|
|
|
|
// remove this camera from scene
|
|
|
|
setScene(nullptr);
|
|
|
|
Node::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::setScene(Scene* scene)
|
|
|
|
{
|
|
|
|
if (_scene != scene)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// remove old scene
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_scene)
|
|
|
|
{
|
|
|
|
auto& cameras = _scene->_cameras;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto it = std::find(cameras.begin(), cameras.end(), this);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (it != cameras.end())
|
|
|
|
cameras.erase(it);
|
|
|
|
_scene = nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// set new scene
|
2019-11-23 20:27:39 +08:00
|
|
|
if (scene)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_scene = scene;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto& cameras = _scene->_cameras;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto it = std::find(cameras.begin(), cameras.end(), this);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (it == cameras.end())
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
_scene->_cameras.emplace_back(this);
|
2021-12-25 10:04:45 +08:00
|
|
|
// notify scene that the camera order is dirty
|
2019-11-23 20:27:39 +08:00
|
|
|
_scene->setCameraOrderDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::clearBackground()
|
|
|
|
{
|
|
|
|
if (_clearBrush)
|
|
|
|
{
|
|
|
|
_clearBrush->drawBackground(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::apply()
|
|
|
|
{
|
|
|
|
_viewProjectionUpdated = _transformUpdated;
|
|
|
|
applyViewport();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::applyViewport()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_director->getRenderer()->setViewPort(_defaultViewport.x, _defaultViewport.y, _defaultViewport.w,
|
|
|
|
_defaultViewport.h);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int Camera::getRenderOrder() const
|
|
|
|
{
|
|
|
|
int result(0);
|
2021-12-25 10:04:45 +08:00
|
|
|
result = 127 << 8;
|
2019-11-23 20:27:39 +08:00
|
|
|
result += _depth;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-25 10:15:31 +08:00
|
|
|
void Camera::setFOV(float fieldOfView)
|
|
|
|
{
|
|
|
|
_fieldOfView = fieldOfView;
|
2022-08-11 01:14:18 +08:00
|
|
|
Camera::updateTransform();
|
2022-06-25 10:15:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::setFarPlane(float farPlane)
|
|
|
|
{
|
|
|
|
_farPlane = farPlane;
|
2022-08-11 01:14:18 +08:00
|
|
|
Camera::updateTransform();
|
2022-06-25 10:15:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::setNearPlane(float nearPlane)
|
|
|
|
{
|
|
|
|
_nearPlane = nearPlane;
|
2022-08-11 01:14:18 +08:00
|
|
|
Camera::updateTransform();
|
2022-06-25 10:15:31 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Camera::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_viewProjectionUpdated = _transformUpdated;
|
|
|
|
return Node::visit(renderer, parentTransform, parentFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::setBackgroundBrush(CameraBackgroundBrush* clearBrush)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(clearBrush);
|
|
|
|
AX_SAFE_RELEASE(_clearBrush);
|
2019-11-23 20:27:39 +08:00
|
|
|
_clearBrush = clearBrush;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera::isBrushValid()
|
|
|
|
{
|
|
|
|
return _clearBrush != nullptr && _clearBrush->isValid();
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|