// File: tomfunkhouser[P]gaps/apps/conf2img/conf2img.cpp
// Function: LoadNextCamera(gaps::R3Point&, gaps::R3Vector&, gaps::R3Vector&)
static int
LoadNextCamera(R3Point& viewpoint, R3Vector& towards, R3Vector& up)
{
  // Update next image index
  current_image_index++;

  // Check input cameras
  if (cameras.NEntries() > 0) {
    if (current_image_index < cameras.NEntries()) {
      // Load camera from input file
      R3Camera *camera = cameras.Kth(current_image_index);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      camera->Load();
      viewpoint = camera->Origin();
      towards = camera->Towards();
      up = camera->Up();
      return 1;
    }
  }
  else if (configuration.NImages() > 0) {
    if (current_image_index < configuration.NImages()) {
      // Load camera for image from configuration
      RGBDImage *image = configuration.Image(current_image_index);
      if (image->NPixels(RN_X) == 0) {
        image->ReadChannels();  // Temporary, just to read width/height :(
        image->ReleaseChannels();
      }
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      image->ProjectionMatrix().Load();
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      image->CameraToWorld().InverseMatrix().Load();
      viewpoint = image->WorldViewpoint();
      towards = image->WorldTowards();
      up = image->WorldUp();
      return 1;
    }
  }

  // Return failure - no more cameras
  return 0;
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x108(%rsp)
movq %rsi, 0x100(%rsp)
movq %rdx, 0xf8(%rsp)
movl 0x22138f(%rip), %eax    # 0x24fab4
addl $0x1, %eax
movl %eax, 0x221386(%rip)    # 0x24fab4
leaq 0x2274eb(%rip), %rdi    # 0x255c20
callq 0x30560
cmpl $0x0, %eax
jle 0x2e818
movl 0x22136b(%rip), %eax    # 0x24fab4
movl %eax, 0x4(%rsp)
leaq 0x2274cc(%rip), %rdi    # 0x255c20
callq 0x30560
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x2e813
movl 0x221347(%rip), %esi    # 0x24fab4
leaq 0x2274ac(%rip), %rdi    # 0x255c20
callq 0x30a80
movq %rax, 0xf0(%rsp)
movl $0x1700, %edi           # imm = 0x1700
callq 0x24e60
callq 0x24de0
movq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x4f860
movq 0xf0(%rsp), %rdi
callq 0x30aa0
movq %rax, %rsi
movq 0x108(%rsp), %rdi
callq 0x105f90
movq 0xf0(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
callq 0x30ab0
movq 0x100(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x107720
movq 0xf0(%rsp), %rdi
callq 0x30b00
movq %rax, %rsi
movq 0xf8(%rsp), %rdi
callq 0x107720
movl $0x1, 0x114(%rsp)
jmp 0x2e98e
jmp 0x2e983
leaq 0x227369(%rip), %rdi    # 0x255b88
callq 0x304b0
cmpl $0x0, %eax
jle 0x2e981
movl 0x221281(%rip), %eax    # 0x24fab4
movl %eax, (%rsp)
leaq 0x22734b(%rip), %rdi    # 0x255b88
callq 0x304b0
movl %eax, %ecx
movl (%rsp), %eax
cmpl %ecx, %eax
jge 0x2e97f
movl 0x22125f(%rip), %esi    # 0x24fab4
leaq 0x22732c(%rip), %rdi    # 0x255b88
callq 0x304d0
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
xorl %esi, %esi
callq 0x30500
cmpl $0x0, %eax
jne 0x2e89f
movq 0xd0(%rsp), %rdi
movq (%rdi), %rax
callq *0xf8(%rax)
movq 0xd0(%rsp), %rdi
movq (%rdi), %rax
callq *0x148(%rax)
movl $0x1701, %edi           # imm = 0x1701
callq 0x24e60
callq 0x24de0
movq 0xd0(%rsp), %rsi
leaq 0x50(%rsp), %rdi
movsd 0x1b075d(%rip), %xmm0   # 0x1df020
movsd 0x1b073d(%rip), %xmm1   # 0x1df008
callq 0x40cb0
leaq 0x50(%rsp), %rdi
callq 0x958d0
movl $0x1700, %edi           # imm = 0x1700
callq 0x24e60
callq 0x24de0
movq 0xd0(%rsp), %rdi
callq 0x30870
movq %rax, %rdi
callq 0xfac70
movq %rax, %rdi
callq 0x958d0
movq 0xd0(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x30b20
movq 0x108(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x105f90
movq 0xd0(%rsp), %rsi
leaq 0x20(%rsp), %rdi
callq 0x30b70
movq 0x100(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x107720
movq 0xd0(%rsp), %rsi
leaq 0x8(%rsp), %rdi
callq 0x30bc0
movq 0xf8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x107720
movl $0x1, 0x114(%rsp)
jmp 0x2e98e
jmp 0x2e981
jmp 0x2e983
movl $0x0, 0x114(%rsp)
movl 0x114(%rsp), %eax
addq $0x118, %rsp            # imm = 0x118
retq
nopl (%rax)