// File: tomfunkhouser[P]gaps/pkgs/RGBD/RGBDSurface.cpp
// Function: gaps::RGBDSurface::UpdateOpenGL()
void RGBDSurface::
UpdateOpenGL(void) 
{
  // Check identifier
  if (opengl_texture_id > 0) return;
  
  // Create identifier
  GLuint identifier;
  glGenTextures(1, &identifier);

  // Allocate pixels
  unsigned char *pixels = new unsigned char [ width * height * 4];

  // Fill pixels
  unsigned char *pixelsp = pixels;
  for (int iy = 0; iy < height; iy++) {
    for (int ix = 0; ix < width; ix++) {
      RNRgb color = TexelColor(ix, iy);
      *pixelsp++ = (color.R() < 1.0) ? color.R() * 255 : 255;
      *pixelsp++ = (color.G() < 1.0) ? color.G() * 255 : 255;
      *pixelsp++ = (color.B() < 1.0) ? color.B() * 255 : 255;
      *pixelsp++ = 255;
    }
  }

  // Define texture
  glBindTexture(GL_TEXTURE_2D, identifier);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
    0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

  // Delete the pixels
  delete [] pixels;

  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  // gluBuild2DMipmaps(GL_TEXTURE_2D, 3, rgb_image.Width(), rgb_image.Height(),
  //   GL_RGB, GL_UNSIGNED_BYTE, (const unsigned char *) rgb_image.Pixels());
 
  // Remember identifier
  opengl_texture_id = identifier;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x30(%rax)
jle 0x26b47
jmp 0x26e16
movl $0x1, %edi
leaq 0x7c(%rsp), %rsi
callq 0x129d0
movq 0x40(%rsp), %rcx
movl 0x28(%rcx), %eax
imull 0x2c(%rcx), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x12040
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movq 0x40(%rsp), %rcx
movl 0x64(%rsp), %eax
cmpl 0x2c(%rcx), %eax
jge 0x26d35
movl $0x0, 0x60(%rsp)
movq 0x40(%rsp), %rcx
movl 0x60(%rsp), %eax
cmpl 0x28(%rcx), %eax
jge 0x26d23
movq 0x40(%rsp), %rsi
movl 0x60(%rsp), %edx
movl 0x64(%rsp), %ecx
leaq 0x48(%rsp), %rdi
callq 0x24520
leaq 0x48(%rsp), %rdi
callq 0x1a180
movaps %xmm0, %xmm1
movsd 0x13f454(%rip), %xmm0   # 0x166030
ucomisd %xmm1, %xmm0
jbe 0x26c00
leaq 0x48(%rsp), %rdi
callq 0x1a180
movsd 0x140294(%rip), %xmm1   # 0x166e88
mulsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
jmp 0x26c10
movsd 0x140280(%rip), %xmm0   # 0x166e88
movsd %xmm0, 0x38(%rsp)
jmp 0x26c10
movsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq 0x68(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x68(%rsp)
movb %cl, (%rax)
leaq 0x48(%rsp), %rdi
callq 0x1a190
movaps %xmm0, %xmm1
movsd 0x13f3ec(%rip), %xmm0   # 0x166030
ucomisd %xmm1, %xmm0
jbe 0x26c68
leaq 0x48(%rsp), %rdi
callq 0x1a190
movsd 0x14022c(%rip), %xmm1   # 0x166e88
mulsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x26c78
movsd 0x140218(%rip), %xmm0   # 0x166e88
movsd %xmm0, 0x30(%rsp)
jmp 0x26c78
movsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq 0x68(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x68(%rsp)
movb %cl, (%rax)
leaq 0x48(%rsp), %rdi
callq 0x1a1a0
movaps %xmm0, %xmm1
movsd 0x13f384(%rip), %xmm0   # 0x166030
ucomisd %xmm1, %xmm0
jbe 0x26cd0
leaq 0x48(%rsp), %rdi
callq 0x1a1a0
movsd 0x1401c4(%rip), %xmm1   # 0x166e88
mulsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0x26ce0
movsd 0x1401b0(%rip), %xmm0   # 0x166e88
movsd %xmm0, 0x28(%rsp)
jmp 0x26ce0
movsd 0x28(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq 0x68(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x68(%rsp)
movb %cl, (%rax)
movq 0x68(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x68(%rsp)
movb $-0x1, (%rax)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x26b9e
jmp 0x26d25
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x26b84
movl 0x7c(%rsp), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x128c0
movl $0xcf5, %edi            # imm = 0xCF5
movl $0x4, %esi
callq 0x12340
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl $0x812f, %edx           # imm = 0x812F
callq 0x129b0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl $0x812f, %edx           # imm = 0x812F
callq 0x129b0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq 0x129b0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq 0x129b0
movl $0x2300, %edi           # imm = 0x2300
movl $0x2200, %esi           # imm = 0x2200
movl $0x2100, %edx           # imm = 0x2100
callq 0x127c0
movq 0x40(%rsp), %rax
movl 0x28(%rax), %ecx
movl 0x2c(%rax), %r8d
movq 0x70(%rsp), %rax
movl $0xde1, %edi            # imm = 0xDE1
xorl %r9d, %r9d
movl $0x1908, %edx           # imm = 0x1908
movl %r9d, %esi
movl $0x1908, (%rsp)         # imm = 0x1908
movl $0x1401, 0x8(%rsp)      # imm = 0x1401
movq %rax, 0x10(%rsp)
callq 0x12250
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x26e0a
movq 0x20(%rsp), %rdi
callq 0x12920
movq 0x40(%rsp), %rax
movl 0x7c(%rsp), %ecx
movl %ecx, 0x30(%rax)
addq $0x88, %rsp
retq
nop