// File: tomfunkhouser[P]gaps/apps/conf2img/conf2img.cpp
// Function: CaptureDepth(gaps::R2Grid&)
static int 
CaptureDepth(R2Grid& image)
{
  // Get viewport dimensions
  GLint viewport[4];
  glGetIntegerv(GL_VIEWPORT, viewport);

  // Get modelview  matrix
  static GLdouble modelview_matrix[16];
  // glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
  for (int i = 0; i < 16; i++) modelview_matrix[i] = 0;
  modelview_matrix[0] = 1.0;
  modelview_matrix[5] = 1.0;
  modelview_matrix[10] = 1.0;
  modelview_matrix[15] = 1.0;
  
  // Get projection matrix
  GLdouble projection_matrix[16];
  glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix);

  // Get viewpoint matrix
  GLint viewport_matrix[16];
  glGetIntegerv(GL_VIEWPORT, viewport_matrix);

  // Allocate pixels
  float *pixels = new float [ image.NEntries() ];

  // Read pixels from frame buffer 
  glReadPixels(0, 0, viewport[2], viewport[3], GL_DEPTH_COMPONENT, GL_FLOAT, pixels); 

  // Clear image
  image.Clear(0.0);
  
  // Convert pixels to depths
  int ix, iy;
  double x, y, z;
  for (int i = 0; i < image.NEntries(); i++) {
    if (RNIsEqual(pixels[i], 1.0)) continue;
    if (RNIsNegativeOrZero(pixels[i])) continue;
    image.IndexToIndices(i, ix, iy);
    gluUnProject(ix, iy, pixels[i], modelview_matrix, projection_matrix, viewport_matrix, &x, &y, &z);
    image.SetGridValue(i, -z);
  }

  // Delete pixels
  delete [] pixels;

  // Return success
  return 1;
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x120(%rsp)
leaq 0x110(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x24990
movl $0x0, 0x10c(%rsp)
cmpl $0x10, 0x10c(%rsp)
jge 0x2f7e0
movslq 0x10c(%rsp), %rcx
leaq 0x22665b(%rip), %rax    # 0x255e20
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x2f7ac
movsd 0x1af840(%rip), %xmm0   # 0x1df028
movsd %xmm0, 0x226630(%rip)   # 0x255e20
movsd 0x1af830(%rip), %xmm0   # 0x1df028
movsd %xmm0, 0x226648(%rip)   # 0x255e48
movsd 0x1af820(%rip), %xmm0   # 0x1df028
movsd %xmm0, 0x226660(%rip)   # 0x255e70
movsd 0x1af810(%rip), %xmm0   # 0x1df028
movsd %xmm0, 0x226678(%rip)   # 0x255e98
leaq 0x80(%rsp), %rsi
movl $0xba7, %edi            # imm = 0xBA7
callq 0x24bb0
leaq 0x40(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x24990
movq 0x120(%rsp), %rdi
callq 0x30fd0
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x24040
movq %rax, 0x38(%rsp)
movl 0x118(%rsp), %edx
movl 0x11c(%rsp), %ecx
movq 0x38(%rsp), %rax
xorl %esi, %esi
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
movl %esi, %edi
movq %rax, (%rsp)
callq 0x24420
movq 0x120(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x11aed0
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x120(%rsp), %rdi
callq 0x30fd0
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f9df
movq 0x38(%rsp), %rax
movslq 0x14(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x220981(%rip), %rax    # 0x250278
movsd (%rax), %xmm2
movsd 0x1af725(%rip), %xmm1   # 0x1df028
callq 0x30ff0
cmpl $0x0, %eax
je 0x2f912
jmp 0x2f9cf
movq 0x38(%rsp), %rax
movslq 0x14(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x22094c(%rip), %rax    # 0x250278
movsd (%rax), %xmm1
callq 0x31020
cmpl $0x0, %eax
je 0x2f93f
jmp 0x2f9cf
movq 0x120(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x31050
cvtsi2sdl 0x34(%rsp), %xmm0
cvtsi2sdl 0x30(%rsp), %xmm1
movq 0x38(%rsp), %rax
movslq 0x14(%rsp), %rcx
movss (%rax,%rcx,4), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0x80(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x226493(%rip), %rdi    # 0x255e20
leaq 0x28(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0x25400
movq 0x120(%rsp), %rdi
movl 0x14(%rsp), %esi
movsd 0x18(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x309f0
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x2f8ba
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x2f9f9
movq 0x8(%rsp), %rdi
callq 0x24db0
movl $0x1, %eax
addq $0x128, %rsp            # imm = 0x128
retq
nopw %cs:(%rax,%rax)