1, rotating camera, so that the camera around the teapot rotation, to achieve the effect of the teapot rotation!
2, rotating ambient light,
3, rotate the object itself, directly make the teapot rotation.
. h file
#ifndef __helloworld_scene_h__#define __helloworld_scene_h__#include "Cocos2d.h" Using_ns_cc;class HELLOWORLD: Public cocos2d::layer{public: static cocos2d::scene* createscene (); virtual BOOL init (); virtual void OnEnter (); virtual void onExit (); Create_func (HelloWorld); Sprite3d * PSPRITE3D; camera* Pcamera; camera* pcamera01;//glprogramstate* state;void Ontouchesbegan (const std::vector<touch*>& touches, cocos2d: : Event *event), void ontouchesmoved (const std::vector<touch*>& touches, cocos2d::event *event); void ontouchesended (const std::vector<touch*>& touches, cocos2d::event *event); float m_fangle;}; #endif//__helloworld_scene_h__
. cpp Files
#include "HelloWorldScene.h" #include "DrawNode3D.h" scene* helloworld::createscene () {//' Scene ' is an autorelease objec T auto scene = Scene::create (); ' Layer ' is an Autorelease object auto layer = Helloworld::create (); Add layer as a child to scene scene->addchild (layer); Return the scene return scene;} On the "Init" need to initialize your Instancebool helloworld::init () {if (! Layer::init ()) {return false; }////////////////size Size = director::getinstance ()->getwinsize ();p Camera01 = camera::createperspective (30.0f, Size.width/size.height, 1.0f, +//(60.0f, Size.width/size.height, 0.1f, Pcamera01->setcameraflag) ( Cameraflag::user1);p camera01->setposition3d (VEC3 (SIZE.WIDTH/2, SIZE.HEIGHT/2, 0.0f));//m_pcamera->lookat ( VEC3 (+, 0));//(480, 0)) Pcamera01->lookat (VEC3 (0,0, 0), VEC3 (0.0f, 1.0f, 0.0f)); This->addchild ( PCAMERA01);//create teapot Model Psprite3d = sprite3d::create ("teapot.c3b");p sprite3d->Setposition3d (VEC3 (0, 0, 0));p sprite3d->setrotation3d (VEC3 ( -90, 0,0));p Sprite3d->setscale (12.0f);p sprite3d- >setcameramask (2); This->addchild (psprite3d);///////m_fangle = 1.0f;//must be assigned an initial value, otherwise the rotation will not be! Auto listener = Eventlistenertouchallatonce::create (); Listener->ontouchesbegan = Cc_callback_2 (HelloWorld::o Ntouchesbegan, this): Listener->ontouchesmoved = Cc_callback_2 (helloworld::ontouchesmoved, this);listener-> ontouchesended = Cc_callback_2 (helloworld::ontouchesended, this);_eventdispatcher-> Addeventlistenerwithscenegraphpriority (listener, this); return true;} void Helloworld::onenter () {layer::onenter (); Autopglprogram = Glprogram::createwithfilenames ("Diffuse.vsh", " Diffuse.fsh "); autostate = Glprogramstate::create (pglprogram); long offset = 0;auto Attributecount = psprite3d-> Getmesh ()->getmeshvertexattribcount (); for (int k = 0; k < Attributecount; k++) {Auto Meshattribute = Psprite3d->g Etmesh ()->getmeshvertexattribute (k); State->setvertexattribpointer (s_attrIbutenames[meshattribute.vertexattrib],meshattribute.size,meshattribute.type,gl_false,psprite3d->getmesh ()- >getvertexsizeinbytes (), (glvoid*) offset); offset + = meshattribute.attribsizebytes;} Camera*pcamera = Director::getinstance ()->getrunningscene ()->getdefaultcamera (); Vec3pos = Pcamera->getposition3d (); Vec3target = VEC3 ( -10, -10, -10);//(480, 0); STATE->SETUNIFORMVEC3 ("V_eyepos", Pos-target);p sprite3d-> Setglprogramstate (state);} void Helloworld::ontouchesbegan (const std::vector<touch*>& touches, cocos2d::event *event) {}void helloworld::ontouchesmoved (const std::vector<touch*>& touches, cocos2d::event *event) {if (Touches.size ()) {Auto Touch = Touches[0];auto Delta = Touch->getdelta (); M_fangle-= Cc_degrees_to_radians (delta.x);//psprite3d-> Setposition3d (VEC3 (100.0f * Sinf (M_fangle), 15.0f, 100.0f * COSF (M_fangle)));//pcamera01->setposition3d (VEC3 ( 100.0f * Sinf (M_fangle), 500.0f, 100.0f * COSF (M_fangle)));p Camera01->lookat (VEC3 (0, 0, 0), VEC3 (0.0f,1.0f, 0.0f));p sprite3d->setrotation3d (VEC3 ( -90.0f, 100.0f * m_fangle,0.0f));}} void helloworld::ontouchesended (const std::vector<touch*>& touches, cocos2d::event *event) {}void Helloworld::onexit () {layer::onexit ();}
Three ways the teapot rotates under the touch mechanism