码迷,mamicode.com
首页 > 其他好文 > 详细

Bullet3之优化PhysicsDraw3D

时间:2015-01-06 20:03:24      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:cocos2dx   bullet   物理引擎   

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

Bullet3之优化PhysicsDraw3D

标签:cocos2dx   bullet   物理引擎   

原文地址:http://blog.csdn.net/ctxdecs/article/details/42464099

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!