圆柱的触摸旋转
来源:互联网 发布:scrm源码 编辑:程序博客网 时间:2024/04/28 08:47
.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 manuallyprotected: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){}
效果图:
0 0
- 圆柱的触摸旋转
- 圆柱的自动旋转
- NYOJ 598 旋转圆柱矩阵
- iOS开发 单手使触摸的视图旋转
- 触摸物体的旋转 移动 缩放功能
- android3D 正方体,触摸旋转
- 图片触摸旋转
- cocos2d-x实现触摸旋转键盘的例子
- 茶壶在触摸机制下旋转的三种方式
- Unity 做触摸 像弹簧一样的旋转
- 关于圆柱的计算
- 圆柱的表面积
- 求圆柱的表面积
- 圆柱的表面积
- 圆柱类的设计
- 圆柱类的设计
- 求圆柱的体积
- 圆柱的体积
- 推荐文章
- effective stl学习笔记
- Linux apache mysql php 安装
- C语言中结构体的初始化
- 极路由H3C(华3)版本号正确的填写格式
- 圆柱的触摸旋转
- 【DSP开发】如何获得浮点型的数据的IEEE的表示
- 九度OJ 1122:吃糖果 (递归)
- 西门子PC Adapter USB,在线S7-200时,提示端口被占用,不搜索站点问题的一个解决方案:
- LiteHttp 第二节:简化请求和非安全方法的使用
- 23种 设计模式
- C语言关键字
- 开发常见的对象-集合类
- STL实现学生通讯录