summaryrefslogtreecommitdiff
path: root/doc/3D.md
blob: c27b03488b15756b8206791c884bf265105d1a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 3D System and Rendering Pipeline

## Coordinate Conventions

### World Space: Z-up, Y-forward
- X = right
- Y = forward
- Z = up
- `look_at` up parameter: `{0, 0, 1}`
- Camera default position: along +Y axis, looking toward origin

### View Space: X-right, Y-up, -Z-forward (standard NDC)
- Camera looks down **-Z** in view space
- `perspective()` encodes this via `m[11] = -1`
- Independent of world-space convention — set by `look_at` construction

### Screen Space: Y-up, origin at bottom-left
- `uv` in `[0,1]`: `(0,0)` = bottom-left, `(1,1)` = top-right
- `st` in `[-1,1]`: NDC directly, y-up
- WebGPU textures are y-down `(0,0)` at top — use `textureSampleYUp()` when sampling with `uv`

### NDC → Screen
- WebGPU NDC: y-up (`+1` = top)
- WebGPU framebuffer: y-down (pixel row 0 at top)
- `uv.y = 0` → NDC `y = -1` → bottom of framebuffer ✓

## Core Concept

Hybrid SDF/rasterization pipeline with physics and collision detection.

## Object Representation

Each object has:
- Bounding volume (box/capsule)
- SDF function (signed distance field)
- Ray-intersection query support
- Material/shader association

## Render Pipeline

1. Move camera and lights along paths
2. Transform and cull bounding volumes (BVH)
3. Project visible volumes to screen
4. Raymarch SDF inside proxy geometry (fragment shader)
5. Multi-pass for shadow maps

## Visual Debugging (Task #39)

Debug mode (`!STRIP_ALL`) provides:
- Wireframe bounding volumes
- Camera/object trajectories
- Collision point visualization
- Ray-intersection display
- Light visualization (direction, cone, shadow maps)

## BVH Acceleration Structure

### Purpose
Efficient spatial queries for rendering (GPU) and physics (CPU).

### Node Layout
```cpp
struct BVHNode {
    float min_x, min_y, min_z;
    int32_t left_idx;   // < 0 = leaf node
    float max_x, max_y, max_z;
    int32_t right_idx;  // leaf: object_index
};
```

WGSL:
```wgsl
struct BVHNode {
    min: vec3<f32>, left_idx: i32,
    max: vec3<f32>, obj_idx_or_right: i32,
};
@group(0) @binding(2) var<storage, read> bvh_nodes: array<BVHNode>;
```

### Construction (CPU)
Rebuild every frame (< 100 objects):
1. Calculate AABBs for all objects
2. Recursive midpoint split (largest variance axis)
3. Linearize tree for GPU upload

### Traversal (GPU)
Fixed-size stack (no recursion in WGSL):
```wgsl
fn map_scene(pos: vec3<f32>) -> f32 {
    var min_dist = 10000.0;
    var stack: array<i32, 32>;
    // Push root, iterate until stack empty
    // Check AABB, evaluate leaf SDFs
    return min_dist;
}
```

## Physics & Collision System

### Components
Extend `Object3D` with:
- Mass/InverseMass
- Velocity (linear)
- Restitution (bounciness)
- Friction

### Broad Phase
Reuse BVH for O(N log N) collision detection.

### Narrow Phase
**Proxy Point Probing** (SDF-based):
1. Transform probe points into neighbor's local space
2. Evaluate SDF at probe points
3. If `d < 0`: collision detected
   - Penetration depth: `p = -d`
   - Normal: `∇SDF` via central differences
   - Contact point: `P_world - N * p`

### Collision Response (Position Based Dynamics)
1. Integrate position: `p_pred = p + v * dt`
2. Broad phase: Find collision pairs (BVH)
3. Narrow phase: Resolve penetrations on `p_pred`
4. Update velocity: `v = (p_pred - p) / dt`
5. Finalize: `p = p_pred`

### Rotation & Angular Momentum
- Quaternion-based orientation
- Angular velocity `vec3`
- Integration: `q = q + 0.5 * dt * [0, w] * q`
- Collision impulses affect both linear and angular momentum

## Integration Plan

1. **CPU-Side SDF Library**: `src/3d/sdf_cpu.h` (sdSphere, sdBox, sdTorus)
2. **Physics System**: `src/3d/physics.h/cc` (Update, ResolveCollision)
3. **Main Loop**: Call `PhysicsSystem::Update` before `Renderer3D::render`

## Future Tasks

- **Task #36**: Blender exporter (convert scenes to binary format)
- **Task #40**: Advanced lighting & transparency (multi-ray-casting)
- **Task #39**: Visual debugging system (wireframes, trajectories)