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 .
2015-06-03 07:56:59 +08:00
Code based GamePlay3D ' s Camera : http : //gameplay3d.org
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2014-08-07 15:23:31 +08:00
# 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"
2015-06-09 16:21:50 +08:00
# include "renderer/CCFrameBuffer.h"
2014-08-07 15:23:31 +08:00
NS_CC_BEGIN
class Scene ;
2015-08-21 10:27:19 +08:00
class CameraBackgroundBrush ;
2015-05-29 13:47:29 +08:00
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 ,
} ;
/**
2015-05-29 13:47:29 +08:00
* Defines a camera .
*/
2014-08-07 15:23:31 +08:00
class CC_DLL Camera : public Node
{
2014-08-12 14:42:08 +08:00
friend class Scene ;
2015-06-03 07:56:59 +08:00
friend class Director ;
2015-05-29 10:41:19 +08:00
friend class EventDispatcher ;
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 ( ) ;
2016-06-16 02:33:25 +08:00
/**
* Get the visiting camera , the visiting camera shall be set on Scene : : render
*/
static const Camera * getVisitingCamera ( ) ;
static const experimental : : Viewport & getDefaultViewport ( ) ;
static void setDefaultViewport ( const experimental : : Viewport & vp ) ;
/**
* Get the default camera of the current running scene .
*/
static Camera * getDefaultCamera ( ) ;
2014-08-07 15:23:31 +08:00
/**
* 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
2015-05-29 10:41:19 +08:00
/* convert the specified point in 3D world-space coordinates into the screen-space coordinates.
2015-02-26 15:20:20 +08:00
*
2015-05-29 10:41:19 +08:00
* Origin point at left top corner in screen - space .
2015-02-26 15:20:20 +08:00
* @ 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
2015-05-29 10:41:19 +08:00
/* convert the specified point in 3D world-space coordinates into the GL-screen-space coordinates.
*
* Origin point at left bottom corner in GL - screen - space .
* @ param src The 3 D world - space position .
* @ return The GL - screen - space position .
*/
Vec2 projectGL ( const Vec3 & src ) const ;
2015-02-26 15:20:20 +08:00
/**
2015-05-29 10:41:19 +08:00
* Convert the specified point of screen - space coordinate into the 3 D world - space coordinate .
2015-02-26 15:20:20 +08:00
*
2015-05-29 10:41:19 +08:00
* Origin point at left top corner in screen - space .
2015-02-26 15:20:20 +08:00
* @ param src The screen - space position .
2015-05-29 10:41:19 +08:00
* @ return The 3 D world - space position .
2015-02-26 15:20:20 +08:00
*/
2015-02-27 10:01:57 +08:00
Vec3 unproject ( const Vec3 & src ) const ;
2015-05-29 10:41:19 +08:00
2014-08-07 15:23:31 +08:00
/**
2015-05-29 10:41:19 +08:00
* Convert the specified point of GL - screen - space coordinate into the 3 D world - space coordinate .
2015-02-26 15:20:20 +08:00
*
2015-05-29 10:41:19 +08:00
* Origin point at left bottom corner in GL - screen - space .
* @ param src The GL - screen - space position .
* @ return The 3 D world - space position .
*/
Vec3 unprojectGL ( const Vec3 & src ) const ;
/**
* Convert the specified point of screen - space coordinate into the 3 D world - space coordinate .
*
* Origin point at left top corner in screen - space .
* @ param size The window size to use .
* @ param src The screen - space position .
* @ param dst The 3 D world - space position .
2015-02-26 15:20:20 +08:00
*/
2015-05-29 10:41:19 +08:00
void unproject ( const Size & size , const Vec3 * src , Vec3 * dst ) const ;
2014-08-07 15:23:31 +08:00
2015-05-29 10:41:19 +08:00
/**
* Convert the specified point of GL - screen - space coordinate into the 3 D world - space coordinate .
*
* Origin point at left bottom corner in GL - screen - space .
* @ param size The window size to use .
* @ param src The GL - screen - space position .
* @ param dst The 3 D world - space position .
*/
void unprojectGL ( const Size & size , const Vec3 * src , Vec3 * dst ) const ;
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
2015-05-26 18:14:37 +08:00
/**
* Get the frustum ' s far plane .
*/
2015-05-26 17:14:26 +08:00
float getFarPlane ( ) const { return _farPlane ; }
2015-05-26 18:14:37 +08:00
/**
* Get the frustum ' s near plane .
*/
2015-05-26 17:14:26 +08:00
float getNearPlane ( ) const { return _nearPlane ; }
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
2015-05-29 14:08:20 +08:00
/**
2015-08-21 10:27:19 +08:00
Before rendering scene with this camera , the background need to be cleared . It clears the depth buffer with max depth by default . Use setBackgroundBrush to modify the default behavior
2015-05-29 14:08:20 +08:00
*/
2015-08-20 16:19:57 +08:00
void clearBackground ( ) ;
2015-05-29 14:08:20 +08:00
/**
Apply the FBO , RenderTargets and viewport .
*/
void apply ( ) ;
2016-06-16 02:33:25 +08:00
/**
2016-06-22 10:22:05 +08:00
Restore the FBO , RenderTargets and viewport .
2016-06-16 02:33:25 +08:00
*/
void restore ( ) ;
2015-05-29 14:08:20 +08:00
/**
2015-09-22 16:08:23 +08:00
Set FBO , which will attach several render target for the rendered result .
2016-06-16 02:33:25 +08:00
*/
2015-06-09 10:59:43 +08:00
void setFrameBufferObject ( experimental : : FrameBuffer * fbo ) ;
2015-05-29 15:52:06 +08:00
/**
Set Viewport for camera .
*/
2016-06-16 02:33:25 +08:00
void setViewport ( const experimental : : Viewport & vp ) ;
2015-07-30 11:53:23 +08:00
/**
* Whether or not the viewprojection matrix was updated since the last frame .
* @ return True if the viewprojection matrix was updated since the last frame .
*/
bool isViewProjectionUpdated ( ) const { return _viewProjectionUpdated ; }
2016-06-16 02:33:25 +08:00
2015-08-20 16:19:57 +08:00
/**
2015-08-21 10:27:19 +08:00
* set the background brush . See CameraBackgroundBrush for more information .
2015-08-20 16:19:57 +08:00
* @ param clearBrush Brush used to clear the background
*/
2015-08-21 10:27:19 +08:00
void setBackgroundBrush ( CameraBackgroundBrush * clearBrush ) ;
2016-06-16 02:33:25 +08:00
2015-08-20 16:19:57 +08:00
/**
* Get clear brush
*/
2015-08-21 10:27:19 +08:00
CameraBackgroundBrush * getBackgroundBrush ( ) const { return _clearBrush ; }
2016-06-16 02:33:25 +08:00
2015-07-30 11:53:23 +08:00
virtual void visit ( Renderer * renderer , const Mat4 & parentTransform , uint32_t parentFlags ) override ;
2015-11-12 09:49:49 +08:00
bool isBrushValid ( ) ;
2014-08-07 15:23:31 +08:00
CC_CONSTRUCTOR_ACCESS :
Camera ( ) ;
~ Camera ( ) ;
2016-06-16 02:33:25 +08:00
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 ) ;
2016-06-16 02:33:25 +08:00
2015-09-22 16:08:23 +08:00
/**set additional matrix for the projection matrix, it multiplies mat to projection matrix when called, used by WP8*/
2014-08-07 15:23:31 +08:00
void setAdditionalProjection ( const Mat4 & mat ) ;
2016-06-16 02:33:25 +08:00
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 ( ) ;
2015-05-29 15:52:06 +08:00
void applyViewport ( ) ;
2016-06-16 02:33:25 +08:00
void restoreFrameBufferObject ( ) ;
void restoreViewport ( ) ;
2014-08-07 15:23:31 +08:00
protected :
2016-06-16 02:33:25 +08:00
static Camera * _visitingCamera ;
static experimental : : Viewport _defaultViewport ;
2014-08-07 15:23:31 +08:00
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 ;
2016-06-16 02:33:25 +08:00
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 ;
2015-07-30 11:53:23 +08:00
bool _viewProjectionUpdated ; //Whether or not the viewprojection matrix was updated since the last frame.
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-09-22 16:08:23 +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 depth
2016-06-16 02:33:25 +08:00
2015-08-21 10:27:19 +08:00
CameraBackgroundBrush * _clearBrush ; //brush used to clear the back ground
2016-06-16 02:33:25 +08:00
2015-06-09 16:21:50 +08:00
experimental : : Viewport _viewport ;
2015-06-09 10:59:43 +08:00
experimental : : FrameBuffer * _fbo ;
2016-06-16 02:33:25 +08:00
GLint _oldViewport [ 4 ] ;
2014-08-07 15:23:31 +08:00
} ;
NS_CC_END
2016-06-16 02:33:25 +08:00
# endif // __CCCAMERA_H_