// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3PlanarGrid.cpp
// Function: gaps::R3PlanarGrid::Draw() const
void R3PlanarGrid::
Draw(void) const
{
  // Just checking
  if (R3Contains(plane, R3null_plane)) return;

  // Define texture
  if (texture_id <= 0) {
    // Create texture id
    GLuint i;
    glGenTextures(1, &i);
    ((R3PlanarGrid *) this)->texture_id = i;
    assert(texture_id > 0);

    // Create texture pixels
    float *pixels = new float [ 4 * grid.NEntries() ];
    for (int i = 0; i < grid.NEntries(); i++) {
      pixels[4*i + 0] = grid.GridValue(i);
      pixels[4*i + 1] = grid.GridValue(i);
      pixels[4*i + 2] = grid.GridValue(i);
      pixels[4*i + 3] = grid.GridValue(i);
    }

    // Begin texture definition
    glBindTexture(GL_TEXTURE_2D, texture_id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, grid.XResolution(), grid.YResolution(), 0, GL_RGBA, GL_FLOAT, pixels);

    // Delete texture pixels
    delete [] pixels;
  }

  // Set texture
  glBindTexture(GL_TEXTURE_2D, texture_id);

  // Push transformation
  R3Affine inverse(transformation);
  inverse.Invert();
  inverse.Push();

  // Set rendering modes
  glEnable(GL_TEXTURE_2D);
  glAlphaFunc(GL_GREATER, 0.1);
  glEnable(GL_ALPHA_TEST);
  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  glDisable(GL_CULL_FACE);

  // Draw polygon
  R3BeginPolygon();
  const R2Box& bbox = grid.WorldBox();
  R3LoadTextureCoords(0.0, 0.0);  R2LoadPoint(bbox[0][0], bbox[0][1]);
  R3LoadTextureCoords(1.0, 0.0);  R2LoadPoint(bbox[1][0], bbox[0][1]);
  R3LoadTextureCoords(1.0, 1.0);  R2LoadPoint(bbox[1][0], bbox[1][1]);
  R3LoadTextureCoords(0.0, 1.0);  R2LoadPoint(bbox[0][0], bbox[1][1]);
  R3EndPolygon();

  // Reset rendering modes
  glEnable(GL_CULL_FACE);
  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  glDisable(GL_ALPHA_TEST);
  glDisable(GL_TEXTURE_2D);

  // Pop transformation
  inverse.Pop();
}

