// File: tomfunkhouser[P]gaps/apps/gapsview/gapsview.cpp
// Function: DrawGrid(gaps::R3Grid*, int, int)
static void
DrawGrid(R3Grid *grid,
  int model_index, int color_scheme)
{
  // Check if should draw grid
  if (!show_grids) return;

  // Push transformation
  grid->GridToWorldTransformation().Push();

  // Draw grid isosurface faces
  if (show_surfaces) {
    glEnable(GL_LIGHTING);
    RNLoadRgb(0.8, 0.8, 0.8);
    grid->DrawIsoSurface(grid_thresholds[model_index]);
  }

  // Draw grid isosurface edges
  if (show_edges) {
    glDisable(GL_LIGHTING);
    RNLoadRgb(1.0, 0.0, 0.0);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    grid->DrawIsoSurface(grid_thresholds[model_index]);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  }

  // Draw grid slices
  if (show_slices) {
    glDisable(GL_LIGHTING);
    RNLoadRgb(1.0, 1.0, 1.0);
    grid->DrawSlice(RN_X, grid_slice_coords[RN_X]);
    grid->DrawSlice(RN_Y, grid_slice_coords[RN_Y]);
    grid->DrawSlice(RN_Z, grid_slice_coords[RN_Z]);
  }

  // Draw grid text
  if (show_text_labels) {
    char buffer[64];
    glDisable(GL_LIGHTING);
    RNLoadRgb(0.0, 0.0, 1.0);
    for (int i = 0; i < grid->XResolution(); i += 10) {
      for (int j = 0; j < grid->YResolution(); j += 10) {
        for (int k = 0; k < grid->ZResolution(); k += 10) {
          RNScalar value = grid->GridValue(i, j, k);
          if (value >= grid_thresholds[model_index]) continue;
          sprintf(buffer, "%.2g", value);
          R3DrawText(i, j, k, buffer);
        }
      }
    }
  }

  // Draw grid threshold
  if (show_grid_threshold) {
    char buffer[128];
    sprintf(buffer, "%f", grid_thresholds[selected_model_index]);
    viewer->DrawText(10, 10, buffer);
  }

  // Pop transformation
  grid->GridToWorldTransformation().Pop();
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movl %esi, 0xfc(%rsp)
movl %edx, 0xf8(%rsp)
cmpl $0x0, 0x2bdf50(%rip)    # 0x2efb84
jne 0x31c3b
jmp 0x31f6f
movq 0x100(%rsp), %rdi
callq 0x39400
movq %rax, %rdi
movq (%rdi), %rax
callq *0xb0(%rax)
cmpl $0x0, 0x2cd071(%rip)    # 0x2feccc
je 0x31c9b
movl $0xb50, %edi            # imm = 0xB50
callq 0x29a50
movsd 0x234421(%rip), %xmm2   # 0x266090
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x38d60
movq 0x100(%rsp), %rdi
movslq 0xfc(%rsp), %rcx
leaq 0x2cd04f(%rip), %rax    # 0x2fece0
movsd (%rax,%rcx,8), %xmm0
callq 0x17bbc0
cmpl $0x0, 0x2cd02e(%rip)    # 0x2fecd0
je 0x31d00
movl $0xb50, %edi            # imm = 0xB50
callq 0x2a130
movsd 0x23435a(%rip), %xmm0   # 0x266010
xorps %xmm2, %xmm2
movaps %xmm2, %xmm1
callq 0x38d60
movl $0x408, %edi            # imm = 0x408
movl $0x1b01, %esi           # imm = 0x1B01
callq 0x29720
movq 0x100(%rsp), %rdi
movslq 0xfc(%rsp), %rcx
leaq 0x2ccff9(%rip), %rax    # 0x2fece0
movsd (%rax,%rcx,8), %xmm0
callq 0x17bbc0
movl $0x408, %edi            # imm = 0x408
movl $0x1b02, %esi           # imm = 0x1B02
callq 0x29720
cmpl $0x0, 0x2cd1d9(%rip)    # 0x2feee0
je 0x31d6b
movl $0xb50, %edi            # imm = 0xB50
callq 0x2a130
movsd 0x2342f5(%rip), %xmm2   # 0x266010
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x38d60
movq 0x100(%rsp), %rdi
movl 0x2ccf6c(%rip), %edx    # 0x2feca0
xorl %esi, %esi
callq 0x17be00
movq 0x100(%rsp), %rdi
movl 0x2ccf5b(%rip), %edx    # 0x2feca4
movl $0x1, %esi
callq 0x17be00
movq 0x100(%rsp), %rdi
movl 0x2ccf47(%rip), %edx    # 0x2feca8
movl $0x2, %esi
callq 0x17be00
cmpl $0x0, 0x2cd172(%rip)    # 0x2feee4
je 0x31f07
movl $0xb50, %edi            # imm = 0xB50
callq 0x2a130
xorps %xmm1, %xmm1
movsd 0x234283(%rip), %xmm2   # 0x266010
movaps %xmm1, %xmm0
callq 0x38d60
movl $0x0, 0xac(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x100(%rsp), %rdi
callq 0x38990
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x31f05
movl $0x0, 0xa8(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x100(%rsp), %rdi
callq 0x389b0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jge 0x31eed
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x100(%rsp), %rdi
callq 0x389d0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x31ed5
movq 0x100(%rsp), %rdi
movl 0xac(%rsp), %esi
movl 0xa8(%rsp), %edx
movl 0xa4(%rsp), %ecx
callq 0x39410
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
movslq 0xfc(%rsp), %rcx
leaq 0x2cce75(%rip), %rax    # 0x2fece0
ucomisd (%rax,%rcx,8), %xmm0
jb 0x31e74
jmp 0x31ebf
leaq 0xb0(%rsp), %rdi
movsd 0x98(%rsp), %xmm0
leaq 0x2345c3(%rip), %rsi    # 0x26644f
movb $0x1, %al
callq 0x2a280
cvtsi2sdl 0xac(%rsp), %xmm0
cvtsi2sdl 0xa8(%rsp), %xmm1
cvtsi2sdl 0xa4(%rsp), %xmm2
leaq 0xb0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x39460
movl 0xa4(%rsp), %eax
addl $0xa, %eax
movl %eax, 0xa4(%rsp)
jmp 0x31e02
jmp 0x31ed7
movl 0xa8(%rsp), %eax
addl $0xa, %eax
movl %eax, 0xa8(%rsp)
jmp 0x31dd1
jmp 0x31eef
movl 0xac(%rsp), %eax
addl $0xa, %eax
movl %eax, 0xac(%rsp)
jmp 0x31da0
jmp 0x31f07
cmpl $0x0, 0x2ccfda(%rip)    # 0x2feee8
je 0x31f56
leaq 0x10(%rsp), %rdi
movslq 0x2ccda8(%rip), %rcx    # 0x2fecc4
leaq 0x2ccdbd(%rip), %rax    # 0x2fece0
movsd (%rax,%rcx,8), %xmm0
leaq 0x2365fa(%rip), %rsi    # 0x268529
movb $0x1, %al
callq 0x2a280
movq 0x2ccd73(%rip), %rdi    # 0x2fecb0
leaq 0x10(%rsp), %rsi
movsd 0x23414e(%rip), %xmm1   # 0x266098
xorl %eax, %eax
movl %eax, %edx
movaps %xmm1, %xmm0
callq 0xed410
movq 0x100(%rsp), %rdi
callq 0x39400
movq %rax, %rdi
movq (%rdi), %rax
callq *0xb8(%rax)
addq $0x108, %rsp            # imm = 0x108
retq
nopw (%rax,%rax)