// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::LoadLights(gaps::R3Box const&, int, int) const
int R3Scene::
LoadLights(const R3Box& world_bbox, int min_index, int max_index) const
{
  // Determine the maximum number of lights
  if (NLights() == 0) return 1;
  int max_lights = max_index - min_index + 1;
  if (max_lights == 0) return 1;

  // Allocate scores for lights
  RNScalar *scores = new RNScalar [ NLights() ];
  for (int i = 0; i < NLights(); i++) scores[i] = 0;
  
  // Score lights
  for (int i = 0; i < NLights(); i++) {
    R3Light *light = Light(i);
    R3Sphere sphere = light->SphereOfInfluence(1E-3);
    if (RNIsZero(sphere.Radius())) continue; 
    RNScalar d = R3Distance(sphere.Centroid(), world_bbox);
    if (RNIsZero(d)) d = RN_EPSILON;
    RNScalar score = sphere.Radius() / d;
    if (score < 1.0) continue; 
    scores[i] = score;
  }

  // Sort lights based on score
  RNArray<R3Light *> sorted_lights = lights;
  for (int i = 0; i < sorted_lights.NEntries(); i++) {
    for (int j = i+1; j < sorted_lights.NEntries(); j++) {
      if (scores[j] < scores[i]) {
        sorted_lights.Swap(i, j);
        RNScalar swap = scores[i];
        scores[i] = scores[j];
        scores[j] = swap;
      }    
    }
  }

  // Load top lights
  sorted_lights.Truncate(max_lights);
  for (int i = 0; i < sorted_lights.NEntries(); i++) {
    R3Light *light = sorted_lights.Kth(i);
    light->Draw(min_index + i);
  }

  // Delete scores
  delete [] scores;

  // Return success
  return 1;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x128(%rsp)
movq %rsi, 0x120(%rsp)
movl %edx, 0x11c(%rsp)
movl %ecx, 0x118(%rsp)
movq 0x128(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x2b9f0
cmpl $0x0, %eax
jne 0x2df8c
movl $0x1, 0x134(%rsp)
jmp 0x2e4b3
movl 0x118(%rsp), %eax
subl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
cmpl $0x0, 0x114(%rsp)
jne 0x2dfbe
movl $0x1, 0x134(%rsp)
jmp 0x2e4b3
movq 0x60(%rsp), %rdi
callq 0x2b9f0
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x22040
movq %rax, 0x108(%rsp)
movl $0x0, 0x104(%rsp)
movq 0x60(%rsp), %rdi
movl 0x104(%rsp), %eax
movl %eax, 0x5c(%rsp)
callq 0x2b9f0
movl %eax, %ecx
movl 0x5c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2e048
movq 0x108(%rsp), %rax
movslq 0x104(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x2dffe
movl $0x0, 0x100(%rsp)
movq 0x60(%rsp), %rdi
movl 0x100(%rsp), %eax
movl %eax, 0x58(%rsp)
callq 0x2b9f0
movl %eax, %ecx
movl 0x58(%rsp), %eax
cmpl %ecx, %eax
jge 0x2e24b
movq 0x60(%rsp), %rdi
movl 0x100(%rsp), %esi
callq 0x2ba10
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rsi
movq (%rsi), %rax
movq 0x50(%rax), %rax
leaq 0xd0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
movsd 0x15ffad(%rip), %xmm0   # 0x18e060
callq *%rax
movq 0x48(%rsp), %rdi
callq 0x487f0
movsd %xmm0, 0x50(%rsp)
jmp 0x2e0c7
movsd 0x50(%rsp), %xmm0
leaq 0x1c81d4(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x48790
movl %eax, 0x44(%rsp)
jmp 0x2e0e3
movl 0x44(%rsp), %eax
cmpl $0x0, %eax
je 0x2e122
movl $0x7, 0xc0(%rsp)
jmp 0x2e217
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x2ba70
jmp 0x2e4c2
leaq 0xa0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0xb2d50
jmp 0x2e139
movq 0x120(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x77e60
movsd %xmm0, 0x38(%rsp)
jmp 0x2e156
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
leaq 0x1c8133(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x48790
movl %eax, 0x34(%rsp)
jmp 0x2e184
movl 0x34(%rsp), %eax
cmpl $0x0, %eax
je 0x2e1a1
leaq 0x1c8114(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm0
movsd %xmm0, 0xb8(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x487f0
movsd %xmm0, 0x28(%rsp)
jmp 0x2e1b6
movsd 0x28(%rsp), %xmm0
divsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x15fe3a(%rip), %xmm0   # 0x18e010
ucomisd 0x98(%rsp), %xmm0
jbe 0x2e1ee
movl $0x7, 0xc0(%rsp)
jmp 0x2e217
movsd 0x98(%rsp), %xmm0
movq 0x108(%rsp), %rax
movslq 0x100(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, 0xc0(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x2ba70
movl 0xc0(%rsp), %eax
testl %eax, %eax
je 0x2e233
jmp 0x2e231
jmp 0x2e235
jmp 0x2e235
movl 0x100(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x100(%rsp)
jmp 0x2e053
movq 0x60(%rsp), %rsi
addq $0x18, %rsi
leaq 0x88(%rsp), %rdi
callq 0x48800
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x20(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x2b8b0
movl %eax, 0x24(%rsp)
jmp 0x2e28a
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2e3e2
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x2b8b0
movl %eax, 0x1c(%rsp)
jmp 0x2e2c9
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2e3ca
movq 0x108(%rsp), %rax
movslq 0x80(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x108(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2e3b2
movl 0x84(%rsp), %esi
movl 0x80(%rsp), %edx
leaq 0x88(%rsp), %rdi
callq 0xefd30
jmp 0x2e32a
movq 0x108(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0x108(%rsp), %rax
movslq 0x80(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x108(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x78(%rsp), %xmm0
movq 0x108(%rsp), %rax
movslq 0x80(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x2e3b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x48200
jmp 0x2e4c2
jmp 0x2e3b4
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x2e2ab
jmp 0x2e3cc
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x2e26c
movl 0x114(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0xefc10
jmp 0x2e3f8
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x10(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x2b8b0
movl %eax, 0x14(%rsp)
jmp 0x2e41b
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2e475
movl 0x74(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0x2baa0
movq %rax, 0x8(%rsp)
jmp 0x2e43f
movq 0x8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movl 0x11c(%rsp), %esi
movl 0x74(%rsp), %eax
addl %eax, %esi
movq (%rdi), %rax
movq 0x70(%rax), %rax
callq *%rax
jmp 0x2e466
jmp 0x2e468
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x2e400
movq 0x108(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x2e490
movq (%rsp), %rdi
callq 0x22da0
movl $0x1, 0x134(%rsp)
movl $0x1, 0xc0(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x48200
movl 0x134(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
retq
movq 0xc8(%rsp), %rdi
callq 0x23290
nop