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"
2014-12-16 16:34:35 +08:00
# include "3d/CCFrustum.h"
2015-05-07 15:09:06 +08:00
# include "renderer/CCQuadCommand.h"
# include "renderer/CCCustomCommand.h"
2014-08-07 15:23:31 +08:00
NS_CC_BEGIN
class Scene ;
2015-05-12 16:43:17 +08:00
class FrameBufferObject ;
2014-11-13 17:30:49 +08:00
/**
* Note :
* Scene creates a default camera . And the default camera mask of Node is 1 , therefore it can be seen by the default camera .
* During rendering the scene , it draws the objects seen by each camera in the added order except default camera . The default camera is the last one being drawn with .
2015-01-09 08:30:00 +08:00
* It ' s usually a good idea to render 3 D objects in a separate camera .
* And set the 3 d camera flag to CameraFlag : : USER1 or anything else except DEFAULT . Dedicate The DEFAULT camera for UI , because it is rendered at last .
* You can change the camera order to get different result when depth test is not enabled .
2014-11-13 17:30:49 +08:00
* For each camera , transparent 3 d sprite is rendered after opaque 3 d sprite and other 2 d objects .
*/
2014-08-07 15:23:31 +08:00
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
{
2014-08-12 14:42:08 +08:00
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 .
*/
2015-01-13 12:06:50 +08:00
static Camera * createPerspective ( float fieldOfView , float aspectRatio , float nearPlane , float farPlane ) ;
2014-08-07 15:23:31 +08:00
/**
* 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 nearPlane The near plane distance .
* @ param farPlane The far plane distance .
*/
2015-01-13 12:06:50 +08:00
static Camera * createOrthographic ( float zoomX , float zoomY , float nearPlane , float farPlane ) ;
2014-08-07 15:23:31 +08:00
2015-02-11 18:14:22 +08:00
/** create default camera, the camera type depends on Director::getProjection, the depth of the default camera is 0 */
2014-08-07 15:23:31 +08:00
static Camera * create ( ) ;
/**
* Gets the type of camera .
*
* @ return The camera type .
*/
2015-01-09 08:30:00 +08:00
Camera : : Type getType ( ) const { return _type ; }
2014-08-07 15:23:31 +08:00
/**get & set Camera flag*/
CameraFlag getCameraFlag ( ) const { return ( CameraFlag ) _cameraFlag ; }
void setCameraFlag ( CameraFlag flag ) { _cameraFlag = ( unsigned short ) flag ; }
2015-01-09 08:30:00 +08:00
2014-08-07 15:23:31 +08:00
/**
2015-01-09 08:30:00 +08:00
* Make Camera looks at target
2014-08-07 15:23:31 +08:00
*
2015-01-09 08:30:00 +08:00
* @ param target The target camera is point at
* @ param up The up vector , usually it ' s Y axis
2014-08-07 15:23:31 +08:00
*/
2015-01-09 08:30:00 +08:00
virtual void lookAt ( const Vec3 & target , const Vec3 & up = Vec3 : : UNIT_Y ) ;
2014-08-07 15:23:31 +08:00
/**
* 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 ;
2015-02-26 15:20:20 +08:00
/* convert the specified point of viewport from world-space coordinates into the screen-space coordinates.
*
* @ param src The world - space position .
* @ return The screen - space position .
*/
2015-02-27 10:01:57 +08:00
Vec2 project ( const Vec3 & src ) const ;
2015-02-26 15:20:20 +08:00
/**
* Convert the specified point of viewport from screen - space coordinate into the world - space coordinate .
*
* @ param src The screen - space position .
* @ return The world - space position .
*/
2015-02-27 10:01:57 +08:00
Vec3 unproject ( const Vec3 & src ) const ;
2014-08-07 15:23:31 +08:00
/**
2015-02-26 15:20:20 +08:00
* Convert the specified point of viewport from screen - space coordinate into the world - space coordinate .
*
* @ param viewport The viewport size to use .
* @ param src The screen - space position .
* @ param dst The world - space position .
*/
2015-02-27 10:01:57 +08:00
void unproject ( const Size & viewport , const Vec3 * src , Vec3 * dst ) const ;
2014-08-07 15:23:31 +08:00
2014-12-16 16:34:35 +08:00
/**
* Is this aabb visible in frustum
*/
2015-01-13 12:06:50 +08:00
bool isVisibleInFrustum ( const AABB * aabb ) const ;
/**
* Get object depth towards camera
*/
float getDepthInView ( const Mat4 & transform ) const ;
2014-12-16 16:34:35 +08:00
2015-02-11 18:14:22 +08:00
/**
* set depth , camera with larger depth is drawn on top of camera with smaller depth , the depth of camera with CameraFlag : : DEFAULT is 0 , user defined camera is - 1 by default
*/
2015-05-12 16:43:17 +08:00
void setDepth ( int8_t depth ) ;
2015-02-11 18:14:22 +08:00
/**
* get depth , camera with larger depth is drawn on top of camera with smaller depth , the depth of camera with CameraFlag : : DEFAULT is 0 , user defined camera is - 1 by default
*/
2015-05-12 16:43:17 +08:00
int8_t getDepth ( ) const { return _depth ; }
/**
get rendered order
*/
int getRenderOrder ( ) const ;
2015-02-11 18:14:22 +08:00
2014-08-07 15:23:31 +08:00
//override
virtual void onEnter ( ) override ;
virtual void onExit ( ) override ;
2015-03-20 17:20:22 +08:00
/**
* Get the visiting camera , the visiting camera shall be set on Scene : : render
*/
2014-08-08 11:49:59 +08:00
static const Camera * getVisitingCamera ( ) { return _visitingCamera ; }
2015-03-20 17:20:22 +08:00
/**
* Get the default camera of the current running scene .
*/
2015-01-09 08:30:00 +08:00
static Camera * getDefaultCamera ( ) ;
2015-05-07 15:09:06 +08:00
void clearBackground ( float depth ) ;
2015-05-12 16:43:17 +08:00
void setFrameBufferObject ( FrameBufferObject * fbo ) ;
2014-08-07 15:23:31 +08:00
CC_CONSTRUCTOR_ACCESS :
Camera ( ) ;
~ Camera ( ) ;
2015-03-20 17:20:22 +08:00
/**
* Set the scene , this method shall not be invoke manually
*/
2014-08-07 15:23:31 +08:00
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 ) ;
2014-08-12 14:42:08 +08:00
/** init camera */
bool initDefault ( ) ;
bool initPerspective ( float fieldOfView , float aspectRatio , float nearPlane , float farPlane ) ;
bool initOrthographic ( float zoomX , float zoomY , float nearPlane , float farPlane ) ;
2015-05-12 16:43:17 +08:00
void applyFrameBufferObject ( ) ;
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-12-16 16:34:35 +08:00
mutable Frustum _frustum ; // camera frustum
mutable bool _frustumDirty ;
2015-05-12 16:43:17 +08:00
int8_t _depth ; //camera depth, the depth of camera with CameraFlag::DEFAULT flag is 0 by default, a camera with larger depth is drawn on top of camera with smaller detph
2014-08-08 11:49:59 +08:00
static Camera * _visitingCamera ;
friend class Director ;
2015-05-12 16:43:17 +08:00
FrameBufferObject * _fbo ;
2014-08-07 15:23:31 +08:00
} ;
NS_CC_END
# endif // __CCCAMERA_H_