// File: tomfunkhouser[P]gaps/apps/scn2cam/scn2cam.cpp
// Function: RasterizeIntoZXGrid(gaps::R2Grid&, gaps::R3SceneNode*, gaps::R3Box const&)
static void
RasterizeIntoZXGrid(R2Grid& grid, R3SceneNode *node,
  const R3Box& world_bbox)
{
  // Check bounding box
  R3Box node_bbox = node->BBox();
  if (!R3Intersects(world_bbox, node_bbox)) return;
  
  // Rasterize elements into grid
  for (int j = 0; j < node->NElements(); j++) {
    R3SceneElement *element = node->Element(j);
    for (int k = 0; k < element->NShapes(); k++) {
      R3Shape *shape = element->Shape(k);
      R3Box shape_bbox = shape->BBox();
      if (!R3Intersects(world_bbox, shape_bbox)) continue;
      if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
        R3TriangleArray *triangles = (R3TriangleArray *) shape;
        for (int m = 0; m < triangles->NTriangles(); m++) {
          R3Triangle *triangle = triangles->Triangle(m);
          R3Box triangle_bbox = triangle->BBox();
          if (!R3Intersects(world_bbox, triangle_bbox)) continue;
          R3TriangleVertex *v0 = triangle->V0();
          R3Point vp0 = v0->Position();
          R2Point p0(vp0.Z(), vp0.X());
          if (!R2Contains(grid.WorldBox(), p0)) continue;
          R3TriangleVertex *v1 = triangle->V1();
          R3Point vp1 = v1->Position();
          R2Point p1(vp1.Z(), vp1.X());
          if (!R2Contains(grid.WorldBox(), p1)) continue;
          R3TriangleVertex *v2 = triangle->V2();
          R3Point vp2 = v2->Position();
          R2Point p2(vp2.Z(), vp2.X());
          if (!R2Contains(grid.WorldBox(), p2)) continue;
          grid.RasterizeWorldTriangle(p0, p1, p2, 1.0);
        }
      }
    }
  }

  // Rasterize children into grid
  for (int j = 0; j < node->NChildren(); j++) {
    R3SceneNode *child = node->Child(j);
    RasterizeIntoZXGrid(grid, child, world_bbox);
  }
}

