175 lines
4.8 KiB
C++
175 lines
4.8 KiB
C++
#ifndef __FaceApp_H__
|
|
#define __FaceApp_H__
|
|
|
|
#include "Application.h"
|
|
#include <functional>
|
|
#include "../third_party/vma/include/vk_mem_alloc.h"
|
|
#include <map>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/matrix_transform.inl>
|
|
#include "nlohmann/json.hpp"
|
|
using namespace std;
|
|
using json = nlohmann::json;
|
|
|
|
struct Motion {
|
|
string type;
|
|
vector<string> png_names;
|
|
string getMotionId() {
|
|
return type;
|
|
}
|
|
NLOHMANN_DEFINE_TYPE_INTRUSIVE(Motion, type, png_names)
|
|
};
|
|
|
|
struct InitArg
|
|
{
|
|
int action_fps;
|
|
float zoom;
|
|
//Motion motion;
|
|
NLOHMANN_DEFINE_TYPE_INTRUSIVE(InitArg, action_fps, zoom);
|
|
};
|
|
|
|
|
|
struct PushConstants {
|
|
float myValue;
|
|
};
|
|
|
|
using Callback = std::function<void(const std::string&)>;
|
|
using AnimationFinishedCallback = std::function<void()>;
|
|
|
|
class FaceApp :public Application
|
|
{
|
|
public:
|
|
FaceApp(/* args */);
|
|
~FaceApp();
|
|
|
|
struct
|
|
{
|
|
glm::mat4 projection;
|
|
glm::mat4 model;
|
|
glm::vec4 view_pos;
|
|
float lod_bias = 0.0f;
|
|
} ubo_vs;
|
|
|
|
void initVulkan() override;
|
|
|
|
|
|
void render(VkCommandBuffer commandBuffer, long long frameTime) override;
|
|
static FaceApp* Get() { return faceIns; }
|
|
void update_face_vertex_buffer(float* pos, int pointCount);
|
|
|
|
virtual bool isInited() override { return inited && faceAppInited; }
|
|
virtual void cleanup() override;
|
|
|
|
Texture tex_bg = {};
|
|
|
|
private:
|
|
glm::vec3 rotation = glm::vec3();
|
|
glm::vec3 camera_pos = glm::vec3();
|
|
private:
|
|
static FaceApp* faceIns;
|
|
VmaAllocator allocator = nullptr;
|
|
bool LoadOBJ(const std::string& filename,std::vector<TextureLoadingVertexStructure>& vertices, std::vector<uint32_t>& indices);
|
|
|
|
void createVmaAllocator();
|
|
void update_uniform_buffers();
|
|
|
|
void createVertexBuffer();
|
|
void uploadVertexData();
|
|
VkDescriptorPool descriptor_pool = VK_NULL_HANDLE;
|
|
void create_face_pipelines();
|
|
void setup_descriptor_pool();
|
|
void setup_descriptor_set_layout();
|
|
void setup_descriptor_set();
|
|
void update_descriptor_set(vector<Texture>& texs);
|
|
const bool kThick = false;
|
|
|
|
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
|
|
VkCommandBuffer beginSingleTimeCommands();
|
|
void endSingleTimeCommands(VkCommandBuffer commandBuffer);
|
|
|
|
// 顶点缓冲区相关
|
|
VkBuffer m_vertexBuffer = VK_NULL_HANDLE;
|
|
VmaAllocation m_vertexBufferAllocation = VK_NULL_HANDLE;
|
|
VkBuffer m_stagingBuffer = VK_NULL_HANDLE;
|
|
VmaAllocation m_stagingBufferAllocation = VK_NULL_HANDLE;
|
|
|
|
// 索引缓冲区相关
|
|
VkBuffer m_indexBuffer = VK_NULL_HANDLE;
|
|
VmaAllocation m_indexBufferAllocation = VK_NULL_HANDLE;
|
|
|
|
// 渲染管线和描述符
|
|
VkPipeline m_graphicsPipeline = VK_NULL_HANDLE;
|
|
VkPipelineLayout m_pipelineLayout = VK_NULL_HANDLE;
|
|
VkDescriptorSetLayout m_descriptorSetLayout = VK_NULL_HANDLE;
|
|
vector<VkDescriptorSet> m_descriptor_sets;
|
|
const uint32_t kMaxTexture = 60;
|
|
vector<Texture> m_texs;
|
|
vector<Texture> m_texs_next;
|
|
vector<Texture> m_texs_ex;
|
|
|
|
|
|
std::vector<TextureLoadingVertexStructure> obj_vertices;
|
|
std::vector<uint32_t> obj_indices;
|
|
std::map<int, int> obj_vertices_map;
|
|
std::map<int, int> vertices_map_3dmax;
|
|
|
|
std::mutex mtx_point;
|
|
long long last_update_time;
|
|
|
|
VkBuffer uniform_buffer_vs;
|
|
VmaAllocation uniform_buffer_allocation; // 需要这个来管理内存
|
|
void* uniform_buffer_mapped = nullptr; // 映射的内存指针
|
|
void createUniformBuffer();
|
|
|
|
|
|
bool faceAppInited = false;
|
|
|
|
float myFloatValue = 1.5f;
|
|
|
|
|
|
|
|
void create_pipelines_bg();
|
|
void setup_descriptor_set_layout_bg();
|
|
void setup_descriptor_set_bg();
|
|
VkPipeline m_graphicsPipeline_bg = VK_NULL_HANDLE;
|
|
VkPipelineLayout m_pipelineLayout_bg = VK_NULL_HANDLE;
|
|
VkDescriptorSetLayout m_descriptorSetLayout_bg = VK_NULL_HANDLE;
|
|
VkDescriptorSet m_descriptor_set_bg = VK_NULL_HANDLE;
|
|
|
|
|
|
public:
|
|
Callback _callback;
|
|
AnimationFinishedCallback _animationFinishedCallback;
|
|
bool _animationLoop = true;
|
|
string preReadyMotion(const std::string& json, Callback callback);
|
|
void changeMotion(const string motion_type, AnimationFinishedCallback callback, bool loop);
|
|
//void changeMotion(Motion& motion);
|
|
InitArg _initArg;
|
|
//Motion _curMotion;
|
|
string _curMotion_type;
|
|
int _curMotion_png_size;
|
|
Motion _nextMotion;
|
|
int _nextMotionIndex;
|
|
|
|
enum MotionState {
|
|
ready,
|
|
loading_next_motion,
|
|
load_next_motion_finished,
|
|
};
|
|
|
|
MotionState _motionState = ready;
|
|
|
|
int _curTexIndex = 0;
|
|
void SetInitArg(const char* arg);
|
|
|
|
void drawFrame(long long frameTime)override;
|
|
|
|
void destroyTexture(VkDevice device, Texture& texture);
|
|
void cleanupResources(VkDevice device, VmaAllocator allocator);
|
|
|
|
bool _running = false;
|
|
};
|
|
|
|
#endif
|