axmol/cocos/2d/CCCamera.cpp

320 lines
8.9 KiB
C++
Raw Normal View History

2014-08-07 15:23:31 +08:00
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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/CCCamera.h"
2014-08-07 15:23:31 +08:00
#include "base/CCDirector.h"
#include "platform/CCGLView.h"
#include "2d/CCScene.h"
NS_CC_BEGIN
2014-08-08 11:49:59 +08:00
Camera* Camera::_visitingCamera = nullptr;
2014-08-07 15:23:31 +08:00
Camera* Camera::create()
{
Camera* camera = new (std::nothrow) Camera();
2014-08-13 15:57:22 +08:00
camera->initDefault();
camera->autorelease();
2014-08-07 15:23:31 +08:00
return camera;
}
Camera* Camera::createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
{
auto ret = new (std::nothrow) Camera();
2014-08-07 15:23:31 +08:00
if (ret)
{
ret->initPerspective(fieldOfView, aspectRatio, nearPlane, farPlane);
2014-08-07 15:23:31 +08:00
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
Camera* Camera::createOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane)
{
auto ret = new (std::nothrow) Camera();
2014-08-07 15:23:31 +08:00
if (ret)
{
ret->initOrthographic(zoomX, zoomY, nearPlane, farPlane);
2014-08-07 15:23:31 +08:00
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
Camera::Camera()
: _scene(nullptr)
2014-08-07 15:23:31 +08:00
, _viewProjectionDirty(true)
, _cameraFlag(1)
2014-12-16 16:34:35 +08:00
, _frustumDirty(true)
, _enableFrustumCull(true)
2014-08-07 15:23:31 +08:00
{
}
Camera::~Camera()
{
}
void Camera::setPosition3D(const Vec3& position)
{
Node::setPosition3D(position);
_transformUpdated = _transformDirty = _inverseDirty = true;
}
const Mat4& Camera::getProjectionMatrix() const
{
return _projection;
}
2014-08-07 15:57:15 +08:00
const Mat4& Camera::getViewMatrix() const
2014-08-07 15:23:31 +08:00
{
Mat4 viewInv(getNodeToWorldTransform());
static int count = sizeof(float) * 16;
if (memcmp(viewInv.m, _viewInv.m, count) != 0)
{
_viewProjectionDirty = true;
_frustumDirty = true;
2014-08-07 15:23:31 +08:00
_viewInv = viewInv;
_view = viewInv.getInversed();
}
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();
Vec3 xaxis;
Vec3::cross(upv, zaxis, &xaxis);
xaxis.normalize();
Vec3 yaxis;
Vec3::cross(zaxis, xaxis, &yaxis);
yaxis.normalize();
Mat4 rotation;
rotation.m[0] = xaxis.x;
rotation.m[1] = xaxis.y;
rotation.m[2] = xaxis.z;
rotation.m[3] = 0;
rotation.m[4] = yaxis.x;
rotation.m[5] = yaxis.y;
rotation.m[6] = yaxis.z;
rotation.m[7] = 0;
rotation.m[8] = zaxis.x;
rotation.m[9] = zaxis.y;
rotation.m[10] = zaxis.z;
rotation.m[11] = 0;
2014-10-31 11:32:10 +08:00
2014-08-07 15:23:31 +08:00
Quaternion quaternion;
Quaternion::createFromRotationMatrix(rotation,&quaternion);
2014-10-31 11:32:10 +08:00
float rotx = atan2f(2 * (quaternion.w * quaternion.x + quaternion.y * quaternion.z), 1 - 2 * (quaternion.x * quaternion.x + quaternion.y * quaternion.y));
float roty = asin(clampf(2 * (quaternion.w * quaternion.y - quaternion.z * quaternion.x) , -1.0f , 1.0f));
float rotz = -atan2(2 * (quaternion.w * quaternion.z + quaternion.x * quaternion.y) , 1 - 2 * (quaternion.y * quaternion.y + quaternion.z * quaternion.z));
setRotation3D(Vec3(CC_RADIANS_TO_DEGREES(rotx),CC_RADIANS_TO_DEGREES(roty),CC_RADIANS_TO_DEGREES(rotz)));
2014-08-07 15:23:31 +08:00
}
2014-08-07 15:57:15 +08:00
const Mat4& Camera::getViewProjectionMatrix() const
2014-08-07 15:23:31 +08:00
{
getViewMatrix();
if (_viewProjectionDirty)
{
_viewProjectionDirty = false;
Mat4::multiply(_projection, _view, &_viewProjection);
}
return _viewProjection;
}
void Camera::setAdditionalProjection(const Mat4& mat)
{
_projection = mat * _projection;
getViewProjectionMatrix();
}
bool Camera::initDefault()
{
auto size = Director::getInstance()->getWinSize();
//create default camera
auto projection = Director::getInstance()->getProjection();
switch (projection)
{
case Director::Projection::_2D:
{
initOrthographic(size.width, size.height, -1024, 1024);
2014-08-13 15:57:22 +08:00
setPosition3D(Vec3(0.0f, 0.0f, 0.0f));
2014-08-14 09:06:06 +08:00
setRotation3D(Vec3(0.f, 0.f, 0.f));
break;
}
case Director::Projection::_3D:
{
float zeye = Director::getInstance()->getZEye();
initPerspective(60, (GLfloat)size.width / size.height, 10, zeye + size.height / 2.0f);
Vec3 eye(size.width/2, size.height/2.0f, zeye), center(size.width/2, size.height/2, 0.0f), up(0.0f, 1.0f, 0.0f);
setPosition3D(eye);
lookAt(center, up);
break;
}
default:
CCLOG("unrecognized projection");
break;
}
return true;
}
bool Camera::initPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
{
_fieldOfView = fieldOfView;
_aspectRatio = aspectRatio;
_nearPlane = nearPlane;
_farPlane = farPlane;
Mat4::createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane, &_projection);
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
//if needed, we need to add a rotation for Landscape orientations on Windows Phone 8 since it is always in Portrait Mode
GLView* view = Director::getInstance()->getOpenGLView();
if(view != nullptr)
{
setAdditionalProjection(view->getOrientationMatrix());
}
#endif
_viewProjectionDirty = true;
_frustumDirty = true;
return true;
}
bool Camera::initOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane)
{
_zoom[0] = zoomX;
_zoom[1] = zoomY;
_nearPlane = nearPlane;
_farPlane = farPlane;
2014-08-13 15:57:22 +08:00
Mat4::createOrthographicOffCenter(0, _zoom[0], 0, _zoom[1], _nearPlane, _farPlane, &_projection);
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
//if needed, we need to add a rotation for Landscape orientations on Windows Phone 8 since it is always in Portrait Mode
GLView* view = Director::getInstance()->getOpenGLView();
if(view != nullptr)
{
setAdditionalProjection(view->getOrientationMatrix());
}
#endif
_viewProjectionDirty = true;
_frustumDirty = true;
return true;
}
2014-08-07 15:57:15 +08:00
void Camera::unproject(const Size& viewport, Vec3* src, Vec3* dst) const
2014-08-07 15:23:31 +08:00
{
assert(dst);
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;
getViewProjectionMatrix().getInversed().transformVector(screen, &screen);
if (screen.w != 0.0f)
{
screen.x /= screen.w;
screen.y /= screen.w;
screen.z /= screen.w;
}
dst->set(screen.x, screen.y, screen.z);
}
2014-12-16 16:34:35 +08:00
void Camera::enableFrustumCull(bool bEnalbe, bool bClipZ)
{
_enableFrustumCull = bEnalbe;
_frustum.setClipZ(bClipZ);
}
bool Camera::visibleInFrustum(const AABB& aabb) const
{
if (_enableFrustumCull)
{
if (_frustumDirty)
{
_frustum.initFrustum(this);
_frustumDirty = false;
}
return !_frustum.isOutFrustum(aabb);
}
return true;
}
2014-08-07 15:23:31 +08:00
void Camera::onEnter()
{
if (_scene == nullptr)
{
2014-08-08 11:49:59 +08:00
auto scene = getScene();
if (scene)
setScene(scene);
2014-08-07 15:23:31 +08:00
}
Node::onEnter();
}
void Camera::onExit()
{
// remove this camera from scene
setScene(nullptr);
Node::onExit();
}
void Camera::setScene(Scene* scene)
{
if (_scene != scene)
{
//remove old scene
if (_scene)
{
auto& cameras = _scene->_cameras;
auto it = std::find(cameras.begin(), cameras.end(), this);
if (it != cameras.end())
cameras.erase(it);
_scene = nullptr;
}
//set new scene
if (scene)
{
_scene = scene;
auto& cameras = _scene->_cameras;
auto it = std::find(cameras.begin(), cameras.end(), this);
if (it == cameras.end())
_scene->_cameras.push_back(this);
}
}
}
NS_CC_END