axmol/cocos/base/CCCamera.h

176 lines
5.4 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.
****************************************************************************/
#ifndef _CCCAMERA_H__
#define _CCCAMERA_H__
#include "2d/CCNode.h"
NS_CC_BEGIN
//class Ray;
class Scene;
enum class CameraFlag
{
DEFAULT = 1,
USER1 = 1 << 1,
USER2 = 1 << 2,
USER3 = 1 << 3,
USER4 = 1 << 4,
USER5 = 1 << 5,
USER6 = 1 << 6,
USER7 = 1 << 7,
USER8 = 1 << 8,
};
/**
* Defines a camera .
*/
class CC_DLL Camera :public Node
{
friend class Scene;
2014-08-07 15:23:31 +08:00
public:
/**
* The type of camera.
*/
enum class Type
{
PERSPECTIVE = 1,
ORTHOGRAPHIC = 2
};
public:
/**
* Creates a perspective camera.
*
* @param fieldOfView The field of view for the perspective camera (normally in the range of 40-60 degrees).
* @param aspectRatio The aspect ratio of the camera (normally the width of the viewport divided by the height of the viewport).
* @param nearPlane The near plane distance.
* @param farPlane The far plane distance.
*/
static Camera* createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane);
/**
* Creates an orthographic camera.
*
* @param zoomX The zoom factor along the X-axis of the orthographic projection (the width of the ortho projection).
* @param zoomY The zoom factor along the Y-axis of the orthographic projection (the height of the ortho projection).
* @param aspectRatio The aspect ratio of the orthographic projection.
* @param nearPlane The near plane distance.
* @param farPlane The far plane distance.
*/
static Camera* createOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane);
/** create default camera, the camera type depends on Director::getProjection */
static Camera* create();
/**
* Gets the type of camera.
*
* @return The camera type.
*/
Camera::Type getType() const { return _type; }
/**get & set Camera flag*/
CameraFlag getCameraFlag() const { return (CameraFlag)_cameraFlag; }
void setCameraFlag(CameraFlag flag) { _cameraFlag = (unsigned short)flag; }
/**
* Sets the position (X, Y, and Z) in its parent's coordinate system
*/
virtual void setPosition3D(const Vec3& position) override;
/**
* Creates a view matrix based on the specified input parameters.
*
* @param eyePosition The eye position.
* @param targetPosition The target's center position.
* @param up The up vector.
* @param dst A matrix to store the result in.
*/
virtual void lookAt(const Vec3& target, const Vec3& up);
/**
* Gets the camera's projection matrix.
*
* @return The camera projection matrix.
*/
const Mat4& getProjectionMatrix() const;
/**
* Gets the camera's view matrix.
*
* @return The camera view matrix.
*/
2014-08-07 15:57:15 +08:00
const Mat4& getViewMatrix() const;
2014-08-07 15:23:31 +08:00
/**get view projection matrix*/
2014-08-07 15:57:15 +08:00
const Mat4& getViewProjectionMatrix() const;
2014-08-07 15:23:31 +08:00
/**
* Convert the specified point of viewport from screenspace coordinate into the worldspace coordinate.
*/
2014-08-07 15:57:15 +08:00
void unproject(const Size& viewport, Vec3* src, Vec3* dst) const;
2014-08-07 15:23:31 +08:00
//override
virtual void onEnter() override;
virtual void onExit() override;
2014-08-08 11:49:59 +08:00
static const Camera* getVisitingCamera() { return _visitingCamera; }
2014-08-07 15:23:31 +08:00
CC_CONSTRUCTOR_ACCESS:
Camera();
~Camera();
void setScene(Scene* scene);
/**set additional matrix for the projection matrix, it multiplys mat to projection matrix when called, used by WP8*/
void setAdditionalProjection(const Mat4& mat);
/** init camera */
bool initDefault();
bool initPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane);
bool initOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane);
2014-08-07 15:23:31 +08:00
protected:
Scene* _scene; //Scene camera belongs to
Mat4 _projection;
2014-08-07 15:57:15 +08:00
mutable Mat4 _view;
mutable Mat4 _viewInv;
mutable Mat4 _viewProjection;
2014-08-07 15:23:31 +08:00
Vec3 _up;
Camera::Type _type;
float _fieldOfView;
float _zoom[2];
float _aspectRatio;
float _nearPlane;
float _farPlane;
2014-08-07 15:57:15 +08:00
mutable bool _viewProjectionDirty;
2014-08-07 15:23:31 +08:00
unsigned short _cameraFlag; // camera flag
2014-08-08 11:49:59 +08:00
static Camera* _visitingCamera;
friend class Director;
2014-08-07 15:23:31 +08:00
};
NS_CC_END
#endif// __CCCAMERA_H_