// File: tomfunkhouser[P]gaps/apps/scnview/scnview.cpp
// Function: DrawNormals(gaps::R3Scene*, gaps::R3SceneNode*)
static void 
DrawNormals(R3Scene *scene, R3SceneNode *node)
{
  // Push transformation
  node->Transformation().Push();

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

  // Draw reference normals
  for (int i = 0; i < node->NReferences(); i++) {
    R3SceneReference *reference = node->Reference(i);
    R3Scene *referenced_scene = reference->ReferencedScene();
    DrawNormals(referenced_scene, referenced_scene->Root());
  }

  // Draw element normals
  RNGrfxBegin(RN_GRFX_LINES);
  double r = 0.01 * scene->BBox().DiagonalRadius();
  for (int i = 0; i < node->NElements(); i++) {
    R3SceneElement *element = node->Element(i);
    for (int j = 0; j < element->NShapes(); j++) {
      R3Shape *shape = element->Shape(j);
      if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
        R3TriangleArray *triangles = (R3TriangleArray *) shape;
        for (int k = 0; k < triangles->NTriangles(); k++) {
          R3Triangle *triangle = triangles->Triangle(k);
          // R3LoadPoint(triangle->Centroid());
          // R3LoadPoint(triangle->Centroid() + r * triangle->Normal());
          for (int v = 0; v < 3; v++) {
            R3TriangleVertex *vertex = triangle->Vertex(v);
            if (!vertex->HasNormal()) continue;
            R3LoadPoint(vertex->Position());
            R3LoadPoint(vertex->Position() + r * vertex->Normal());
          }
        }
      }
    }
  }
  RNGrfxEnd();
  
  // Pop transformation
  node->Transformation().Pop();
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x2bae0
movq %rax, %rdi
movq (%rdi), %rax
callq *0xb0(%rax)
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x2baf0
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x27cf1
movq 0xd8(%rsp), %rdi
movl 0xd4(%rsp), %esi
callq 0x2bb10
movq %rax, 0xc8(%rsp)
movq 0xe0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x27c50
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x27c8b
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x2bb40
movl %eax, %ecx
movl 0x28(%rsp), %eax
cmpl %ecx, %eax
jge 0x27d8c
movq 0xd8(%rsp), %rdi
movl 0xc4(%rsp), %esi
callq 0x2bb60
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x2bb90
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x2b510
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x27c50
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x27cfc
movl $0x1, %edi
callq 0x2b960
movq 0xe0(%rsp), %rdi
callq 0x2b590
movq %rax, %rdi
callq 0x2b5b0
movaps %xmm0, %xmm1
movsd 0x166282(%rip), %xmm0   # 0x18e038
mulsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x2bba0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x27fca
movq 0xd8(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x2bbc0
movq %rax, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x98(%rsp), %rdi
callq 0x2bbf0
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x27fb2
movq 0x98(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x2bc10
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
movq (%rdi), %rax
callq *0xb8(%rax)
movl %eax, 0x14(%rsp)
callq 0xac600
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jne 0x27f9a
movq 0x88(%rsp), %rax
movq %rax, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x80(%rsp), %rdi
callq 0x2bc40
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jge 0x27f98
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
callq 0x2bc60
movq %rax, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
cmpl $0x3, 0x6c(%rsp)
jge 0x27f86
movq 0x70(%rsp), %rdi
movl 0x6c(%rsp), %esi
callq 0x2bc90
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
callq 0x2bcb0
cmpl $0x0, %eax
jne 0x27f0d
jmp 0x27f76
movq 0x60(%rsp), %rdi
callq 0x2bcf0
movq %rax, %rdi
callq 0x2b610
movq 0x60(%rsp), %rdi
callq 0x2bcf0
movq %rax, 0x8(%rsp)
movsd 0xa8(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x60(%rsp), %rdi
callq 0x2bd00
movsd (%rsp), %xmm0
movq %rax, %rsi
leaq 0x30(%rsp), %rdi
callq 0x2b630
movq 0x8(%rsp), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0xcce30
leaq 0x48(%rsp), %rdi
callq 0x2b610
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x27ede
jmp 0x27f88
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x27e9d
jmp 0x27f9a
jmp 0x27f9c
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x27e1b
jmp 0x27fb4
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x27dce
callq 0x2b9d0
movq 0xd8(%rsp), %rdi
callq 0x2bae0
movq %rax, %rdi
movq (%rdi), %rax
callq *0xb8(%rax)
addq $0xe8, %rsp
retq