libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::MXModel Class Reference

Vulkan mesh loader and GPU resource manager. More...

#include <vk_model.hpp>

Collaboration diagram for mx::MXModel:
[legend]

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

Detailed Description

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.

Constructor & Destructor Documentation

◆ MXModel() [1/3]

mx::MXModel::MXModel ( )
default

Default constructor.

Referenced by MXModel(), MXModel(), operator=(), and operator=().

◆ ~MXModel()

mx::MXModel::~MXModel ( )
default

Destructor.

◆ MXModel() [2/3]

mx::MXModel::MXModel ( const MXModel & )
delete

References MXModel().

◆ MXModel() [3/3]

mx::MXModel::MXModel ( MXModel && )
delete

References MXModel().

Member Function Documentation

◆ cleanup()

void mx::MXModel::cleanup ( VkDevice device)

Free all Vulkan buffer resources.

Parameters
deviceLogical device that owns the buffers.

Definition at line 430 of file vk_model.cpp.

References indexBuffer_, indexBufferMemory_, vertexBuffer_, and vertexBufferMemory_.

Referenced by upload().

◆ compressIndices()

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().

◆ copyBuffer()

void mx::MXModel::copyBuffer ( VkDevice device,
VkCommandPool commandPool,
VkQueue graphicsQueue,
VkBuffer srcBuffer,
VkBuffer dstBuffer,
VkDeviceSize size )
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().

◆ createBuffer()

void mx::MXModel::createBuffer ( VkDevice device,
VkPhysicalDevice physicalDevice,
VkDeviceSize size,
VkBufferUsageFlags usage,
VkMemoryPropertyFlags properties,
VkBuffer & buffer,
VkDeviceMemory & bufferMemory )
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().

◆ draw()

void mx::MXModel::draw ( VkCommandBuffer cmd) const

Record a bind + indexed draw into a command buffer.

Parameters
cmdActive Vulkan command buffer (must be in recording state).

Definition at line 446 of file vk_model.cpp.

References indexBuffer_, indexCount(), and vertexBuffer_.

◆ drawSubMesh()

void mx::MXModel::drawSubMesh ( VkCommandBuffer cmd,
size_t index ) const

Draw a single sub-mesh (binds vertex/index buffers and issues indexed draw).

Parameters
cmdActive command buffer.
indexSub-mesh index.

Definition at line 459 of file vk_model.cpp.

References indexBuffer_, subMeshes_, and vertexBuffer_.

◆ findMemoryType()

uint32_t mx::MXModel::findMemoryType ( VkPhysicalDevice physicalDevice,
uint32_t typeFilter,
VkMemoryPropertyFlags properties )
staticprivate

Find a memory type index satisfying typeFilter and properties.

Definition at line 506 of file vk_model.cpp.

Referenced by createBuffer().

◆ indexBuffer()

VkBuffer mx::MXModel::indexBuffer ( ) const
inline
Returns
Vulkan index buffer handle.

Definition at line 181 of file vk_model.hpp.

References indexBuffer_.

◆ indexCount()

uint32_t mx::MXModel::indexCount ( ) const
inline
Returns
Number of indices in the index buffer.

Definition at line 176 of file vk_model.hpp.

References indices_.

Referenced by draw().

◆ indices()

const std::vector< uint32_t > & mx::MXModel::indices ( ) const
inline
Returns
Read-only reference to the index array.

Definition at line 174 of file vk_model.hpp.

References indices_.

◆ load()

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.

Parameters
pathPath to the model file.
positionScaleUniform scale applied to all vertex positions.

Definition at line 45 of file vk_model.cpp.

References mx::ends_with(), loadMXMOD(), and loadOBJ().

◆ loadMTL()

void mx::MXModel::loadMTL ( const std::string & path)
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().

◆ loadMXMOD()

void mx::MXModel::loadMXMOD ( const std::string & path,
float positionScale )
private

◆ loadOBJ()

void mx::MXModel::loadOBJ ( const std::string & path,
float positionScale )
private

◆ materials()

const std::vector< MXMaterial > & mx::MXModel::materials ( ) const
inline
Returns
Read-only reference to parsed MTL materials.

Definition at line 200 of file vk_model.hpp.

References materials_.

◆ mtlLibPath()

const std::string & mx::MXModel::mtlLibPath ( ) const
inline
Returns
Path to the MTL library referenced by the OBJ, or empty.

Definition at line 202 of file vk_model.hpp.

References mtlLibPath_.

