#ifndef __FaceApp_H__ #define __FaceApp_H__ #include "Application.h" #include #include "../third_party/vma/include/vk_mem_alloc.h" #include #include #include #include #include "nlohmann/json.hpp" using namespace std; using json = nlohmann::json; struct Frame { string name; float x; float y; NLOHMANN_DEFINE_TYPE_INTRUSIVE(Frame, name, x, y) }; struct Motion { string name; vector< Frame> frames; string getMotionId() { return name; } NLOHMANN_DEFINE_TYPE_INTRUSIVE(Motion, name, frames) }; struct MotionList { vector motions; NLOHMANN_DEFINE_TYPE_INTRUSIVE(MotionList, motions) }; struct InitArg { int action_fps; float zoom; float r; float g; float b; float radius; float offset_x; float offset_y; //Motion motion; NLOHMANN_DEFINE_TYPE_INTRUSIVE(InitArg, action_fps, zoom, r, g, b, radius, offset_x, offset_y); }; struct PushConstants { float zoom = 0.5f; float r = 0; float g = 0; float b = 0; float radius = 240; float ux = 0; float uy = 0; float offset_x = 0; float offset_y = 0; }; using Callback = std::function; using AnimationFinishedCallback = std::function; 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 _applicationInited && _faceAppInited && _sceondInited; } 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& vertices, std::vector& 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& texs, vector& descriptSet); 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 m_descriptor_sets_left; //vector m_descriptor_sets_right; const uint32_t kTextureInit = 1; const uint32_t kTextureMax = 20; vector m_texs_left; map motion_list_map; //vector m_texs_right; //bool cur_left = true; //vector* _cur_texs; vector m_texs_thick; std::vector dummy_data; unsigned dummy_w = 4096; unsigned dummy_h = 2048; std::vector obj_vertices; std::vector obj_indices; std::map obj_vertices_map; std::map vertices_map_3dmax; std::mutex mtx_point; std::mutex changeMotionMtx; long long last_update_time; VkBuffer uniform_buffer_vs; VmaAllocation uniform_buffer_allocation; // 需要这个来管理内存 void* uniform_buffer_mapped = nullptr; // 映射的内存指针 void createUniformBuffer(); //float myFloatValue = 1.5f; PushConstants pushConstants; 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; bool _secondfaceAppInited = true; public: void clearnSecondFaceApp(); void Start(); void Stop(); // Called from main thread in response to APP_CMD_TERM_WINDOW. // Serializes against JNI callbacks (processImageNative / passDataToNative / // changeMotionList) by taking all three FaceApp mutexes + createTextureMtx, // then tears down window-dependent Vulkan objects via Application. void onWindowLost(); // Called from main thread in response to APP_CMD_INIT_WINDOW. // Does the full first-time initVulkan() on the very first call, and a // lightweight swapchain/surface rebuild on subsequent calls. void onWindowInit(); void loadMotionThread(); std::thread worker_; bool _isLoadMotion = false; bool _isChangeMostion = false; Callback _callback_loadfinish; AnimationFinishedCallback _animationFinishedCallback; bool _animationLoop = true; string preLoadMotionList(string motion_list_str, Callback callback); Motion getMotionByName(string name); map_loadMotionMap; vector _curLoadMotionList; vector_curMotions; void changeMotionList(vector motions, 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 _curFrameIndex = 0; int _curMotionIndex = 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; bool _playMotion = true; void StopMotion(); void ResumeMotion(); bool _faceAppInited = false; }; #endif