Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D

由于刚开始学的时候不知道Bullet的单位1.0代表多大,所以制作出的模型的大小也无法判断。

不用担心,Bullet提供了一个类btIDebugDraw,这个泪已经实现了很多绘制功能,我们要做的就是实现几个虚函数。

我们继承btIDebugDraw,实现虚函数

class PhysicsDraw3D : public btIDebugDraw
{
public:
	void drawLine(const btVector3& from,const btVector3& to,const btVector3& color);
	void drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,
          btScalar distance,int lifeTime,const btVector3& color);
	void reportErrorWarning(const char* warningString);
	void draw3dText(const btVector3& location,const char* textString);
	void setDebugMode(int debugMode);
	int getDebugMode() const;
// ...................
private:
	int _debugDrawMode;
// ...................
};

目前只需要drawLine,等以后涉及到其他方面时再来实现其他的绘制功能

drawContactPoint, draw3dText 什么也不做。

_debugDrawMode 一定要定义,因为btIDebugDraw在绘制时会检查需要绘制什么。

实现绘制模式

void PhysicsDraw3D::setDebugMode(int debugMode)
{
	_debugDrawMode = debugMode;
}

int PhysicsDraw3D::getDebugMode() const
{
	return _debugDrawMode;
}

下面重点说一下drawLine,我们知道cocos2dx 的DrawNode 提供的都是绘制2d图元的功能,

而且修改起来也比较复杂,本着一切从简,主要是为了学习Bullet,所以我们就去copy一下

DrawPrimitives绘制Line的方法

同样DrawPrimitives::drawLine也是一个绘制2d Line的方法,那么就应该把它修改成可以绘制3D Line的方法,先观察这个drawLine的实现


void drawLine(const Vec2& origin, const Vec2& destination)
{
	lazy_init();			

	Vec2 vertices[2] = {
		Vec2(origin.x, origin.y),
		Vec2(destination.x, destination.y)
	};

	_color.r = color.x();
	_color.g = color.y();
	_color.b = color.z();

	s_shader->use();
	s_shader->setUniformsForBuiltins();
	s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);

	GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
#ifdef EMSCRIPTEN
	setGLBufferData(vertices, 16);
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#endif // EMSCRIPTEN
	glDrawArrays(GL_LINES, 0, 2);

	CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);
}

首先初始化一些数据(待会看), 

1.vertices 就是存放的2d line的起点终点,如果绘制3d line 自然要改成Vec3

s_shader 使用shader这个shader是cocos2dx 启动时预载入的

2.EMSCRIPTEN 看这 http://www.cocos2d-x.org/wiki/Emscripten_usage

Cocos2D-X’s Emscripten support allows games written in C*+ to be compiled to JavaScript and deployed directly to the web......

既然是js要用的暂且忽略

3.重点的重点来了 http://www.baike.com/wiki/glVertexAttribPointer

glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, 

GL_FALSE, 0, vertices);

其中的2就是便是一个顶点的坐标数量,2d 自然是2, 3d的就是3了

注意:在glVertexAttribPointer 之前一定要开启深度测试

glEnable(GL_DEPTH_TEST);绘制完成后要关闭深度检测glDisable(GL_DEPTH_TEST);

如果不开启深度测试看看这个效果你就明白了


可以看到阿狸的手被遮挡了,因为不开启深度测试gl就不会根据深度来显示,而是后绘制的会覆盖先绘制的

开启后


4.对这个划线方法修改以后

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

	_shader->use();
	_shader->setUniformsForBuiltins();
	_shader->setUniformLocationWith4fv(_colorLocation, (GLfloat*) &_color.r, 1);

	glEnable(GL_DEPTH_TEST);

	GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, vertices);
	glDrawArrays(GL_LINES, 0, 2);

glDisable(GL_DEPTH_TEST);
	CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);
}

再来看看lazy_init

   //
        // Position and 1 color passed as a uniform (to simulate glColor4ub )
        //
        s_shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
        s_shader->retain();
        
        s_colorLocation = s_shader->getUniformLocation("u_color");
        CHECK_GL_ERROR_DEBUG();
        s_pointSizeLocation = s_shader->getUniformLocation("u_pointSize");
        CHECK_GL_ERROR_DEBUG();

        s_initialized = true;

其实就是获取shader

s_colorLocation     color属性,可以对制定绘制时的颜色

s_pointSizeLocation    point size 属性   可以制定绘制点的大小 

那么添加到PhysicsDraw3D, 就如下

bool PhysicsDraw3D::initDraw()
{
	_shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
	if (_shader == nullptr)
	{
		return false;
	}
	_shader->retain();

	_colorLocation = _shader->getUniformLocation("u_color");
	CHECK_GL_ERROR_DEBUG();
	_pointSizeLocation = _shader->getUniformLocation("u_pointSize");
	CHECK_GL_ERROR_DEBUG();

	_debugDrawMode = btIDebugDraw::DBG_MAX_DEBUG_DRAW_MODE; // 绘制全部调试信息
	return true;
}

方便起见,为PhysicsDraw3D 添加构建方法

PhysicsDraw3D* PhysicsDraw3D::create()
{
	auto draw = new PhysicsDraw3D;
	if (draw && draw->initDraw())
	{
		return draw;
	}

	return nullptr;
}

销毁方法

void PhysicsDraw3D::destroy()
{
	CC_SAFE_RELEASE_NULL(_shader);
	delete this;
}

将PhysicsDraw3D整合到PhysicsWorld3D

为PhysicsWorld3D添加私有变量

PhysicsDraw3D* _debugDraw;

并在initWorld 最后添加

_debugDraw = PhysicsDraw3D::create();
_world->setDebugDrawer(_debugDraw);

在destroy 前添加

_debugDraw->destroy();
_debugDraw = nullptr;

完整代码见文章最后

测试这个PhysicsDraw3D

在HelloWorld重载

void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
	_world->debugDraw();
	Layer::draw(renderer, transform, flags);
}

按F5编译通过后可以看到Plane为一个坐标,box有包围盒和法向量

后续

box提供一个冲量box会在原来的基础上增加一定的速度,

这个速度就是impulse / mass, 看一下applyCentralImpulse的实现就明白了

_box->applyCentralImpulse(btVector3(0, 0, -10));

增加touch listener

bool HelloWorld::initListener()
{
	_touchListener = EventListenerTouchOneByOne::create();
	if (_touchListener == nullptr)
	{
		return false;
	}
	_touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
	_touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
	_touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);

	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_touchListener, this);

	return true;
}

触摸时提供冲量

bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event)
{
	_box->setActivationState(ACTIVE_TAG);
	_box->applyCentralImpulse(btVector3(0, 0, -10));
	return true;
}

Box跑远了



完整源码


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值