How to apply Bullet physics to drawn Opengl 3d shapes

我的梦境 提交于 2019-12-03 03:18:57

After you downloaded Bullet Physics SDK, look the file GL_ShapeDrawer.cpp,you will find some interresting functions such like drawSphere, drawCylinder,drawOpenGl.. the last one i mentionned lets you draw any type of suported shape :

  • CUSTOM_CONVEX_SHAPE_TYPE
  • BOX_SHAPE_PROXYTYPE
  • UNIFORM_SCALING_SHAPE_PROXYTYPE
  • COMPOUND_SHAPE_PROXYTYPE
  • BOX_SHAPE_PROXYTYPE
  • SPHERE_SHAPE_PROXYTYPE

Each type of shape from shape->getShapeType() has its own dedicated function for rendering with opengl

But my method involves to

  • 1 - Load the 3D model with a mesh loader
  • 2 - Create the graphics shape with opengl functions using the previous mesh loader
  • 3 - Create bullet shapes from the vertices used by the mesh loader
  • 4 - Implement the Bullet Motion State (when a shape is going to be rotated or translated or just "transformed", you syncronize the "btTransform" of your bullet shape with the transformation matrix of the graphic shape, then update the graphics vertices)

Personnaly, i'm using Irrlicht because Irrlicht is the "write less,do more" solution of opengl :p

Ciro Santilli 新疆改造中心法轮功六四事件

Bullet 2.83 Example Browser

The Bullet example browser has moved away from the examples/ExampleBrowser/GL_ShapeDrawer.cpp by default, which uses outdated immediate OpenGL 2 methods. It can still be turned on depending on the value of the NO_OPENGL3 macro and command line arguments.

OpenGLWindow/GLInstancingRenderer.cpp is the new OpenGL 3 rendering centerpiece, and contains the glBindVertexArray call in GLInstancingRenderer::registerShape.

This method is indirectly called from OpenGLGuiHelper::autogenerateGraphicsObjects, which loops over the objects in the btDiscreteDynamicsWorld and generates a graphic version of those shapes.

The central part of the conversion seems to be OpenGLGuiHelper::createCollisionShapeGraphicsObjectInternal.

The color of the objects simply loops over 4 possible colors from OpenGLGuiHelper::sColors.

autogenerateGraphicsObjects is called from the initPhysics of several examples.

There are also some explicitly encoded cubes and spheres at: SimpleOpenGL3App.cpp, which can serve as a starting point if you want the possibility of graphics that differ from physics, as should be the case for every serious project: graphics should be more detailed, as they are less computationally expensive. Those are used only in a few examples.

If you are going to study the example browser, I recommend using an IDE with a debugger: it is too much indirection from my brain's static analysis. I've done this analysis with KDevelop4.

Bullet is independent of OpenGL, it's up to you to apply the transformation to your geometry.

You can store your transforms into separate objects instead of modifying your vertices. It's what do Vehicle Dynamics Engine Demo (JOGL + JBullet) and JMonkeyEngine 3 which has its own Bullet binding and a renderer based on JOGL 2 (and some other renderers that I don't use).

Please look at our demos using JOGL and JBullet here: https://github.com/sgothel/jogl-demos/tree/master/src/jbullet

Without studying the specifics of Bullet or OpenGL (I've coded in OpenGL a little) it seems you wish to apply Bullet to an object twice, once for the CollisionShape (whatever that is) and then for the drawn opengl object.

It seems it would be a matter of creating your OpenGL object in a memory object, applying the Bullet method to get your CollisionShape (whatever that is), saving this data in memory, then applying the Bullet method to this new triangle list for world positioning (if my understanding of your question is right).

It would be similar to applying 2 different transforms on an object separately without the benefit of matrix math. You would calculate the new positions of the vectors based on rotation, then after you have these points move them in the world based on translation. You would have an intermediate step that is used in memory to apply your second translation. That is the way I had done it in my first 3d rendered I made without learning how it was done to see if I could come up with it on my own.

Maybe this will help you, maybe it won't. If it doesn't, perhaps you can explain where you are having troubles and I (or others) can look into the specifics of OpenGL and Bullet to come up with the actual algorithm for you (although I'm sure it probably exists somewhere). If you search enough you'll probably find code on the Interwebs that does something similar that you can modify, or you can try to do it on your own.

It could be my method wouldn't work, but I see no reason why it wouldn't if I understand your question correctly (which I may not, not knowing what a "CollisionShape" is in the first place).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!