embedded-3dgfx 0.2.0

3D graphics rendering for embedded systems (fork of embedded-gfx by Kezii)
Documentation
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# embedded-3dgfx Examples

This directory contains 23+ interactive examples demonstrating the full capabilities of embedded-3dgfx: 3D rendering, physics, skeletal animation, and soft body dynamics.

## Prerequisites

These examples use `embedded-graphics-simulator` for desktop visualization. SDL2 is required:

### macOS
```bash
brew install sdl2
```

### Ubuntu/Debian
```bash
sudo apt-get install libsdl2-dev
```

### Windows
Download SDL2 development libraries from [libsdl.org](https://www.libsdl.org/download-2.0.php)

## Running Examples

All examples require the `std` feature:

```bash
cargo run --example <name> --features std
```

## 🎨 3D Rendering Examples

### Core Rendering

#### `basic_rendering` - Render Mode Cycling
Demonstrates three fundamental render modes: Points, Lines, and Solid.

**Controls:**
- `SPACE` - Cycle through render modes
- `ESC` - Exit

```bash
cargo run --example basic_rendering --features std
```

#### `rotating_cube` - Animated 3D Rotation
Continuously rotating cube with FPS counter.

**Features:**
- Time-based transformations
- Real-time FPS display
- Smooth multi-axis rotation

```bash
cargo run --example rotating_cube --features std
```

#### `scene_viewer` - Interactive Multi-Mesh Scene
Complex scene with multiple objects and camera controls.

**Controls:**
- `Arrow Keys` - Rotate camera
- `+/-` - Zoom
- `ESC` - Exit

```bash
cargo run --example scene_viewer --features std
```

### Lighting & Shading

#### `lighting_demo` - Directional Lighting
Shows diffuse lighting with ambient term on rotating objects.

**Controls:**
- `SPACE` - Toggle light auto-rotation
- `Arrow Keys` - Manual light control
- `ESC` - Exit

```bash
cargo run --example lighting_demo --features std
```

#### `gouraud_demo` - Smooth Color Interpolation
Demonstrates Gouraud shading with per-vertex colors.

```bash
cargo run --example gouraud_demo --features std
```

#### `blinn_phong_demo` - Specular Highlights
Shows specular reflections using Blinn-Phong shading.

**Features:**
- Specular highlights
- Shininess control
- Multiple STL models

```bash
cargo run --example blinn_phong_demo --features std
```

### Visual Effects

#### `fog_dithering_demo` - Atmospheric Effects
Interactive fog and dithering effects.

**Controls:**
- `F` - Toggle fog
- `D` - Toggle dithering
- `+/-` - Adjust fog distance
- `ESC` - Exit

```bash
cargo run --example fog_dithering_demo --features std
```

#### `billboard_demo` - Camera-Facing Sprites
Demonstrates billboarding for particles and sprites.

```bash
cargo run --example billboard_demo --features std
```

### Advanced Features

#### `texture_mapping_demo` - UV Mapping
Shows affine texture mapping with multiple patterns.

**Features:**
- Multiple texture patterns
- UV coordinate mapping
- Texture switching

```bash
cargo run --example texture_mapping_demo --features std
```

#### `dma_rendering_demo` - Double Buffering
Performance comparison between single and double buffering.

**Features:**
- ~30% FPS improvement demonstration
- Swap chain visualization

```bash
cargo run --example dma_rendering_demo --features std
```

#### `lod_demo` - Level of Detail
Distance-based mesh detail switching.

**Features:**
- 3-10× performance improvement at distance
- Smooth LOD transitions

```bash
cargo run --example lod_demo --features std
```

#### `vertex_animation_demo` - Keyframe Animation
Animated models using keyframe interpolation.

```bash
cargo run --example vertex_animation_demo --features std
```

#### `stl_viewer` - STL Model Viewer
Load and view STL models (Suzanne, teapot, Blåhaj).

**Controls:**
- `1/2/3` - Switch models
- `R` - Rotate model

```bash
cargo run --example stl_viewer --features std
```

#### `painters_algorithm_demo` - Z-buffer-free Rendering
Back-to-front triangle sorting without Z-buffer (saves 1.92MB RAM).

**Controls:**
- `SPACE` - Toggle Painter's Algorithm vs Z-buffer
- `R` - Rotate objects

```bash
cargo run --example painters_algorithm_demo --features std
```

## ⚛️ Physics Examples

*See [PHYSICS_EXAMPLES.md](../PHYSICS_EXAMPLES.md) for detailed physics documentation.*

### Rigid Body Dynamics

#### `physics_rolling_ball`**START HERE**
Beginner-friendly introduction to physics.

**Features:**
- Gravity simulation
- Friction and rolling motion
- Static ramp

**Controls:**
- `R` - Reset ball
- `SPACE` - Apply impulse
- `F` - Toggle friction

```bash
cargo run --example physics_rolling_ball --features std
```

#### `physics_bouncing_balls` - Multi-Body Collisions
Multiple spheres colliding with each other and the ground.

```bash
cargo run --example physics_bouncing_balls --features std
```

#### `physics_pendulum` - Constraint Joints
Swinging pendulum using distance constraints.

```bash
cargo run --example physics_pendulum --features std
```

#### `physics_newtons_cradle` - Chain Constraints
Classic Newton's cradle with multiple distance constraints.

```bash
cargo run --example physics_newtons_cradle --features std
```

#### `physics_stack_tower` - Stacking & Friction
Boxes stacking with realistic friction.

```bash
cargo run --example physics_stack_tower --features std
```

#### `physics_domino_chain` - Sequential Collisions
Domino chain reaction simulation.

```bash
cargo run --example physics_domino_chain --features std
```

#### `physics_wrecking_ball` - Demolition
Wrecking ball knocking down a wall.

```bash
cargo run --example physics_wrecking_ball --features std
```

#### `physics_demo` - Comprehensive Showcase
Original comprehensive physics demo showing all features together.

```bash
cargo run --example physics_demo --features std
```

#### `capsule_physics_demo` - Capsule Colliders
Capsule colliders interacting with spheres and AABBs.

**Features:**
- Capsule-sphere collisions
- Capsule-AABB collisions
- Capsule-capsule collisions
- Realistic tumbling

**Controls:**
- `SPACE` - Spawn capsule
- `C` - Add cube
- `S` - Add sphere
- `R` - Reset

```bash
cargo run --example capsule_physics_demo --features std
```

#### `raycast_demo` - Ray Casting
Interactive ray casting with visual feedback.

**Features:**
- Ray-sphere intersection
- Ray-AABB intersection
- Ray-capsule intersection
- Hit highlighting

**Controls:**
- `SPACE` - Shoot ray
- `Arrow Keys` - Rotate camera
- `R` - Reset

```bash
cargo run --example raycast_demo --features std
```

## 🦴 Skeletal Animation Examples

*See [SKELETAL_AND_SOFTBODY.md](../SKELETAL_AND_SOFTBODY.md) for detailed documentation.*

#### `skeletal_animation_demo` - Bone Hierarchy & Skinning
Animated articulated arm with hierarchical bones.

**Features:**
- Parent-child bone relationships
- Linear blend skinning (SSD)
- Real-time deformation
- Up to 4 bone influences per vertex

**Controls:**
- `SPACE` - Toggle animation
- `UP/DOWN` - Rotate shoulder
- `LEFT/RIGHT` - Rotate elbow
- `R` - Reset pose
- `ESC` - Exit

```bash
cargo run --example skeletal_animation_demo --features std
```

## 🧊 Soft Body Physics Examples

*See [SKELETAL_AND_SOFTBODY.md](../SKELETAL_AND_SOFTBODY.md) for detailed documentation.*

#### `cloth_simulation` - Mass-Spring Cloth
Hanging cloth with wind interaction.

**Features:**
- Mass-spring network
- Structural and shear springs
- Pinned constraints
- Wind forces
- Gravity toggle

**Controls:**
- `W` - Wind right
- `S` - Wind left
- `G` - Toggle gravity
- `R` - Reset
- `ESC` - Exit

```bash
cargo run --example cloth_simulation --features std
```

#### `jelly_cube_demo` - Deformable Cube
Bouncing soft body with volume preservation.

**Features:**
- 3D mass-spring network
- Pressure/volume preservation
- Ground collision
- Bouncy behavior

**Controls:**
- `SPACE` - Drop cube
- `UP` - Apply upward force
- `P` - Toggle pressure
- `R` - Reset
- `ESC` - Exit

```bash
cargo run --example jelly_cube_demo --features std
```

## 🎯 Example Categories

### By Difficulty

**Beginner:**
1. `rotating_cube` - Basic 3D rendering
2. `basic_rendering` - Render modes
3. `physics_rolling_ball` - Physics intro

**Intermediate:**
4. `lighting_demo` - Lighting basics
5. `scene_viewer` - Camera control
6. `physics_bouncing_balls` - Multi-body physics
7. `billboard_demo` - Special effects

**Advanced:**
8. `skeletal_animation_demo` - Character animation
9. `cloth_simulation` - Soft body dynamics
10. `raycast_demo` - Ray casting
11. `physics_newtons_cradle` - Complex constraints

### By Feature

**Rendering:**
- `basic_rendering`, `rotating_cube`, `scene_viewer`
- `lighting_demo`, `gouraud_demo`, `blinn_phong_demo`
- `fog_dithering_demo`, `texture_mapping_demo`

**Physics:**
- `physics_rolling_ball`, `physics_bouncing_balls`
- `physics_pendulum`, `physics_newtons_cradle`
- `physics_stack_tower`, `physics_domino_chain`
- `physics_wrecking_ball`, `physics_demo`
- `capsule_physics_demo`, `raycast_demo`

**Animation:**
- `vertex_animation_demo` - Keyframe animation
- `skeletal_animation_demo` - Bone-based animation
- `cloth_simulation` - Physics-based animation
- `jelly_cube_demo` - Soft body animation

**Performance:**
- `dma_rendering_demo` - Double buffering
- `lod_demo` - Level of detail
- `painters_algorithm_demo` - Memory optimization

## 🔧 Customizing Examples

### Changing Resolution

Edit the example source:
```rust
// Change from 320×240 to 640×480
let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(640, 480));
let mut engine = K3dengine::new(640, 480);

// Adjust window scale
let output_settings = OutputSettingsBuilder::new().scale(1).build(); // Was .scale(2)
```

### Adjusting Performance

```rust
// Reduce quality for higher FPS
engine.camera.set_near_far(2.0, 20.0);  // Narrower depth range

// Use LOD for distant objects
mesh.set_lod(Some(medium_detail), Some(low_detail), lod_levels);

// Disable expensive features
// Remove fog, dithering, or use simpler shading
```

## 🚀 Performance Tips

1. **Use LOD** - 3-10× fewer triangles at distance
2. **Enable DMA** - 30% FPS improvement
3. **Tune Z-buffer** - Better depth precision
4. **Frustum culling** - Automatic 50-200% speedup
5. **Backface culling** - Eliminates ~50% of faces

## 📊 Example Performance

On desktop (Apple M1):
- Simple scenes: 1000+ FPS
- Complex scenes (500+ triangles): 200-400 FPS
- Physics (16 bodies): 60 FPS
- Soft body (64 particles): 60 FPS

On embedded (STM32 Cortex-M33 @ 100MHz):
- Simple scenes: 60-120 FPS
- Medium scenes (100 triangles): 30-60 FPS
- Physics (8 bodies): 30-60 FPS

## 🎓 Learning Path

**Recommended order for learning:**

1. **Rendering Basics**
   - `rotating_cube``basic_rendering``scene_viewer`

2. **Lighting & Effects**
   - `lighting_demo``gouraud_demo``fog_dithering_demo`

3. **Physics Fundamentals**
   - `physics_rolling_ball``physics_bouncing_balls``physics_pendulum`

4. **Advanced Physics**
   - `physics_newtons_cradle``capsule_physics_demo``raycast_demo`

5. **Animation & Soft Body**
   - `skeletal_animation_demo``cloth_simulation``jelly_cube_demo`

## 🐛 Troubleshooting

**SDL2 not found:**
```
error: failed to run custom build command for `sdl2-sys`
```
→ Install SDL2 (see Prerequisites above)

**Feature errors:**
```
error: could not find `perfcounter` in `embedded_3dgfx`
```
→ Add `--features std` to cargo run command

**Window crashes:**
```
called `Option::unwrap()` on a `None` value
```
→ Ensure SDL2 is properly installed and display is available

## 📝 Notes

- All examples target 60 FPS (16ms frame time)
- Physics examples use fixed timestep (0.016s)
- Soft body examples may run slower on some systems
- Examples are for demonstration - optimize for production use
- Window close button works - press ESC or close window to exit

## 🔗 Related Documentation

- **[../PHYSICS_EXAMPLES.md]../PHYSICS_EXAMPLES.md** - Detailed physics demo guide
- **[../SKELETAL_AND_SOFTBODY.md]../SKELETAL_AND_SOFTBODY.md** - Animation & soft body docs
- **[../README.md]../README.md** - Main library documentation