Bullet3之优化PhysicsDraw3D - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

Bullet3之优化PhysicsDraw3D

发表时间:2020-10-19

发布人:葵宇科技

浏览次数:46


static PhysicsDraw3D* createWithLayer(Node* layer);
为了测试bullet物体的大年夜小,促写完的PhysicsDraw3D的效力低的要命,这也是为什么cocos2dx弃用了DrawPrimitives,而去应用DrawNode
DrawPrimitives每次绘制都去调用glDrawElements,假如每帧绘制10000条线段,那么就要调用10000次glDrawElements,可奏效力之低。
而DrawNode采取的是批处理的方法,当drawLine的时刻不是急速绘制,而是将线段的信息添加到数组里,当draw时同一调用gl的绘制函数
10000/1可不是一个小数量啊。
下图应用DrawPrimitives办法
[img]http://img.blog.csdn.net/20150106195017671?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3R4ZGVjcw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center    [img]http://img.blog.csdn.net/20150106194800454?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3R4ZGVjcw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
参加40个Sphere帧率就掉落到40,70的帧率更是惨不忍睹
void clearDraw();
修改PhysicsDraw3D
烧毁时也要将_drawNode大年夜Parent中移除
下图应用DrawNode办法
3. 保存drawPoint, drawPoints, drawLine,其他的绘制函数不要
[img]http://img.blog.csdn.net/20150106195102312?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3R4ZGVjcw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center    [img]http://img.blog.csdn.net/20150106194934328?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3R4ZGVjcw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
为懂得决这个问题就要参照DrawNode实现一个简单的DrawNode3D
不管三七二十一,将DrawNode的头文件代码copy,删去一些不须要的,
1.修改V2F_C4B_T2F 为 V3F_C4B_T2F
2.修改Vec2为Vec3,要绘制3D
#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按照膳绫擎所说同样修改
要记住

因为顶点有三个元素,.cpp代码过多,请在文┞仿最后下载源码,要留意的是绘制时开启深度测试
 
删除成员变量,添加DrawNode3D* _drawNode,因为DrawNode3D持续自Node所以创建时要将其添加到父节点上,
修改create,init为如下
bool initWithLayer(Node* layer);
同时添加
我们知道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();
}

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

github

相关案例查看更多