// File: tomfunkhouser[P]gaps/pkgs/RGBD/RGBDImage.cpp
// Function: gaps::RGBDImage::DrawImage(int, double) const
void RGBDImage::
DrawImage(int color_scheme, RNLength depth) const
{
  // Check image
  if ((width == 0) || (height == 0)) return;

  // Update/select opengl texture
  if (opengl_texture_id <= 0) ((RGBDImage *) this)->UpdateOpenGL();
  if (opengl_texture_id <= 0) return;
  glBindTexture(GL_TEXTURE_2D, opengl_texture_id);
  glEnable(GL_TEXTURE_2D);

  // Set color
  glDisable(GL_LIGHTING);
  RNLoadRgb(1.0, 1.0, 1.0);

  // Check depth
  if (depth > 0) {
    // Draw textured polygon on view plane
    R3Point viewpoint = WorldViewpoint();
    R3Vector towards = WorldTowards();
    R3Vector right = WorldRight();
    R3Vector up = WorldUp();
    R3Point c = viewpoint + depth * towards;
    R3Vector dx = (depth * 0.5*width / intrinsics[0][0]) * right;
    R3Vector dy = (depth * 0.5*height / intrinsics[1][1]) * up;
    c -= dx*(intrinsics[0][2] - 0.5*width)/width;
    c -= dy*(intrinsics[1][2] - 0.5*height)/height;
    RNGrfxBegin(RN_GRFX_QUADS);
    R3LoadTextureCoords(0, 0);
    R3LoadPoint(c - dx - dy);
    R3LoadTextureCoords(1, 0);
    R3LoadPoint(c + dx - dy);
    R3LoadTextureCoords(1, 1);
    R3LoadPoint(c + dx + dy);
    R3LoadTextureCoords(0, 1);
    R3LoadPoint(c - dx + dy);
    RNGrfxEnd();
  }
  else {
    // Draw textured polygon on screen plane (in pixels)
    RNGrfxBegin(RN_GRFX_QUADS);
    R3LoadTextureCoords(0, 0);
    R3LoadPoint(0, 0, 0);
    R3LoadTextureCoords(1, 0);
    R3LoadPoint(width-1, 0, 0);
    R3LoadTextureCoords(1, 1);
    R3LoadPoint(width-1, height-1, 0);
    R3LoadTextureCoords(0, 1);
    R3LoadPoint(0, height-1, 0);
    RNGrfxEnd();
  }
  
  // Unselect opengl texture
  glDisable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, 0);
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x210(%rsp)
movl %esi, 0x20c(%rsp)
movsd %xmm0, 0x200(%rsp)
movq 0x210(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x28(%rax)
je 0x2b0bd
movq 0x18(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jne 0x2b0c2
jmp 0x2b54f
movq 0x18(%rsp), %rax
cmpl $0x0, 0x1c8(%rax)
jg 0x2b0de
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
callq *0x1d0(%rax)
movq 0x18(%rsp), %rax
cmpl $0x0, 0x1c8(%rax)
jg 0x2b0f1
jmp 0x2b54f
movq 0x18(%rsp), %rax
movl 0x1c8(%rax), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x128c0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x12640
movl $0xb50, %edi            # imm = 0xB50
callq 0x121c0
movsd 0x13af0e(%rip), %xmm2   # 0x166030
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x27f20
movsd 0x200(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2b4b2
movq 0x18(%rsp), %rsi
leaq 0x1e8(%rsp), %rdi
callq 0x19db0
movq 0x18(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x1a320
movq 0x18(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x303e0
movq 0x18(%rsp), %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x22a50
movsd 0x200(%rsp), %xmm0
leaq 0x170(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x227f0
leaq 0x188(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x170(%rsp), %rdx
callq 0x8af60
movq 0x18(%rsp), %rdi
movsd 0x13ae55(%rip), %xmm0   # 0x166028
mulsd 0x200(%rsp), %xmm0
cvtsi2sdl 0x28(%rdi), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
addq $0x140, %rdi            # imm = 0x140
xorl %esi, %esi
callq 0x238a0
movsd 0x8(%rsp), %xmm0
divsd (%rax), %xmm0
leaq 0x158(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x227f0
movq 0x18(%rsp), %rdi
movsd 0x13ae03(%rip), %xmm0   # 0x166028
mulsd 0x200(%rsp), %xmm0
cvtsi2sdl 0x2c(%rdi), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
addq $0x140, %rdi            # imm = 0x140
movl $0x1, %esi
callq 0x238a0
movsd 0x10(%rsp), %xmm0
divsd 0x8(%rax), %xmm0
leaq 0x140(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x227f0
movq 0x18(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
xorl %esi, %esi
callq 0x238a0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movsd 0x10(%rcx), %xmm1
cvtsi2sdl 0x28(%rax), %xmm0
movsd 0x13b2c5(%rip), %xmm2   # 0x166560
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
leaq 0x110(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x8c9d0
movq 0x18(%rsp), %rax
cvtsi2sdl 0x28(%rax), %xmm0
leaq 0x128(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x8d040
leaq 0x188(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x8b6e0
movq 0x18(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movl $0x1, %esi
callq 0x238a0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movsd 0x10(%rcx), %xmm1
cvtsi2sdl 0x2c(%rax), %xmm0
movsd 0x13b244(%rip), %xmm2   # 0x166560
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
leaq 0xe0(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x8c9d0
movq 0x18(%rsp), %rax
cvtsi2sdl 0x2c(%rax), %xmm0
leaq 0xf8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x8d040
leaq 0x188(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x8b6e0
movl $0x7, %edi
callq 0x27c00
xorl %esi, %esi
movl %esi, %edi
callq 0x305f0
leaq 0xb0(%rsp), %rdi
leaq 0x188(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0x8b900
leaq 0xc8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x8b900
leaq 0xc8(%rsp), %rdi
callq 0x27c90
movl $0x1, %edi
xorl %esi, %esi
callq 0x305f0
leaq 0x80(%rsp), %rdi
leaq 0x188(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0x8af60
leaq 0x98(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x8b900
leaq 0x98(%rsp), %rdi
callq 0x27c90
movl $0x1, %esi
movl %esi, %edi
callq 0x305f0
leaq 0x50(%rsp), %rdi
leaq 0x188(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0x8af60
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x8af60
leaq 0x68(%rsp), %rdi
callq 0x27c90
xorl %edi, %edi
movl $0x1, %esi
callq 0x305f0
leaq 0x20(%rsp), %rdi
leaq 0x188(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0x8b900
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x8af60
leaq 0x38(%rsp), %rdi
callq 0x27c90
callq 0x27cd0
jmp 0x2b539
movl $0x7, %edi
callq 0x27c00
xorl %esi, %esi
movl %esi, %edi
callq 0x305f0
xorl %edx, %edx
movl %edx, %edi
movl %edx, %esi
callq 0x30610
movl $0x1, %edi
xorl %esi, %esi
callq 0x305f0
movq 0x18(%rsp), %rax
movl 0x28(%rax), %edi
subl $0x1, %edi
xorl %edx, %edx
movl %edx, %esi
callq 0x30610
movl $0x1, %esi
movl %esi, %edi
callq 0x305f0
movq 0x18(%rsp), %rax
movl 0x28(%rax), %edi
subl $0x1, %edi
movl 0x2c(%rax), %esi
subl $0x1, %esi
xorl %edx, %edx
callq 0x30610
xorl %edi, %edi
movl $0x1, %esi
callq 0x305f0
movq 0x18(%rsp), %rax
movl 0x2c(%rax), %esi
subl $0x1, %esi
xorl %edx, %edx
movl %edx, %edi
callq 0x30610
callq 0x27cd0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x121c0
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
callq 0x128c0
addq $0x218, %rsp            # imm = 0x218
retq
nopw (%rax,%rax)