// File: tomfunkhouser[P]gaps/apps/scn2cam/scn2cam.cpp
// Function: DrawNodeWithOpenGL(gaps::R3Scene*, gaps::R3SceneNode*, gaps::R3SceneNode*, int)
static void 
DrawNodeWithOpenGL(R3Scene *scene, R3SceneNode *node, R3SceneNode *selected_node, int image_type)
{
  // Set color based on node index
  RNFlags draw_flags = R3_DEFAULT_DRAW_FLAGS;
  if (image_type == NODE_INDEX_IMAGE) {
    draw_flags = R3_SURFACES_DRAW_FLAG;
    unsigned int node_index = node->SceneIndex() + 1;
    unsigned char color[4];
    color[0] = (node_index >> 16) & 0xFF;
    color[1] = (node_index >>  8) & 0xFF;
    color[2] = (node_index      ) & 0xFF;
    RNLoadRgb(color);
  }
  
  // Draw elements
  if (!selected_node || (selected_node == node)) {
    for (int i = 0; i < node->NElements(); i++) {
      R3SceneElement *element = node->Element(i);
      element->Draw(draw_flags);
    }
  }

  // Draw references 
  if (!selected_node || (selected_node == node)) {
    for (int i = 0; i < node->NReferences(); i++) {
      R3SceneReference *reference = node->Reference(i);
      reference->Draw(draw_flags);
    }
  }

  // Draw children
  for (int i = 0; i < node->NChildren(); i++) {
    R3SceneNode *child = node->Child(i);
    DrawNodeWithOpenGL(scene, child, selected_node, image_type);
  }
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movl %ecx, 0x6c(%rsp)
leaq 0x60(%rsp), %rdi
movl $0x772, %esi            # imm = 0x772
callq 0x33580
cmpl $0x0, 0x6c(%rsp)
jne 0x2faa4
leaq 0x58(%rsp), %rdi
movl $0x2, %esi
callq 0x33580
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x78(%rsp), %rdi
callq 0x335a0
addl $0x1, %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, 0x50(%rsp)
movl 0x54(%rsp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, 0x51(%rsp)
movl 0x54(%rsp), %eax
andl $0xff, %eax
movb %al, 0x52(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x335b0
cmpq $0x0, 0x70(%rsp)
je 0x2fab8
movq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
jne 0x2fb1b
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x78(%rsp), %rdi
callq 0x32480
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x2fb19
movq 0x78(%rsp), %rdi
movl 0x4c(%rsp), %esi
callq 0x331c0
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
movq 0x60(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x63780
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x2fac0
jmp 0x2fb1b
cmpq $0x0, 0x70(%rsp)
je 0x2fb2f
movq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
jne 0x2fb92
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x78(%rsp), %rdi
callq 0x335c0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jge 0x2fb90
movq 0x78(%rsp), %rdi
movl 0x34(%rsp), %esi
callq 0x335e0
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movq 0x60(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x63fe0
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x2fb37
jmp 0x2fb92
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x78(%rsp), %rdi
callq 0x33610
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x2fbf1
movq 0x78(%rsp), %rdi
movl 0x1c(%rsp), %esi
callq 0x33630
movq %rax, 0x10(%rsp)
movq 0x80(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x70(%rsp), %rdx
movl 0x6c(%rsp), %ecx
callq 0x2fa10
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x2fb9a
addq $0x88, %rsp
retq
nopl (%rax)