码迷,mamicode.com
首页 > 其他好文 > 详细

茶壶在触摸机制下旋转的三种方式

时间:2016-05-11 07:24:56      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

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<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文件

#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<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)));
		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<Touch*>& touches, cocos2d::Event  *event)
{

}
void HelloWorld::onExit()
{
	Layer::onExit();
}



茶壶在触摸机制下旋转的三种方式

标签:

原文地址:http://blog.csdn.net/anzhongliu/article/details/51368131

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!