// Assembly:
subq $0x238, %rsp            # imm = 0x238
movq %rdi, 0x230(%rsp)
movq 0x230(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
leaq 0x51f7aa(%rip), %rsi    # 0x5fdce0
callq 0x8af60
cmpl $0x0, %eax
je 0xde545
jmp 0xdeb50
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x238(%rax)
jg 0xde80a
movl $0x1, %edi
leaq 0x22c(%rsp), %rsi
callq 0x137d0
movq 0xc0(%rsp), %rdi
movl 0x22c(%rsp), %eax
movl %eax, 0x238(%rdi)
addq $0x130, %rdi            # imm = 0x130
callq 0xdec90
shll $0x2, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x13030
movq %rax, 0x220(%rsp)
movl $0x0, 0x21c(%rsp)
movq 0xc0(%rsp), %rdi
movl 0x21c(%rsp), %eax
movl %eax, 0xbc(%rsp)
addq $0x130, %rdi            # imm = 0x130
callq 0xdec90
movl %eax, %ecx
movl 0xbc(%rsp), %eax
cmpl %ecx, %eax
jge 0xde6ff
movq 0xc0(%rsp), %rdi
addq $0x130, %rdi            # imm = 0x130
movl 0x21c(%rsp), %esi
callq 0xdecb0
movq 0xc0(%rsp), %rdi
cvtsd2ss %xmm0, %xmm0
movq 0x220(%rsp), %rax
movl 0x21c(%rsp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
addq $0x130, %rdi            # imm = 0x130
movl 0x21c(%rsp), %esi
callq 0xdecb0
movq 0xc0(%rsp), %rdi
cvtsd2ss %xmm0, %xmm0
movq 0x220(%rsp), %rax
movl 0x21c(%rsp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
addq $0x130, %rdi            # imm = 0x130
movl 0x21c(%rsp), %esi
callq 0xdecb0
movq 0xc0(%rsp), %rdi
cvtsd2ss %xmm0, %xmm0
movq 0x220(%rsp), %rax
movl 0x21c(%rsp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
addq $0x130, %rdi            # imm = 0x130
movl 0x21c(%rsp), %esi
callq 0xdecb0
cvtsd2ss %xmm0, %xmm0
movq 0x220(%rsp), %rax
movl 0x21c(%rsp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x21c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x21c(%rsp)
jmp 0xde5c6
movq 0xc0(%rsp), %rax
movl 0x238(%rax), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x136f0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl $0x2900, %edx           # imm = 0x2900
callq 0x137b0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl $0x2900, %edx           # imm = 0x2900
callq 0x137b0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2601, %edx           # imm = 0x2601
callq 0x137b0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2601, %edx           # imm = 0x2601
callq 0x137b0
movl $0x2300, %edi           # imm = 0x2300
movl $0x2200, %esi           # imm = 0x2200
movl $0x2100, %edx           # imm = 0x2100
callq 0x13650
movq 0xc0(%rsp), %rdi
addq $0x130, %rdi            # imm = 0x130
callq 0x6b940
movq 0xc0(%rsp), %rdi
movl %eax, 0xac(%rsp)
addq $0x130, %rdi            # imm = 0x130
callq 0x6b960
movl 0xac(%rsp), %ecx
movl %eax, %r8d
movq 0x220(%rsp), %rax
movl $0xde1, %edi            # imm = 0xDE1
xorl %r9d, %r9d
movl $0x1908, %edx           # imm = 0x1908
movl %r9d, %esi
movl $0x1908, (%rsp)         # imm = 0x1908
movl $0x1406, 0x8(%rsp)      # imm = 0x1406
movq %rax, 0x10(%rsp)
callq 0x131f0
movq 0x220(%rsp), %rax
movq %rax, 0xb0(%rsp)
cmpq $0x0, %rax
je 0xde808
movq 0xb0(%rsp), %rdi
callq 0x13750
jmp 0xde80a
movq 0xc0(%rsp), %rax
movl 0x238(%rax), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x136f0
movq 0xc0(%rsp), %rsi
addq $0x20, %rsi
leaq 0x108(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x309e0
movq 0xa0(%rsp), %rdi
callq 0xdecd0
jmp 0xde852
leaq 0x108(%rsp), %rdi
callq 0x76820
jmp 0xde861
movl $0xde1, %edi            # imm = 0xDE1
callq 0x13510
jmp 0xde86d
movl $0x204, %edi            # imm = 0x204
movss 0xc8112(%rip), %xmm0    # 0x1a698c
callq 0x13130
jmp 0xde881
movl $0xbc0, %edi            # imm = 0xBC0
callq 0x13510
jmp 0xde88d
movl $0xb52, %edi            # imm = 0xB52
movl $0x1, %esi
callq 0x13100
jmp 0xde89e
movl $0xb44, %edi            # imm = 0xB44
callq 0x13170
jmp 0xde8aa
callq 0x793f0
jmp 0xde8b1
movq 0xc0(%rsp), %rsi
addq $0x130, %rsi            # imm = 0x130
leaq 0xc8(%rsp), %rdi
callq 0x548f0
jmp 0xde8cf
leaq 0xc8(%rsp), %rax
movq %rax, 0xf0(%rsp)
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x794e0
jmp 0xde8ec
movq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x5b1f0
movq %rax, 0x98(%rsp)
jmp 0xde905
movq 0x98(%rsp), %rdi
xorl %esi, %esi
callq 0x45390
movsd %xmm0, 0x90(%rsp)
jmp 0xde91f
movq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x5b1f0
movq %rax, 0x88(%rsp)
jmp 0xde938
movq 0x88(%rsp), %rdi
movl $0x1, %esi
callq 0x45390
movsd %xmm0, 0x80(%rsp)
jmp 0xde955
movsd 0x80(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
callq 0xdecf0
jmp 0xde96e
movsd 0xc57f2(%rip), %xmm0    # 0x1a4168
xorps %xmm1, %xmm1
callq 0x794e0
jmp 0xde980
movq 0xf0(%rsp), %rdi
movl $0x1, %esi
callq 0x5b1f0
movq %rax, 0x78(%rsp)
jmp 0xde999
movq 0x78(%rsp), %rdi
xorl %esi, %esi
callq 0x45390
movsd %xmm0, 0x70(%rsp)
jmp 0xde9ad
movq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x5b1f0
movq %rax, 0x68(%rsp)
jmp 0xde9c3
movq 0x68(%rsp), %rdi
movl $0x1, %esi
callq 0x45390
movsd %xmm0, 0x60(%rsp)
jmp 0xde9da
movsd 0x60(%rsp), %xmm1
movsd 0x70(%rsp), %xmm0
callq 0xdecf0
jmp 0xde9ed
movsd 0xc5773(%rip), %xmm1    # 0x1a4168
movaps %xmm1, %xmm0
callq 0x794e0
jmp 0xde9ff
movq 0xf0(%rsp), %rdi
movl $0x1, %esi
callq 0x5b1f0
movq %rax, 0x58(%rsp)
jmp 0xdea18
movq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0x45390
movsd %xmm0, 0x50(%rsp)
jmp 0xdea2c
movq 0xf0(%rsp), %rdi
movl $0x1, %esi
callq 0x5b1f0
movq %rax, 0x48(%rsp)
jmp 0xdea45
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x45390
movsd %xmm0, 0x40(%rsp)
jmp 0xdea5c
movsd 0x40(%rsp), %xmm1
movsd 0x50(%rsp), %xmm0
callq 0xdecf0
jmp 0xdea6f
xorps %xmm0, %xmm0
movsd 0xc56ee(%rip), %xmm1    # 0x1a4168
callq 0x794e0
jmp 0xdea81
movq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x5b1f0
movq %rax, 0x38(%rsp)
jmp 0xdea97
movq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x45390
movsd %xmm0, 0x30(%rsp)
jmp 0xdeaab
movq 0xf0(%rsp), %rdi
movl $0x1, %esi
callq 0x5b1f0
movq %rax, 0x28(%rsp)
jmp 0xdeac4
movq 0x28(%rsp), %rdi
movl $0x1, %esi
callq 0x45390
movsd %xmm0, 0x20(%rsp)
jmp 0xdeadb
movsd 0x20(%rsp), %xmm1
movsd 0x30(%rsp), %xmm0
callq 0xdecf0
jmp 0xdeaee
callq 0x79410
jmp 0xdeaf5
movl $0xb44, %edi            # imm = 0xB44
callq 0x13510
jmp 0xdeb01
movl $0xb52, %edi            # imm = 0xB52
xorl %esi, %esi
callq 0x13100
jmp 0xdeb0f
movl $0xbc0, %edi            # imm = 0xBC0
callq 0x13170
jmp 0xdeb1b
movl $0xde1, %edi            # imm = 0xDE1
callq 0x13170
jmp 0xdeb27
leaq 0x108(%rsp), %rdi
callq 0x76890
jmp 0xdeb36
leaq 0xc8(%rsp), %rdi
callq 0x54990
leaq 0x108(%rsp), %rdi
callq 0xd4520
addq $0x238, %rsp            # imm = 0x238
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
jmp 0xdeb8f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x54990
leaq 0x108(%rsp), %rdi
callq 0xd4520
movq 0x100(%rsp), %rdi
callq 0x13970
nopl (%rax)