// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
leaq 0x10(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x23880
xorl %eax, %eax
leaq 0x13e710(%rip), %rcx    # 0x168b00
cmpq $0x80, %rax
je 0x2a403
andq $0x0, (%rax,%rcx)
addq $0x8, %rax
jmp 0x2a3f0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x13e6ec(%rip)    # 0x168b00
movq %rax, 0x13e70d(%rip)    # 0x168b28
movq %rax, 0x13e72e(%rip)    # 0x168b50
movq %rax, 0x13e74f(%rip)    # 0x168b78
leaq 0x70(%rsp), %rsi
movl $0xba7, %edi            # imm = 0xBA7
callq 0x23a60
leaq 0x30(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x23880
movslq 0xcc(%rbx), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0x23040
movq %rax, %r12
movl 0x18(%rsp), %edx
movl 0x1c(%rsp), %ecx
movq %rax, (%rsp)
xorl %r13d, %r13d
xorl %edi, %edi
xorl %esi, %esi
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x233b0
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x9183c
leaq 0x139c29(%rip), %r14    # 0x1640c8
movsd 0xc2bc9(%rip), %xmm4    # 0xed070
movapd 0xc2bd1(%rip), %xmm5    # 0xed080
leaq 0x20(%rsp), %r15
leaq 0x8(%rsp), %rbp
movslq 0xcc(%rbx), %rax
cmpq %rax, %r13
jge 0x2a561
cvtss2sd (%r12,%r13,4), %xmm2
movsd (%r14), %xmm0
movaps %xmm2, %xmm1
addsd %xmm4, %xmm1
ucomisd %xmm2, %xmm0
jae 0x2a559
movapd %xmm1, %xmm3
cmpnlepd %xmm0, %xmm3
xorpd %xmm5, %xmm0
cmpnlepd %xmm1, %xmm0
orpd %xmm3, %xmm0
movd %xmm0, %eax
testb $0x1, %al
je 0x2a559
movl %r13d, %eax
cltd
idivl 0xc8(%rbx)
cvtsi2sd %edx, %xmm0
cvtsi2sd %eax, %xmm1
leaq 0x13e5e8(%rip), %rdi    # 0x168b00
leaq 0x70(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movq %r15, %r8
movq %rbp, %r9
callq 0x24240
movapd 0xc2b46(%rip), %xmm5    # 0xed080
movsd 0xc2b2e(%rip), %xmm4    # 0xed070
movsd 0x8(%rsp), %xmm0
xorpd %xmm5, %xmm0
movq 0xb8(%rbx), %rax
movlpd %xmm0, (%rax,%r13,8)
incq %r13
jmp 0x2a4b9
movq %r12, %rdi
callq 0x23c30
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq