// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::WriteObj(gaps::R3SceneNode*, gaps::R3Affine const&, int&, int&, int&, int&, _IO_FILE*) const
int R3Scene::
WriteObj(R3SceneNode *node, const R3Affine& transformation, int &ngroups, int& nvertices, int& nnormals, int& ntexture_coords, FILE *fp) const
{
  // Write group name
  if (node->Name()) fprintf(fp, "g %s\n", node->Name());
  else fprintf(fp, "g GROUP_%d\n", ++ngroups);
  
  // Write elements
  for (int i = 0; i < node->NElements(); i++) {
    R3SceneElement *element = node->Element(i);

    // Write material
    R3Material *material = element->Material();
    if (material) fprintf(fp, "usemtl %s\n", material->Name());
    else fprintf(fp, "usemtl _DEFAULT_\n");
      
    // Write shapes
    for (int j = 0; j < element->NShapes(); j++) {
      R3Shape *shape = element->Shape(j);
      if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
        R3TriangleArray *triangles = (R3TriangleArray *) shape;
        if (triangles->NVertices() == 0) continue;
        
        // Allocate indices for positions, texture coordinates, and normals
        int *pi = new int [ triangles->NVertices() ];
        for (int k = 0; k < triangles->NVertices(); k++) pi[k] = -1;
        int *ni = new int [ triangles->NVertices() ];
        for (int k = 0; k < triangles->NVertices(); k++) ni[k] = -1;
        int *ti = new int [ triangles->NVertices() ];
        for (int k = 0; k < triangles->NVertices(); k++) ti[k] = -1;

        // Write vertices
        for (int k = 0; k < triangles->NVertices(); k++) {
          R3TriangleVertex *v = triangles->Vertex(k);
          v->SetMark(k);
          if (TRUE) {
            R3Point p = v->Position();
            p.Transform(transformation);
            fprintf(fp, "v %g %g %g\n", p.X(), p.Y(), p.Z());
            pi[k] = ++nvertices;
          }
          if (v->Flags()[R3_VERTEX_NORMALS_DRAW_FLAG]) {
            R3Vector n = v->Normal();
            n.Transform(transformation);
            fprintf(fp, "vn %g %g %g\n", n.X(), n.Y(), n.Z());
            ni[k] = ++nnormals;
          }
          if (v->Flags()[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG]) {
            R2Point t = v->TextureCoords();
            fprintf(fp, "vt %g %g\n", t.X(), t.Y());
            ti[k] = ++ntexture_coords;
          }
        }
       
        // Write triangles
        for (int k = 0; k < triangles->NTriangles(); k++) {
          R3Triangle *triangle = triangles->Triangle(k);
          R3TriangleVertex *v0 = triangle->V0();
          R3TriangleVertex *v1 = triangle->V1();
          R3TriangleVertex *v2 = triangle->V2();
          unsigned int i0 = v0->Mark();
          unsigned int i1 = v1->Mark();
          unsigned int i2 = v2->Mark();
          if (triangle->Flags()[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG] && triangle->Flags()[R3_VERTEX_NORMALS_DRAW_FLAG]) {
            if (transformation.IsMirrored()) fprintf(fp, "f %u/%u/%u %u/%u/%u %u/%u/%u\n", pi[i2], ti[i2], ni[i2], pi[i1], ti[i1], ni[i1], pi[i0], ti[i0], ni[i0]);
            else fprintf(fp, "f %u/%u/%u %u/%u/%u %u/%u/%u\n", pi[i0], ti[i0], ni[i0], pi[i1], ti[i1], ni[i1], pi[i2], ti[i2], ni[i2]);
          }
          else if (triangle->Flags()[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG]) {
            if (transformation.IsMirrored()) fprintf(fp, "f %u/%u %u/%u %u/%u\n",  pi[i2], ti[i2],  pi[i1], ti[i1],  pi[i0], ti[i0]);
            else fprintf(fp, "f %u/%u %u/%u %u/%u\n",  pi[i0], ti[i0],  pi[i1], ti[i1],  pi[i2], ti[i2]);
          }
          else if (triangle->Flags()[R3_VERTEX_NORMALS_DRAW_FLAG]) {
            if (transformation.IsMirrored()) fprintf(fp, "f %u//%u %u//%u %u//%u\n",  pi[i2], ni[i2],  pi[i1], ni[i1],  pi[i0], ni[i0]);
            else fprintf(fp, "f %u//%u %u//%u %u//%u\n",  pi[i0], ni[i0],  pi[i1], ni[i1],  pi[i2], ni[i2]);
          }
          else {
            if (transformation.IsMirrored()) fprintf(fp, "f %u %u %u\n",  pi[i2], pi[i1], pi[i0]);
            else fprintf(fp, "f %u %u %u\n", pi[i0], pi[i1], pi[i2]);
          }
        }

        // Delete indices for positions, texture coordinates, and normals
        delete [] pi;
        delete [] ni;
        delete [] ti;
      }
    }
  }

  // Write children
  for (int i = 0; i < node->NChildren(); i++) {
    R3SceneNode *child = node->Child(i);

    // Update transformation
    R3Affine child_transformation = R3identity_affine;
    child_transformation.Transform(transformation);
    child_transformation.Transform(child->Transformation());

    // Write child
    if (!WriteObj(child, child_transformation, ngroups, nvertices, nnormals, ntexture_coords, fp)) return 0;
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r9, %rbx
movq %r8, %r14
movq %rdx, %r15
movq %rdi, 0x40(%rsp)
movq 0x1b8(%rsp), %rdi
movq %rsi, 0x28(%rsp)
movq 0x1a0(%rsi), %rdx
testq %rdx, %rdx
movq %rcx, 0x48(%rsp)
je 0x3327b
leaq 0xaa4fd(%rip), %rsi     # 0xdd76f
xorl %eax, %eax
callq 0x21d90
jmp 0x3328f
movl (%rcx), %edx
incl %edx
movl %edx, (%rcx)
leaq 0xa9485(%rip), %rsi     # 0xdc70d
xorl %eax, %eax
callq 0x21d90
movq %r15, 0x8(%rsp)
movq %r14, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x28(%rsp), %rcx
cmpl $0x0, 0x3c(%rcx)
jle 0x33786
movq $-0x1, %r15
xorl %edx, %edx
movq 0x30(%rcx), %rax
movq (%rax,%rdx,8), %r14
movq 0x8(%r14), %rax
testq %rax, %rax
movq %rdx, 0x50(%rsp)
je 0x332e8
movq 0x10(%rax), %rdx
movq 0x1b8(%rsp), %rdi
leaq 0xa943a(%rip), %rsi     # 0xdc719
xorl %eax, %eax
callq 0x21d90
jmp 0x33306
movl $0x11, %esi
movl $0x1, %edx
leaq 0xa942b(%rip), %rdi     # 0xdc724
movq 0x1b8(%rsp), %rcx
callq 0x222a0
cmpl $0x0, 0x1c(%r14)
jle 0x3376c
xorl %r12d, %r12d
movq %r14, 0x58(%rsp)
movq 0x10(%r14), %rax
movq (%rax,%r12,8), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0xb8(%rax)
movl %eax, %ebx
callq 0x6867c
cmpl %eax, %ebx
jne 0x3375c
movslq 0x14(%r13), %rax
testq %rax, %rax
je 0x3375c
movq %rax, %rdi
shlq $0x2, %rdi
testl %eax, %eax
cmovsq %r15, %rdi
callq 0x21040
movl 0x14(%r13), %edx
testl %edx, %edx
jle 0x33379
xorl %ecx, %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
incq %rcx
movslq 0x14(%r13), %rdx
cmpq %rdx, %rcx
jl 0x33366
movq %rax, 0x10(%rsp)
movslq %edx, %rax
leaq (,%rax,4), %rdi
testl %eax, %eax
cmovsq %r15, %rdi
callq 0x21040
movl 0x14(%r13), %edx
testl %edx, %edx
movq 0x30(%rsp), %r14
movq 0x8(%rsp), %rbp
jle 0x333bb
xorl %ecx, %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
incq %rcx
movslq 0x14(%r13), %rdx
cmpq %rdx, %rcx
jl 0x333a8
movq %rax, 0x20(%rsp)
movq %r12, 0x60(%rsp)
movslq %edx, %rax
leaq (,%rax,4), %rdi
testl %eax, %eax
cmovsq %r15, %rdi
callq 0x21040
cmpl $0x0, 0x14(%r13)
jle 0x333f7
xorl %edx, %edx
movl $0xffffffff, (%rax,%rdx,4) # imm = 0xFFFFFFFF
incq %rdx
movslq 0x14(%r13), %rcx
cmpq %rcx, %rdx
jl 0x333e4
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x14(%r13)
jle 0x3352a
xorl %r12d, %r12d
movq 0x8(%r13), %rax
movq (%rax,%r12,8), %r15
movl %r12d, 0x60(%r15)
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x762ca
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x76bd6
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movq 0x1b8(%rsp), %rdi
leaq 0xa92e4(%rip), %rsi     # 0xdc736
movb $0x3, %al
callq 0x21d90
movl (%r14), %eax
incl %eax
movl %eax, (%r14)
movq 0x10(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
testb $0x1, 0x59(%r15)
je 0x334c9
leaq 0x18(%r15), %rsi
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x76d62
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x7792e
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movq 0x1b8(%rsp), %rdi
leaq 0xa9294(%rip), %rsi     # 0xdc742
movb $0x3, %al
callq 0x21d90
movq 0x38(%rsp), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
movq 0x20(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
testb $0x2, 0x59(%r15)
je 0x3351a
addq $0x48, %r15
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x82aa0
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movq 0x1b8(%rsp), %rdi
leaq 0xa9253(%rip), %rsi     # 0xdc74f
movb $0x2, %al
callq 0x21d90
movq 0x1b0(%rsp), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
movq 0x18(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movslq 0x14(%r13), %rax
cmpq %rax, %r12
jl 0x3340a
cmpl $0x0, 0x24(%r13)
jle 0x3372d
xorl %r15d, %r15d
movq 0x18(%r13), %rax
movq (%rax,%r15,8), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rsi
movl 0x60(%rcx), %r12d
movl 0x60(%rdx), %ebp
movl 0x60(%rsi), %r14d
movq 0x78(%rax), %rbx
btl $0x9, %ebx
jb 0x33598
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
btl $0x8, %ebx
movq 0x10(%rsp), %rsi
movl (%rsi,%rbp,4), %r10d
jb 0x335de
testl %eax, %eax
movq 0x1b8(%rsp), %rdi
je 0x3366c
movl (%rsi,%r14,4), %edx
movl (%rsi,%r12,4), %r8d
jmp 0x33674
btl $0x8, %ebx
jb 0x3360c
btl $0x9, %ebx
jae 0x33561
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x10(%rsp), %rsi
movl (%rsi,%rbp,4), %r8d
movq 0x18(%rsp), %rdi
movl (%rdi,%rbp,4), %r9d
testl %eax, %eax
je 0x336ef
movl (%rsi,%r14,4), %edx
movl (%rdi,%r14,4), %ecx
movl (%rsi,%r12,4), %r10d
movl (%rdi,%r12,4), %r11d
jmp 0x336ff
movq 0x20(%rsp), %r8
movl (%r8,%rbp,4), %r9d
testl %eax, %eax
movq 0x1b8(%rsp), %rdi
je 0x3368a
movl (%rsi,%r14,4), %edx
movl (%r8,%r14,4), %ecx
movl (%rsi,%r12,4), %r11d
movl (%r8,%r12,4), %ebx
jmp 0x3369a
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x10(%rsp), %rsi
movl (%rsi,%rbp,4), %r9d
movq 0x18(%rsp), %rbx
movl (%rbx,%rbp,4), %r10d
movq 0x20(%rsp), %rdi
movl (%rdi,%rbp,4), %r11d
testl %eax, %eax
je 0x336ab
movl (%rsi,%r14,4), %edx
movl (%rbx,%r14,4), %ecx
movl (%rdi,%r14,4), %r8d
movq %rbx, %rax
movl (%rsi,%r12,4), %ebx
movl (%rax,%r12,4), %r14d
movl (%rdi,%r12,4), %r12d
subq $0x8, %rsp
movq 0x1c0(%rsp), %rdi
leaq 0xa90f5(%rip), %rsi     # 0xdc759
xorl %eax, %eax
pushq %r12
pushq %r14
jmp 0x336df
movl (%rsi,%r12,4), %edx
movl (%rsi,%r14,4), %r8d
leaq 0xa9129(%rip), %rsi     # 0xdc7a4
movl %r10d, %ecx
xorl %eax, %eax
callq 0x21d90
jmp 0x3371d
movl (%rsi,%r12,4), %edx
movl (%r8,%r12,4), %ecx
movl (%rsi,%r14,4), %r11d
movl (%r8,%r14,4), %ebx
leaq 0xa90eb(%rip), %rsi     # 0xdc78c
movl %r10d, %r8d
xorl %eax, %eax
pushq %rbx
pushq %r11
jmp 0x33714
movl (%rsi,%r12,4), %edx
movl (%rbx,%r12,4), %ecx
movl (%rdi,%r12,4), %r8d
movq %rbx, %rax
movl (%rsi,%r14,4), %ebx
movl (%rax,%r14,4), %r12d
movl (%rdi,%r14,4), %r14d
subq $0x8, %rsp
movq 0x1c0(%rsp), %rdi
leaq 0xa9080(%rip), %rsi     # 0xdc759
xorl %eax, %eax
pushq %r14
pushq %r12
pushq %rbx
pushq %r11
pushq %r10
callq 0x21d90
addq $0x30, %rsp
jmp 0x3371d
movl (%rsi,%r12,4), %edx
movl (%rdi,%r12,4), %ecx
movl (%rsi,%r14,4), %r10d
movl (%rdi,%r14,4), %r11d
movq 0x1b8(%rsp), %rdi
leaq 0xa9069(%rip), %rsi     # 0xdc777
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x21d90
addq $0x10, %rsp
incq %r15
movslq 0x24(%r13), %rax
cmpq %rax, %r15
jl 0x33538
movq 0x10(%rsp), %rdi
callq 0x21c30
movq 0x20(%rsp), %rdi
callq 0x21c30
movq 0x18(%rsp), %rdi
callq 0x21c30
movq $-0x1, %r15
movq 0x58(%rsp), %r14
movq 0x60(%rsp), %r12
incq %r12
movslq 0x1c(%r14), %rax
cmpq %rax, %r12
jl 0x33319
movq 0x50(%rsp), %rdx
incq %rdx
movq 0x28(%rsp), %rcx
movslq 0x3c(%rcx), %rax
cmpq %rax, %rdx
jl 0x332b6
cmpl $0x0, 0x2c(%rcx)
jle 0x33862
xorl %r12d, %r12d
leaq 0x1079ae(%rip), %r14    # 0x13b148
addq $0x10, %r14
leaq 0x113edb(%rip), %rax    # 0x147680
leaq 0x8(%rax), %r15
movl $0x88, %r13d
addq 0x10b822(%rip), %r13    # 0x13efd8
leaq 0x68(%rsp), %rbp
movq 0x20(%rcx), %rax
movq (%rax,%r12,8), %rbx
movq %r14, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x72bb6
leaq 0xf0(%rsp), %rdi
movq %r13, %rsi
callq 0x72bb6
leaq 0x113e94(%rip), %rax    # 0x147680
movq 0x108(%rax), %rax
movq %rax, 0x170(%rsp)
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x709e6
leaq 0x58(%rbx), %rsi
movq %rbp, %rdi
callq 0x709e6
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq 0x48(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r9
pushq 0x1b8(%rsp)
pushq 0x1b8(%rsp)
callq 0x3322e
addq $0x10, %rsp
movq %rbp, %rdi
callq 0x70878
incq %r12
movq 0x28(%rsp), %rcx
movslq 0x2c(%rcx), %rax
cmpq %rax, %r12
jl 0x337bb
movl $0x1, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x70f00
jmp 0x33895
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x70878
movq %rbx, %rdi
callq 0x220d0
nop