nx_raycast_voxel_test.nx source
↩ module page · 74 lines · 2798 B
1// nx_raycast_voxel_test.nx -- smoke for nx_raycast_voxel.
2
3import "nx_syscalls.nx"
4import "nx_palette.nx"
5import "nx_tissue.nx"
6import "nx_raycast_voxel.nx"
7
8func main() -> i64 {
9 if NX_RF_N_FACES != 7 { return 1 }
10 if nx_rf_is_valid(NX_RF_X_POS) != 1 { return 2 }
11 if nx_rf_is_valid(7) != 0 { return 3 }
12
13 // 8x8x8 world with stone block at (3,3,3)
14 let world: *NxTissue = nx_tissue_new(8, 8, 8, NX_BPP_3)
15 nx_tissue_set(world, 3, 3, 3, 1)
16
17 // Ray from (0.5, 3.5, 3.5) heading +X at unit speed
18 let ray: *NxRay = nx_ray_new(
19 8192, 57344, 57344, // 0.5, 3.5, 3.5 in Q14
20 NX_RV_Q14, 0, 0, // +X unit
21 16)
22 let hit: *NxRayHit = (sys_mmap(56)) as *NxRayHit
23 let v: nx_int = nx_raycast_voxel(ray, world, hit)
24 if v != NX_RV_HIT { return 4 }
25 if hit.hit_x != 3 { return 5 }
26 if hit.hit_y != 3 { return 6 }
27 if hit.hit_z != 3 { return 7 }
28 if hit.palette_idx != 1 { return 8 }
29 if hit.face != NX_RF_X_NEG { return 9 } // ray entered from -X side
30
31 // Ray pointing AWAY from any block (in empty world direction)
32 let world2: *NxTissue = nx_tissue_new(8, 8, 8, NX_BPP_3)
33 // no blocks placed
34 let ray2: *NxRay = nx_ray_new(8192, 8192, 8192,
35 NX_RV_Q14, 0, 0, 16)
36 let hit2: *NxRayHit = (sys_mmap(56)) as *NxRayHit
37 let v2: nx_int = nx_raycast_voxel(ray2, world2, hit2)
38 // Either MAX_STEPS or OUT_OF_BOX (depends on where ray exits)
39 if v2 == NX_RV_HIT { return 10 }
40
41 // Ray hitting Y face
42 let world3: *NxTissue = nx_tissue_new(8, 8, 8, NX_BPP_3)
43 nx_tissue_set(world3, 1, 4, 1, 2)
44 let ray3: *NxRay = nx_ray_new(
45 24576, 8192, 24576, // 1.5, 0.5, 1.5
46 0, NX_RV_Q14, 0, // +Y direction
47 16)
48 let hit3: *NxRayHit = (sys_mmap(56)) as *NxRayHit
49 let v3: nx_int = nx_raycast_voxel(ray3, world3, hit3)
50 if v3 != NX_RV_HIT { return 11 }
51 if hit3.hit_y != 4 { return 12 }
52 if hit3.face != NX_RF_Y_NEG { return 13 }
53 if hit3.palette_idx != 2 { return 14 }
54
55 // Max steps exhaustion
56 let world4: *NxTissue = nx_tissue_new(8, 8, 8, NX_BPP_3)
57 let ray4: *NxRay = nx_ray_new(8192, 8192, 8192,
58 NX_RV_Q14, 0, 0, 2)
59 let hit4: *NxRayHit = (sys_mmap(56)) as *NxRayHit
60 let v4: nx_int = nx_raycast_voxel(ray4, world4, hit4)
61 if v4 == NX_RV_HIT { return 15 }
62
63 // Face-normal helper: hit.face was X_NEG (ray going +X entered -X face)
64 // so outward normal is -X = -Q14
65 let nx_q14: *i64 = (sys_mmap(8)) as *i64
66 let ny_q14: *i64 = (sys_mmap(8)) as *i64
67 let nz_q14: *i64 = (sys_mmap(8)) as *i64
68 nx_ray_hit_face_normal_q14(hit, nx_q14, ny_q14, nz_q14)
69 if nx_q14[0] != 0 - NX_RV_Q14 { return 16 }
70 if ny_q14[0] != 0 { return 17 }
71 if nz_q14[0] != 0 { return 18 }
72
73 return 0
74}