// File: tomfunkhouser[P]gaps/apps/scnview/scnview.cpp
// Function: DrawRays(gaps::R3Scene*)
static void 
DrawRays(R3Scene *scene)
{
  // Ray intersection variables
  R3SceneNode *node;
  R3Material *material;
  R3Shape *shape;
  R3Point point;
  R3Vector normal;
  RNScalar t;

  // Ray generation variables
  int istep = scene->Viewport().Width() / 10;
  int jstep = scene->Viewport().Height() / 10;
  if (istep == 0) istep = 1;
  if (jstep == 0) jstep = 1;

  // Draw intersection point and normal for some rays
  for (int i = istep/2; i < scene->Viewport().Width(); i += istep) {
    for (int j = jstep/2; j < scene->Viewport().Height(); j += jstep) {
      // R3Ray ray = scene->Viewer().WorldRay(i, j);
      R3Ray ray = viewer->WorldRay(i, j);
      if (scene->Intersects(ray, &node, &material, &shape, &point, &normal, &t)) {
        // Get intersection information
        const R3Brdf *brdf = (material) ? material->Brdf() : NULL;

        // Compute color
        RNRgb color = scene->Ambient();
        if (brdf) {
          color += brdf->Emission();
          for (int k = 0; k < scene->NLights(); k++) {
            R3Light *light = scene->Light(k);
            const R3Point& eye = viewer->Camera().Origin();
            color += light->Reflection(*brdf, eye, point, normal);
          }
        }

        // Draw intersection
        RNLoadRgb(color);
        double radius = 0.01 * t;
        R3Sphere(point, radius).Draw();
        R3Span(point, point + 2 * radius * normal).Draw();
      }
    }
  }
}

// Assembly:
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, 0x1d0(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0xcc860
leaq 0x188(%rsp), %rdi
callq 0xcd9c0
movq 0x1d0(%rsp), %rdi
callq 0x2be30
movq %rax, %rdi
callq 0x2be50
movl $0xa, %ecx
cltd
idivl %ecx
movl %eax, 0x17c(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x2be30
movq %rax, %rdi
callq 0x2be60
movl $0xa, %ecx
cltd
idivl %ecx
movl %eax, 0x178(%rsp)
cmpl $0x0, 0x17c(%rsp)
jne 0x28076
movl $0x1, 0x17c(%rsp)
cmpl $0x0, 0x178(%rsp)
jne 0x2808b
movl $0x1, 0x178(%rsp)
movl 0x17c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x174(%rsp)
movl 0x174(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x2be30
movq %rax, %rdi
callq 0x2be50
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x28421
movl 0x178(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x170(%rsp)
movl 0x170(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x2be30
movq %rax, %rdi
callq 0x2be60
movl %eax, %ecx
movl 0x20(%rsp), %eax
cmpl %ecx, %eax
jge 0x28405
movq 0x1d3b0e(%rip), %rsi    # 0x1fbc28
cvtsi2sdl 0x174(%rsp), %xmm0
cvtsi2sdl 0x170(%rsp), %xmm1
leaq 0x140(%rsp), %rdi
callq 0x5bca0
movq 0x1d0(%rsp), %rdi
leaq 0x1ce170(%rip), %rax    # 0x1f62b8
movsd (%rax), %xmm1
leaq 0x140(%rsp), %rsi
leaq 0x1c8(%rsp), %rdx
leaq 0x1c0(%rsp), %rcx
leaq 0x1b8(%rsp), %r8
leaq 0x1a0(%rsp), %r9
leaq 0x188(%rsp), %r10
leaq 0x180(%rsp), %rax
xorps %xmm0, %xmm0
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x2dd90
cmpl $0x0, %eax
je 0x283e9
cmpq $0x0, 0x1c0(%rsp)
je 0x281bd
movq 0x1c0(%rsp), %rdi
callq 0x2be70
movq %rax, 0x18(%rsp)
jmp 0x281c6
xorl %eax, %eax
movq %rax, 0x18(%rsp)
jmp 0x281c6
movq 0x18(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x2be80
movq %rax, %rsi
leaq 0x120(%rsp), %rdi
callq 0xef300
cmpq $0x0, 0x138(%rsp)
je 0x282e8
movq 0x138(%rsp), %rdi
callq 0x2bea0
movq %rax, %rsi
leaq 0x120(%rsp), %rdi
callq 0xef500
movl $0x0, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x2b9f0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x282e6
movq 0x1d0(%rsp), %rdi
movl 0x11c(%rsp), %esi
callq 0x2ba10
movq %rax, 0x110(%rsp)
movq 0x1d39b8(%rip), %rdi    # 0x1fbc28
callq 0x2b4d0
movq %rax, %rdi
callq 0x2b900
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rsi
movq 0x138(%rsp), %rdx
movq 0x108(%rsp), %rcx
movq (%rsi), %rax
leaq 0xf0(%rsp), %rdi
leaq 0x1a0(%rsp), %r8
leaq 0x188(%rsp), %r9
callq *0x58(%rax)
leaq 0x120(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0xef500
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x28227
jmp 0x282e8
leaq 0x120(%rsp), %rdi
callq 0x2b540
movsd 0x180(%rsp), %xmm0
movsd 0x165d32(%rip), %xmm1   # 0x18e038
mulsd %xmm1, %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd 0xe8(%rsp), %xmm0
leaq 0xc0(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0xb2bd0
leaq 0xb8(%rsp), %rdi
movl $0x772, %esi            # imm = 0x772
callq 0x2b520
jmp 0x28345
movq 0xb8(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x68e80
jmp 0x2835c
leaq 0xc0(%rsp), %rdi
callq 0x2ba70
movsd 0x165cdf(%rip), %xmm0   # 0x18e050
mulsd 0xe8(%rsp), %xmm0
leaq 0x28(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x2b630
leaq 0x40(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0xcce30
leaq 0x58(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0xcb2a0
leaq 0x58(%rsp), %rdi
callq 0x668e0
jmp 0x283e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x2ba70
jmp 0x28429
jmp 0x283eb
movl 0x178(%rsp), %eax
addl 0x170(%rsp), %eax
movl %eax, 0x170(%rsp)
jmp 0x280e5
jmp 0x28407
movl 0x17c(%rsp), %eax
addl 0x174(%rsp), %eax
movl %eax, 0x174(%rsp)
jmp 0x280a1
addq $0x1d8, %rsp            # imm = 0x1D8
retq
movq 0xb0(%rsp), %rdi
callq 0x23290
nopw %cs:(%rax,%rax)