I am building a 3d video game engine using Vulkan. The game will also use Bullet to handle game physics. What would be the best way to handle the model vertex information?
Assistant
One common approach would be to use a custom file format for your 3D models that contains vertex positions, normals, texture coordinates, and any other necessary data (such as bone weights for animation). You can then load these model files into your engine and pass the vertex information to Vulkan to create vertex buffers for rendering.
For example, you could use a format like glTF or OBJ for your model files. These formats are widely supported and have libraries available for loading them into your engine. You can also write your own custom format tailored to your engine's needs.
When it comes to physics, you can use a separate representation of your models that is more optimized for collision detection. Bullet provides its own collision shape types, such as spheres, capsules, and convex hulls, that you can use to create collision shapes for your models. You can then add these shapes to Bullet's rigid bodies to enable physics simulation.
User
I have vertex positions, normals and texture coordinates for the 3D models. How should the class structure be set up to allow this information to be used by both Bullet and Vulkan? I am using C++ as the programming language.
Assistant
One approach would be to create a "Mesh" class that contains the vertex information and can be used by both Vulkan and Bullet. Here's an example of what the class definition might look like:
In this example, the Mesh class contains the vertex and index data, as well as Vulkan-specific members for storing the vertex and index buffers. The class also has methods for creating a Vulkan-compatible vertex binding and attribute descriptions, and for creating a bullet-compatible triangle mesh from the vertex data.
Here's an example implementation of the constructor and Vulkan-specific methods:
In this example implementation, the constructor creates Vulkan-compatible vertex and index buffers from the given vertex data and index data. The GetBindingDescription and GetAttributeDescriptions methods return the vertex binding and input attribute descriptions needed by Vulkan. The GetVertexBuffer, GetVertexBufferOffset, GetIndexBuffer, and GetIndexCount methods allow Vulkan to retrieve the buffers and index count for rendering.
For handling the Bullet physics, you can implement the CreateBulletTriangleMesh method, which creates a bullet-compatible triangle mesh from the vertex data. Here's an example implementation:
In this example implementation, the CreateBulletTriangleMesh method creates a btTriangleMesh object from the vertex data and index data. The method uses the addTriangle method to add each triangle to the mesh.
With this Mesh class structure, you can create Mesh objects from your 3D model vertex data and use them for both rendering with Vulkan and physics simulation with Bullet.