code wiki / _hdl_build / nx_skinnorm_gate.nx

nx_skinnorm_gate.nx source

↩ module page · 59 lines · 4397 B

1// nx_skinnorm_gate.nx -- GATE for skinned-normal lighting (mr_face_lambert + mr_isqrt over nx_render_core): lighting 2// computed from the ACTUAL face geometry (cross-product normal), so it is CORRECT under skinning deformation -- no 3// pre-baked normal. Proves: (T1) cosine law from the computed normal, (T2 MEASURED) deforming a face toward the light 4// brightens it (lighting tracks the geometry), (T3) perpendicular=0, (T4 teeth) degenerate face=0 (no div0), (T5) 5// back-face clamps to 0, (T6) the integer sqrt is correct. All Q14 no-float. expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_render_core.nx" 8import "nx_meshrender.nx" 9 10func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func g_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var x: i64=v; if x<0{b[0]=45 as u8;sys_write(1,b,1);x=0-x} if x==0{b[0]=48 as u8;sys_write(1,b,1);return 0} var d: i64=0; var y: i64=x; while y>0{d=d+1;y=y/10} var i: i64=d-1; y=x; while i>=0{b[i]=(48+(y%10)) as u8;y=y/10;i=i-1} sys_write(1,b,d); return 0 } 12func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c } 13 14func main() -> i64 { 15 g_puts("nx_skinnorm_gate (skinned-normal lighting: brightness from the ACTUAL deformed geometry)\n" as *u8) 16 let Q: i64 = 16384 17 let Q2: i64 = 268435456 // Q*Q 18 let Q4: i64 = 72057594037927936 // Q2*Q2 19 let LX: i64 = 11585 // unit light 45deg in xz: (0.707,0,0.707) 20 let LZ: i64 = 11585 21 22 // T1: a flat face in z=0 (computed normal +z), 45deg light -> cosine law = 11585 23 let b1: i64 = mr_face_lambert(0,0,0, Q,0,0, 0,Q,0, LX,0,LZ) 24 // T2: tilt the same face (push the +x vertex back in -z) so the computed normal swings toward the light -> brighter 25 let b2: i64 = mr_face_lambert(0,0,0, Q,0,0-Q, 0,Q,0, LX,0,LZ) 26 // T3: flat face, light along +x (perpendicular to +z normal) -> 0 27 let b3: i64 = mr_face_lambert(0,0,0, Q,0,0, 0,Q,0, Q,0,0) 28 // T4 teeth: degenerate (all three points identical) -> 0, no div0 29 let b4: i64 = mr_face_lambert(0,0,0, 0,0,0, 0,0,0, LX,0,LZ) 30 // T5: flat face normal +z, light -z -> back-face, clamp 0 31 let b5: i64 = mr_face_lambert(0,0,0, Q,0,0, 0,Q,0, 0,0,0-Q) 32 // T6: integer sqrt correctness 33 let s1: i64 = mr_isqrt(Q4); let s2: i64 = mr_isqrt(144); let s3: i64 = mr_isqrt(0) 34 35 g_puts(" flat45="); g_pn(b1); g_puts(" tilted_toward="); g_pn(b2); g_puts(" perp="); g_pn(b3); g_puts(" degenerate="); g_pn(b4); g_puts(" backface="); g_pn(b5); g_puts(" isqrt(Q4)="); g_pn(s1); g_puts("\n" as *u8) 36 37 var pass: i64=0; var t: i64=0 38 var c1: i64=0; if b1==11585 { c1=1 } 39 pass=pass+ck("T1: cosine law from the COMPUTED normal (flat face, 45deg light) == 11585" as *u8, c1); t=t+1 40 var c2: i64=0; if b2>b1 { if b2>16000 { c2=1 } } 41 pass=pass+ck("T2 (MEASURED): deforming the face toward the light brightens it (11585 -> ~Q) = lighting tracks geometry" as *u8, c2); t=t+1 42 var c3: i64=0; if b3==0 { c3=1 } 43 pass=pass+ck("T3: face perpendicular to the light is dark (0)" as *u8, c3); t=t+1 44 var c4: i64=0; if b4==0 { c4=1 } 45 pass=pass+ck("T4 (teeth): degenerate zero-area face -> 0 (no divide-by-zero, no hallucinated light)" as *u8, c4); t=t+1 46 var c5: i64=0; if b5==0 { c5=1 } 47 pass=pass+ck("T5: back-face (normal away from light) clamps to 0" as *u8, c5); t=t+1 48 var c6: i64=0; if s1==Q2 { if s2==12 { if s3==0 { c6=1 } } } 49 pass=pass+ck("T6: integer sqrt correct (isqrt(Q^4)==Q^2, isqrt(144)==12, isqrt(0)==0)" as *u8, c6); t=t+1 50 51 var okall: i64=0; if pass==t { okall=1 } 52 g_puts("---- nx_skinnorm_gate: passed "); g_pn(pass); g_puts(" / "); g_pn(t); g_puts(" ----\n" as *u8) 53 if okall==1 { 54 let logf: i64=sys_openat_append("knowledge/status/skinnorm_gate.log" as *u8, 420) 55 if logf>=0 { let msg: *u8="SKINNORM GREEN: lighting from the computed (deformed) face normal -- cosine law + tracks-deformation + back-face/degenerate teeth + isqrt, no-float\n" as *u8; var ml: i64=0; while msg[ml]!=(0 as u8){ml=ml+1} let z: i64=sys_write(logf,msg,ml); sys_close(logf) } 56 g_puts("verdict=GREEN (a skinned/deformed mesh shades CORRECTLY -- lighting derives from the real geometry)\n" as *u8); sys_exit(0); return 0 57 } 58 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 59}