// File: /workspace/github_cpp_makefile_star2_O3/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 0x20(%rsp), %r15
movl $0xba2, %edi            # imm = 0xBA2
movq %r15, %rsi
callq 0x23880
xorpd %xmm0, %xmm0
movapd %xmm0, 0x14e861(%rip)   # 0x179ae0
movapd %xmm0, 0x14e879(%rip)   # 0x179b00
movapd %xmm0, 0x14e8a1(%rip)   # 0x179b30
movapd %xmm0, 0x14e8b9(%rip)   # 0x179b50
movapd %xmm0, 0x14e8a1(%rip)   # 0x179b40
movapd %xmm0, 0x14e879(%rip)   # 0x179b20
movapd %xmm0, 0x14e861(%rip)   # 0x179b10
movapd %xmm0, 0x14e839(%rip)   # 0x179af0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x14e818(%rip)    # 0x179ae0
movq %rax, 0x14e839(%rip)    # 0x179b08
movq %rax, 0x14e85a(%rip)    # 0x179b30
movq %rax, 0x14e87b(%rip)    # 0x179b58
leaq 0x70(%rsp), %rsi
movl $0xba7, %edi            # imm = 0xBA7
callq 0x23a50
leaq 0x30(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x23880
movslq 0xcc(%rbx), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x23040
movq %rax, %r14
movl 0x8(%r15), %edx
movl 0xc(%r15), %ecx
movq %rax, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x233a0
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x9e73c
movl 0xcc(%rbx), %eax
testl %eax, %eax
jle 0x2b43f
leaq 0x149d46(%rip), %rcx    # 0x1750a8
movsd (%rcx), %xmm0
xorl %r15d, %r15d
movsd 0xd9cff(%rip), %xmm4    # 0x105070
movapd 0xd9d07(%rip), %xmm5    # 0x105080
leaq 0x18(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0x8(%rsp), %rbp
xorps %xmm2, %xmm2
cvtss2sd (%r14,%r15,4), %xmm2
movaps %xmm2, %xmm1
addsd %xmm4, %xmm1
ucomisd %xmm2, %xmm0
jae 0x2b430
movapd %xmm0, %xmm3
xorpd %xmm5, %xmm3
cmpnlepd %xmm1, %xmm3
cmpnlepd %xmm0, %xmm1
orpd %xmm3, %xmm1
movd %xmm1, %ecx
testb $0x1, %cl
je 0x2b430
movl %r15d, %eax
cltd
idivl 0xc8(%rbx)
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
leaq 0x14e700(%rip), %rdi    # 0x179ae0
leaq 0x70(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r12, %rcx
movq %r13, %r8
movq %rbp, %r9
callq 0x24220
movapd 0xd9c80(%rip), %xmm5    # 0x105080
movsd 0xd9c68(%rip), %xmm4    # 0x105070
movsd 0x8(%rsp), %xmm0
xorpd %xmm5, %xmm0
movq 0xb8(%rbx), %rax
movlpd %xmm0, (%rax,%r15,8)
leaq 0x149c82(%rip), %rax    # 0x1750a8
movsd (%rax), %xmm0
movl 0xcc(%rbx), %eax
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x2b388
movq %r14, %rdi
callq 0x23c30
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq