void CtestLayer::ccTouchesMoved(cocos2d::CCSet* touches , cocos2d::CCEvent* event)
{
    
    CCSetIterator it;
    CCTouch* touch;
    for( it = touches->begin(); it != touches->end(); it++)
    {
        touch = (CCTouch*)(*it);
        
        if(!touch)
            break;
        
        CCPoint pointInWinSpace = touch->getLocationInView();
       
        //----update mos
        m_mosPosf=m_mosPos;
        m_mosPos=pointInWinSpace;
        //----update eyePos
        m_A+=-(m_mosPos.x-m_mosPosf.x)*0.4;
        m_B+=(m_mosPos.y-m_mosPosf.y)*0.4;
        if(m_B>89.9)m_B=89.9;
        if(m_B<-89.9)m_B=-89.9;
        updateEyePos();
        m_root3d->getCamera3D()->setEyePos(m_eyePos);
    }
}
void ThirdPersonCamera::updateViewMatrix(Pipeline& p)
{
	p.setMatrixMode(VIEW_MATRIX);
	p.loadIdentity();


    updateEyePos();

    glm::vec3 up = glm::vec3(0.0f,1.0f,0.0f);
    lookAt(m_eye, m_target, up);



	m_viewMatrix = m_viewMatrix * glm::translate(0.0f, -m_eyeOffset.y, 0.0f);

	m_eye.y += m_eyeOffset.y;

    p.setMatrixMode(VIEW_MATRIX);
    p.addMatrix(m_viewMatrix);

	p.setViewPosition(m_eye);
}
bool CtestLayer::init() {
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    CCSize winSize=CCDirector::sharedDirector()->getWinSize();
    //enable touch
    setTouchEnabled( true );
    //enable update
    scheduleUpdate();
    //update eye pos
    updateEyePos();
    //-----------------------------
    //root3d
    m_root3d=new Cc3dRoot();
    m_root3d->autorelease();
    m_root3d->init();
    m_root3d->setNodeName("root3d");
    this->addChild(m_root3d);

    //camera
    Cc3dCamera*camera=m_root3d->getCamera3D();
    m_r=(winSize.height/2)/tanf(camera->getFovy()/2*M_PI/180);
    camera->setEyePos(cc3dv4(0, 0, m_r, 1));
    camera->setCenter(cc3dv4(0, 0, 0, 1));
    camera->setUp(cc3dv4(0, 1, 0, 0));
    camera->setProjectionMode(ec3dPerspectiveMode);
    //lightSource
    Cc3dLightSource*lightSource=new Cc3dLightSource();
    lightSource->autorelease();
    lightSource->init();
    m_root3d->addChild(lightSource);
    lightSource->setAmbient(cc3dv4(0.8, 0.8, 0.8, 1));
    lightSource->setPosition3D(cc3dv4(600, 900, 1200, 1));
    //program
    Cc3dProgram*program=c3dGetProgram_c3dClassicLighting();
    //material
    Cc3dMaterial*material=new Cc3dMaterial();
    material->autorelease();
    material->init();
    material->setSpecular(cc3dv4(0.5, 0.5, 0.5, 1));
    material->setShininess(10);
    //texture
    CCTexture2D*tex=CCTextureCache::sharedTextureCache()->addImage("white_outline.png");




    //actors
    float r=40;
    m_nRow=3;
    m_nCol=4;
    m_nDeep=2;
    m_actor3dMat3d.resize(m_nRow);
    for(int i=0; i<m_nRow; i++) {
        m_actor3dMat3d[i].resize(m_nCol);
        for(int j=0; j<m_nCol; j++) {
            m_actor3dMat3d[i][j].resize(m_nDeep);
        }
    }


    float xspan=m_nCol*2*r;
    float yspan=m_nRow*2*r;
    float zspan=m_nDeep*2*r;
    float xmin=-xspan/2;
    float ymin=-yspan/2;
    float zmin=-zspan/2;
    for(int i=0; i<m_nRow; i++) {
        for(int j=0; j<m_nCol; j++) {
            for(int k=0; k<m_nDeep; k++) {
                //mesh
                Cc3dMesh*mesh_box=c3dCreateBoxMesh(r,cc3dv4(1, 1, 1, 1),cc3dv4(1, 1, 1, 1),cc3dv4(1, 1, 1, 1),cc3dv4(1, 1, 1, 1),
                                                   cc3dv4(1, 1, 1, 1),cc3dv4(1, 1, 1, 1),cc3dv4(1, 1, 1, 1),cc3dv4(1, 1, 1, 1));
                mesh_box->setTexture(tex);
                mesh_box->setMaterial(material);
                mesh_box->setNodeName("mesh_box");
                //actor
                Cc3dActor* actor3D=new Cc3dActor();
                actor3D->autorelease();
                actor3D->init();
                float x=xmin+r+j*2*r;
                float y=ymin+r+(m_nRow-1-i)*2*r;
                float z=zmin+r+(m_nDeep-1-k)*2*r;
                actor3D->setPosition3D(cc3dv4(x, y, z, 1));
                actor3D->addMesh(mesh_box);
                actor3D->setLightSource(lightSource);
                actor3D->setCamera3D(camera);
                actor3D->setPassUnifoCallback(passUnifoCallback_classicLighting);
                actor3D->setProgram(program);
                actor3D->setNodeName("actor3d");
                m_root3d->addChild(actor3D,0);
                m_actor3dMat3d[i][j][k]=actor3D;

            }
        }
    }


    //submit actors
    for(int i=0; i<m_nRow; i++) {
        for(int j=0; j<m_nCol; j++) {
            for(int k=0; k<m_nDeep; k++) {
                Cc3dActor*actor3D=m_actor3dMat3d[i][j][k];
                actor3D->submit(GL_STATIC_DRAW);
            }
        }
    }




    //controlButton_swithProjMode
    {
        CCScale9Sprite* btnUp=CCScale9Sprite::create("button.png");
        CCScale9Sprite* btnDn=CCScale9Sprite::create("button_dn.png");
        CCLabelTTF*title=CCLabelTTF::create("clear", "Helvetica", 30);
        CCControlButton* controlButton=CCControlButton::create(title, btnUp);
        controlButton->setBackgroundSpriteForState(btnDn,CCControlStateHighlighted);
        controlButton->setPreferredSize(CCSize(180,80));
        controlButton->setPosition(ccp(400,100));
        controlButton->addTargetWithActionForControlEvents(this, (SEL_CCControlHandler)(&CtestLayer::clearCallBack), CCControlEventTouchDown);
        this->addChild(controlButton);
        m_controlButton_clear=controlButton;

    }



    //projection mode label
    m_pLabel=CCLabelTTF::create("proj mode: Perspective", "Arial", 35);
    m_pLabel->setPosition(ccp(origin.x + visibleSize.width*(3.0/4),
                              origin.y + visibleSize.height - m_pLabel->getContentSize().height-100));
    this->addChild(m_pLabel, 1);


    return true;
}
bool CtestLayer::init(){
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    CCSize winSize=CCDirector::sharedDirector()->getWinSize();
    float ZEye=CCDirector::sharedDirector()->getZEye();
    //enable touch
	setTouchEnabled( true );
    //enable update
    scheduleUpdate();
    //update eye pos
    updateEyePos();
    //-----------------------------
    //root3d
    m_root3d=new Cc3dRoot();
    m_root3d->autorelease();
    m_root3d->init();
    m_root3d->setNodeName("root3d");
    this->addChild(m_root3d);
    
    //camera
    Cc3dCamera*camera=m_root3d->getCamera3D();
    m_r=(winSize.height/2)/tanf(camera->getFovy()/2*M_PI/180);
    camera->setEyePos(cc3dv4(0, 0, m_r, 1));
    camera->setCenter(cc3dv4(0, 0, 0, 1));
    camera->setUp(cc3dv4(0, 1, 0, 0));
    camera->setProjectionMode(ec3dPerspectiveMode);
    //lightSource
    Cc3dLightSource*lightSource=new Cc3dLightSource();
    lightSource->autorelease();
    lightSource->init();
    m_root3d->addChild(lightSource);
    lightSource->setAmbient(cc3dv4(0.8, 0.8, 0.8, 1));
    lightSource->setPosition3D(cc3dv4(600, 900, 1200, 1));
    //program
    Cc3dProgram*program=c3dGetProgram_c3dClassicLighting();

    //actor3D
    m_actor3D=c3dSimpleLoadActor("toolKitRes/model/apple_cfc");
    m_actor3D->setLightSource(lightSource);
    m_actor3D->setCamera3D(camera);
    m_actor3D->setPassUnifoCallback(passUnifoCallback_classicLighting);
    m_actor3D->setProgram(program);
    m_actor3D->setNodeName("actor3D");
    m_root3d->addChild(m_actor3D,0);
        
    m_actor3D->scale3D(4, 4, 4);
    m_actor3D->setPosition3D(Cc3dVector4(0,-130,0,1));
    
    //submit

    m_actor3D->submit(GL_STATIC_DRAW);
    

    

    //controlButton_swithProjMode
    {
        CCScale9Sprite* btnUp=CCScale9Sprite::create("button.png");
        CCScale9Sprite* btnDn=CCScale9Sprite::create("button_dn.png");
        CCLabelTTF*title=CCLabelTTF::create("proj mode", "Helvetica", 30);
        CCControlButton* controlButton=CCControlButton::create(title, btnUp);
        controlButton->setBackgroundSpriteForState(btnDn,CCControlStateHighlighted);
        controlButton->setPreferredSize(CCSize(180,80));
        controlButton->setPosition(ccp(400,100));
        controlButton->addTargetWithActionForControlEvents(this, (SEL_CCControlHandler)(&CtestLayer::switchProjModeCallBack), CCControlEventTouchDown);
        this->addChild(controlButton);
        m_controlButton_swithProjMode=controlButton;
        
    }
    
    //controlButton_transform
    {
        CCScale9Sprite* btnUp=CCScale9Sprite::create("button.png");
        CCScale9Sprite* btnDn=CCScale9Sprite::create("button_dn.png");
        CCLabelTTF*title=CCLabelTTF::create("transform", "Helvetica", 30);
        CCControlButton* controlButton=CCControlButton::create(title, btnUp);
        controlButton->setBackgroundSpriteForState(btnDn,CCControlStateHighlighted);
        controlButton->setPreferredSize(CCSize(180,80));
        controlButton->setPosition(ccp(700,100));
        controlButton->addTargetWithActionForControlEvents(this, (SEL_CCControlHandler)(&CtestLayer::transformCallBack), CCControlEventTouchDown);
        this->addChild(controlButton);
        m_controlButton_transform=controlButton;
        
    }
    //projection mode label
    m_pLabel=CCLabelTTF::create("proj mode: Perspective", "Arial", 35);
    m_pLabel->setPosition(ccp(origin.x + visibleSize.width*(3.0/4),
                              origin.y + visibleSize.height - m_pLabel->getContentSize().height-100));
    this->addChild(m_pLabel, 1);

    
    return true;
}
bool CtestLayer::init(){
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    CCSize winSize=CCDirector::sharedDirector()->getWinSize();
    //enable touch
	setTouchEnabled( true );
    //enable update
    scheduleUpdate();
    //update eye pos
    updateEyePos();
    //-----------------------------
    //root3d
    m_root3d=new Cc3dRoot();
    m_root3d->autorelease();
    m_root3d->init();
    m_root3d->setNodeName("root3d");
    this->addChild(m_root3d);
    
    //camera
    Cc3dCamera*camera=m_root3d->getCamera3D();
    m_r=(winSize.height/2)/tanf(camera->getFovy()/2*M_PI/180);
    camera->setEyePos(cc3dv4(0, 0, m_r, 1));
    camera->setCenter(cc3dv4(0, 0, 0, 1));
    camera->setUp(cc3dv4(0, 1, 0, 0));
    camera->setProjectionMode(ec3dPerspectiveMode);
    //lightSource
    Cc3dLightSource*lightSource=new Cc3dLightSource();
    lightSource->autorelease();
    lightSource->init();
    m_root3d->addChild(lightSource);
    lightSource->setAmbient(cc3dv4(0.8, 0.8, 0.8, 1));
    lightSource->setPosition3D(cc3dv4(600, 900, 1200, 1));
    //program
    Cc3dProgram*program=c3dGetProgram_c3dClassicLighting();
    //material
    Cc3dMaterial*material=new Cc3dMaterial();
    material->autorelease();
    material->init();
    material->setSpecular(cc3dv4(0.5, 0.5, 0.5, 1));
    material->setShininess(10);
    //texture
    CCTexture2D*tex_logo=CCTextureCache::sharedTextureCache()->addImage("logo.png");
    CCTexture2D*tex_logo2=CCTextureCache::sharedTextureCache()->addImage("logo2.png");

    //mesh
    Cc3dMesh*mesh_ball=c3dCreateBallMesh(100, 20, 10, cc3dv4(1, 0.5, 0, 1), cc3dv4(0, 1, 0, 1));
    mesh_ball->setTexture(tex_logo2);
    mesh_ball->setMaterial(material);
    mesh_ball->setNodeName("mesh_ball");
    Cc3dMesh*mesh_box=c3dCreateBoxMesh(100,cc3dv4(1, 0, 0, 1),cc3dv4(0, 1, 0, 1),cc3dv4(0, 0, 1, 1),cc3dv4(1, 1, 0, 1),
                                         cc3dv4(0, 0, 1, 1),cc3dv4(1, 1, 0, 1),cc3dv4(1, 0, 0, 1),cc3dv4(0, 1, 0, 1));
    mesh_box->setTexture(tex_logo);
    mesh_box->setMaterial(material);
    mesh_box->setNodeName("mesh_box");
    Cc3dMesh*mesh_cone=c3dCreateConeMesh(100, 270, 20, 10, true, cc3dv4(1, 0, 0, 1), cc3dv4(0, 1, 0, 1));
    mesh_cone->setTexture(tex_logo);
    mesh_cone->setMaterial(material);
    mesh_cone->setNodeName("mesh_cone");

  
    //actors
    const int actorCount=3;
    vector<Cc3dMesh*> meshList;
    meshList.push_back(mesh_box);
    meshList.push_back(mesh_cone);
    meshList.push_back(mesh_ball);
    vector<Cc3dVector4> posList;
    posList.push_back(cc3dv4(-250, 0, 0, 1));
    posList.push_back(cc3dv4(0, -100, 0, 1));
    posList.push_back(cc3dv4(250, 0, 0, 1));
    for(int i=0;i<actorCount;i++){
        Cc3dActor* actor3D=new Cc3dActor();
        actor3D->autorelease();
        actor3D->init();
        actor3D->setPosition3D(posList[i]);
        actor3D->addMesh(meshList[i]);
        actor3D->setLightSource(lightSource);
        actor3D->setCamera3D(camera);
        actor3D->setPassUnifoCallback(passUnifoCallback_classicLighting);
        actor3D->setProgram(program);
        actor3D->setNodeName("actor3d");
        m_root3d->addChild(actor3D,0);
        m_actor3dList.push_back(actor3D);
    }
    //submit actors
    for(int i=0;i<(int)m_actor3dList.size();i++){
        Cc3dActor*actor3D=m_actor3dList[i];
        actor3D->submit(GL_STATIC_DRAW);
    }

    

    //controlButton_swithProjMode
    {
        CCScale9Sprite* btnUp=CCScale9Sprite::create("button.png");
        CCScale9Sprite* btnDn=CCScale9Sprite::create("button_dn.png");
        CCLabelTTF*title=CCLabelTTF::create("proj mode", "Helvetica", 30);
        CCControlButton* controlButton=CCControlButton::create(title, btnUp);
        controlButton->setBackgroundSpriteForState(btnDn,CCControlStateHighlighted);
        controlButton->setPreferredSize(CCSize(180,80));
        controlButton->setPosition(ccp(400,100));
        controlButton->addTargetWithActionForControlEvents(this, (SEL_CCControlHandler)(&CtestLayer::switchProjModeCallBack), CCControlEventTouchDown);
        this->addChild(controlButton);
        m_controlButton_swithProjMode=controlButton;
        
    }
    
    //controlButton_transform
    {
        CCScale9Sprite* btnUp=CCScale9Sprite::create("button.png");
        CCScale9Sprite* btnDn=CCScale9Sprite::create("button_dn.png");
        CCLabelTTF*title=CCLabelTTF::create("transform", "Helvetica", 30);
        CCControlButton* controlButton=CCControlButton::create(title, btnUp);
        controlButton->setBackgroundSpriteForState(btnDn,CCControlStateHighlighted);
        controlButton->setPreferredSize(CCSize(180,80));
        controlButton->setPosition(ccp(700,100));
        controlButton->addTargetWithActionForControlEvents(this, (SEL_CCControlHandler)(&CtestLayer::transformCallBack), CCControlEventTouchDown);
        this->addChild(controlButton);
        m_controlButton_transform=controlButton;
        
    }
    //projection mode label
    m_pLabel=CCLabelTTF::create("proj mode: Perspective", "Arial", 35);
    m_pLabel->setPosition(ccp(origin.x + visibleSize.width*(3.0/4),
                              origin.y + visibleSize.height - m_pLabel->getContentSize().height-100));
    this->addChild(m_pLabel, 1);

    
    return true;
}