// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %rbx
leaq 0x4bac63(%rip), %rsi    # 0x5262c0
callq 0x47a64
testl %eax, %eax
jne 0x6b8e0
movl 0x238(%rbx), %esi
testl %esi, %esi
jg 0x6b796
pushq $0x1
popq %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x13730
movl (%r14), %ebp
movl %ebp, 0x238(%rbx)
movslq 0x1fc(%rbx), %r15
movq %r15, %rax
shlq $0x4, %rax
testq %r15, %r15
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x13030
movq %rax, %r14
movq 0x1e8(%rbx), %rax
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
shlq $0x3, %rdx
cmpq %rcx, %rdx
je 0x6b6e3
cvtsd2ss (%rax,%rcx), %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movups %xmm0, (%r14,%rcx,2)
addq $0x8, %rcx
jmp 0x6b6ca
movl $0xde1, %edi            # imm = 0xDE1
movl %ebp, %esi
callq 0x13640
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl $0x2900, %edx           # imm = 0x2900
callq 0x13710
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl $0x2900, %edx           # imm = 0x2900
callq 0x13710
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2601, %edx           # imm = 0x2601
callq 0x13710
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2601, %edx           # imm = 0x2601
callq 0x13710
movl $0x2300, %edi           # imm = 0x2300
movl $0x2200, %esi           # imm = 0x2200
movl $0x2100, %edx           # imm = 0x2100
callq 0x135a0
movl 0x1f0(%rbx), %ecx
movl 0x1f4(%rbx), %r8d
subq $0x8, %rsp
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1908, %edx           # imm = 0x1908
xorl %r9d, %r9d
pushq %r14
pushq $0x1406                 # imm = 0x1406
pushq $0x1908                 # imm = 0x1908
callq 0x131e0
addq $0x20, %rsp
movq %r14, %rdi
callq 0x136a0
movl 0x238(%rbx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x13640
leaq 0x20(%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x20a04
leaq 0x30(%rsp), %rdi
callq 0x68eba
leaq 0x28(%rsp), %rdi
callq 0x3e4e8
movl $0xde1, %edi            # imm = 0xDE1
callq 0x13490
movss 0x5f168(%rip), %xmm0    # 0xca93c
movl $0x204, %edi            # imm = 0x204
callq 0x13120
movl $0xbc0, %edi            # imm = 0xBC0
callq 0x13490
pushq $0x1
popq %rsi
movl $0xb52, %edi            # imm = 0xB52
callq 0x130f0
movl $0xb44, %edi            # imm = 0xB44
callq 0x13160
callq 0x3fc03
addq $0x130, %rbx            # imm = 0x130
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x2ed88
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
callq 0x13a00
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x133a0
movsd 0x5c90e(%rip), %xmm0    # 0xc8148
xorps %xmm1, %xmm1
callq 0x13a00
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
callq 0x133a0
movsd 0x5c8ed(%rip), %xmm0    # 0xc8148
movaps %xmm0, %xmm1
callq 0x13a00
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
callq 0x133a0
movsd 0x5c8cc(%rip), %xmm1    # 0xc8148
xorps %xmm0, %xmm0
callq 0x13a00
movsd 0x8(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
callq 0x133a0
callq 0x136d0
movl $0xb44, %edi            # imm = 0xB44
callq 0x13490
movl $0xb52, %edi            # imm = 0xB52
xorl %esi, %esi
callq 0x130f0
movl $0xbc0, %edi            # imm = 0xBC0
callq 0x13160
movl $0xde1, %edi            # imm = 0xDE1
callq 0x13160
leaq 0x28(%rsp), %rdi
callq 0x3e524
movq %rsp, %rdi
callq 0x91792
leaq 0x28(%rsp), %rdi
callq 0x6686a
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6b8f0
movq %rax, %rbx
jmp 0x6b900
movq %rax, %rbx
movq %rsp, %rdi
callq 0x91792
leaq 0x28(%rsp), %rdi
callq 0x6686a
movq %rbx, %rdi
callq 0x138f0
nop