展会信息港展会大全

Bullet(Cocos2dx)之优化PhysicsDraw3D,cocos2dxdraw
来源:互联网   发布日期:2015-09-28 14:03:08   浏览:2617次  

导读: Bullet(Cocos2dx)之优化PhysicsDraw3D,cocos2dxdraw 为了测试bullet物体的大小,匆匆写完的PhysicsDraw3D的效率低的要命,这也是为什么co...

Bullet(Cocos2dx)之优化PhysicsDraw3D,cocos2dxdraw

为了测试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;

class DrawNode3D : public Node

{

public:

static DrawNode3D* create();

void drawPoint(const Vec3& point, const float pointSize, const Color4F &color);

void drawPoints(const Vec3 *position, unsigned int numberOfPoints, const Color4F &color);

void drawLine(const Vec3 &origin, const Vec3 &destination, const Color4F &color);

// Overrides

virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;

void clear();

const BlendFunc& getBlendFunc() const;

void setBlendFunc(const BlendFunc &blendFunc);

void onDraw(const Mat4 &transform, uint32_t flags);

void onDrawGLLine(const Mat4 &transform, uint32_t flags);

void onDrawGLPoint(const Mat4 &transform, uint32_t flags);

CC_CONSTRUCTOR_ACCESS:

DrawNode3D();

virtual ~DrawNode3D();

virtual bool init();

protected:

void ensureCapacity(int count);

void ensureCapacityGLPoint(int count);

void ensureCapacityGLLine(int count);

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为如下

static PhysicsDraw3D* createWithLayer(Node* layer);

bool initWithLayer(Node* layer);

同时添加

void clearDraw();

我们知道DrawNode如果不执行clear,那么就不会清空上一帧的绘制数据

具体修改如下:

PhysicsDraw3D* PhysicsDraw3D::createWithLayer(Node* layer)

{

auto draw = new PhysicsDraw3D;

if (draw && draw->initWithLayer(layer))

{

return draw;

}

return nullptr;

}

bool PhysicsDraw3D::initWithLayer(Node* layer)

{

_drawNode = DrawNode3D::create();

layer->addChild(_drawNode);

_debugDrawMode = btIDebugDraw::DBG_MAX_DEBUG_DRAW_MODE;

return true;

}

void PhysicsDraw3D::clearDraw()

{

_drawNode->clear();

}

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

void PhysicsDraw3D::destroy()

{

_drawNode->removeFromParent();

delete this;

}

drawLine也就简化了

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())

};

_color.r = color.x();

_color.g = color.y();

_color.b = color.z();

_color.a = 1.f;

_drawNode->drawLine(vertices[0], vertices[1], _color);

}

PhysicsWorld3D 创建的静态函数添加

static PhysicsWorld3D* createWithDebug(Node* layer, const btVector3& gravity = btVector3(0, -10, 0));

为的就是创建调试绘制

bool PhysicsWorld3D::initWorldWithDebug(Node* layer, const btVector3& gravity)

{

if (!this->initWorld(gravity))

{

return false;

}

_debugDraw = PhysicsDraw3D::createWithLayer(layer);

_world->setDebugDrawer(_debugDraw);

return true;

}

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

void PhysicsWorld3D::debugDraw()

{

if (_debugDraw)

{

_debugDraw->clearDraw();

_world->debugDrawWorld();

}

}

完整源码

csdn

github

http://www.bkjia.com/Androidjc/939705.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/939705.htmlTechArticleBullet(Cocos2dx)之优化PhysicsDraw3D,cocos2dxdraw 为了测试bullet物体的大小,匆匆写完的PhysicsDraw3D的效率低的要命,这也是为什么cocos2dx弃用了Dra...

赞助本站

人工智能实验室

相关热词: android开发 应用开发

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港