// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R2Texture.cpp
// Function: gaps::R2Texture::Load() const
void R2Texture::
Load(void) const
{
  // Check if needs to be loaded
  if (id >= 0) return;
  assert(image);

  // Create texture id
  GLuint i;
  glGenTextures(1, &i);
  ((R2Texture *) this)->id = i;
  assert(id > 0);

  // Begin texture definition
  glBindTexture(GL_TEXTURE_2D, id);

  // Define texture parameters
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, s_wrap);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, t_wrap);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, blend);

  // Determine texture format
  GLenum format = GL_RGB;
  if (image->Depth() == 1) format = GL_LUMINANCE;
  else if (image->Depth() == 2) format = GL_LUMINANCE_ALPHA;
  else if (image->Depth() == 3) format = GL_RGB;
  else if (image->Depth() == 4) format = GL_RGBA;
  else RNAbort("Illegal texture image");

#ifdef AT_ONE_POINT_THIS_WAS_NEEDED_FOR_MESA
  // For some reason, gluBuild2DMipmaps segfaults in mesa
  glTexImage2D(GL_TEXTURE_2D, 0, format, image->Width(), image->Height(),
    0, format, GL_UNSIGNED_BYTE, (const unsigned char *) image->Pixels());
#else
  // Define texture
  if ((min_filter == R2_NEAREST_MIPMAP_NEAREST_TEXTURE_FILTER) ||
      (min_filter == R2_NEAREST_MIPMAP_LINEAR_TEXTURE_FILTER) ||
      (min_filter == R2_LINEAR_MIPMAP_NEAREST_TEXTURE_FILTER) ||
      (min_filter == R2_LINEAR_MIPMAP_LINEAR_TEXTURE_FILTER)) {
    gluBuild2DMipmaps(GL_TEXTURE_2D, format, image->Width(), image->Height(),
      format, GL_UNSIGNED_BYTE, (const unsigned char *) image->Pixels());
  }
  else {
    glTexImage2D(GL_TEXTURE_2D, 0, format, image->Width(), image->Height(),
      0, format, GL_UNSIGNED_BYTE, (const unsigned char *) image->Pixels());
  }
#endif

  // End texture definition
  glBindTexture(GL_TEXTURE_2D, 0);
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x48(%rax)
jl 0x65e5e
jmp 0x660b3
movl $0x1, %edi
leaq 0x4c(%rsp), %rsi
callq 0x22e80
movq 0x40(%rsp), %rax
movl 0x4c(%rsp), %ecx
movl %ecx, 0x48(%rax)
movl 0x48(%rax), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x22ce0
movq 0x40(%rsp), %rax
movl 0x28(%rax), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
callq 0x22e40
movq 0x40(%rsp), %rax
movl 0x2c(%rax), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
callq 0x22e40
movq 0x40(%rsp), %rax
movl 0x30(%rax), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
callq 0x22e40
movq 0x40(%rsp), %rax
movl 0x34(%rax), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
callq 0x22e40
movq 0x40(%rsp), %rax
movl 0x38(%rax), %edx
movl $0x2300, %edi           # imm = 0x2300
movl $0x2200, %esi           # imm = 0x2200
callq 0x22b30
movq 0x40(%rsp), %rax
movl $0x1907, 0x48(%rsp)     # imm = 0x1907
movq 0x20(%rax), %rdi
callq 0x661f0
cmpl $0x1, %eax
jne 0x65f1e
movl $0x1909, 0x48(%rsp)     # imm = 0x1909
jmp 0x65f89
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x661f0
cmpl $0x2, %eax
jne 0x65f3b
movl $0x190a, 0x48(%rsp)     # imm = 0x190A
jmp 0x65f87
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x661f0
cmpl $0x3, %eax
jne 0x65f58
movl $0x1907, 0x48(%rsp)     # imm = 0x1907
jmp 0x65f85
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x661f0
cmpl $0x4, %eax
jne 0x65f75
movl $0x1908, 0x48(%rsp)     # imm = 0x1908
jmp 0x65f83
leaq 0x129f40(%rip), %rdi    # 0x18febc
movb $0x0, %al
callq 0xf39f0
jmp 0x65f85
jmp 0x65f87
jmp 0x65f89
movq 0x40(%rsp), %rax
cmpl $0x2700, 0x30(%rax)     # imm = 0x2700
je 0x65fc1
movq 0x40(%rsp), %rax
cmpl $0x2702, 0x30(%rax)     # imm = 0x2702
je 0x65fc1
movq 0x40(%rsp), %rax
cmpl $0x2701, 0x30(%rax)     # imm = 0x2701
je 0x65fc1
movq 0x40(%rsp), %rax
cmpl $0x2703, 0x30(%rax)     # imm = 0x2703
jne 0x6602e
movq 0x40(%rsp), %rax
movl 0x48(%rsp), %ecx
movl %ecx, 0x30(%rsp)
movq 0x20(%rax), %rdi
callq 0x66200
movl %eax, %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x34(%rsp)
movq 0x20(%rax), %rdi
callq 0x66210
movl %eax, %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x38(%rsp)
movl 0x48(%rsp), %ecx
movl %ecx, 0x3c(%rsp)
movq 0x20(%rax), %rdi
callq 0x66220
movl 0x30(%rsp), %esi
movl 0x34(%rsp), %edx
movl 0x38(%rsp), %ecx
movl 0x3c(%rsp), %r8d
movl $0xde1, %edi            # imm = 0xDE1
movl $0x1401, %r9d           # imm = 0x1401
movq %rax, (%rsp)
callq 0x22580
jmp 0x660a7
movq 0x40(%rsp), %rax
movl 0x48(%rsp), %ecx
movl %ecx, 0x20(%rsp)
movq 0x20(%rax), %rdi
callq 0x66200
movl %eax, %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x24(%rsp)
movq 0x20(%rax), %rdi
callq 0x66210
movl %eax, %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x28(%rsp)
movl 0x48(%rsp), %ecx
movl %ecx, 0x2c(%rsp)
movq 0x20(%rax), %rdi
callq 0x66220
movl 0x20(%rsp), %edx
movl 0x24(%rsp), %ecx
movl 0x28(%rsp), %r8d
movl 0x2c(%rsp), %r10d
movl $0xde1, %edi            # imm = 0xDE1
xorl %r9d, %r9d
movl %r9d, %esi
movl %r10d, (%rsp)
movl $0x1401, 0x8(%rsp)      # imm = 0x1401
movq %rax, 0x10(%rsp)
callq 0x22360
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
callq 0x22ce0
addq $0x58, %rsp
retq
nopl (%rax,%rax)