// File: tomfunkhouser[P]gaps/pkgs/RGBD/RGBDSurface.cpp
// Function: gaps::RGBDSurface::DrawTexture(int) const
void RGBDSurface::
DrawTexture(int color_scheme) const
{
  // Check channels
  if (NChannels() == 0) return;
  
  // Update/select opengl texture
  if (opengl_texture_id <= 0) ((RGBDSurface *) this)->UpdateOpenGL();
  if (opengl_texture_id <= 0) return;
  glBindTexture(GL_TEXTURE_2D, opengl_texture_id);
  glEnable(GL_TEXTURE_2D);

  // Load color
  LoadColor(color_scheme);

  // Draw rectangle
  if (rectangle) {
    RNGrfxBegin(RN_GRFX_QUADS);
    R3LoadTextureCoords(0.0, 0.0);
    R3LoadPoint(rectangle->Corner(0,0));
    R3LoadTextureCoords(1.0, 0.0);
    R3LoadPoint(rectangle->Corner(1,0));
    R3LoadTextureCoords(1.0, 1.0);
    R3LoadPoint(rectangle->Corner(1,1));
    R3LoadTextureCoords(0.0, 1.0);
    R3LoadPoint(rectangle->Corner(0,1));
    RNGrfxEnd();
  }
  else if (mesh) {
    if ((width > 0) && (height > 0)) {
      RNGrfxBegin(RN_GRFX_TRIANGLES);
      for (int i = 0; i < mesh->NFaces(); i++) {
        R3MeshFace *face = mesh->Face(i);
        for (int j = 0; j < 3; j ++) {
          R3MeshVertex *vertex = mesh->VertexOnFace(face, j);
          const R3Point& world_position = mesh->VertexPosition(vertex);
          R2Point texture_position, surface_position = mesh->VertexTextureCoords(vertex);
          TransformSurfaceToTexture(surface_position, texture_position);
          texture_position[0] = texture_position[0] / width;
          texture_position[1] = texture_position[1] / height;
          R3LoadTextureCoords(texture_position);
          R3LoadPoint(world_position);
        }
      }
      RNGrfxEnd();
    }
  }

  // Unselect opengl texture
  glDisable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, 0);
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movl %esi, 0xcc(%rsp)
movq 0xd0(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x277e0
cmpl $0x0, %eax
jne 0x25df2
jmp 0x26142
movq 0x18(%rsp), %rax
cmpl $0x0, 0x30(%rax)
jg 0x25e0b
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
callq *0xe0(%rax)
movq 0x18(%rsp), %rax
cmpl $0x0, 0x30(%rax)
jg 0x25e1b
jmp 0x26142
movq 0x18(%rsp), %rax
movl 0x30(%rax), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x128c0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x12640
movq 0x18(%rsp), %rdi
movl 0xcc(%rsp), %esi
movq (%rdi), %rax
callq *0x80(%rax)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x80(%rax)
je 0x25f59
movl $0x7, %edi
callq 0x27c00
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x27e90
movq 0x18(%rsp), %rax
movq 0x80(%rax), %rsi
leaq 0xb0(%rsp), %rdi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x27a50
leaq 0xb0(%rsp), %rdi
callq 0x27c90
movsd 0x14018a(%rip), %xmm0   # 0x166030
xorps %xmm1, %xmm1
callq 0x27e90
movq 0x18(%rsp), %rax
movq 0x80(%rax), %rsi
leaq 0x98(%rsp), %rdi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x27a50
leaq 0x98(%rsp), %rdi
callq 0x27c90
movsd 0x14014d(%rip), %xmm1   # 0x166030
movaps %xmm1, %xmm0
callq 0x27e90
movq 0x18(%rsp), %rax
movq 0x80(%rax), %rsi
leaq 0x80(%rsp), %rdi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x27a50
leaq 0x80(%rsp), %rdi
callq 0x27c90
xorps %xmm0, %xmm0
movsd 0x14010d(%rip), %xmm1   # 0x166030
callq 0x27e90
movq 0x18(%rsp), %rax
movq 0x80(%rax), %rsi
leaq 0x68(%rsp), %rdi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x27a50
leaq 0x68(%rsp), %rdi
callq 0x27c90
callq 0x27cd0
jmp 0x2612c
movq 0x18(%rsp), %rax
cmpq $0x0, 0x88(%rax)
je 0x2612a
movq 0x18(%rsp), %rax
cmpl $0x0, 0x28(%rax)
jle 0x26128
movq 0x18(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jle 0x26128
movl $0x4, %edi
callq 0x27c00
movl $0x0, 0x64(%rsp)
movq 0x18(%rsp), %rax
movl 0x64(%rsp), %ecx
movl %ecx, 0x14(%rsp)
movq 0x88(%rax), %rdi
callq 0x19f00
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x26123
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rdi
movl 0x64(%rsp), %esi
callq 0x22bb0
movq %rax, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
cmpl $0x3, 0x54(%rsp)
jge 0x26111
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rdi
movq 0x58(%rsp), %rsi
movl 0x54(%rsp), %edx
callq 0x22be0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x48(%rsp)
movq 0x88(%rax), %rdi
movq 0x48(%rsp), %rsi
callq 0x22b70
movq %rax, 0x40(%rsp)
leaq 0x30(%rsp), %rdi
callq 0xcfd70
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rdi
movq 0x48(%rsp), %rsi
callq 0x22b90
movq %rax, %rsi
leaq 0x20(%rsp), %rdi
callq 0xcfdb0
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq *0x40(%rax)
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x27a30
movq %rax, %rcx
movq 0x18(%rsp), %rax
movsd (%rcx), %xmm0
cvtsi2sdl 0x28(%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x27a30
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
callq 0x27a30
movq %rax, %rcx
movq 0x18(%rsp), %rax
movsd (%rcx), %xmm0
cvtsi2sdl 0x2c(%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
callq 0x27a30
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x30(%rsp), %rdi
callq 0x27ec0
movq 0x40(%rsp), %rdi
callq 0x27c90
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x25fe5
jmp 0x26113
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x25f9c
callq 0x27cd0
jmp 0x2612a
jmp 0x2612c
movl $0xde1, %edi            # imm = 0xDE1
callq 0x121c0
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
callq 0x128c0
addq $0xd8, %rsp
retq
nopw (%rax,%rax)