由于刚开始学的时候不知道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也是一个绘制2dLine的方法,那么就应该把它修改成可以绘制3DLine的方法,先观察这个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); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION,vertices); #endif // EMSCRIPTEN glDrawArrays(GL_LInes,2); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2); }
首先初始化一些数据(待会看),
1.vertices就是存放的2dline的起点终点,如果绘制3dline自然要改成Vec3
s_shader使用shader这个shader是cocos2dx启动时预载入的
2.EMSCRIPTEN看这http://www.cocos2d-x.org/wiki/Emscripten_usage
Cocos2D-X’sEmscriptensupportallowsgameswritteninC*+tobecompiledtoJavaScriptanddeployeddirectlytotheweb......
既然是js要用的暂且忽略
3.重点的重点来了http://www.baike.com/wiki/glVertexAttribPointer
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION,
GL_FALSE,vertices);
其中的2就是便是一个顶点的坐标数量,2d自然是2,3d的就是3了
注意:在glVertexAttribPointer之前一定要开启深度测试
glEnable(GL_DEPTH_TEST);绘制完成后要关闭深度检测gldisable(GL_DEPTH_TEST);
如果不开启深度测试看看这个效果你就明白了
可以看到阿狸的手被遮挡了,因为不开启深度测试gl就不会根据深度来显示,而是后绘制的会覆盖先绘制的
开启后
void PhysicsDraw3D::drawLine(const btVector3& from,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,vertices); glDrawArrays(GL_LInes,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_colorLocationcolor属性,可以对制定绘制时的颜色
s_pointSizeLocationpointsize属性可以制定绘制点的大小
那么添加到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::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有包围盒和法向量
后续
这个速度就是impulse/mass,看一下applyCentralImpulse的实现就明白了
_Box->applyCentralImpulse(btVector3(0,-10));
增加touchlistener
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,-10)); return true; }
Box跑远了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。