编写完顶点渲染程序,先保存一下

This commit is contained in:
xsl
2025-10-19 19:49:27 +08:00
parent 572c53c0f4
commit 3c63027bf5
32 changed files with 2714 additions and 6225 deletions
+8 -8
View File
@@ -13,14 +13,14 @@ void VK_CHECK(VkResult ret)
assert(ret == VK_SUCCESS);
}
std::string AppBase::getPath(const std::string path)
{
#ifdef _WIN32
std::string android_path = "app/src/main/assets/" + path;
return android_path;
#endif
return path;
}
//std::string AppBase::getPath(const std::string path)
//{
//#ifdef _WIN32
// std::string android_path = "app/src/main/assets/" + path;
// return android_path;
//#endif
// return path;
//}
std::vector<char> AppBase::readFile(const std::string& path)
{
+12 -3
View File
@@ -11,7 +11,7 @@
// } \
// } while (0)
#include <volk.h>
//#include <volk.h>
#ifdef _WIN32
#define logOut std::cout
@@ -29,12 +29,15 @@
#include <fstream>
#include <iostream>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
#include <vector>
#include <optional>
#include <set>
#include <thread>
#include <mutex>
#include <chrono>
#include <ctime>
void VK_CHECK(VkResult ret);
@@ -52,10 +55,16 @@ class AppBase
{
public:
std::vector<char> readFile(const std::string& enginePath);
std::string getPath(const std::string path);
//std::string getPath(const std::string path);
VkShaderModule createShaderModule(VkDevice& device, const std::vector<char>& code);
const std::vector<const char*> validationLayers = {"VK_LAYER_KHRONOS_validation"};
protected:
long long getCurrentTimeMillis() {
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
}
VkInstance instance;
void createInstance();
bool checkValidationLayerSupport();
+12 -9
View File
@@ -178,8 +178,6 @@ void Application::createLogicalDevice() {
throw std::runtime_error("failed to create logical device!");
}
v_device = std::make_unique<vkb::core::DeviceC>(v_gpu, device, surface);
vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
}
@@ -480,11 +478,7 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
// 绑定图形管线
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
// 绘制三角形
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
render(commandBuffer);
// 结束渲染流程
vkCmdEndRenderPass(commandBuffer);
@@ -494,6 +488,15 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im
}
}
void Application::render(VkCommandBuffer commandBuffer)
{
// 绑定图形管线
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
// 绘制三角形
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
}
void Application::drawFrame()
{
// 1. 等待前一帧完成
@@ -651,10 +654,10 @@ uint32_t Application::findMemoryType(VkPhysicalDevice physicalDevice, uint32_t t
Texture Application::loadTexture(std::string path)
{
const char* filename = getPath(path).c_str();
std::vector<char> data = readFile(path);
std::vector<unsigned char> image;
unsigned w, h;
unsigned error = lodepng::decode(image, w, h, filename, LCT_RGBA, 8);
unsigned error = lodepng::decode(image, w, h, data.data(), LCT_RGBA, 8);
Texture tex;
processWithVulkan(image.data(), w, h, w * 4, image.size(), tex, true);
return tex;
+2 -5
View File
@@ -6,8 +6,6 @@
#include <thread>
#include <mutex>
#include <vulkan/vulkan.hpp>
#include "framework/core/buffer.h"
#include "framework/core/device.h"
struct Texture
{
@@ -44,12 +42,11 @@ public:
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
void createSyncObjects();
void drawFrame();
virtual void render(VkCommandBuffer commandBuffer);
bool isInited() { return inited; }
protected:
std::unique_ptr< vkb::core::InstanceC> v_instance;
std::unique_ptr<vkb::PhysicalDevice> v_gpu;
std::unique_ptr<vkb::core::DeviceC> v_device;
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; // 物理设备
VkDevice device; // 逻辑设备
VkQueue graphicsQueue; // 图形队列
+274 -13
View File
@@ -1,37 +1,298 @@
#include "FaceApp.h"
#include "common/helpers.h"
#include "hardcode_data.h"
#include <sstream>
FaceApp* FaceApp::faceIns = nullptr;
FaceApp::FaceApp(/* args */)
{
faceIns = this;
}
FaceApp::~FaceApp()
{
}
bool FaceApp::LoadOBJ(const std::string& filename,
std::vector<TextureLoadingVertexStructure>& vertices,
std::vector<uint32_t>& indices) {
// 临时存储从OBJ文件读取的原始数据
std::vector<float> temp_positions;
std::vector<float> temp_texcoords;
std::vector<float> temp_normals;
// 用于处理顶点索引
std::vector<int> vertexIndices, uvIndices, normalIndices;
std::vector<char> data = readFile(filename);
// 将 vector<char> 转换为以 null 结尾的字符串(安全做法)
std::string content(data.begin(), data.end());
std::istringstream iss(content); // 用字符串创建字符串流
std::string line;
while (std::getline(iss, line)) {
// 跳过空行和注释行
if (line.empty() || line[0] == '#') {
continue;
}
std::istringstream iss(line);
std::string type;
iss >> type;
if (type == "v") { // 顶点位置
float x, y, z;
iss >> x >> y >> z;
temp_positions.push_back(x);
temp_positions.push_back(y);
temp_positions.push_back(z);
}
else if (type == "vt") { // 纹理坐标
float u, v;
iss >> u >> v;
temp_texcoords.push_back(u);
temp_texcoords.push_back(1 - v);
}
else if (type == "vn") { // 法线
float nx, ny, nz;
iss >> nx >> ny >> nz;
temp_normals.push_back(nx);
temp_normals.push_back(ny);
temp_normals.push_back(nz);
}
else if (type == "f") { // 面(三角形)
std::string vertex1, vertex2, vertex3;
iss >> vertex1 >> vertex2 >> vertex3;
// 处理每个顶点的索引
for (const std::string& vertex : { vertex1, vertex2, vertex3 }) {
std::istringstream viss(vertex);
std::string v, vt, vn;
// 解析顶点索引格式:v/vt/vn 或 v//vn 或 v
std::getline(viss, v, '/');
std::getline(viss, vt, '/');
std::getline(viss, vn, '/');
int posIndex = std::stoi(v) - 1; // OBJ索引从1开始
int texIndex = -1, normIndex = -1;
if (!vt.empty()) texIndex = std::stoi(vt) - 1;
if (!vn.empty()) normIndex = std::stoi(vn) - 1;
vertexIndices.push_back(posIndex);
uvIndices.push_back(texIndex);
normalIndices.push_back(normIndex);
}
}
}
// 创建顶点数据
vertices.clear();
indices.clear();
// 用于去重的哈希映射
std::map<std::string, uint32_t> vertexMap;
for (size_t i = 0; i < vertexIndices.size(); i++) {
int posIndex = vertexIndices[i];
int texIndex = uvIndices[i];
int normIndex = normalIndices[i];
// 创建唯一标识符
std::string vertexKey = std::to_string(posIndex) + "/" +
std::to_string(texIndex) + "/" +
std::to_string(normIndex);
// 检查是否已经存在相同的顶点
if (vertexMap.find(vertexKey) != vertexMap.end()) {
// 使用现有顶点的索引
indices.push_back(vertexMap[vertexKey]);
}
else {
// 创建新顶点
TextureLoadingVertexStructure vertex;
// 设置位置
if (posIndex >= 0 && posIndex * 3 + 2 < temp_positions.size()) {
vertex.pos[0] = temp_positions[posIndex * 3];
vertex.pos[1] = temp_positions[posIndex * 3 + 1];
vertex.pos[2] = temp_positions[posIndex * 3 + 2];
}
else {
vertex.pos[0] = vertex.pos[1] = vertex.pos[2] = 0.0f;
}
// 设置纹理坐标
if (texIndex >= 0 && texIndex * 2 + 1 < temp_texcoords.size()) {
vertex.uv[0] = temp_texcoords[texIndex * 2];
vertex.uv[1] = temp_texcoords[texIndex * 2 + 1];
}
else {
vertex.uv[0] = vertex.uv[1] = 0.0f;
}
// 设置法线
if (normIndex >= 0 && normIndex * 3 + 2 < temp_normals.size()) {
vertex.normal[0] = temp_normals[normIndex * 3];
vertex.normal[1] = temp_normals[normIndex * 3 + 1];
vertex.normal[2] = temp_normals[normIndex * 3 + 2];
}
else {
vertex.normal[0] = vertex.normal[1] = 0.0f;
vertex.normal[2] = 1.0f; // 默认法线
}
// 添加新顶点并记录索引
uint32_t newIndex = static_cast<uint32_t>(vertices.size());
vertices.push_back(vertex);
indices.push_back(newIndex);
obj_vertices_map[newIndex] = posIndex;
vertexMap[vertexKey] = newIndex;
}
}
return true;
}
void FaceApp::render(VkCommandBuffer commandBuffer)
{
Application::render(commandBuffer);
}
void FaceApp::initVulkan()
{
Application::initVulkan();
VmaAllocatorCreateInfo allocatorInfo = {};
allocatorInfo.physicalDevice = physicalDevice;
allocatorInfo.device = device;
allocatorInfo.instance = instance;
vmaCreateAllocator(&allocatorInfo, &allocator);
createVertexBuffer();
LoadOBJ("face_picture_3dmax.obj", obj_vertices, obj_indices);
uploadVertexData();
}
void FaceApp::generate_quad()
void FaceApp::createVertexBuffer()
{
//std::vector<TextureLoadingVertexStructure>& vertices = obj_vertices;
//std::vector<uint32_t> indices = obj_indices;
VkDeviceSize vertexBufferSize = sizeof(TextureLoadingVertexStructure) * obj_vertices.capacity();
VkDeviceSize indexBufferSize = sizeof(uint32_t) * obj_indices.capacity();
//index_count = static_cast<uint32_t>(indices.size());
// 创建顶点缓冲区(设备本地,用于渲染)
VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
bufferInfo.size = vertexBufferSize;
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
//auto vertex_buffer_size = vkb::to_u32(vertices.size() * sizeof(TextureLoadingVertexStructure));
//auto index_buffer_size = vkb::to_u32(indices.size() * sizeof(uint32_t));
VmaAllocationCreateInfo allocInfo = {};
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
//vkb::core::DeviceC& cDevice = reinterpret_cast<vkb::core::DeviceC&>(*v_device);
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &m_vertexBuffer, &m_vertexBufferAllocation, nullptr);
//auto vertex_buffer = std::make_unique<vkb::core::BufferC>(cDevice, vertex_buffer_size,VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,VMA_MEMORY_USAGE_CPU_TO_GPU);
//vertex_buffer->update(vertices.data(), vertex_buffer_size);
// 创建暂存缓冲区(CPU可见,用于上传数据)
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
//index_buffer = std::make_unique<vkb::core::BufferC>(cDevice,index_buffer_size,VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,VMA_MEMORY_USAGE_CPU_TO_GPU);
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &m_stagingBuffer, &m_stagingBufferAllocation, nullptr);
//index_buffer->update(indices.data(), index_buffer_size);
// 创建索引缓冲区
bufferInfo.size = indexBufferSize;
bufferInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &m_indexBuffer, &m_indexBufferAllocation, nullptr);
}
void ReceiveFacePoint(float* pos, int pointCount, int width, int height)
{
FaceApp* self = FaceApp::Get();
if (self != nullptr)
{
FaceApp::Get()->update_face_vertex_buffer(pos, pointCount);
}
}
void FaceApp::update_face_vertex_buffer(float* pos, int pointCount)
{
std::lock_guard<std::mutex> lock(mtx_point);
last_update_time = getCurrentTimeMillis();
for (int i = 0; i < obj_vertices.size(); ++i)
{
int face_index = obj_vertices_map[HardCodeData::Get().indexMap[i]];
float x = pos[face_index * 3 + 0];
float y = pos[face_index * 3 + 1];
float z = pos[face_index * 3 + 2];
obj_vertices[i].pos[0] = x;
obj_vertices[i].pos[1] = y;
obj_vertices[i].pos[2] = z;
}
uploadVertexData();
}
void FaceApp::uploadVertexData() {
// 上传顶点数据
void* data;
vmaMapMemory(allocator, m_stagingBufferAllocation, &data);
memcpy(data, obj_vertices.data(), sizeof(TextureLoadingVertexStructure) * obj_vertices.size());
vmaUnmapMemory(allocator, m_stagingBufferAllocation);
// 复制到设备内存
copyBuffer(m_stagingBuffer, m_vertexBuffer, sizeof(TextureLoadingVertexStructure) * obj_vertices.size());
// 上传索引数据(如果需要暂存缓冲区,可以创建另一个)
vmaMapMemory(allocator, m_stagingBufferAllocation, &data);
memcpy(data, obj_indices.data(), sizeof(uint32_t) * obj_indices.size());
vmaUnmapMemory(allocator, m_stagingBufferAllocation);
copyBuffer(m_stagingBuffer, m_indexBuffer, sizeof(uint32_t) * obj_indices.size());
}
void FaceApp::copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size)
{
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
VkBufferCopy copyRegion = {};
copyRegion.size = size;
vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
endSingleTimeCommands(commandBuffer);
}
VkCommandBuffer FaceApp::beginSingleTimeCommands() {
VkCommandBufferAllocateInfo allocInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO };
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = commandPool;
allocInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer;
vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
VkCommandBufferBeginInfo beginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(commandBuffer, &beginInfo);
return commandBuffer;
}
void FaceApp::endSingleTimeCommands(VkCommandBuffer commandBuffer) {
vkEndCommandBuffer(commandBuffer);
VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(graphicsQueue);
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
}
+38 -5
View File
@@ -2,7 +2,8 @@
#define __FaceApp_H__
#include "Application.h"
#include "vma/include/vk_mem_alloc.h"
#include <map>
struct TextureLoadingVertexStructure
@@ -24,16 +25,48 @@ public:
void initVulkan() override;
void render(VkCommandBuffer commandBuffer) override;
static FaceApp* Get() { return faceIns; }
void update_face_vertex_buffer(float* pos, int pointCount);
private:
void generate_quad();
static FaceApp* faceIns;
VmaAllocator allocator = nullptr;
bool LoadOBJ(const std::string& filename,
std::vector<TextureLoadingVertexStructure>& vertices,
std::vector<uint32_t>& indices);
void createVertexBuffer();
void uploadVertexData();
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;
std::unique_ptr<vkb::core::BufferC> vertex_buffer;
std::unique_ptr<vkb::core::BufferC> index_buffer;
uint32_t index_count;
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;
};
#endif
File diff suppressed because one or more lines are too long