Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 茶壺在觸摸機制下旋轉的三種方式

茶壺在觸摸機制下旋轉的三種方式

編輯:關於Android編程

1、旋轉攝像機,使攝像機圍繞茶壺旋轉,達到茶壺自轉的效果!

2、旋轉環境光,

3、旋轉物體自身,直接使茶壺自轉。


.h文件

#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& 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__

.cpp文件

#include "HelloWorldScene.h"
#include "DrawNode3D.h"



Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    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 "init" you need to initialize your instance
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);//(60.0f, size.width / size.height, 0.1f, 1000)
	pCamera01->setCameraFlag(CameraFlag::USER1);
	pCamera01->setPosition3D(Vec3(size.width / 2, size.height / 2, 0.0f));
	//m_pCamera->lookAt(Vec3(20, 200, 0));//(480, 320, 0))
	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);//(480, 320, 0);

	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);
		//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)));
		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();
}



  1. 上一頁:
  2. 下一頁:
Copyright © Android教程網 All Rights Reserved