◆ operator=() [1/2]

MXModel & mx::MXModel::operator= ( const MXModel & )
delete

References MXModel().

◆ operator=() [2/2]

MXModel & mx::MXModel::operator= ( MXModel && )
delete

References MXModel().

◆ subMesh()

const SubMesh & mx::MXModel::subMesh ( size_t i) const
inline
Returns
Read-only reference to a sub-mesh by index.

Definition at line 188 of file vk_model.hpp.

References subMeshes_.

◆ subMeshCount()

size_t mx::MXModel::subMeshCount ( ) const
inline
Returns
Number of sub-meshes (groups) in the model.

Definition at line 186 of file vk_model.hpp.

References subMeshes_.

◆ subMeshes()

const std::vector< SubMesh > & mx::MXModel::subMeshes ( ) const
inline
Returns
Read-only reference to all sub-meshes.

Definition at line 190 of file vk_model.hpp.

References subMeshes_.

◆ textureIndex()

uint32_t mx::MXModel::textureIndex ( ) const
inline
Returns
Texture sub-mesh index encoded in the OBJ file (first sub-mesh).

Definition at line 183 of file vk_model.hpp.

References textureIndex_.

◆ upload()

void mx::MXModel::upload ( VkDevice device,
VkPhysicalDevice physicalDevice,
VkCommandPool commandPool,
VkQueue graphicsQueue )

Upload parsed geometry to device-local Vulkan buffers.

Parameters
deviceLogical device.
physicalDevicePhysical device (for memory type queries).
commandPoolCommand pool for staging commands.
graphicsQueueQueue 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_.

◆ vertexBuffer()

VkBuffer mx::MXModel::vertexBuffer ( ) const
inline
Returns
Vulkan vertex buffer handle.

Definition at line 179 of file vk_model.hpp.

References vertexBuffer_.

◆ vertices()

const std::vector< VKVertex > & mx::MXModel::vertices ( ) const
inline
Returns
Read-only reference to the vertex array.

Definition at line 172 of file vk_model.hpp.

References vertices_.

Member Data Documentation

◆ indexBuffer_

VkBuffer mx::MXModel::indexBuffer_ = VK_NULL_HANDLE
private

Definition at line 214 of file vk_model.hpp.

Referenced by cleanup(), draw(), drawSubMesh(), indexBuffer(), and upload().

◆ indexBufferMemory_

VkDeviceMemory mx::MXModel::indexBufferMemory_ = VK_NULL_HANDLE
private

Definition at line 215 of file vk_model.hpp.

Referenced by cleanup(), and upload().

◆ indices_

std::vector<uint32_t> mx::MXModel::indices_
private

Parsed index data.

Definition at line 206 of file vk_model.hpp.

Referenced by compressIndices(), indexCount(), indices(), loadMXMOD(), loadOBJ(), and upload().

◆ materials_

std::vector<MXMaterial> mx::MXModel::materials_
private

Parsed MTL materials.

Definition at line 209 of file vk_model.hpp.

Referenced by loadMTL(), loadOBJ(), and materials().

◆ mtlLibPath_

std::string mx::MXModel::mtlLibPath_
private

Path to .mtl file from OBJ mtllib.

Definition at line 210 of file vk_model.hpp.

Referenced by loadOBJ(), and mtlLibPath().

◆ subMeshes_

std::vector<SubMesh> mx::MXModel::subMeshes_
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().

◆ textureIndex_

uint32_t mx::MXModel::textureIndex_ = 0
private

Sub-mesh texture index.

Definition at line 207 of file vk_model.hpp.

Referenced by loadMXMOD(), loadOBJ(), and textureIndex().

◆ vertexBuffer_

VkBuffer mx::MXModel::vertexBuffer_ = VK_NULL_HANDLE
private

Definition at line 212 of file vk_model.hpp.

Referenced by cleanup(), draw(), drawSubMesh(), upload(), and vertexBuffer().

◆ vertexBufferMemory_

VkDeviceMemory mx::MXModel::vertexBufferMemory_ = VK_NULL_HANDLE
private

Definition at line 213 of file vk_model.hpp.

Referenced by cleanup(), and upload().

◆ vertices_

std::vector<VKVertex> mx::MXModel::vertices_
private

Parsed vertex data.

Definition at line 205 of file vk_model.hpp.

Referenced by compressIndices(), loadMXMOD(), loadOBJ(), upload(), and vertices().


The documentation for this class was generated from the following files: