libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
vk_abstract_model.hpp
Go to the documentation of this file.
1/**
2 * @file vk_abstract_model.hpp
3 * @brief High-level Vulkan 3-D model wrapper with texture and descriptor management.
4 *
5 * VKAbstractModel wraps MXModel, automatically handling texture loading,
6 * uniform buffer allocation, descriptor pool/set creation, and bounding-box
7 * centring so that callers only need to supply file paths and a scale.
8 */
9#ifndef __ABSTRACT__
10#define __ABSTRACT__
11
12#include"vk.hpp"
13#include"vk_model.hpp"
14
15namespace mx {
16
17/**
18 * @class VKAbstractModel
19 * @brief Convenience wrapper that owns an MXModel plus its Vulkan textures,
20 * UBOs, and descriptor sets.
21 *
22 * Typical usage:
23 * @code
24 * VKAbstractModel mdl;
25 * mdl.load(win, "mesh.obj", "textures.tex", "assets", 1.0f);
26 * // each frame:
27 * mdl.updateUBO(win, imageIndex, ubo);
28 * // on resize:
29 * mdl.resize(win);
30 * // on shutdown:
31 * mdl.cleanup(win);
32 * @endcode
33 */
35 public:
36 /** @brief Default constructor. */
37 VKAbstractModel() = default;
42
43 /**
44 * @brief Load a model, its textures, and create all GPU resources.
45 * @param win Owning Vulkan window (provides device handles).
46 * @param model Path to the mesh file (.obj / .mxmod / .mxmod.z).
47 * @param text Path to a .tex file listing texture image filenames.
48 * @param path Directory prefix prepended to each texture filename.
49 * @param scale Uniform scale applied to all vertex positions.
50 */
51 void load(VKWindow *win, const std::string &model, const std::string &text, const std::string &path, float scale);
52
53 /**
54 * @brief Set custom vertex and fragment shaders and create a per-model pipeline.
55 * @param win Owning Vulkan window.
56 * @param vert Path to the vertex shader SPIR-V file.
57 * @param frag Path to the fragment shader SPIR-V file.
58 */
59 void setShaders(VKWindow *win, const std::string &vert, const std::string &frag);
60
61 /**
62 * @brief Record rendering commands (currently a placeholder).
63 * @param cmd Command buffer index.
64 */
65 void render(int cmd);
66
67 /**
68 * @brief Destroy all Vulkan resources owned by this model.
69 * @param win Owning Vulkan window.
70 */
71 void cleanup(VKWindow *win);
72
73 /**
74 * @brief Recreate UBOs and descriptor sets after a swapchain resize.
75 * @param win Owning Vulkan window.
76 */
77 void resize(VKWindow *win);
78
79 /**
80 * @brief Copy a UBO into the mapped buffer for a given frame.
81 * @param win Owning Vulkan window.
82 * @param imageIndex Swapchain image index for this frame.
83 * @param ubo Uniform data to upload.
84 */
85 void updateUBO(VKWindow *win, uint32_t imageIndex, const mx::UniformBufferObject &ubo);
86
87 /**
88 * @struct TexEntry
89 * @brief Vulkan image, memory, and view for one loaded texture.
90 */
91 struct TexEntry {
92 VkImage image = VK_NULL_HANDLE; ///< Vulkan image handle.
93 VkDeviceMemory memory = VK_NULL_HANDLE; ///< Device memory backing the image.
94 VkImageView view = VK_NULL_HANDLE; ///< Image view for shader sampling.
95 uint32_t w = 0, h = 0; ///< Texture dimensions in pixels.
96 };
97
98 mx::MXModel obj; ///< Underlying mesh data and GPU buffers.
99 std::vector<TexEntry> modelTextures; ///< Per-sub-mesh texture entries.
100 std::vector<VkDescriptorSet> modelDescriptorSets; ///< Descriptor sets (frames x textures).
101 glm::vec3 modelCenterOffset; ///< Translation to centre the bounding box at origin.
102 float modelRenderScale; ///< Scale factor normalising model extent.
103
104 /**
105 * @brief Get the per-model pipeline, or VK_NULL_HANDLE if using the window default.
106 * @return Pipeline handle (fill mode).
107 */
108 VkPipeline getModelPipeline() const { return modelPipeline; }
109
110 /**
111 * @brief Get the per-model wireframe pipeline, or VK_NULL_HANDLE if not available.
112 * @return Pipeline handle (wireframe mode).
113 */
115
116 /**
117 * @brief Create a lightweight instance sharing geometry & textures from a source model.
118 *
119 * Only creates its own UBOs and descriptor sets. The source model must
120 * outlive this instance and be cleaned up after all instances.
121 *
122 * @param win Owning Vulkan window.
123 * @param source Fully-loaded model to share geometry/textures from.
124 */
125 void loadInstance(VKWindow *win, VKAbstractModel &source);
126
127 /**
128 * @brief Check whether this model has custom shaders set.
129 * @return @c true if setShaders() was called.
130 */
131 bool hasCustomShaders() const { return !vertShaderPath_.empty(); }
132
133 /** @brief Check whether this is a lightweight instance (shared geometry). */
134 bool isInstance() const { return isInstance_; }
135 private:
136 bool isInstance_ = false; ///< True if geometry/textures are shared from another model.
137 VkDescriptorPool modelDescriptorPool = VK_NULL_HANDLE; ///< Descriptor pool for this model.
138 VkPipeline modelPipeline = VK_NULL_HANDLE; ///< Per-model graphics pipeline (fill).
139 VkPipeline modelPipelineWireframe = VK_NULL_HANDLE; ///< Per-model wireframe pipeline.
140 std::string vertShaderPath_; ///< Vertex shader SPIR-V path.
141 std::string fragShaderPath_; ///< Fragment shader SPIR-V path.
142 std::vector<VkBuffer> modelUniformBuffers; ///< Per-frame uniform buffers.
143 std::vector<VkDeviceMemory> modelUniformBufferMemory; ///< Memory for per-frame UBOs.
144 std::vector<void*> modelUniformBuffersMapped; ///< Persistently mapped UBO pointers.
145 friend class VKWindow;
146
147 /** @brief Clean up only UBO and descriptor resources (used by instances). */
149
150 /**
151 * @brief Load texture images listed in a .tex file and upload to GPU.
152 * @param win Owning Vulkan window.
153 * @param tex Path to the .tex manifest file.
154 * @param path Directory prefix for image files.
155 */
156 void loadModelTextures(VKWindow *win, const std::string &tex, const std::string &path);
157
158 /**
159 * @brief Load textures using map_Kd paths from parsed MTL materials.
160 * @param win Owning Vulkan window.
161 * @param path Directory prefix for image files.
162 */
163 void loadModelTexturesFromMTL(VKWindow *win, const std::string &path);
164
165 /**
166 * @brief Create the Vulkan descriptor pool sized for this model's textures.
167 * @param win Owning Vulkan window.
168 */
170
171 /**
172 * @brief Allocate and write descriptor sets binding UBOs and textures.
173 * @param win Owning Vulkan window.
174 */
176
177 /**
178 * @brief Create a per-model graphics pipeline from stored shader paths.
179 * @param win Owning Vulkan window.
180 */
181 void createModelPipeline(VKWindow *win);
182 };
183}
184
185
186#endif
Vulkan mesh loader and GPU resource manager.
Definition vk_model.hpp:122
Convenience wrapper that owns an MXModel plus its Vulkan textures, UBOs, and descriptor sets.
std::vector< VkDescriptorSet > modelDescriptorSets
Descriptor sets (frames x textures).
std::vector< VkBuffer > modelUniformBuffers
Per-frame uniform buffers.
std::string fragShaderPath_
Fragment shader SPIR-V path.
bool isInstance_
True if geometry/textures are shared from another model.
void createModelPipeline(VKWindow *win)
Create a per-model graphics pipeline from stored shader paths.
void loadModelTexturesFromMTL(VKWindow *win, const std::string &path)
Load textures using map_Kd paths from parsed MTL materials.
void createModelDescriptorSets(VKWindow *win)
Allocate and write descriptor sets binding UBOs and textures.
std::vector< TexEntry > modelTextures
Per-sub-mesh texture entries.
VKAbstractModel(VKAbstractModel &&)=delete
void loadModelTextures(VKWindow *win, const std::string &tex, const std::string &path)
Load texture images listed in a .tex file and upload to GPU.
VKAbstractModel & operator=(const VKAbstractModel &)=delete
std::vector< void * > modelUniformBuffersMapped
Persistently mapped UBO pointers.
void createModelDescriptorPool(VKWindow *win)
Create the Vulkan descriptor pool sized for this model's textures.
glm::vec3 modelCenterOffset
Translation to centre the bounding box at origin.
void resize(VKWindow *win)
Recreate UBOs and descriptor sets after a swapchain resize.
void cleanup(VKWindow *win)
Destroy all Vulkan resources owned by this model.
void load(VKWindow *win, const std::string &model, const std::string &text, const std::string &path, float scale)
Load a model, its textures, and create all GPU resources.
VKAbstractModel & operator=(VKAbstractModel &&)=delete
VkPipeline modelPipelineWireframe
Per-model wireframe pipeline.
void loadInstance(VKWindow *win, VKAbstractModel &source)
Create a lightweight instance sharing geometry & textures from a source model.
std::string vertShaderPath_
Vertex shader SPIR-V path.
bool hasCustomShaders() const
Check whether this model has custom shaders set.
VkPipeline getModelPipelineWireframe() const
Get the per-model wireframe pipeline, or VK_NULL_HANDLE if not available.
void render(int cmd)
Record rendering commands (currently a placeholder).
VkDescriptorPool modelDescriptorPool
Descriptor pool for this model.
VKAbstractModel(const VKAbstractModel &)=delete
bool isInstance() const
Check whether this is a lightweight instance (shared geometry).
std::vector< VkDeviceMemory > modelUniformBufferMemory
Memory for per-frame UBOs.
float modelRenderScale
Scale factor normalising model extent.
VKAbstractModel()=default
Default constructor.
VkPipeline getModelPipeline() const
Get the per-model pipeline, or VK_NULL_HANDLE if using the window default.
void setShaders(VKWindow *win, const std::string &vert, const std::string &frag)
Set custom vertex and fragment shaders and create a per-model pipeline.
void cleanupInstanceResources(VKWindow *win)
Clean up only UBO and descriptor resources (used by instances).
void updateUBO(VKWindow *win, uint32_t imageIndex, const mx::UniformBufferObject &ubo)
Copy a UBO into the mapped buffer for a given frame.
VkPipeline modelPipeline
Per-model graphics pipeline (fill).
Primary Vulkan rendering window and device management class.
Definition vk.hpp:126
Definition cfg.cpp:11
Definition object.hpp:15
Standard UBO layout uploaded to shaders each frame.
Definition vk.hpp:102
Vulkan image, memory, and view for one loaded texture.
VkDeviceMemory memory
Device memory backing the image.
VkImageView view
Image view for shader sampling.
VkImage image
Vulkan image handle.
uint32_t h
Texture dimensions in pixels.
Vulkan application window and supporting structs for libmx.
Vulkan 3-D model loader and GPU buffer manager.