// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::InsertSceneElement(gaps::R3Scene*, gaps::R3SceneNode*, gaps::R3Material*, gaps::RNArray<gaps::R3TriangleVertex*> const&, gaps::RNArray<gaps::R3Triangle*> const&, int)
static int
InsertSceneElement(R3Scene *scene, R3SceneNode *node, R3Material *material, 
  const RNArray<R3TriangleVertex *>& verts, const RNArray<R3Triangle *>& tris,
  RNBoolean smooth_normals = FALSE)
{
  // Create material if none
  if (!material) {
    R3Brdf *brdf = new R3Brdf(R3default_brdf, "Default");
    scene->InsertBrdf(brdf);
    material = new R3Material(brdf, "Default");
    scene->InsertMaterial(material);
  }
  
  // Find element
  R3SceneElement *element = NULL;
  for (int i = 0; i < node->NElements(); i++) {
    R3SceneElement *g = node->Element(i);
    if (g->Material() == material) { element = g; break; }
  }

  // Create element
  if (!element) {
    element = new R3SceneElement(material);
  }

  // Set vertex marks
  for (int i = 0; i < verts.NEntries(); i++) {
    R3TriangleVertex *vertex = verts.Kth(i);
    vertex->SetMark(0);
  }

  // Create copies of verts and tris for this element
  RNArray<R3Triangle *> element_tris;
  RNArray<R3TriangleVertex *> element_verts;
  for (int i = 0; i < tris.NEntries(); i++) {
    R3Triangle *triangle = tris.Kth(i);
    R3TriangleVertex *element_vertex[3];
    for (int j = 0; j < 3; j++) {
      R3TriangleVertex *vertex = triangle->Vertex(j);
      if (vertex->Mark() == 0) {
        element_verts.Insert(new R3TriangleVertex(*vertex));
        vertex->SetMark(element_verts.NEntries());
      }
      element_vertex[j] = element_verts.Kth(vertex->Mark()-1); 
    }
    R3Triangle *element_triangle = new R3Triangle(element_vertex[0], element_vertex[1], element_vertex[2]);
    element_tris.Insert(element_triangle);
  }

  // Create shape (triangle array)
  R3TriangleArray *shape = new R3TriangleArray(element_verts, element_tris);

  // Create smooth normals
  if (smooth_normals) shape->CreateVertexNormals();

  // Insert shape
  element->InsertShape(shape);
        
  // Insert element
  node->InsertElement(element);

  // Return success
  return 1;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq %rdx, 0x120(%rsp)
movq %rcx, 0x118(%rsp)
movq %r8, 0x110(%rsp)
movl %r9d, 0x10c(%rsp)
cmpq $0x0, 0x120(%rsp)
jne 0x3c891
movl $0xb8, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x58(%rsp)
leaq 0x1bfe2e(%rip), %rsi    # 0x1fc5f0
leaq 0x152f63(%rip), %rdx    # 0x18f72c
callq 0x645c0
jmp 0x3c7d0
movq 0x58(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x130(%rsp), %rdi
movq 0x100(%rsp), %rsi
callq 0x2ccd0
movl $0x30, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x50(%rsp)
movq 0x100(%rsp), %rsi
leaq 0x152f16(%rip), %rdx    # 0x18f72c
callq 0x63e20
jmp 0x3c81d
movq 0x50(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x130(%rsp), %rdi
movq 0x120(%rsp), %rsi
callq 0x2cbd0
jmp 0x3c891
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0xb8, %esi
callq 0x22a00
jmp 0x3cd38
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x30, %esi
callq 0x22a00
jmp 0x3cd38
movq $0x0, 0xe8(%rsp)
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x128(%rsp), %rdi
callq 0x2bba0
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x3c924
movq 0x128(%rsp), %rdi
movl 0xe4(%rsp), %esi
callq 0x2bbc0
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x48b50
cmpq 0x120(%rsp), %rax
jne 0x3c90f
movq 0xd8(%rsp), %rax
movq %rax, 0xe8(%rsp)
jmp 0x3c924
jmp 0x3c911
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x3c8a8
cmpq $0x0, 0xe8(%rsp)
jne 0x3c98a
movl $0x60, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq 0x120(%rsp), %rsi
callq 0x58f20
jmp 0x3c953
movq 0x40(%rsp), %rax
movq %rax, 0xe8(%rsp)
jmp 0x3c98a
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x60, %esi
callq 0x22a00
jmp 0x3cd38
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x118(%rsp), %rdi
callq 0x2b8b0
movl %eax, %ecx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x3c9f5
movq 0x118(%rsp), %rdi
movl 0xd4(%rsp), %esi
callq 0x49090
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rdi
xorl %esi, %esi
callq 0x48bb0
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x3c995
leaq 0xb8(%rsp), %rdi
callq 0x49140
leaq 0xa8(%rsp), %rdi
callq 0x48d30
jmp 0x3ca11
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x110(%rsp), %rdi
callq 0x2b8b0
movl %eax, %ecx
movl 0x38(%rsp), %eax
cmpl %ecx, %eax
jge 0x3cc3e
movq 0x110(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x492b0
movq %rax, 0x30(%rsp)
jmp 0x3ca5d
movq 0x30(%rsp), %rax
movq %rax, 0x98(%rsp)
movl $0x0, 0x7c(%rsp)
cmpl $0x3, 0x7c(%rsp)
jge 0x3cbab
movq 0x98(%rsp), %rdi
movl 0x7c(%rsp), %esi
callq 0x2bc90
movq %rax, 0x28(%rsp)
jmp 0x3ca95
movq 0x28(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
callq 0x48c50
cmpl $0x0, %eax
jne 0x3cb67
movl $0x68, %edi
callq 0x229e0
movq %rax, 0x20(%rsp)
jmp 0x3cac3
movq 0x20(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0xb0640
jmp 0x3cad4
movq 0x20(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x48f60
jmp 0x3cae8
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x2b8b0
movq 0x18(%rsp), %rdi
movl %eax, %esi
callq 0x48bb0
jmp 0x3cb67
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
jmp 0x3cd2b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
jmp 0x3cd1e
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x3cd1e
movq 0x70(%rsp), %rdi
callq 0x48c50
movl %eax, %esi
decl %esi
leaq 0xa8(%rsp), %rdi
callq 0x49090
movq %rax, 0x10(%rsp)
jmp 0x3cb89
movq 0x10(%rsp), %rcx
movslq 0x7c(%rsp), %rax
movq %rcx, 0x80(%rsp,%rax,8)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x3ca72
movl $0x88, %edi
callq 0x229e0
movq %rax, 0x8(%rsp)
jmp 0x3cbbc
movq 0x8(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
movq 0x90(%rsp), %rcx
callq 0xb1110
jmp 0x3cbe0
movq 0x8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x49190
jmp 0x3cbfe
jmp 0x3cc00
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x3ca1c
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x88, %esi
callq 0x22a00
jmp 0x3cd1e
movl $0x70, %edi
callq 0x229e0
movq %rax, (%rsp)
jmp 0x3cc4e
movq (%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0xacc40
jmp 0x3cc69
movq (%rsp), %rax
movq %rax, 0x60(%rsp)
cmpl $0x0, 0x10c(%rsp)
je 0x3cccc
movq 0x60(%rsp), %rdi
leaq 0x154f78(%rip), %rax    # 0x191c00
movsd (%rax), %xmm0
movsd 0x151914(%rip), %xmm1   # 0x18e5a8
mulsd %xmm1, %xmm0
movq (%rdi), %rax
movq 0x230(%rax), %rax
callq *%rax
jmp 0x3cca6
jmp 0x3cccc
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x70, %esi
callq 0x22a00
jmp 0x3cd1e
movq 0xe8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x59370
jmp 0x3cce0
movq 0x128(%rsp), %rdi
movq 0xe8(%rsp), %rsi
callq 0x57510
jmp 0x3ccf7
leaq 0xa8(%rsp), %rdi
callq 0x49340
leaq 0xb8(%rsp), %rdi
callq 0x492a0
movl $0x1, %eax
addq $0x138, %rsp            # imm = 0x138
retq
leaq 0xa8(%rsp), %rdi
callq 0x49340
leaq 0xb8(%rsp), %rdi
callq 0x492a0
movq 0xf8(%rsp), %rdi
callq 0x23290
nopw %cs:(%rax,%rax)