From 0db09f12c30eb31d07eed8e276d680389352896c Mon Sep 17 00:00:00 2001 From: Xiang Silian Date: Sun, 28 Sep 2025 17:18:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90UV=E5=AF=B9=E9=BD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/texture_loading/texture_loading.cpp | 84 +++++++++++++++---- samples/api/texture_loading/texture_loading.h | 3 + 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/samples/api/texture_loading/texture_loading.cpp b/samples/api/texture_loading/texture_loading.cpp index 8b35903..be65e4d 100644 --- a/samples/api/texture_loading/texture_loading.cpp +++ b/samples/api/texture_loading/texture_loading.cpp @@ -9,6 +9,8 @@ #include #endif +using namespace std; + TextureLoading::TextureLoading() { zoom = -1.f; @@ -570,7 +572,7 @@ void TextureLoading::build_command_buffers() - draw_point_cloud(draw_cmd_buffers[i]); + //draw_point_cloud(draw_cmd_buffers[i]); //draw_point_cloud_line(draw_cmd_buffers[i]); @@ -622,6 +624,35 @@ void TextureLoading::draw() #include +bool TextureLoading::LoadOBJ_test(const std::string& filename, std::vector& temp_positions) +{ + std::ifstream file(filename); + if (!file.is_open()) { + return false; + } + temp_positions.clear(); + std::string line; + while (std::getline(file, 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); + } + } + file.close(); +} + bool TextureLoading::LoadOBJ(const std::string& filename, std::vector& vertices, std::vector& indices) { @@ -661,7 +692,7 @@ bool TextureLoading::LoadOBJ(const std::string& filename, float u, v; iss >> u >> v; temp_texcoords.push_back(u); - temp_texcoords.push_back(v); + temp_texcoords.push_back(1-v); } else if (type == "vn") { // 法线 float nx, ny, nz; @@ -758,6 +789,7 @@ bool TextureLoading::LoadOBJ(const std::string& filename, uint32_t newIndex = static_cast(vertices.size()); vertices.push_back(vertex); indices.push_back(newIndex); + obj_vertices_map[newIndex] = posIndex; vertexMap[vertexKey] = newIndex; } } @@ -1627,7 +1659,7 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) return false; } - myFloatValue = 1.0f; + myFloatValue = 1.5f; // --- 加载前景纹理 (示例) --- int width = 640; @@ -1720,23 +1752,36 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) #ifdef _WIN32 - if (LoadOBJ("assets/face_with_uv.obj", obj_vertices, obj_indices)) + LoadOBJ("assets/DemoHead.obj", obj_vertices, obj_indices); + LoadOBJ_test("assets/face.obj", positions_test); #else - if (LoadOBJ("/sdcard/Android/data/com.khronos.vulkan_samples/files/assets/face_with_uv.obj", obj_vertices, obj_indices)) + LoadOBJ("/sdcard/Android/data/com.khronos.vulkan_samples/files/assets/face_with_uv.obj", obj_vertices, obj_indices); #endif // _WIN32 - { - std::cout << "成功加载OBJ文件" << std::endl; - std::cout << "顶点数量: " << obj_vertices.size() << std::endl; - std::cout << "索引数量: " << obj_indices.size() << std::endl; - } - else { - std::cout << "加载OBJ文件失败" << std::endl; - } generate_quad(); prepared = true; + for (int i = 0; i < obj_vertices.size(); ++i) + { + int face_index = obj_vertices_map[i]; + float x = positions_test[face_index * 3 + 0]; + float y = positions_test[face_index * 3 + 1]; + float z = positions_test[face_index * 3 + 2]; + + float obj_x = obj_vertices[i].pos[0]; + float obj_y = obj_vertices[i].pos[1]; + float obj_z = obj_vertices[i].pos[2]; + + //cout << "index: " << i << " face_index:" << face_index << " diff_x:" << (x - obj_x) << " diff_y:" << (y - obj_y) << " diff_z:" << (z - obj_z) << endl; + obj_vertices[i].pos[0] = x; + obj_vertices[i].pos[1] = y; + obj_vertices[i].pos[2] = z; + } + + auto vertex_buffer_size = vkb::to_u32(obj_vertices.size() * sizeof(TextureLoadingVertexStructure)); + vertex_buffer->update(obj_vertices.data(), vertex_buffer_size); + update_uniform_buffers_point(47, -0.5, -0.51, 0, 1, 1, 1, 0, 0, 0, 0, 0, -1); //start(); return true; @@ -1835,11 +1880,16 @@ void ReceiveFacePoint(float* pos, int pointCount, int width, int height) void TextureLoading::update_face_vertex_buffer(float* pos, int pointCount) { - for (int i = 0; i < pointCount; ++i) + for (int i = 0; i < obj_vertices.size(); ++i) { - obj_vertices[i].pos[0] = pos[i * 3 + 0]; - obj_vertices[i].pos[1] = pos[i * 3 + 1]; - obj_vertices[i].pos[2] = pos[i * 3 + 2]; + int face_index = obj_vertices_map[i]; + float x = positions_test[face_index * 3 + 0]; + float y = positions_test[face_index * 3 + 1]; + float z = positions_test[face_index * 3 + 2]; + + obj_vertices[i].pos[0] = x; + obj_vertices[i].pos[1] = y; + obj_vertices[i].pos[2] = z; } auto vertex_buffer_size = vkb::to_u32(obj_vertices.size() * sizeof(TextureLoadingVertexStructure)); diff --git a/samples/api/texture_loading/texture_loading.h b/samples/api/texture_loading/texture_loading.h index e2e5cb7..dbe457f 100644 --- a/samples/api/texture_loading/texture_loading.h +++ b/samples/api/texture_loading/texture_loading.h @@ -210,9 +210,12 @@ public: void update_point_vertex_buffer_line_save(std::vector& vertices); bool LoadOBJ(const std::string& filename,std::vector& vertices,std::vector& indices); + bool LoadOBJ_test(const std::string& filename, std::vector& positions); private: + std::vector positions_test; std::vector obj_vertices; std::vector obj_indices; + std::map obj_vertices_map; std::thread workerThread; bool running = false;