|
libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
|
Vulkan mesh loader and GPU resource manager. More...
#include <vk_model.hpp>
Public Member Functions | |
| MXModel ()=default | |
| Default constructor. | |
| ~MXModel ()=default | |
| Destructor. | |
| MXModel (const MXModel &)=delete | |
| MXModel & | operator= (const MXModel &)=delete |
| MXModel (MXModel &&)=delete | |
| MXModel & | operator= (MXModel &&)=delete |
| void | load (const std::string &path, float positionScale=1.0f) |
| Parse a model file (.mxmod, .mxmod.z, or .obj) and populate the vertex/index vectors. | |
| void | upload (VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue) |
| Upload parsed geometry to device-local Vulkan buffers. | |
| void | cleanup (VkDevice device) |
| Free all Vulkan buffer resources. | |
| void | draw (VkCommandBuffer cmd) const |
| Record a bind + indexed draw into a command buffer. | |
| void | compressIndices () |
| Deduplicate vertices and rebuild the index list. | |
| const std::vector< VKVertex > & | vertices () const |
| const std::vector< uint32_t > & | indices () const |
| uint32_t | indexCount () const |
| VkBuffer | vertexBuffer () const |
| VkBuffer | indexBuffer () const |
| uint32_t | textureIndex () const |
| size_t | subMeshCount () const |
| const SubMesh & | subMesh (size_t i) const |
| const std::vector< SubMesh > & | subMeshes () const |
| void | drawSubMesh (VkCommandBuffer cmd, size_t index) const |
| Draw a single sub-mesh (binds vertex/index buffers and issues indexed draw). | |
| const std::vector< MXMaterial > & | materials () const |
| const std::string & | mtlLibPath () const |
Private Member Functions | |
| void | loadOBJ (const std::string &path, float positionScale) |
| Load a Wavefront OBJ file. | |
| void | loadMXMOD (const std::string &path, float positionScale) |
| Load an MXMOD format file. | |
| void | loadMTL (const std::string &path) |
| Parse a Wavefront .mtl file and populate materials_. | |
Static Private Member Functions | |
| static void | createBuffer (VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory) |
| Allocate a Vulkan buffer with the specified usage and memory flags. | |
| static uint32_t | findMemoryType (VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties) |
| Find a memory type index satisfying typeFilter and properties. | |
| static void | copyBuffer (VkDevice device, VkCommandPool commandPool, VkQueue graphicsQueue, VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) |
| Copy bytes from one Vulkan buffer to another via a staging command. | |
Private Attributes | |
| std::vector< VKVertex > | vertices_ |
| Parsed vertex data. | |
| std::vector< uint32_t > | indices_ |
| Parsed index data. | |
| uint32_t | textureIndex_ = 0 |
| Sub-mesh texture index. | |
| std::vector< SubMesh > | subMeshes_ |
| Sub-mesh ranges (one per group). | |
| std::vector< MXMaterial > | materials_ |
| Parsed MTL materials. | |
| std::string | mtlLibPath_ |
| Path to .mtl file from OBJ mtllib. | |
| VkBuffer | vertexBuffer_ = VK_NULL_HANDLE |
| VkDeviceMemory | vertexBufferMemory_ = VK_NULL_HANDLE |
| VkBuffer | indexBuffer_ = VK_NULL_HANDLE |
| VkDeviceMemory | indexBufferMemory_ = VK_NULL_HANDLE |
Vulkan mesh loader and GPU resource manager.
Loads a Wavefront OBJ file, optionally deduplicates vertices (compressIndices), uploads the geometry to device-local Vulkan buffers, and provides a draw() helper that records indexed draw commands. Supports multiple sub-meshes (groups) with separate texture indices.
Definition at line 122 of file vk_model.hpp.
|
default |
Default constructor.
Referenced by MXModel(), MXModel(), operator=(), and operator=().
|
default |
Destructor.
|
delete |
References MXModel().
|
delete |
References MXModel().
| void mx::MXModel::cleanup | ( | VkDevice | device | ) |
Free all Vulkan buffer resources.
| device | Logical device that owns the buffers. |
Definition at line 430 of file vk_model.cpp.
References indexBuffer_, indexBufferMemory_, vertexBuffer_, and vertexBufferMemory_.
Referenced by upload().
| void mx::MXModel::compressIndices | ( | ) |
Deduplicate vertices and rebuild the index list.
Reduces GPU bandwidth by removing duplicate VKVertex entries and rewriting the index buffer to reference the canonical copies.
Definition at line 19 of file vk_model.cpp.
References indices_, and vertices_.
Referenced by loadMXMOD(), and loadOBJ().
|
staticprivate |
Copy bytes from one Vulkan buffer to another via a staging command.
Definition at line 519 of file vk_model.cpp.
Referenced by upload().
|
staticprivate |
Allocate a Vulkan buffer with the specified usage and memory flags.
Definition at line 478 of file vk_model.cpp.
References findMemoryType().
Referenced by upload().
| void mx::MXModel::draw | ( | VkCommandBuffer | cmd | ) | const |
Record a bind + indexed draw into a command buffer.
| cmd | Active Vulkan command buffer (must be in recording state). |
Definition at line 446 of file vk_model.cpp.
References indexBuffer_, indexCount(), and vertexBuffer_.
| void mx::MXModel::drawSubMesh | ( | VkCommandBuffer | cmd, |
| size_t | index ) const |
Draw a single sub-mesh (binds vertex/index buffers and issues indexed draw).
| cmd | Active command buffer. |
| index | Sub-mesh index. |
Definition at line 459 of file vk_model.cpp.
References indexBuffer_, subMeshes_, and vertexBuffer_.
|
staticprivate |
Find a memory type index satisfying typeFilter and properties.
Definition at line 506 of file vk_model.cpp.
Referenced by createBuffer().
|
inline |
Definition at line 181 of file vk_model.hpp.
References indexBuffer_.
|
inline |
Definition at line 176 of file vk_model.hpp.
References indices_.
Referenced by draw().
|
inline |
Definition at line 174 of file vk_model.hpp.
References indices_.
| void mx::MXModel::load | ( | const std::string & | path, |
| float | positionScale = 1.0f ) |
Parse a model file (.mxmod, .mxmod.z, or .obj) and populate the vertex/index vectors.
| path | Path to the model file. |
| positionScale | Uniform scale applied to all vertex positions. |
Definition at line 45 of file vk_model.cpp.
References mx::ends_with(), loadMXMOD(), and loadOBJ().
|
private |
Parse a Wavefront .mtl file and populate materials_.
Definition at line 554 of file vk_model.cpp.
References mx::MXMaterial::d, mx::MXMaterial::illum, mx::MXMaterial::ka, mx::MXMaterial::kd, mx::MXMaterial::ks, mx::MXMaterial::map_kd, materials_, mx::MXMaterial::name, and mx::MXMaterial::ns.
Referenced by loadOBJ().
|
private |
Load an MXMOD format file.
Definition at line 217 of file vk_model.cpp.
References compressIndices(), mx::decompressString(), mx::ends_with(), mx::SubMesh::firstIndex, mx::SubMesh::indexCount, indices_, mx::VKVertex::normal, mx::VKVertex::pos, mx::readFile(), subMeshes_, mx::VKVertex::texCoord, mx::SubMesh::textureIndex, textureIndex_, and vertices_.
Referenced by load().
|
private |
Load a Wavefront OBJ file.
Definition at line 53 of file vk_model.cpp.
References compressIndices(), mx::SubMesh::firstIndex, mx::SubMesh::indexCount, indices_, loadMTL(), mx::SubMesh::materialName, materials_, mtlLibPath_, mx::VKVertex::normal, mx::VKVertex::pos, subMeshes_, mx::VKVertex::texCoord, mx::SubMesh::textureIndex, textureIndex_, and vertices_.
Referenced by load().
|
inline |
Definition at line 200 of file vk_model.hpp.
References materials_.
|
inline |
Definition at line 202 of file vk_model.hpp.
References mtlLibPath_.
|
inline |
Definition at line 188 of file vk_model.hpp.
References subMeshes_.
|
inline |
Definition at line 186 of file vk_model.hpp.
References subMeshes_.
|
inline |
Definition at line 190 of file vk_model.hpp.
References subMeshes_.
|
inline |
Definition at line 183 of file vk_model.hpp.
References textureIndex_.
| void mx::MXModel::upload | ( | VkDevice | device, |
| VkPhysicalDevice | physicalDevice, | ||
| VkCommandPool | commandPool, | ||
| VkQueue | graphicsQueue ) |
Upload parsed geometry to device-local Vulkan buffers.
| device | Logical device. |
| physicalDevice | Physical device (for memory type queries). |
| commandPool | Command pool for staging commands. |
| graphicsQueue | Queue used to submit staging commands. |
Definition at line 369 of file vk_model.cpp.
References cleanup(), copyBuffer(), createBuffer(), indexBuffer_, indexBufferMemory_, indices_, vertexBuffer_, vertexBufferMemory_, and vertices_.
|
inline |
Definition at line 179 of file vk_model.hpp.
References vertexBuffer_.
|
inline |
Definition at line 172 of file vk_model.hpp.
References vertices_.
|
private |
Definition at line 214 of file vk_model.hpp.
Referenced by cleanup(), draw(), drawSubMesh(), indexBuffer(), and upload().
|
private |
Definition at line 215 of file vk_model.hpp.
|
private |
Parsed index data.
Definition at line 206 of file vk_model.hpp.
Referenced by compressIndices(), indexCount(), indices(), loadMXMOD(), loadOBJ(), and upload().
|
private |
Parsed MTL materials.
Definition at line 209 of file vk_model.hpp.
Referenced by loadMTL(), loadOBJ(), and materials().
|
private |
Path to .mtl file from OBJ mtllib.
Definition at line 210 of file vk_model.hpp.
Referenced by loadOBJ(), and mtlLibPath().
|
private |
Sub-mesh ranges (one per group).
Definition at line 208 of file vk_model.hpp.
Referenced by drawSubMesh(), loadMXMOD(), loadOBJ(), subMesh(), subMeshCount(), and subMeshes().
|
private |
Sub-mesh texture index.
Definition at line 207 of file vk_model.hpp.
Referenced by loadMXMOD(), loadOBJ(), and textureIndex().
|
private |
Definition at line 212 of file vk_model.hpp.
Referenced by cleanup(), draw(), drawSubMesh(), upload(), and vertexBuffer().
|
private |
Definition at line 213 of file vk_model.hpp.
|
private |
Parsed vertex data.
Definition at line 205 of file vk_model.hpp.
Referenced by compressIndices(), loadMXMOD(), loadOBJ(), upload(), and vertices().