Bullet(Cocos2dx)之优化PhysicsDraw3D

前端之家收集整理的这篇文章主要介绍了Bullet(Cocos2dx)之优化PhysicsDraw3D前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Bullet(Cocos2dx)之优化PhysicsDraw3D

分类 cocos2d-x 物理引擎 168人阅读 评论(0) 收藏 举报

为了测试bullet物体的大小,匆匆写完的PhysicsDraw3D的效率低的要命,这也是为什么cocos2dx弃用了DrawPrimitives,而去使用DrawNode

DrawPrimitives每次绘制都去调用glDrawElements,假如每帧绘制10000条线段,那么就要调用10000次glDrawElements,可见效率之低。

而DrawNode采取的是批处理的方式,当drawLine的时候不是立即绘制,而是将线段的信息添加到数组里,当draw时统一调用gl的绘制函数

10000/1可不是一个小数目啊。

下图使用DrawPrimitives方法


加入40Sphere帧率就掉到4070的帧率更是惨不忍睹


下图使用DrawNode方法


为了解决这个问题就要参照DrawNode实现一个简单的DrawNode3D

不管三七二十一,将DrawNode的头文件代码copy,删去一些不需要的,

1.修改V2F_C4B_T2FV3F_C4B_T2F

2.修改Vec2为Vec3,要绘制3D

3.保留drawPoint,drawPoints,drawLine,其他的绘制函数不要

  1. #ifndef__DRAW_NODE_3D_H__
  2. #define__DRAW_NODE_3D_H__
  3. #include"cocos2d.h"
  4. USING_NS_CC;
  5. classDrawNode3D:publicNode
  6. {
  7. public:
  8. staticDrawNode3D*create();
  9. voiddrawPoint(constVec3&point,constfloatpointSize,constColor4F&color);
  10. voiddrawPoints(constVec3*position,unsignedintnumberOfPoints,constColor4F&color);
  11. voiddrawLine(constVec3&origin,constVec3&destination,constColor4F&color);
  12. //Overrides
  13. virtualvoiddraw(Renderer*renderer,constMat4&transform,uint32_tflags)override;
  14. voidclear();
  15. constBlendFunc&getBlendFunc()const;
  16. voidsetBlendFunc(constBlendFunc&blendFunc);
  17. voidonDraw(constMat4&transform,uint32_tflags);
  18. voidonDrawGLLine(constMat4&transform,uint32_tflags);
  19. voidonDrawGLPoint(constMat4&transform,uint32_tflags);
  20. CC_CONSTRUCTOR_ACCESS:
  21. DrawNode3D();
  22. virtual~DrawNode3D();
  23. virtualboolinit();
  24. protected:
  25. voidensureCapacity(intcount);
  26. voidensureCapacityGLPoint(intcount);
  27. voidensureCapacityGLLine(intcount);
  28. GLuint_vao;
  29. GLuint_vbo;
  30. GLuint_vaoGLPoint;
  31. GLuint_vboGLPoint;
  32. GLuint_vaoGLLine;
  33. GLuint_vboGLLine;
  34. int_bufferCapacity;
  35. GLsizei_bufferCount;
  36. V3F_C4B_T2F*_buffer;
  37. int_bufferCapacityGLPoint;
  38. GLsizei_bufferCountGLPoint;
  39. V3F_C4B_T2F*_bufferGLPoint;
  40. Color4F_pointColor;
  41. int_pointSize;
  42. int_bufferCapacityGLLine;
  43. GLsizei_bufferCountGLLine;
  44. V3F_C4B_T2F*_bufferGLLine;
  45. BlendFunc_blendFunc;
  46. CustomCommand_customCommand;
  47. CustomCommand_customCommandGLPoint;
  48. CustomCommand_customCommandGLLine;
  49. bool_dirty;
  50. bool_dirtyGLPoint;
  51. bool_dirtyGLLine;
  52. private:
  53. CC_DISALLOW_COPY_AND_ASSIGN(DrawNode3D);
  54. };
  55. #endif

对于DrawNode.cpp按照上面所说同样修改

要记住

  1. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION,3,GL_FLOAT,GL_FALSE,sizeof(V3F_C4B_T2F),(GLvoid*)offsetof(V3F_C4B_T2F,vertices));
  2. 要将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,那么就不会清空上一帧的绘制数据

具体修改如下:

  1. PhysicsDraw3D*PhysicsDraw3D::createWithLayer(Node*layer)
  2. {
  3. autodraw=newPhysicsDraw3D;
  4. if(draw&&draw->initWithLayer(layer))
  5. {
  6. returndraw;
  7. }
  8. returnnullptr;
  9. }
  10. boolPhysicsDraw3D::initWithLayer(Node*layer)
  11. {
  12. _drawNode=DrawNode3D::create();
  13. layer->addChild(_drawNode);
  14. _debugDrawMode=btIDebugDraw::DBG_MAX_DEBUG_DRAW_MODE;
  15. returntrue;
  16. }
  17. voidPhysicsDraw3D::clearDraw()
  18. {
  19. _drawNode->clear();
  20. }

销毁时也要将_drawNode从Parent中移除

  1. voidPhysicsDraw3D::destroy()
  2. {
  3. _drawNode->removeFromParent();
  4. deletethis;
  5. }
  6. drawLine也就简化了
  7. voidPhysicsDraw3D::drawLine(constbtVector3&from,constbtVector3&to,constbtVector3&color)
  8. {
  9. Vec3vertices[2]={
  10. Vec3(from.x(),from.y(),from.z()),
  11. Vec3(to.x(),to.y(),to.z())
  12. };
  13. _color.r=color.x();
  14. _color.g=color.y();
  15. _color.b=color.z();
  16. _color.a=1.f;
  17. _drawNode->drawLine(vertices[0],vertices[1],_color);
  18. }

PhysicsWorld3D创建的静态函数添加

  1. staticPhysicsWorld3D*createWithDebug(Node*layer,constbtVector3&gravity=btVector3(0,-10,0));

为的就是创建调试绘制

  1. boolPhysicsWorld3D::initWorldWithDebug(Node*layer,constbtVector3&gravity)
  2. {
  3. if(!this->initWorld(gravity))
  4. {
  5. returnfalse;
  6. }
  7. _debugDraw=PhysicsDraw3D::createWithLayer(layer);
  8. _world->setDebugDrawer(_debugDraw);
  9. returntrue;
  10. }

同时删除initWorld对_debugDraw的创建,每次绘制时需要判断是否为debug

  1. voidPhysicsWorld3D::debugDraw()
  2. {
  3. if(_debugDraw)
  4. {
  5. _debugDraw->clearDraw();
  6. _world->debugDrawWorld();
  7. }
  8. }

完整源码

csdn

github

原文链接:https://www.f2er.com/cocos2dx/344778.html

猜你在找的Cocos2d-x相关文章