// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::DrawImagePlane() const
void R3SurfelViewer::
DrawImagePlane(void) const
{
  // Get/check stuff
  if (!selected_image) return;
  if (selected_image->ImageWidth() <= 0) return;
  if (selected_image->ImageHeight() <= 0) return;
  if (!image_plane_visibility) return;

#if (RN_OS != RN_MAC)
  // For some unknown reason, drawing a textured quad hangs the GPU for some images
  // (very rarely, but it is repeatable). 

  // Get convenient variables
  RNLength depth = image_plane_depth;
  R3SurfelImage *image = selected_image;
  R3Point c = image->Viewpoint() + depth * image->Towards();
  c -= ((image->ImageCenter().X() - 0.5*image->ImageWidth()) / (0.5*image->ImageWidth())) * image->Right() * depth;
  c -= ((image->ImageCenter().Y() - 0.5*image->ImageHeight()) / (0.5*image->ImageHeight())) * image->Up() * depth;
  R3Vector dx = depth * tan(image->XFOV()) * image->Right();
  R3Vector dy = depth * tan(image->YFOV()) * image->Up();

  // Update selected image texture
  ((R3SurfelViewer *) this)->UpdateSelectedImageTexture();

  // Draw image as textured quad
  if (selected_image_texture_id > 0) {
    // Bind texture
    glBindTexture(GL_TEXTURE_2D, selected_image_texture_id);
    glEnable(GL_TEXTURE_2D);

    // Draw quad
    glDisable(GL_LIGHTING);
    RNLoadRgb(1.0, 1.0, 1.0);
    RNGrfxBegin(RN_GRFX_QUADS);
    R3LoadTextureCoords(0.0, 0.0);
    R3LoadPoint(c - dx - dy);
    R3LoadTextureCoords(1.0, 0.0);
    R3LoadPoint(c + dx - dy);
    R3LoadTextureCoords(1.0, 1.0);
    R3LoadPoint(c + dx + dy);
    R3LoadTextureCoords(0.0, 1.0);
    R3LoadPoint(c - dx + dy);
    RNGrfxEnd();

    // Unbind texture
    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);
  }
#endif
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x210(%rsp)
movq 0x210(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, 0x2d0(%rax)
jne 0x3570b
jmp 0x35bf8
movq 0x60(%rsp), %rax
movq 0x2d0(%rax), %rdi
callq 0x3e330
cmpl $0x0, %eax
jg 0x35726
jmp 0x35bf8
movq 0x60(%rsp), %rax
movq 0x2d0(%rax), %rdi
callq 0x3e350
cmpl $0x0, %eax
jg 0x35741
jmp 0x35bf8
movq 0x60(%rsp), %rax
cmpl $0x0, 0x328(%rax)
jne 0x35754
jmp 0x35bf8
movq 0x60(%rsp), %rax
movsd 0x2e0(%rax), %xmm0
movsd %xmm0, 0x208(%rsp)
movq 0x2d0(%rax), %rax
movq %rax, 0x200(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e430
movq %rax, 0x8(%rsp)
movsd 0x208(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x200(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x3e450
movsd (%rsp), %xmm0
leaq 0x1d0(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x2f4b0
movq 0x8(%rsp), %rsi
leaq 0x1e8(%rsp), %rdi
leaq 0x1d0(%rsp), %rdx
callq 0x162880
movq 0x200(%rsp), %rdi
callq 0x3e4a0
movq %rax, %rdi
callq 0x3dc80
movsd %xmm0, 0x10(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e330
movsd 0x10(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x20bcf4(%rip), %xmm2   # 0x241510
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e330
movsd 0x18(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
movsd 0x20b7ef(%rip), %xmm1   # 0x241038
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e4c0
movsd 0x20(%rsp), %xmm0
movq %rax, %rsi
leaq 0x188(%rsp), %rdi
callq 0x2f4b0
movsd 0x208(%rsp), %xmm0
leaq 0x1a0(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x1642f0
leaq 0x1e8(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x163000
movq 0x200(%rsp), %rdi
callq 0x3e4a0
movq %rax, %rdi
callq 0x3dcb0
movsd %xmm0, 0x28(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e350
movsd 0x28(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x20bc29(%rip), %xmm2   # 0x241510
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e350
movsd 0x30(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
movsd 0x20b724(%rip), %xmm1   # 0x241038
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e4e0
movsd 0x38(%rsp), %xmm0
movq %rax, %rsi
leaq 0x158(%rsp), %rdi
callq 0x2f4b0
movsd 0x208(%rsp), %xmm0
leaq 0x170(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x1642f0
leaq 0x1e8(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x163000
movsd 0x208(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e510
callq 0x28540
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e4c0
movsd 0x48(%rsp), %xmm0
movq %rax, %rsi
leaq 0x140(%rsp), %rdi
callq 0x2f4b0
movsd 0x208(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e570
callq 0x28540
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x200(%rsp), %rdi
callq 0x3e4e0
movsd 0x58(%rsp), %xmm0
movq %rax, %rsi
leaq 0x128(%rsp), %rdi
callq 0x2f4b0
movq 0x60(%rsp), %rdi
movq (%rdi), %rax
callq *0x260(%rax)
movq 0x60(%rsp), %rax
cmpl $0x0, 0x5e8(%rax)
jbe 0x35bf8
movq 0x60(%rsp), %rax
movl 0x5e8(%rax), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x28d30
movl $0xde1, %edi            # imm = 0xDE1
callq 0x28960
movl $0xb50, %edi            # imm = 0xB50
callq 0x28240
movsd 0x20b599(%rip), %xmm2   # 0x241010
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x2f2e0
movl $0x7, %edi
callq 0x3e030
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x3e3e0
leaq 0xf8(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x163220
leaq 0x110(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x163220
leaq 0x110(%rsp), %rdi
callq 0x3e310
movsd 0x20b52a(%rip), %xmm0   # 0x241010
xorps %xmm1, %xmm1
callq 0x3e3e0
leaq 0xc8(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x162880
leaq 0xe0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x163220
leaq 0xe0(%rsp), %rdi
callq 0x3e310
movsd 0x20b4d3(%rip), %xmm1   # 0x241010
movaps %xmm1, %xmm0
callq 0x3e3e0
leaq 0x98(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x162880
leaq 0xb0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x162880
leaq 0xb0(%rsp), %rdi
callq 0x3e310
xorps %xmm0, %xmm0
movsd 0x20b479(%rip), %xmm1   # 0x241010
callq 0x3e3e0
leaq 0x68(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x163220
leaq 0x80(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x162880
leaq 0x80(%rsp), %rdi
callq 0x3e310
callq 0x3e090
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
callq 0x28d30
movl $0xde1, %edi            # imm = 0xDE1
callq 0x28240
addq $0x218, %rsp            # imm = 0x218
retq