Пример #1
0
void CtestLayer::update(float dt){
    if(m_isDoUpdate==false)return;
    m_actor3dList[0]->rotateRelativeToParent3D(cc3dv4(0, 1, 0, 0), 120*dt);
    m_actor3dList[0]->rotate3D(cc3dv4(1, 0, 0, 0), 120*dt);
    m_actor3dList[2]->rotateRelativeToParent3D(cc3dv4(0, 1, 0, 0), 120*dt);
    m_actor3dList[2]->rotate3D(cc3dv4(0, 1, 0, 0), 240*dt);

    
}
Пример #2
0
void CtestLayer::update(float dt){
    if(m_isDoUpdate==false)return;
    m_actor3D->rotate3D(cc3dv4(0, 1, 0, 0), 120*dt);

    
}
Пример #3
0
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;
}
Пример #4
0
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;
}
Пример #5
0
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;
}
Пример #6
0
void Cball::makeBall(float r,int nSlice,int nStack,string texFilePath){
	
	//make ball
	float dA=360.0/nSlice;
    float dB=180.0/nStack;
    const float PIDIV180=c3d_PI/180;
    vector<Cc3dVector4> positionList;
    vector<Cc3dVector2> texCoordList;
    vector<Cc3dVector4> normalList;
    vector<Cc3dIDTriangle> IDtriList;
    //generate positionList, texCoordList, normalList, colorList
    for(int i=0;i<=nStack;i++){
        float B=-90+i*dB;
        float y=r*sinf(B*PIDIV180);
        float cosB=cosf(B*PIDIV180);
        for (int j=0; j<=nSlice; j++) {
            float A=j*dA;
            float R=r*cosB;
            float x=R*cosf(A*PIDIV180);
            float z=R*sinf(A*PIDIV180);
            Cc3dVector4 position(x,y,z,1);
            float s=(float)j/nSlice+0.25;
            float t=1-(float)i/nStack;
            Cc3dVector2 texCoord(s,t);
            Cc3dVector4 normal=normalize(cc3dv4(position.x(), position.y(), position.z(), 0));
            positionList.push_back(position);
            texCoordList.push_back(texCoord);
            normalList.push_back(normal);
        }
    }
    //generate IDtriList
    for(int i=0;i<nStack;i++){
        for(int j=0;j<nSlice;j++){
            int vID_ld=(nSlice+1)*i+j;
            int vID_rd=vID_ld+1;
            int vID_ru=vID_rd+(nSlice+1);
            int vID_lu=vID_ru-1;
            Cc3dIDTriangle IDtri0(vID_ld,vID_rd,vID_ru);
            Cc3dIDTriangle IDtri1(vID_ld,vID_ru,vID_lu);
            IDtriList.push_back(IDtri0);
            IDtriList.push_back(IDtri1);
        }
    }
    Cc3dSubMesh*subMesh=new Cc3dSubMesh();
	subMesh->autorelease();
	int nPos=(int)positionList.size();
	for(int i=0;i<nPos;i++){
		Cc3dVector4&pos=positionList[i];
		Cc3dVector2&texCoord=texCoordList[i];
		Cc3dVector4&norm=normalList[i];
		subMesh->addVertex(Cc3dVertex(pos,texCoord,norm));
	}
	int nIDtri=(int)IDtriList.size();
	for(int i=0;i<nIDtri;i++){
		Cc3dIDTriangle&IDtri=IDtriList[i];
		subMesh->addIDtri(IDtri);
	}


	//--texture
	if(texFilePath.empty()==false){
		Cc3dTexture*texture=Cc3dTextureCache::sharedTextureCache()->addImage(texFilePath,GL_CLAMP_TO_EDGE,GL_CLAMP_TO_EDGE);
		subMesh->setTexture(texture);
	}

	//----mesh
	Cc3dMesh*mesh=new Cc3dMesh();
	mesh->autorelease();
	mesh->addSubMesh(subMesh);
	//----model
	this->addMesh(mesh);
}