// Assembly:
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rdi, 0x2f0(%rsp)
movq %rsi, 0x2e8(%rsp)
movq %rdx, 0x2e0(%rsp)
movq 0x2e8(%rsp), %rdi
callq 0x60be0
movq %rax, %rsi
leaq 0x2a8(%rsp), %rdi
movq %rdi, 0xc8(%rsp)
callq 0xc4a10
movq 0xc8(%rsp), %rsi
movq 0x2e0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0x7b790
movl %eax, 0xd4(%rsp)
jmp 0x30826
movl 0xd4(%rsp), %eax
cmpl $0x0, %eax
jne 0x3085b
movl $0x1, 0x298(%rsp)
jmp 0x30f78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
jmp 0x30f8d
movl $0x0, 0x294(%rsp)
movl 0x294(%rsp), %eax
movl %eax, 0xc4(%rsp)
movq 0x2e8(%rsp), %rdi
callq 0x32480
movl %eax, %ecx
movl 0xc4(%rsp), %eax
cmpl %ecx, %eax
jge 0x30ee6
movq 0x2e8(%rsp), %rdi
movl 0x294(%rsp), %esi
callq 0x331c0
movq %rax, 0xb8(%rsp)
jmp 0x308b0
movq 0xb8(%rsp), %rax
movq %rax, 0x288(%rsp)
movl $0x0, 0x284(%rsp)
movl 0x284(%rsp), %eax
movl %eax, 0xb4(%rsp)
movq 0x288(%rsp), %rdi
callq 0x331f0
movl %eax, %ecx
movl 0xb4(%rsp), %eax
cmpl %ecx, %eax
jge 0x30ece
movq 0x288(%rsp), %rdi
movl 0x284(%rsp), %esi
callq 0x33210
movq %rax, 0xa8(%rsp)
jmp 0x30915
movq 0xa8(%rsp), %rax
movq %rax, 0x278(%rsp)
movq 0x278(%rsp), %rsi
movq (%rsi), %rax
movq 0x88(%rax), %rax
leaq 0x240(%rsp), %rdi
callq *%rax
jmp 0x30943
movq 0x2e0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
leaq 0x240(%rsp), %rsi
callq 0x7b790
movl %eax, 0xa4(%rsp)
jmp 0x30965
movl 0xa4(%rsp), %eax
cmpl $0x0, %eax
jne 0x3099a
movl $0x7, 0x298(%rsp)
jmp 0x30e88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
jmp 0x30ebc
movq 0x278(%rsp), %rdi
movq (%rdi), %rax
movq 0xb8(%rax), %rax
callq *%rax
movl %eax, 0xa0(%rsp)
jmp 0x309b7
callq 0xb9d80
movl %eax, 0x9c(%rsp)
jmp 0x309c5
movl 0xa0(%rsp), %eax
movl 0x9c(%rsp), %ecx
cmpl %ecx, %eax
jne 0x30e7d
movq 0x278(%rsp), %rax
movq %rax, 0x238(%rsp)
movl $0x0, 0x234(%rsp)
movl 0x234(%rsp), %eax
movl %eax, 0x98(%rsp)
movq 0x238(%rsp), %rdi
callq 0x332a0
movl %eax, %ecx
movl 0x98(%rsp), %eax
cmpl %ecx, %eax
jge 0x30e7b
movq 0x238(%rsp), %rdi
movl 0x234(%rsp), %esi
callq 0x332c0
movq %rax, 0x90(%rsp)
jmp 0x30a40
movq 0x90(%rsp), %rax
movq %rax, 0x228(%rsp)
movq 0x228(%rsp), %rsi
movq (%rsi), %rax
movq 0x88(%rax), %rax
leaq 0x1f0(%rsp), %rdi
callq *%rax
jmp 0x30a6e
movq 0x2e0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
leaq 0x1f0(%rsp), %rsi
callq 0x7b790
movl %eax, 0x8c(%rsp)
jmp 0x30a90
movl 0x8c(%rsp), %eax
cmpl $0x0, %eax
jne 0x30ac5
movl $0xa, 0x298(%rsp)
jmp 0x30e38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
jmp 0x30e6c
movq 0x228(%rsp), %rdi
callq 0x35bf0
movq %rax, 0x80(%rsp)
jmp 0x30adc
movq 0x80(%rsp), %rax
movq %rax, 0x1e8(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x35c00
movq %rax, 0x78(%rsp)
jmp 0x30b00
movq 0x78(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0xd9f80
jmp 0x30b14
leaq 0x1d0(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x34910
movq 0x68(%rsp), %rdi
movsd %xmm0, 0x70(%rsp)
callq 0x34870
movaps %xmm0, %xmm1
movsd 0x70(%rsp), %xmm0
leaq 0x1c0(%rsp), %rdi
callq 0x10e490
jmp 0x30b4e
movq 0x2f0(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x35c10
jmp 0x30b65
leaq 0x198(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0xed750
movl %eax, 0x64(%rsp)
jmp 0x30b80
movl 0x64(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x63(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x35b70
movb 0x63(%rsp), %al
testb $0x1, %al
jne 0x30ba7
jmp 0x30bdd
movl $0xa, 0x298(%rsp)
jmp 0x30e38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x35b70
jmp 0x30e6c
movq 0x228(%rsp), %rdi
callq 0x35cb0
movq %rax, 0x58(%rsp)
jmp 0x30bf1
movq 0x58(%rsp), %rax
movq %rax, 0x190(%rsp)
movq 0x190(%rsp), %rdi
callq 0x35c00
movq %rax, 0x50(%rsp)
jmp 0x30c12
movq 0x50(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0xd9f80
jmp 0x30c26
leaq 0x178(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x34910
movq 0x40(%rsp), %rdi
movsd %xmm0, 0x48(%rsp)
callq 0x34870
movaps %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
leaq 0x168(%rsp), %rdi
callq 0x10e490
jmp 0x30c60
movq 0x2f0(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x35c10
jmp 0x30c77
leaq 0x140(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0xed750
movl %eax, 0x3c(%rsp)
jmp 0x30c92
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x3b(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x35b70
movb 0x3b(%rsp), %al
testb $0x1, %al
jne 0x30cb9
jmp 0x30cef
movl $0xa, 0x298(%rsp)
jmp 0x30e38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x35b70
jmp 0x30e6c
movq 0x228(%rsp), %rdi
callq 0x35cc0
movq %rax, 0x30(%rsp)
jmp 0x30d03
movq 0x30(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rdi
callq 0x35c00
movq %rax, 0x28(%rsp)
jmp 0x30d24
movq 0x28(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0xd9f80
jmp 0x30d38
leaq 0x120(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x34910
movq 0x18(%rsp), %rdi
movsd %xmm0, 0x20(%rsp)
callq 0x34870
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
leaq 0x110(%rsp), %rdi
callq 0x10e490
jmp 0x30d72
movq 0x2f0(%rsp), %rsi
leaq 0xe8(%rsp), %rdi
callq 0x35c10
jmp 0x30d89
leaq 0xe8(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xed750
movl %eax, 0x14(%rsp)
jmp 0x30da4
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x13(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x35b70
movb 0x13(%rsp), %al
testb $0x1, %al
jne 0x30dcb
jmp 0x30dfb
movl $0xa, 0x298(%rsp)
jmp 0x30e38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x35b70
jmp 0x30e6c
movq 0x2f0(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
leaq 0x168(%rsp), %rdx
leaq 0x110(%rsp), %rcx
movsd 0x18a265(%rip), %xmm0   # 0x1bb088
xorl %r8d, %r8d
callq 0x35cd0
jmp 0x30e2d
movl $0x0, 0x298(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0x33500
movl 0x298(%rsp), %eax
testl %eax, %eax
je 0x30e54
jmp 0x30e52
jmp 0x30e56
jmp 0x30e56
movl 0x234(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x234(%rsp)
jmp 0x309f6
leaq 0x1f0(%rsp), %rdi
callq 0x33500
jmp 0x30ebc
jmp 0x30e7d
movl $0x0, 0x298(%rsp)
leaq 0x240(%rsp), %rdi
callq 0x33500
movl 0x298(%rsp), %eax
testl %eax, %eax
je 0x30ea4
jmp 0x30ea2
jmp 0x30ea6
jmp 0x30ea6
movl 0x284(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x284(%rsp)
jmp 0x308cb
leaq 0x240(%rsp), %rdi
callq 0x33500
jmp 0x30f8d
jmp 0x30ed0
movl 0x294(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x294(%rsp)
jmp 0x30866
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x2e8(%rsp), %rdi
callq 0x33610
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x30f6d
movq 0x2e8(%rsp), %rdi
movl 0xe4(%rsp), %esi
callq 0x33630
movq %rax, (%rsp)
jmp 0x30f2d
movq (%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x2f0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq 0x2e0(%rsp), %rdx
callq 0x307c0
jmp 0x30f58
jmp 0x30f5a
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x30ef1
movl $0x0, 0x298(%rsp)
leaq 0x2a8(%rsp), %rdi
callq 0x33500
addq $0x2f8, %rsp            # imm = 0x2F8
retq
leaq 0x2a8(%rsp), %rdi
callq 0x33500
movq 0x2a0(%rsp), %rdi
callq 0x24250
nopw (%rax,%rax)