編輯:關於 Android 程式設計
在 3D 場景中讓模型「轉起來」,本質上有三種截然不同的思路:
一是旋轉攝影機,讓鏡頭繞著茶壺轉,從視覺上營造出茶壺自轉的效果;二是旋轉環境光,藉由光影變化達到同樣的觀感;三是直接旋轉物體自身,讓茶壺真正自轉。本文以下方的範例採用的正是第三種——直接對模型套用旋轉。
先來看看頭檔的定義:
#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; void onTouchesBegan(const std::vector& touches, cocos2d::Event *event); void onTouchesMoved(const std::vector& touches, cocos2d::Event *event); void onTouchesEnded(const std::vector& touches, cocos2d::Event *event); float m_fAngle; }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" #include "DrawNode3D.h" Scene* HelloWorld::createScene() { auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if (!Layer::init()) { return false; } Size size = Director::getInstance()->getWinSize(); pCamera01 = Camera::createPerspective(30.0f, size.width / size.height, 1.0f, 1000); pCamera01->setCameraFlag(CameraFlag::USER1); pCamera01->setPosition3D(Vec3(size.width / 2, size.height / 2, 0.0f)); pCamera01->lookAt(Vec3(0,0, 0), Vec3(0.0f, 1.0f, 0.0f)); this->addChild(pCamera01); //創建茶壺模型 pSprite3D = Sprite3D::create("teapot.c3b"); pSprite3D->setPosition3D(Vec3(0, 0, 0)); pSprite3D->setRotation3D(Vec3(-90, 0,0)); pSprite3D->setScale(12.0f); pSprite3D->setCameraMask(2); this->addChild(pSprite3D); m_fAngle = 1.0f;//一定要賦初始值,否則旋轉不了! auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, 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(); auto pGLProgram = GLProgram::createWithFilenames("Diffuse.vsh", "Diffuse.fsh"); auto state = GLProgramState::create(pGLProgram); long offset = 0; auto attributeCount = pSprite3D->getMesh()->getMeshVertexAttribCount(); for (int k = 0; k < attributeCount; k++) { auto meshattribute = pSprite3D->getMesh()->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(); Vec3 pos = pCamera->getPosition3D(); Vec3 target = Vec3(-10, -10, -10); state->setUniformVec3("v_eyepos", pos - target); pSprite3D->setGLProgramState(state); } void HelloWorld::onTouchesBegan(const std::vector& touches, cocos2d::Event *event) { } void HelloWorld::onTouchesMoved(const std::vector& touches, cocos2d::Event *event) { if (touches.size()) { auto touch = touches[0]; auto delta = touch->getDelta(); m_fAngle -= CC_DEGREES_TO_RADIANS(delta.x); pCamera01->lookAt(Vec3(0, 0, 0), Vec3(0.0f,1.0f, 0.0f)); pSprite3D->setRotation3D(Vec3(-90.0f, 100.0f * m_fAngle,0.0f )); } } void HelloWorld::onTouchesEnded(const std::vector& touches, cocos2d::Event *event) { } void HelloWorld::onExit() { Layer::onExit(); }
程式碼中有兩個細節特別值得留意。其一,m_fAngle 一定要賦初始值,否則茶壺根本不會旋轉——這是很多人第一次實作時踩過的坑。其二,旋轉的核心邏輯集中在 onTouchesMoved 中:我們透過 touch->getDelta() 取得手指的橫向位移,換算成角度增量後累加到 m_fAngle,再用 setRotation3D 把它套用到茶壺的 Y 軸上,如此一來,手指滑動的幅度就能對應到模型旋轉的角度,操作起來相當直覺順手。
Android 監聽輸入法彈出與收起的可靠做法
用過 iOS 的都會注意到一個細節:鍵盤收起的同時,輸入框也會自動關閉。那麼在 Android 上要怎麼監聽輸入法的彈出與收起?本文提供一種實測可靠的實作方式。 第一
Java final 關鍵字:修變量、修方法、修類的三種語義
final 看起來簡單,實際上它在不同位置承擔著完全不同的語義。理清這三種用法,寫出來的代碼意圖會清楚很多。 1、修飾成員變量:把值鎖死 這是最主要的用途之一,作用類似
Android 四大元件之 Service(六):用 Binder 取得服務代理
前幾節我們學過了如何用 startService 啟動服務、用 bindService 綁定服務,也認識了服務的生命週期,以及什麼是 IntentService。 可能有
使用 MPAndroidChart 實作可旋轉餅圖
在需要呈現統計比例時,餅狀圖是常見選擇。若希望圖表更具互動性,可讓使用者透過觸控旋轉或查看不同扇區。本文以 MPAndroidChart 為例,說明如何在 Android