Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D
来源:互联网 发布:apache php exec 权限 编辑:程序博客网 时间:2024/04/28 19:00
由于刚开始学的时候不知道Bullet的单位1.0代表多大,所以制作出的模型的大小也无法判断。
不用担心,Bullet提供了一个类btIDebugDraw,这个泪已经实现了很多绘制功能,我们要做的就是实现几个虚函数。
我们继承btIDebugDraw,实现虚函数
class PhysicsDraw3D : public btIDebugDraw{public:void drawLine(const btVector3& from,const btVector3& to,const btVector3& color);void drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB, btScalar distance,int lifeTime,const btVector3& color);void reportErrorWarning(const char* warningString);void draw3dText(const btVector3& location,const char* textString);void setDebugMode(int debugMode);int getDebugMode() const;// ...................private:int _debugDrawMode;// ...................};
目前只需要drawLine,等以后涉及到其他方面时再来实现其他的绘制功能
drawContactPoint, draw3dText 什么也不做。
_debugDrawMode 一定要定义,因为btIDebugDraw在绘制时会检查需要绘制什么。
实现绘制模式
void PhysicsDraw3D::setDebugMode(int debugMode){_debugDrawMode = debugMode;}int PhysicsDraw3D::getDebugMode() const{return _debugDrawMode;}
下面重点说一下drawLine,我们知道cocos2dx 的DrawNode 提供的都是绘制2d图元的功能,
而且修改起来也比较复杂,本着一切从简,主要是为了学习Bullet,所以我们就去copy一下
DrawPrimitives绘制Line的方法
同样DrawPrimitives::drawLine也是一个绘制2d Line的方法,那么就应该把它修改成可以绘制3D Line的方法,先观察这个drawLine的实现
void drawLine(const Vec2& origin, const Vec2& destination){lazy_init();Vec2 vertices[2] = {Vec2(origin.x, origin.y),Vec2(destination.x, destination.y)};_color.r = color.x();_color.g = color.y();_color.b = color.z();s_shader->use();s_shader->setUniformsForBuiltins();s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );#ifdef EMSCRIPTENsetGLBufferData(vertices, 16);glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);#elseglVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);#endif // EMSCRIPTENglDrawArrays(GL_LINES, 0, 2);CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);}
首先初始化一些数据(待会看),
1.vertices 就是存放的2d line的起点终点,如果绘制3d line 自然要改成Vec3
s_shader 使用shader这个shader是cocos2dx 启动时预载入的
2.EMSCRIPTEN 看这 http://www.cocos2d-x.org/wiki/Emscripten_usage
Cocos2D-X’s Emscripten support allows games written in C*+ to be compiled to JavaScript and deployed directly to the web......
既然是js要用的暂且忽略
3.重点的重点来了 http://www.baike.com/wiki/glVertexAttribPointer
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT,
GL_FALSE, 0, vertices);
其中的2就是便是一个顶点的坐标数量,2d 自然是2, 3d的就是3了
注意:在glVertexAttribPointer 之前一定要开启深度测试
glEnable(GL_DEPTH_TEST);绘制完成后要关闭深度检测glDisable(GL_DEPTH_TEST);
如果不开启深度测试看看这个效果你就明白了
可以看到阿狸的手被遮挡了,因为不开启深度测试gl就不会根据深度来显示,而是后绘制的会覆盖先绘制的
开启后
4.对这个划线方法修改以后
void PhysicsDraw3D::drawLine(const btVector3& from,const btVector3& to,const btVector3& color){Vec3 vertices[2] = {Vec3(from.x(), from.y(), from.z()),Vec3(to.x(), to.y(), to.z())};_shader->use();_shader->setUniformsForBuiltins();_shader->setUniformLocationWith4fv(_colorLocation, (GLfloat*) &_color.r, 1);glEnable(GL_DEPTH_TEST);GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, vertices);glDrawArrays(GL_LINES, 0, 2);glDisable(GL_DEPTH_TEST);CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);}
再来看看lazy_init
// // Position and 1 color passed as a uniform (to simulate glColor4ub ) // s_shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR); s_shader->retain(); s_colorLocation = s_shader->getUniformLocation("u_color"); CHECK_GL_ERROR_DEBUG(); s_pointSizeLocation = s_shader->getUniformLocation("u_pointSize"); CHECK_GL_ERROR_DEBUG(); s_initialized = true;
其实就是获取shader。
s_colorLocation color属性,可以对制定绘制时的颜色
s_pointSizeLocation point size 属性 可以制定绘制点的大小
那么添加到PhysicsDraw3D, 就如下
bool PhysicsDraw3D::initDraw(){_shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);if (_shader == nullptr){return false;}_shader->retain();_colorLocation = _shader->getUniformLocation("u_color");CHECK_GL_ERROR_DEBUG();_pointSizeLocation = _shader->getUniformLocation("u_pointSize");CHECK_GL_ERROR_DEBUG();_debugDrawMode = btIDebugDraw::DBG_MAX_DEBUG_DRAW_MODE; // 绘制全部调试信息return true;}
方便起见,为PhysicsDraw3D 添加构建方法
PhysicsDraw3D* PhysicsDraw3D::create(){auto draw = new PhysicsDraw3D;if (draw && draw->initDraw()){return draw;}return nullptr;}
销毁方法
void PhysicsDraw3D::destroy(){CC_SAFE_RELEASE_NULL(_shader);delete this;}
将PhysicsDraw3D整合到PhysicsWorld3D
为PhysicsWorld3D添加私有变量
PhysicsDraw3D* _debugDraw;
并在initWorld 最后添加
_debugDraw = PhysicsDraw3D::create();_world->setDebugDrawer(_debugDraw);
在destroy 前添加
_debugDraw->destroy();_debugDraw = nullptr;
完整代码见文章最后
测试这个PhysicsDraw3D
在HelloWorld重载
void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags){_world->debugDraw();Layer::draw(renderer, transform, flags);}
按F5编译通过后可以看到Plane为一个坐标,box有包围盒和法向量
后续
给box提供一个冲量box会在原来的基础上增加一定的速度,
这个速度就是impulse / mass, 看一下applyCentralImpulse的实现就明白了
_box->applyCentralImpulse(btVector3(0, 0, -10));
增加touch listener
bool HelloWorld::initListener(){_touchListener = EventListenerTouchOneByOne::create();if (_touchListener == nullptr){return false;}_touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);_touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);_touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_touchListener, this);return true;}
触摸时提供冲量
bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event){_box->setActivationState(ACTIVE_TAG);_box->applyCentralImpulse(btVector3(0, 0, -10));return true;}
Box跑远了
完整源码
- Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D
- Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D
- Bullet(Cocos2dx)之优化PhysicsDraw3D
- Bullet(Cocos2dx)之优化PhysicsDraw3D
- Bullet(Cocos2dx)之封装PhysicsWorld3D
- Bullet(Cocos2dx)之创建地形
- Bullet(Cocos2dx)之创建地形
- Bullet(Cocos2dx)之使用cocos2dx测试PhysicsWorld3D
- Bullet(Cocos2dx)之使用cocos2dx测试PhysicsWorld3D
- Bullet(Cocos2dx)之Hello World(vs2012)
- Bullet(Cocos2dx)之交叉编译Android,集成到cocos2dx3.x
- Bullet(Cocos2dx)之分析刚体创建与销毁(Primitives)
- Bullet(Cocos2dx)之交叉编译Android,集成到cocos2dx3.x
- Bullet(Cocos2dx)之分析刚体创建与销毁(Primitives)
- Bullet(Cocos2dx)之凸多面体形状和组合形状
- cocos2dx调试之cocos2dx的Log改造
- cocos2dx之渲染树的绘制
- cocos2dx之渲染树的绘制
- Excel导入到数据库(二)
- 查找单链表中的环入口点
- I/O多路转接---epoll的笔记
- 更多编译器对C++11的支持比较
- Android 动画学习笔记
- Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D
- Eclipse下使用SVN版本控制
- pig数据结构、读写操作、诊断操作、内建函数
- 系统调优
- 放荡青春,我们没有谁对谁错
- 1.图层树 ---iOS-Core-Animation-Advanced-Techniques
- js正则表达式:验证邮箱格式、密码复杂度、手机号码、QQ号码
- Singleton模式的各种实现方式。
- C++实现生成均匀分布的数字