Bullet(Cocos2dx)之优化PhysicsDraw3D
为了测试bullet物体的大小,匆匆写完的PhysicsDraw3D的效率低的要命,这也是为什么cocos2dx弃用了DrawPrimitives,而去使用DrawNode
DrawPrimitives每次绘制都去调用glDrawElements,假如每帧绘制10000条线段,那么就要调用10000次glDrawElements,可见效率之低。
而DrawNode采取的是批处理的方式,当drawLine的时候不是立即绘制,而是将线段的信息添加到数组里,当draw时统一调用gl的绘制函数
10000/1可不是一个小数目啊。
下图使用DrawPrimitives方法
加入40个Sphere帧率就掉到40,70的帧率更是惨不忍睹
下图使用DrawNode方法
为了解决这个问题就要参照DrawNode实现一个简单的DrawNode3D
不管三七二十一,将DrawNode的头文件代码copy,删去一些不需要的,
1.修改V2F_C4B_T2F为V3F_C4B_T2F
2.修改Vec2为Vec3,要绘制3D
3.保留drawPoint,drawPoints,drawLine,其他的绘制函数不要
- #ifndef__DRAW_NODE_3D_H__
- #define__DRAW_NODE_3D_H__
- #include"cocos2d.h"
- USING_NS_CC;
- classDrawNode3D:publicNode
- {
- public:
- staticDrawNode3D*create();
- voiddrawPoint(constVec3&point,constfloatpointSize,constColor4F&color);
- voiddrawPoints(constVec3*position,unsignedintnumberOfPoints,constColor4F&color);
- voiddrawLine(constVec3&origin,constVec3&destination,constColor4F&color);
- //Overrides
- virtualvoiddraw(Renderer*renderer,constMat4&transform,uint32_tflags)override;
- voidclear();
- constBlendFunc&getBlendFunc()const;
- voidsetBlendFunc(constBlendFunc&blendFunc);
- voidonDraw(constMat4&transform,uint32_tflags);
- voidonDrawGLLine(constMat4&transform,uint32_tflags);
- voidonDrawGLPoint(constMat4&transform,uint32_tflags);
- CC_CONSTRUCTOR_ACCESS:
- DrawNode3D();
- virtual~DrawNode3D();
- virtualboolinit();
- protected:
- voidensureCapacity(intcount);
- voidensureCapacityGLPoint(intcount);
- voidensureCapacityGLLine(intcount);
- GLuint_vao;
- GLuint_vbo;
- GLuint_vaoGLPoint;
- GLuint_vboGLPoint;
- GLuint_vaoGLLine;
- GLuint_vboGLLine;
- int_bufferCapacity;
- GLsizei_bufferCount;
- V3F_C4B_T2F*_buffer;
- int_bufferCapacityGLPoint;
- GLsizei_bufferCountGLPoint;
- V3F_C4B_T2F*_bufferGLPoint;
- Color4F_pointColor;
- int_pointSize;
- int_bufferCapacityGLLine;
- GLsizei_bufferCountGLLine;
- V3F_C4B_T2F*_bufferGLLine;
- BlendFunc_blendFunc;
- CustomCommand_customCommand;
- CustomCommand_customCommandGLPoint;
- CustomCommand_customCommandGLLine;
- bool_dirty;
- bool_dirtyGLPoint;
- bool_dirtyGLLine;
- private:
- CC_DISALLOW_COPY_AND_ASSIGN(DrawNode3D);
- };
- #endif
对于DrawNode.cpp按照上面所说同样修改
要记住
- glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION,3,GL_FLOAT,GL_FALSE,sizeof(V3F_C4B_T2F),(GLvoid*)offsetof(V3F_C4B_T2F,vertices));
- 要将GLProgram::VERTEX_ATTRIB_POSITION,2改为GLProgram::VERTEX_ATTRIB_POSITION,3
因为顶点有三个元素,.cpp代码过多,请在文章最后下载源码,要注意的是绘制时开启深度测试
修改PhysicsDraw3D
删除成员变量,添加DrawNode3D*_drawNode,由于DrawNode3D继承自Node所以创建时要将其添加到父节点上,
修改create,init为如下
staticPhysicsDraw3D*createWithLayer(Node*layer);
boolinitWithLayer(Node*layer);
同时添加
voidclearDraw();
我们知道DrawNode如果不执行clear,那么就不会清空上一帧的绘制数据
具体修改如下:
- PhysicsDraw3D*PhysicsDraw3D::createWithLayer(Node*layer)
- {
- autodraw=newPhysicsDraw3D;
- if(draw&&draw->initWithLayer(layer))
- {
- returndraw;
- }
- returnnullptr;
- }
- boolPhysicsDraw3D::initWithLayer(Node*layer)
- {
- _drawNode=DrawNode3D::create();
- layer->addChild(_drawNode);
- _debugDrawMode=btIDebugDraw::DBG_MAX_DEBUG_DRAW_MODE;
- returntrue;
- }
- voidPhysicsDraw3D::clearDraw()
- {
- _drawNode->clear();
- }
销毁时也要将_drawNode从Parent中移除
- voidPhysicsDraw3D::destroy()
- {
- _drawNode->removeFromParent();
- deletethis;
- }
- drawLine也就简化了
- voidPhysicsDraw3D::drawLine(constbtVector3&from,constbtVector3&to,constbtVector3&color)
- {
- Vec3vertices[2]={
- Vec3(from.x(),from.y(),from.z()),
- Vec3(to.x(),to.y(),to.z())
- };
- _color.r=color.x();
- _color.g=color.y();
- _color.b=color.z();
- _color.a=1.f;
- _drawNode->drawLine(vertices[0],vertices[1],_color);
- }
- staticPhysicsWorld3D*createWithDebug(Node*layer,constbtVector3&gravity=btVector3(0,-10,0));
为的就是创建调试绘制
- boolPhysicsWorld3D::initWorldWithDebug(Node*layer,constbtVector3&gravity)
- {
- if(!this->initWorld(gravity))
- {
- returnfalse;
- }
- _debugDraw=PhysicsDraw3D::createWithLayer(layer);
- _world->setDebugDrawer(_debugDraw);
- returntrue;
- }
同时删除initWorld对_debugDraw的创建,每次绘制时需要判断是否为debug
- voidPhysicsWorld3D::debugDraw()
- {
- if(_debugDraw)
- {
- _debugDraw->clearDraw();
- _world->debugDrawWorld();
- }
- }
完整源码
csdn