【Cocos2d-X开发学习笔记】第07期:渲染框架之摄像机类(CCCamera)的使用

来源:互联网 发布:回转企鹅罐 知乎 编辑:程序博客网 时间:2024/04/30 06:21

本系列学习教程使用的是cocos2d-x-2.1.4版本(截至目前为止最新稳定版) ,PC开发环境Windows7,C++开发环境VS2010  




一、摄像机类


     所有节点都拥有一个摄像机类CCCamera。只有通过摄像机类,节点才会被渲染出来。当节点发生缩放旋转和位置

化的时候,都需要覆盖CCCamera类,让这个节点通过CCCamera类重新渲染。


注意:CCNode类里有些方法可以实现缩放、旋转和位置变化,当使用摄像机类实现这些的时候,那些方法就不能同时使用了。使用摄像机类也不可以同时使用世界坐标了。


CCCamera类的继承关系如下图 所示。


      Cocos2D-X中的CCCamera类使用OpenGL的gluLookAt函数来设置位置。gluLookAt函数有三组关于坐标的参数,

其中“Eye”系列的x、y、z坐标参数是视角的位置,而“Center”系列的x、y、z坐标参数是所视目标的坐标位置,

“Up”系列的x、y、z坐标参数是摄像机方向的向量坐标。关于这三个参数,你可以理解为以“Eye”为起点,沿着

“Up”方向,朝着“Center”看。


注意 :这里需要说明的是,在三维效果中,使用CCCamera类是可以的,但是如果你只需要一些二维特效的话,那么更推荐跟随类CCFollow。跟随类CCFollow的相关知识,我们会在后面学习到。


CCCamera类的主要保护成员数据如下图所示。



CCCamera类的主要公共函数如下图所示。


 

二、实例:CCCamera类的使用

 

      CCCamera类可以实现节点对象的缩放旋转等,下面就让我们来实现摄像机类实现缩放的实例。

<1> 首先新建一个Cocos2D-X项目,然后在HelloWorldScene.h文件中添加如下代码所示。

class HelloWorld : public cocos2d::CCLayer{ float    m_z;public:    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();  void update(float dt);    // there's no 'id' in cpp, so we recommand to return the exactly class pointer    static cocos2d::CCScene* scene();        // a selector callback    void menuCloseCallback(CCObject* pSender);    // implement the "static node()" method manually    CREATE_FUNC(HelloWorld);};#endif  

添加了一个成员变量m_z和一个成员函数update。

 

<2> 然后在HelloWorldScene.cpp文件的init函数中添加如下所示代码。

bool HelloWorld::init(){    bool bRet = false;    do     {             CC_BREAK_IF(! CCLayer::init());// Create a "close" menu item with close icon, it's an auto release object.        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(            "CloseNormal.png",            "CloseSelected.png",            this,            menu_selector(HelloWorld::menuCloseCallback));        CC_BREAK_IF(! pCloseItem);        // Place the menu item bottom-right conner.        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));        // Create a menu with the "close" menu item, it's an auto release object.        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);        pMenu->setPosition(CCPointZero);        CC_BREAK_IF(! pMenu);        // Add the menu to HelloWorld layer as a child layer.        this->addChild(pMenu, 1);        CCSize s = CCDirector::sharedDirector()->getWinSize();        CCSprite *sprite;CCCamera *cam;    // LEFTsprite = CCSprite::create("grossini.png");addChild( sprite, 0);        sprite->setPosition( ccp(s.width/4*1, s.height/2) );cam = sprite->getCamera();cam->setEyeXYZ(0, 0, 415/2);cam->setCenterXYZ(0, 0, 0);    // CENTERsprite = CCSprite::create("grossini.png");addChild( sprite, 0, 40);sprite->setPosition(ccp(s.width/4*2, s.height/2));    // RIGHTsprite = CCSprite::create("grossini.png");addChild( sprite, 0, 20);sprite->setPosition(ccp(s.width/4*3, s.height/2));        m_z = 0;        scheduleUpdate();        bRet = true;    } while (0);    return bRet;}


<3> 最后在update函数中添加如下所示代码。

void HelloWorld::update(float dt){    CCNode *sprite;    CCCamera *cam;        m_z += dt * 100;        sprite = getChildByTag(20);//获得摄像机    cam = sprite->getCamera();//设置z轴位置    cam->setEyeXYZ(0, 0, m_z);        sprite = getChildByTag(40);    cam = sprite->getCamera();    cam->setEyeXYZ(0, 0, -m_z);    }

      通过getCamera函数获得摄像机实例,并通过设置视角的z轴位置来实现缩放效果。需要再次强调的是:不推荐

使用摄像机实现二维特效,本实例只是一个使用的范例。

 

实例运行效果图。

 

实例源码下载