// File: tomfunkhouser[P]gaps/apps/confview/confview.cpp
// Function: DrawPatches(int, double, double, double)
static void
DrawPatches(int color_scheme = RGBD_PHOTO_COLOR_SCHEME,
  double patch_width = 100, double patch_height = 100,
  double patch_scale = 2)
  
{
  // Check selected position
  if (selected_position == R3unknown_point) return;

  // Set projection matrix
  glMatrixMode(GL_PROJECTION);  
  glPushMatrix();
  glLoadIdentity();
  gluOrtho2D(0, GLUTwindow_width, 0, GLUTwindow_height);

  // Set model view matrix
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  // Draw images
  int count = 0;
  for (int i = 0; i < configuration.NImages(); i++) {
    RGBDImage *image = configuration.Image(i);
    if (!image->RedChannel()) continue;
    if (!CheckVisibility(image, selected_position)) continue;

    // Get center position
    R2Point center_position;
    if (!image->TransformWorldToImage(selected_position, center_position)) continue;

    // Set patch viewport
    glViewport(count*patch_width, 0, patch_width, patch_height);

    // Draw image
    RNLoadRgb(1, 1, 1);
    glPushMatrix();
    glTranslated(0.5*GLUTwindow_width, 0.5*GLUTwindow_height, 0.0);
    glScaled(patch_scale, patch_scale, patch_scale);
    glTranslated(-center_position.X(), -center_position.Y(), 0.0);
    image->DrawImage(color_scheme, 0);
    glPopMatrix();

    // Draw box
    RNLoadRgb(0, 0, 0);
    R2Box(0, 0, GLUTwindow_width, GLUTwindow_height).Draw();

    // Update counter
    count++;
  }

  // Reset viewport
  glViewport(0, 0, GLUTwindow_width, GLUTwindow_height);
  
  // Reset projection matrix
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();

  // Reset model view matrix
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();  
}

// Assembly:
subq $0xc8, %rsp
movl %edi, 0xc4(%rsp)
movsd %xmm0, 0xb8(%rsp)
movsd %xmm1, 0xb0(%rsp)
movsd %xmm2, 0xa8(%rsp)
leaq 0x212538(%rip), %rdi    # 0x23bc78
leaq 0x215cd1(%rip), %rsi    # 0x23f418
callq 0xed340
cmpl $0x0, %eax
je 0x29756
jmp 0x29a2c
movl $0x1701, %edi           # imm = 0x1701
callq 0x24e80
callq 0x25140
callq 0x24e00
cvtsi2sdl 0x20c36a(%rip), %xmm1 # 0x235adc
cvtsi2sdl 0x20c366(%rip), %xmm3 # 0x235ae0
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
callq 0x24610
movl $0x1700, %edi           # imm = 0x1700
callq 0x24e80
callq 0x25140
callq 0x24e00
movl $0x0, 0xa4(%rsp)
movl $0x0, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0x48(%rsp)
leaq 0x21241f(%rip), %rdi    # 0x23bbe0
callq 0x2ea00
movl %eax, %ecx
movl 0x48(%rsp), %eax
cmpl %ecx, %eax
jge 0x299f9
movl 0xa0(%rsp), %esi
leaq 0x2123fe(%rip), %rdi    # 0x23bbe0
callq 0x2ea20
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0x2ef20
cmpq $0x0, %rax
jne 0x29807
jmp 0x299c9
movq 0x98(%rsp), %rdi
leaq 0x212462(%rip), %rsi    # 0x23bc78
callq 0x2e670
cmpl $0x0, %eax
jne 0x29825
jmp 0x299c9
leaq 0x88(%rsp), %rdi
callq 0x11d5e0
movq 0x98(%rsp), %rdi
movq (%rdi), %rax
leaq 0x212434(%rip), %rsi    # 0x23bc78
leaq 0x88(%rsp), %rdx
callq *0xb0(%rax)
cmpl $0x0, %eax
jne 0x2985c
jmp 0x299c9
cvtsi2sdl 0xa4(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm0
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
cvttsd2si %xmm0, %edx
cvttsd2si 0xb0(%rsp), %ecx
xorl %esi, %esi
movl %esi, 0x34(%rsp)
callq 0x24240
movl $0x1, %edx
movl %edx, %edi
movl %edx, %esi
callq 0x2efd0
callq 0x25140
cvtsi2sdl 0x20c233(%rip), %xmm0 # 0x235adc
movsd 0x19d77f(%rip), %xmm2   # 0x1c7030
mulsd %xmm2, %xmm0
cvtsi2sdl 0x20c223(%rip), %xmm1 # 0x235ae0
mulsd %xmm2, %xmm1
xorps %xmm2, %xmm2
movsd %xmm2, 0x38(%rsp)
callq 0x24ac0
movsd 0xa8(%rsp), %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x24ab0
leaq 0x88(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x2ed30
movq 0x8(%rsp), %rdi
movapd 0x19d79e(%rip), %xmm1   # 0x1c70a0
movaps %xmm1, 0x10(%rsp)
pxor %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x2ed40
movaps 0x10(%rsp), %xmm3
movsd 0x38(%rsp), %xmm2
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
pxor %xmm3, %xmm1
callq 0x24ac0
movsd 0x38(%rsp), %xmm0
movq 0x98(%rsp), %rdi
movl 0xc4(%rsp), %esi
movq (%rdi), %rax
movq 0xd0(%rax), %rax
callq *%rax
callq 0x253c0
movl 0x34(%rsp), %edx
movl %edx, %edi
movl %edx, %esi
callq 0x2efd0
movsd 0x38(%rsp), %xmm1
cvtsi2sdl 0x20c168(%rip), %xmm2 # 0x235adc
cvtsi2sdl 0x20c164(%rip), %xmm3 # 0x235ae0
leaq 0x60(%rsp), %rdi
movq %rdi, 0x40(%rsp)
movaps %xmm1, %xmm0
callq 0x117b90
movq 0x40(%rsp), %rdi
leaq 0x216046(%rip), %rax    # 0x23f9e0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rsi
callq 0xf8af0
jmp 0x299ae
leaq 0x60(%rsp), %rdi
callq 0x2f000
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x297af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x2f000
jmp 0x29a34
movl 0x20c0dd(%rip), %edx    # 0x235adc
movl 0x20c0db(%rip), %ecx    # 0x235ae0
xorl %esi, %esi
movl %esi, %edi
callq 0x24240
movl $0x1701, %edi           # imm = 0x1701
callq 0x24e80
callq 0x253c0
movl $0x1700, %edi           # imm = 0x1700
callq 0x24e80
callq 0x253c0
addq $0xc8, %rsp
retq
movq 0x50(%rsp), %rdi
callq 0x252b0
nop