diff options
Diffstat (limited to 'src/3d')
| -rw-r--r-- | src/3d/object.h | 3 | ||||
| -rw-r--r-- | src/3d/renderer.h | 13 | ||||
| -rw-r--r-- | src/3d/visual_debug.cc | 27 | ||||
| -rw-r--r-- | src/3d/visual_debug.h | 3 |
4 files changed, 42 insertions, 4 deletions
diff --git a/src/3d/object.h b/src/3d/object.h index 0c8edd8..a7ce0b8 100644 --- a/src/3d/object.h +++ b/src/3d/object.h @@ -40,11 +40,12 @@ class Object3D { bool is_static; AssetId mesh_asset_id; + void* user_data; // For tool-specific data, not for general use Object3D(ObjectType t = ObjectType::CUBE) : position(0, 0, 0), rotation(0, 0, 0, 1), scale(1, 1, 1), type(t), color(1, 1, 1, 1), velocity(0, 0, 0), mass(1.0f), restitution(0.5f), - is_static(false), mesh_asset_id((AssetId)0) { + is_static(false), mesh_asset_id((AssetId)0), user_data(nullptr) { } mat4 get_model_matrix() const { diff --git a/src/3d/renderer.h b/src/3d/renderer.h index 5caf19b..5c9fd38 100644 --- a/src/3d/renderer.h +++ b/src/3d/renderer.h @@ -65,13 +65,23 @@ class Renderer3D { // Set whether to use BVH acceleration void SetBvhEnabled(bool enabled) { bvh_enabled_ = enabled; } - private: struct MeshGpuData { WGPUBuffer vertex_buffer; WGPUBuffer index_buffer; uint32_t num_indices; }; + // HACK for test_mesh tool + void override_mesh_buffers(const MeshGpuData* data) { + temp_mesh_override_ = data; + } + +#if !defined(STRIP_ALL) + VisualDebug& GetVisualDebug() { return visual_debug_; } +#endif + + private: + void create_pipeline(); WGPURenderPipeline create_pipeline_impl(bool use_bvh); void create_mesh_pipeline(); @@ -98,6 +108,7 @@ class Renderer3D { bool bvh_enabled_ = true; std::map<AssetId, MeshGpuData> mesh_cache_; + const MeshGpuData* temp_mesh_override_ = nullptr; // HACK for test_mesh tool WGPUTextureView noise_texture_view_ = nullptr; WGPUTextureView sky_texture_view_ = nullptr; diff --git a/src/3d/visual_debug.cc b/src/3d/visual_debug.cc index 86f12b4..009a1e1 100644 --- a/src/3d/visual_debug.cc +++ b/src/3d/visual_debug.cc @@ -107,7 +107,7 @@ void VisualDebug::create_pipeline(WGPUTextureFormat format) { #if defined(DEMO_CROSS_COMPILE_WIN32) pipeline_desc.vertex.entryPoint = "vs_main"; #else - pipeline_desc.vertex.entryPoint = {"vs_main", 7}; + pipeline_desc.vertex.entryPoint = str_view("vs_main"); #endif pipeline_desc.vertex.bufferCount = 1; pipeline_desc.vertex.buffers = &vertex_layout; @@ -117,7 +117,7 @@ void VisualDebug::create_pipeline(WGPUTextureFormat format) { #if defined(DEMO_CROSS_COMPILE_WIN32) fragment_state.entryPoint = "fs_main"; #else - fragment_state.entryPoint = {"fs_main", 7}; + fragment_state.entryPoint = str_view("fs_main"); #endif fragment_state.targetCount = 1; @@ -203,6 +203,29 @@ void VisualDebug::add_aabb(const vec3& min, const vec3& max, } } +void VisualDebug::add_mesh_normals(const mat4& transform, uint32_t num_vertices, + const MeshVertex* vertices) { + if (!vertices || num_vertices == 0) + return; + + mat4 normal_matrix = mat4::transpose(transform.inverse()); + + for (uint32_t i = 0; i < num_vertices; ++i) { + const auto& v = vertices[i]; + + vec4 p_world = transform * vec4(v.p[0], v.p[1], v.p[2], 1.0f); + vec3 n_object = vec3(v.n[0], v.n[1], v.n[2]); + vec4 n_world_h = normal_matrix * vec4(n_object.x, n_object.y, n_object.z, 0.0f); + vec3 n_world = n_world_h.xyz().normalize(); + + lines_.push_back( + {p_world.xyz(), p_world.xyz() + n_world * 0.1f, // 0.1 is the length + {0.0f, 1.0f, 1.0f} // Cyan color + }); + } +} + + void VisualDebug::update_buffers(const mat4& view_proj) { // Update Uniforms wgpuQueueWriteBuffer(wgpuDeviceGetQueue(device_), uniform_buffer_, 0, diff --git a/src/3d/visual_debug.h b/src/3d/visual_debug.h index 6173fc4..ebccf45 100644 --- a/src/3d/visual_debug.h +++ b/src/3d/visual_debug.h @@ -8,6 +8,7 @@ #include "gpu/gpu.h" #include "util/mini_math.h" +#include "3d/object.h" #include <vector> struct DebugLine { @@ -27,6 +28,8 @@ class VisualDebug { void add_aabb(const vec3& min, const vec3& max, const vec3& color); + void add_mesh_normals(const mat4& transform, uint32_t num_vertices, const MeshVertex* vertices); + // Render all queued primitives and clear the queue void render(WGPURenderPassEncoder pass, const mat4& view_proj); |
