圆柱的触摸旋转

.h文件:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();
	float _cylinder_texture_offset;
	float _shining_duraion;
	cocos2d::GLProgramState * _state;
	void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event);
	void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event);
	void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event);
    // implement the "static create()" method manually
protected:
	cocos2d::Camera *_camera;
	cocos2d::Label *_particleLab;
	float _angle;

    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

.cpp文件:

#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "Particle3D/CCParticleSystem3D.h"
USING_NS_CC;
using namespace cocostudio::timeline;

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

//use custom camera  


bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
	

	//auto rootNode = CSLoader::createNode("MainScene.csb");
	//rootNode->setCameraMask((unsigned short)CameraFlag::USER1);
	//rootNode->setScale(0.01);
   // addChild(rootNode);
	/////
	_cylinder_texture_offset = 0;
	_shining_duraion = 0;

	//Size visibleSize = Director::getInstance()->getVisibleSize();
	//auto camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
//	camera->setCameraFlag(CameraFlag::USER1);
	

	//create cylinder  
	auto cylinder = Sprite3D::create("Sprite3DTest/cylinder.c3b");

	//create and set our custom shader  
	auto shader = GLProgram::createWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag");
	_state = GLProgramState::create(shader);
	cylinder->setGLProgramState(_state);

	_state->setUniformFloat("offset", _cylinder_texture_offset);
	_state->setUniformFloat("duration", _shining_duraion);
	//pass mesh's attribute to shader  
	long offset = 0;
	auto attributeCount = cylinder->getMesh()->getMeshVertexAttribCount();
	for (auto i = 0; i < attributeCount; i++) {
		auto meshattribute = cylinder->getMesh()->getMeshVertexAttribute(i);
		_state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
			meshattribute.size,
			meshattribute.type,
			GL_FALSE,
			cylinder->getMesh()->getVertexSizeInBytes(),
			(GLvoid*)offset);
		offset += meshattribute.attribSizeBytes;
	}

	//create the second texture for cylinder  
	auto shining_texture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/caustics.png");
	Texture2D::TexParams tRepeatParams;//set texture parameters  
	tRepeatParams.magFilter = GL_NEAREST;
	tRepeatParams.minFilter = GL_NEAREST;
	tRepeatParams.wrapS = GL_REPEAT;
	tRepeatParams.wrapT = GL_REPEAT;
	shining_texture->setTexParameters(tRepeatParams);
	//pass the texture sampler to our custom shader  
	_state->setUniformTexture("caustics", shining_texture);


	this->addChild(cylinder);
	cylinder->setCameraMask((unsigned short)CameraFlag::USER1);

	//adjust cylinder's position & rotation  
	cylinder->setPosition3D(Vec3(0, -15, -50));
	cylinder->setRotation3D(Vec3(-90, 0, 0));





	//////

	//////
	Size size = Director::getInstance()->getWinSize();
	_camera = Camera::createPerspective(30.0f, size.width / size.height, 1.0f, 1000.0f);
	_camera->setPosition3D(Vec3(size.width / 2, size.height/2, 100.0f));
	_camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
	_camera->setCameraFlag(CameraFlag::USER1);
	this->addChild(_camera);
	
	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);
	scheduleUpdate();
    return true;
}

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();
		_angle -= CC_DEGREES_TO_RADIANS(delta.x);
		_camera->setPosition3D(Vec3(100.0f * sinf(_angle), 0.0f, 100.0f * cosf(_angle)));
		_camera->lookAt(Vec3(2.0f, 5.0f, 10.0f), Vec3(0.0f, 1.0f, 0.0f));
	}
}

void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event  *event)
{

}

效果图:


原文地址:https://www.cnblogs.com/Anzhongliu/p/6091902.html