// File: tomfunkhouser[P]gaps/apps/scnview/scnview.cpp
// Function: GLUTRedraw()
void GLUTRedraw(void)
{
  // Check scene
  if (!scene) return;

  // Clear window 
  // background_color = scene->Background();
  glClearColor(background_color.R(), background_color.G(), background_color.B(), 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // Set backface culling
  if (show_backfacing) glDisable(GL_CULL_FACE);
  else glEnable(GL_CULL_FACE);

  // Set viewing transformation
  viewer->Camera().Load();

  // Draw camera
  if (show_cameras) {
    glDisable(GL_LIGHTING);
    glLineWidth(5);
    RNLoadRgb(0.0, 1.0, 0.0);
    DrawCameras(scene, cameras);
    glLineWidth(1);
  }

  // Draw lights
  if (show_lights) {
    glDisable(GL_LIGHTING);
    RNLoadRgb(1.0, 1.0, 1.0);
    glLineWidth(5);
    DrawLights(scene);
    glLineWidth(1);
  }

  // Draw normals
  if (show_normals) {
    glDisable(GL_LIGHTING);
    RNLoadRgb(0.0, 1.0, 1.0);
    DrawNormals(scene, scene->Root());
  }

  // Draw rays
  if (show_rays) {
    glDisable(GL_LIGHTING);
    RNLoadRgb(0.0, 1.0, 0.0);
    glLineWidth(3);
    DrawRays(scene);
    glLineWidth(1);
  }

  // Draw selected node
  if (selected_node) {
    glDisable(GL_LIGHTING);
    static R3Material selection_material(new R3Brdf(1.0, 0.0, 0.0), "Selection");
    R3Affine transformation = selected_node->CumulativeParentTransformation();
    transformation.Push();
    RNLoadRgb(1.0, 0.0, 0.0);
    glLineWidth(3.0);
    selected_node->Draw(R3_EDGES_DRAW_FLAG);
    glLineWidth(1.0);
    transformation.Pop();
  }

  // Draw front faces in front of scene
  if (show_faces && (show_backfacing == 2)) {
    glEnable(GL_LIGHTING);
    RNLoadRgb(RNwhite_rgb);
    scene->LoadLights(headlight);
    glDepthRange(0, 0.9999);
    glEnable(GL_CULL_FACE);
    scene->Draw();
    glDisable(GL_CULL_FACE);
    glDepthRange(0, 1);
  }

  // Draw faces
  if (show_faces) {
    glEnable(GL_LIGHTING);
    RNLoadRgb(RNwhite_rgb);
    scene->LoadLights(headlight);
    scene->Draw();
  }

  // Draw edges
  if (show_edges) {
    glDisable(GL_LIGHTING);
    RNLoadRgb(0.0, 1.0, 0.0);
    scene->Draw(R3_EDGES_DRAW_FLAG);
  }

  // Draw bboxes
  if (show_bboxes) {
    glDisable(GL_LIGHTING);
    RNLoadRgb(1.0, 0.0, 0.0);
    DrawBBoxes(scene, scene->Root());
  }

  // Draw axes
  if (show_axes) {
    RNScalar d = scene->BBox().DiagonalRadius();
    glDisable(GL_LIGHTING);
    glLineWidth(3);
    R3BeginLine();
    RNLoadRgb(1, 0, 0);
    R3LoadPoint(R3zero_point + 0.5 * d * R3negx_vector);
    R3LoadPoint(R3zero_point + d * R3posx_vector);
    R3EndLine();
    R3BeginLine();
    RNLoadRgb(0, 1, 0);
    R3LoadPoint(R3zero_point + 0.5 * d * R3negy_vector);
    R3LoadPoint(R3zero_point + d * R3posy_vector);
    R3EndLine();
    R3BeginLine();
    RNLoadRgb(0, 0, 1);
    R3LoadPoint(R3zero_point + 0.5 * d * R3negz_vector);
    R3LoadPoint(R3zero_point + d * R3posz_vector);
    R3EndLine();
    glLineWidth(1);
  }

  // Draw frame time
  if (show_frame_rate) {
    char buffer[128];
    static RNTime last_time;
    double frame_time = last_time.Elapsed();
    last_time.Read();
    if ((frame_time > 0) && (frame_time < 10)) {
      glDisable(GL_LIGHTING);
      RNLoadRgb(1.0, 1.0, 1.0);
      sprintf(buffer, "%.1f fps", 1.0 / frame_time);
      viewer->DrawText(100, 100, buffer);
    }
  }  

  // Capture image and exit
  if (output_image_name) {
    R2Image image(GLUTwindow_width, GLUTwindow_height, 3);
    image.Capture();
    image.Write(output_image_name);
    GLUTStop();
  }

  // Swap buffers 
  glutSwapBuffers();
}

// Assembly:
subq $0x358, %rsp            # imm = 0x358
cmpq $0x0, 0x1d4de9(%rip)    # 0x1fbc18
jne 0x26e36
jmp 0x2775f
leaq 0x1d4da3(%rip), %rdi    # 0x1fbbe0
callq 0x2b4a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4c(%rsp)
leaq 0x1d4d8d(%rip), %rdi    # 0x1fbbe0
callq 0x2b4b0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x48(%rsp)
leaq 0x1d4d77(%rip), %rdi    # 0x1fbbe0
callq 0x2b4c0
movss 0x48(%rsp), %xmm1
movaps %xmm0, %xmm2
movss 0x4c(%rsp), %xmm0
cvtsd2ss %xmm2, %xmm2
movss 0x1671ff(%rip), %xmm3   # 0x18e088
callq 0x22fd0
movl $0x4100, %edi           # imm = 0x4100
callq 0x22220
cmpl $0x0, 0x1d4d81(%rip)    # 0x1fbc20
je 0x26ead
movl $0xb44, %edi            # imm = 0xB44
callq 0x22230
jmp 0x26eb7
movl $0xb44, %edi            # imm = 0xB44
callq 0x22910
movq 0x1d4d6a(%rip), %rdi    # 0x1fbc28
callq 0x2b4d0
movq %rax, %rdi
xorl %esi, %esi
callq 0x5e9f0
cmpl $0x0, 0x1d4d5c(%rip)    # 0x1fbc30
je 0x26f20
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
movss 0x1671a4(%rip), %xmm0   # 0x18e08c
callq 0x23320
xorps %xmm2, %xmm2
movsd 0x167118(%rip), %xmm1   # 0x18e010
movaps %xmm2, %xmm0
callq 0x2b4e0
movq 0x1d4d11(%rip), %rdi    # 0x1fbc18
movq 0x1d4d2a(%rip), %rsi    # 0x1fbc38
callq 0x27780
movss 0x16716d(%rip), %xmm0   # 0x18e088
callq 0x23320
cmpl $0x0, 0x1d4d19(%rip)    # 0x1fbc40
je 0x26f6c
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
movsd 0x1670d5(%rip), %xmm2   # 0x18e010
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x2b4e0
movss 0x16713e(%rip), %xmm0   # 0x18e08c
callq 0x23320
movq 0x1d4cbe(%rip), %rdi    # 0x1fbc18
callq 0x27820
movss 0x167121(%rip), %xmm0   # 0x18e088
callq 0x23320
cmpl $0x0, 0x1d4cd1(%rip)    # 0x1fbc44
je 0x26fb7
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
xorps %xmm0, %xmm0
movsd 0x167086(%rip), %xmm2   # 0x18e010
movaps %xmm2, %xmm1
callq 0x2b4e0
movq 0x1d4c7f(%rip), %rax    # 0x1fbc18
movq %rax, 0x40(%rsp)
movq 0x1d4c73(%rip), %rdi    # 0x1fbc18
callq 0x2b510
movq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x27c50
cmpl $0x0, 0x1d4c8a(%rip)    # 0x1fbc48
je 0x27003
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
xorps %xmm2, %xmm2
movsd 0x16703b(%rip), %xmm1   # 0x18e010
movaps %xmm2, %xmm0
callq 0x2b4e0
movss 0x1670ab(%rip), %xmm0   # 0x18e090
callq 0x23320
movq 0x1d4c27(%rip), %rdi    # 0x1fbc18
callq 0x27ff0
movss 0x16708a(%rip), %xmm0   # 0x18e088
callq 0x23320
cmpq $0x0, 0x1d4c45(%rip)    # 0x1fbc50
je 0x271cf
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
cmpb $0x0, 0x1d4c66(%rip)    # 0x1fbc88
jne 0x270b1
leaq 0x1d4c59(%rip), %rdi    # 0x1fbc88
callq 0x23310
cmpl $0x0, %eax
je 0x270b1
movl $0xb8, %edi
callq 0x229e0
movq %rax, 0x38(%rsp)
jmp 0x2704a
movq 0x38(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorps %xmm3, %xmm3
movsd 0x166fb2(%rip), %xmm5   # 0x18e010
movaps %xmm5, %xmm0
movaps %xmm3, %xmm1
movaps %xmm3, %xmm2
movaps %xmm5, %xmm4
callq 0x64880
jmp 0x27071
movq 0x38(%rsp), %rsi
leaq 0x1d4bdb(%rip), %rdi    # 0x1fbc58
leaq 0x167190(%rip), %rdx    # 0x18e214
callq 0x63e20
jmp 0x2708b
leaq 0x3cf2e(%rip), %rdi     # 0x63fc0
leaq 0x1d4bbf(%rip), %rsi    # 0x1fbc58
leaq 0x1cea18(%rip), %rdx    # 0x1f5ab8
callq 0x228b0
leaq 0x1d4bdc(%rip), %rdi    # 0x1fbc88
callq 0x22680
movq 0x1d4b98(%rip), %rsi    # 0x1fbc50
leaq 0x238(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x572d0
movq 0x30(%rsp), %rdi
callq 0x66690
jmp 0x270d6
movsd 0x166f32(%rip), %xmm0   # 0x18e010
xorps %xmm2, %xmm2
movaps %xmm2, %xmm1
callq 0x2b4e0
jmp 0x270eb
movss 0x166f9d(%rip), %xmm0   # 0x18e090
callq 0x23320
jmp 0x270fa
movq 0x1d4b4f(%rip), %rax    # 0x1fbc50
movq %rax, 0x28(%rsp)
leaq 0x230(%rsp), %rdi
movl $0x1, %esi
callq 0x2b520
jmp 0x2711a
movq 0x28(%rsp), %rdi
movq 0x230(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x58170
jmp 0x27132
movss 0x166f4e(%rip), %xmm0   # 0x18e088
callq 0x23320
jmp 0x27141
leaq 0x238(%rsp), %rdi
callq 0x66700
jmp 0x27150
leaq 0x238(%rsp), %rdi
callq 0xc1b00
jmp 0x271cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
jmp 0x27198
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
movl $0xb8, %esi
callq 0x22a00
leaq 0x1d4ae9(%rip), %rdi    # 0x1fbc88
callq 0x22660
jmp 0x27767
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
leaq 0x238(%rsp), %rdi
callq 0xc1b00
jmp 0x27767
cmpl $0x0, 0x1ce8ea(%rip)    # 0x1f5ac0
je 0x2727a
cmpl $0x2, 0x1d4a3d(%rip)    # 0x1fbc20
jne 0x2727a
movl $0xb50, %edi            # imm = 0xB50
callq 0x22910
leaq 0x1d8a4e(%rip), %rdi    # 0x1ffc48
callq 0x2b540
movq 0x1d4a12(%rip), %rdi    # 0x1fbc18
movl 0x1d4a84(%rip), %esi    # 0x1fbc90
movl $0x7, %edx
callq 0x2de20
xorps %xmm0, %xmm0
movsd 0x166df7(%rip), %xmm1   # 0x18e018
callq 0x23210
movl $0xb44, %edi            # imm = 0xB44
callq 0x22910
movq 0x1d49e1(%rip), %rax    # 0x1fbc18
movq %rax, 0x20(%rsp)
leaq 0x228(%rsp), %rdi
movl $0x772, %esi            # imm = 0x772
callq 0x2b520
movq 0x20(%rsp), %rdi
movq 0x228(%rsp), %rsi
callq 0x2e4d0
movl $0xb44, %edi            # imm = 0xB44
callq 0x22230
xorps %xmm0, %xmm0
movsd 0x166d9b(%rip), %xmm1   # 0x18e010
callq 0x23210
cmpl $0x0, 0x1ce83f(%rip)    # 0x1f5ac0
je 0x272e0
movl $0xb50, %edi            # imm = 0xB50
callq 0x22910
leaq 0x1d89b4(%rip), %rdi    # 0x1ffc48
callq 0x2b540
movq 0x1d4978(%rip), %rdi    # 0x1fbc18
movl 0x1d49ea(%rip), %esi    # 0x1fbc90
movl $0x7, %edx
callq 0x2de20
movq 0x1d4961(%rip), %rax    # 0x1fbc18
movq %rax, 0x18(%rsp)
leaq 0x220(%rsp), %rdi
movl $0x772, %esi            # imm = 0x772
callq 0x2b520
movq 0x18(%rsp), %rdi
movq 0x220(%rsp), %rsi
callq 0x2e4d0
cmpl $0x0, 0x1d49ad(%rip)    # 0x1fbc94
je 0x27336
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
xorps %xmm2, %xmm2
movsd 0x166d12(%rip), %xmm1   # 0x18e010
movaps %xmm2, %xmm0
callq 0x2b4e0
movq 0x1d490b(%rip), %rax    # 0x1fbc18
movq %rax, 0x10(%rsp)
leaq 0x218(%rsp), %rdi
movl $0x1, %esi
callq 0x2b520
movq 0x10(%rsp), %rdi
movq 0x218(%rsp), %rsi
callq 0x2e4d0
cmpl $0x0, 0x1d495b(%rip)    # 0x1fbc98
je 0x27381
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
movsd 0x166cbf(%rip), %xmm0   # 0x18e010
xorps %xmm2, %xmm2
movaps %xmm2, %xmm1
callq 0x2b4e0
movq 0x1d48b5(%rip), %rax    # 0x1fbc18
movq %rax, 0x8(%rsp)
movq 0x1d48a9(%rip), %rdi    # 0x1fbc18
callq 0x2b510
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x28440
cmpl $0x0, 0x1d4914(%rip)    # 0x1fbc9c
je 0x275d3
movq 0x1d4883(%rip), %rdi    # 0x1fbc18
callq 0x2b590
movq %rax, %rdi
callq 0x2b5b0
movsd %xmm0, 0x210(%rsp)
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
movss 0x166cd3(%rip), %xmm0   # 0x18e090
callq 0x23320
callq 0x2b5d0
movl $0x1, %edi
xorl %edx, %edx
movl %edx, %esi
callq 0x2b5e0
movsd 0x166c43(%rip), %xmm0   # 0x18e020
mulsd 0x210(%rsp), %xmm0
leaq 0x1e0(%rsp), %rdi
leaq 0x1d7f53(%rip), %rsi    # 0x1ff348
callq 0x2b630
leaq 0x1f8(%rsp), %rdi
leaq 0x1d7dd7(%rip), %rsi    # 0x1ff1e0
leaq 0x1e0(%rsp), %rdx
callq 0xcce30
leaq 0x1f8(%rsp), %rdi
callq 0x2b610
movsd 0x210(%rsp), %xmm0
leaq 0x1b0(%rsp), %rdi
leaq 0x1d7ec5(%rip), %rsi    # 0x1ff300
callq 0x2b630
leaq 0x1c8(%rsp), %rdi
leaq 0x1d7d91(%rip), %rsi    # 0x1ff1e0
leaq 0x1b0(%rsp), %rdx
callq 0xcce30
leaq 0x1c8(%rsp), %rdi
callq 0x2b610
callq 0x2b670
callq 0x2b5d0
xorl %edx, %edx
movl $0x1, %esi
movl %edx, %edi
callq 0x2b5e0
movsd 0x166b97(%rip), %xmm0   # 0x18e020
mulsd 0x210(%rsp), %xmm0
leaq 0x180(%rsp), %rdi
leaq 0x1d7ebf(%rip), %rsi    # 0x1ff360
callq 0x2b630
leaq 0x198(%rsp), %rdi
leaq 0x1d7d2b(%rip), %rsi    # 0x1ff1e0
leaq 0x180(%rsp), %rdx
callq 0xcce30
leaq 0x198(%rsp), %rdi
callq 0x2b610
movsd 0x210(%rsp), %xmm0
leaq 0x150(%rsp), %rdi
leaq 0x1d7e31(%rip), %rsi    # 0x1ff318
callq 0x2b630
leaq 0x168(%rsp), %rdi
leaq 0x1d7ce5(%rip), %rsi    # 0x1ff1e0
leaq 0x150(%rsp), %rdx
callq 0xcce30
leaq 0x168(%rsp), %rdi
callq 0x2b610
callq 0x2b670
callq 0x2b5d0
xorl %esi, %esi
movl $0x1, %edx
movl %esi, %edi
callq 0x2b5e0
movsd 0x166aeb(%rip), %xmm0   # 0x18e020
mulsd 0x210(%rsp), %xmm0
leaq 0x120(%rsp), %rdi
leaq 0x1d7e2b(%rip), %rsi    # 0x1ff378
callq 0x2b630
leaq 0x138(%rsp), %rdi
leaq 0x1d7c7f(%rip), %rsi    # 0x1ff1e0
leaq 0x120(%rsp), %rdx
callq 0xcce30
leaq 0x138(%rsp), %rdi
callq 0x2b610
movsd 0x210(%rsp), %xmm0
leaq 0xf0(%rsp), %rdi
leaq 0x1d7d9d(%rip), %rsi    # 0x1ff330
callq 0x2b630
leaq 0x108(%rsp), %rdi
leaq 0x1d7c39(%rip), %rsi    # 0x1ff1e0
leaq 0xf0(%rsp), %rdx
callq 0xcce30
leaq 0x108(%rsp), %rdi
callq 0x2b610
callq 0x2b670
movss 0x166aba(%rip), %xmm0   # 0x18e088
callq 0x23320
cmpl $0x0, 0x1d46c6(%rip)    # 0x1fbca0
je 0x276e0
cmpb $0x0, 0x1d46d1(%rip)    # 0x1fbcb8
jne 0x27614
leaq 0x1d46c8(%rip), %rdi    # 0x1fbcb8
callq 0x23310
cmpl $0x0, %eax
je 0x27614
leaq 0x1d46a7(%rip), %rdi    # 0x1fbca8
callq 0xeeb80
jmp 0x27608
leaq 0x1d46a9(%rip), %rdi    # 0x1fbcb8
callq 0x22680
leaq 0x1d468d(%rip), %rdi    # 0x1fbca8
callq 0xeec40
movsd %xmm0, 0x68(%rsp)
leaq 0x1d467b(%rip), %rdi    # 0x1fbca8
callq 0x2b680
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x276de
movsd 0x1669db(%rip), %xmm0   # 0x18e028
ucomisd 0x68(%rsp), %xmm0
jbe 0x276de
movl $0xb50, %edi            # imm = 0xB50
callq 0x22230
movsd 0x1669a5(%rip), %xmm2   # 0x18e010
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x2b4e0
leaq 0x70(%rsp), %rdi
movsd 0x16698d(%rip), %xmm0   # 0x18e010
divsd 0x68(%rsp), %xmm0
leaq 0x166b8e(%rip), %rsi    # 0x18e21e
movb $0x1, %al
callq 0x22160
movq 0x1d458a(%rip), %rdi    # 0x1fbc28
leaq 0x70(%rsp), %rsi
movsd 0x166985(%rip), %xmm1   # 0x18e030
xorl %eax, %eax
movl %eax, %edx
movaps %xmm1, %xmm0
callq 0x5d100
jmp 0x276de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
leaq 0x1d45e4(%rip), %rdi    # 0x1fbcb8
callq 0x22660
jmp 0x27767
jmp 0x276e0
cmpq $0x0, 0x1d45d8(%rip)    # 0x1fbcc0
je 0x2775a
movl 0x1ce3d4(%rip), %esi    # 0x1f5ac4
movl 0x1ce3d2(%rip), %edx    # 0x1f5ac8
leaq 0x50(%rsp), %rdi
movq %rdi, (%rsp)
movl $0x3, %ecx
callq 0xe5c60
movq (%rsp), %rdi
callq 0xe7080
jmp 0x27714
movq 0x1d45a5(%rip), %rsi    # 0x1fbcc0
leaq 0x50(%rsp), %rdi
callq 0x2b6a0
jmp 0x27727
callq 0x26e00
jmp 0x2772e
leaq 0x50(%rsp), %rdi
callq 0xe6180
jmp 0x2775a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
leaq 0x50(%rsp), %rdi
callq 0xe6180
jmp 0x27767
callq 0x179fe0
addq $0x358, %rsp            # imm = 0x358
retq
movq 0x350(%rsp), %rdi
callq 0x23290
nopw %cs:(%rax,%rax)