完成UV对齐

This commit is contained in:
xsl
2025-09-28 17:18:37 +08:00
parent 481bdefa74
commit 0db09f12c3
2 changed files with 70 additions and 17 deletions
+67 -17
View File
@@ -9,6 +9,8 @@
#include <string>
#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 <unordered_map>
bool TextureLoading::LoadOBJ_test(const std::string& filename, std::vector<float>& 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<TextureLoadingVertexStructure>& vertices,
std::vector<uint32_t>& 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<uint32_t>(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));
@@ -210,9 +210,12 @@ public:
void update_point_vertex_buffer_line_save(std::vector<LineVertex>& vertices);
bool LoadOBJ(const std::string& filename,std::vector<TextureLoadingVertexStructure>& vertices,std::vector<uint32_t>& indices);
bool LoadOBJ_test(const std::string& filename, std::vector<float>& positions);
private:
std::vector<float> positions_test;
std::vector<TextureLoadingVertexStructure> obj_vertices;
std::vector<uint32_t> obj_indices;
std::map<int, int> obj_vertices_map;
std::thread workerThread;
bool running = false;