-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelLoader.h
96 lines (78 loc) · 3.43 KB
/
ModelLoader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef MODELLOADER_H
#define MODELLOADER_H
#include <vulkan/vulkan.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <glm/glm.hpp>
#include <QMutex>
#include <QMutexLocker>
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <fstream>
#include <stb_image.h>
// 结构体声明
struct Vertex {
glm::vec3 Position; // 顶点位置
glm::vec3 Normal; // 顶点法线
glm::vec2 TexCoords; // 纹理坐标
glm::vec3 Tangent; // 切线
glm::vec3 Bitangent; // 副切线
};
struct Texture {
VkImage image; // Vulkan图像对象
VkDeviceMemory imageMemory; // 图像内存
VkImageView imageView; // 图像视图
VkSampler sampler; // 纹理采样器
std::string type; // 纹理类型
std::string path; // 纹理路径
};
// 类声明
class ModelLoader {
public:
// 构造函数,初始化 Vulkan 设备、物理设备、图形队列和命令池
ModelLoader(VkDevice device, VkPhysicalDevice physicalDevice, VkQueue graphicsQueue, VkCommandPool commandPool);
// 析构函数,确保释放所有 Vulkan 资源
~ModelLoader();
// 加载模型文件
bool loadModel(const std::string& filePath);
private:
VkDevice device; // Vulkan 设备
VkPhysicalDevice physicalDevice; // Vulkan 物理设备
VkQueue graphicsQueue; // Vulkan 图形队列
VkCommandPool commandPool; // Vulkan 命令池
std::unordered_map<std::string, Texture> loadedTextures; // 已加载纹理的哈希映射
std::vector<VkBuffer> vertexBuffers; // 顶点缓冲区
std::vector<VkDeviceMemory> vertexBufferMemories; // 顶点缓冲区内存
std::vector<VkBuffer> indexBuffers; // 索引缓冲区
std::vector<VkDeviceMemory> indexBufferMemories; // 索引缓冲区内存
QMutex mutex; // 线程安全的互斥锁
// 递归处理节点
void processNode(aiNode* node, const aiScene* scene);
// 处理网格
void processMesh(aiMesh* mesh, const aiScene* scene);
// 加载材质的纹理
void loadMaterialTextures(aiMaterial* material, const aiScene* scene);
// 加载单个纹理
void loadTexture(aiMaterial* material, aiTextureType type, const std::string& typeName);
// 创建 Vulkan 纹理
Texture createVulkanTexture(const char* path);
// 创建顶点缓冲区
void createVertexBuffer(const std::vector<Vertex>& vertices);
// 创建索引缓冲区
void createIndexBuffer(const std::vector<uint32_t>& indices);
// 清理 Vulkan 资源
void cleanup();
// 错误记录函数
void logError(const std::string& message);
};
// 函数声明
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory);
void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory);
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout);
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
VkImageView createImageView(VkImage image, VkFormat format);
VkSampler createTextureSampler();
#endif // MODELLOADER_H