diff options
Diffstat (limited to 'src/tests/test_mesh.cc')
| -rw-r--r-- | src/tests/test_mesh.cc | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/src/tests/test_mesh.cc b/src/tests/test_mesh.cc index 0294d9b..8c64d9b 100644 --- a/src/tests/test_mesh.cc +++ b/src/tests/test_mesh.cc @@ -169,7 +169,15 @@ bool load_obj_and_create_buffers(const char* path, Object3D& out_obj) { std::string parts[3] = {s1, s2, s3}; RawFace face = {}; for (int i = 0; i < 3; ++i) { - sscanf(parts[i].c_str(), "%d/%d/%d", &face.v[i], &face.vt[i], &face.vn[i]); + // Handle v//vn format + if (parts[i].find("//") != std::string::npos) { + sscanf(parts[i].c_str(), "%d//%d", &face.v[i], &face.vn[i]); + face.vt[i] = 0; + } else { + int res = sscanf(parts[i].c_str(), "%d/%d/%d", &face.v[i], &face.vt[i], &face.vn[i]); + if (res == 2) face.vn[i] = 0; + else if (res == 1) { face.vt[i] = 0; face.vn[i] = 0; } + } } raw_faces.push_back(face); } @@ -213,6 +221,21 @@ bool load_obj_and_create_buffers(const char* path, Object3D& out_obj) { } if (final_vertices.empty()) return false; + + // Calculate AABB and center the mesh + float min_x = 1e10f, min_y = 1e10f, min_z = 1e10f; + float max_x = -1e10f, max_y = -1e10f, max_z = -1e10f; + for (const auto& v : final_vertices) { + min_x = std::min(min_x, v.p[0]); min_y = std::min(min_y, v.p[1]); min_z = std::min(min_z, v.p[2]); + max_x = std::max(max_x, v.p[0]); max_y = std::max(max_y, v.p[1]); max_z = std::max(max_z, v.p[2]); + } + float cx = (min_x + max_x) * 0.5f; + float cy = (min_y + max_y) * 0.5f; + float cz = (min_z + max_z) * 0.5f; + for (auto& v : final_vertices) { + v.p[0] -= cx; v.p[1] -= cy; v.p[2] -= cz; + } + out_obj.local_extent = vec3((max_x - min_x) * 0.5f, (max_y - min_y) * 0.5f, (max_z - min_z) * 0.5f); g_mesh_gpu_data.num_indices = final_indices.size(); g_mesh_gpu_data.vertex_buffer = gpu_create_buffer(g_device, final_vertices.size() * sizeof(MeshVertex), WGPUBufferUsage_Vertex | WGPUBufferUsage_CopyDst, final_vertices.data()).buffer; @@ -260,8 +283,9 @@ int main(int argc, char** argv) { g_renderer.set_noise_texture(g_textures.get_texture_view("noise")); // --- Create Scene --- - Object3D floor(ObjectType::PLANE); - floor.scale = vec3(20.0f, 1.0f, 20.0f); + Object3D floor(ObjectType::BOX); + floor.position = vec3(0, -2.0f, 0); + floor.scale = vec3(25.0f, 0.2f, 25.0f); floor.color = vec4(0.5f, 0.5f, 0.5f, 1.0f); g_scene.add_object(floor); @@ -271,11 +295,11 @@ int main(int argc, char** argv) { return 1; } mesh_obj.color = vec4(1.0f, 0.7f, 0.2f, 1.0f); - mesh_obj.position = {0, 1, 0}; + mesh_obj.position = {0, 1.5, 0}; // Elevate a bit more g_scene.add_object(mesh_obj); g_camera.position = vec3(0, 3, 5); - g_camera.target = vec3(0, 1, 0); + g_camera.target = vec3(0, 1.5, 0); while (!platform_should_close(&platform_state)) { platform_poll(&platform_state); @@ -287,7 +311,7 @@ int main(int argc, char** argv) { if (debug_mode) { auto* vertices = (std::vector<MeshVertex>*)g_scene.objects[1].user_data; - g_renderer.GetVisualDebug().add_mesh_normals(g_scene.objects[1].get_model_matrix(), vertices->size(), vertices->data()); + g_renderer.GetVisualDebug().add_mesh_normals(g_scene.objects[1].get_model_matrix(), (uint32_t)vertices->size(), vertices->data()); } WGPUSurfaceTexture surface_tex; |
