// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x1b8(%rsp), %rbp
movq %rsi, 0x18(%rsp)
movq 0x1a0(%rsi), %rdx
testq %rdx, %rdx
movq %rcx, 0x48(%rsp)
je 0x32f66
leaq 0xa8825(%rip), %rsi     # 0xdb77f
movq %rbp, %rdi
xorl %eax, %eax
callq 0x21d80
jmp 0x32f7d
movl (%rcx), %edx
incl %edx
movl %edx, (%rcx)
leaq 0xa77aa(%rip), %rsi     # 0xda71d
movq %rbp, %rdi
xorl %eax, %eax
callq 0x21d80
movq %r14, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq 0x18(%rsp), %rcx
cmpl $0x0, 0x3c(%rcx)
jle 0x33422
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 0x32fcc
movq 0x10(%rax), %rdx
movq %rbp, %rdi
leaq 0xa7766(%rip), %rsi     # 0xda729
xorl %eax, %eax
callq 0x21d80
jmp 0x32fe5
movl $0x11, %esi
movl $0x1, %edx
leaq 0xa7757(%rip), %rdi     # 0xda734
movq %rbp, %rcx
callq 0x22290
cmpl $0x0, 0x1c(%r14)
jle 0x33408
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 0x678ec
cmpl %eax, %ebx
jne 0x333f8
movslq 0x14(%r13), %rax
testq %rax, %rax
je 0x333f8
movq %rax, %rdi
shlq $0x2, %rdi
testl %eax, %eax
cmovsq %r15, %rdi
callq 0x21040
movslq 0x14(%r13), %rdi
testq %rdi, %rdi
jle 0x3305e
xorl %edx, %edx
movl $0xffffffff, (%rax,%rdx,4) # imm = 0xFFFFFFFF
incq %rdx
movl 0x14(%r13), %ecx
movslq %ecx, %rdi
cmpq %rdi, %rdx
jl 0x33046
jmp 0x33060
movl %edi, %ecx
movq %rax, 0x20(%rsp)
shlq $0x2, %rdi
testl %ecx, %ecx
cmovsq %r15, %rdi
callq 0x21040
movslq 0x14(%r13), %rdi
testq %rdi, %rdi
movq 0x28(%rsp), %r14
movq %r12, 0x60(%rsp)
jle 0x330a1
xorl %ecx, %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
incq %rcx
movl 0x14(%r13), %edx
movslq %edx, %rdi
cmpq %rdi, %rcx
jl 0x33089
jmp 0x330a3
movl %edi, %edx
movq %rax, (%rsp)
shlq $0x2, %rdi
testl %edx, %edx
cmovsq %r15, %rdi
callq 0x21040
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x14(%r13)
jle 0x33205
xorl %eax, %eax
movq 0x8(%rsp), %rdx
movl $0xffffffff, (%rdx,%rax,4) # imm = 0xFFFFFFFF
incq %rax
movslq 0x14(%r13), %rcx
cmpq %rcx, %rax
jl 0x330cd
testl %ecx, %ecx
jle 0x33205
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 0x753da
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x75ce0
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movq %rbp, %rdi
leaq 0xa7616(%rip), %rsi     # 0xda746
movb $0x3, %al
callq 0x21d80
movl (%r14), %eax
incl %eax
movl %eax, (%r14)
movq 0x20(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
movq 0x58(%r15), %rax
btl $0x8, %eax
jae 0x331aa
leaq 0x18(%r15), %rsi
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x75e6e
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x769de
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movq %rbp, %rdi
leaq 0xa75c6(%rip), %rsi     # 0xda752
movb $0x3, %al
callq 0x21d80
movq 0x30(%rsp), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
movq (%rsp), %rcx
movl %eax, (%rcx,%r12,4)
movq 0x58(%r15), %rax
btl $0x9, %eax
jae 0x331f5
addq $0x48, %r15
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x819c8
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movq %rbp, %rdi
leaq 0xa7588(%rip), %rsi     # 0xda75f
movb $0x2, %al
callq 0x21d80
movq 0x1b0(%rsp), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movslq 0x14(%r13), %rax
cmpq %rax, %r12
jl 0x330eb
cmpl $0x0, 0x24(%r13)
jle 0x333ca
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), %r14d
movl 0x60(%rsi), %ebp
movq 0x78(%rax), %rbx
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
btl $0x9, %ebx
movq 0x20(%rsp), %rsi
movl (%rsi,%r14,4), %r11d
jb 0x3326a
btl $0x8, %ebx
jb 0x33295
testl %eax, %eax
je 0x332e9
movl (%rsi,%rbp,4), %edx
movl (%rsi,%r12,4), %r8d
jmp 0x332f1
btl $0x8, %ebx
movq 0x8(%rsp), %r9
movl (%r9,%r14,4), %r10d
jb 0x332b8
testl %eax, %eax
je 0x33312
movl (%rsi,%rbp,4), %edx
movl (%r9,%rbp,4), %ecx
movl (%rsi,%r12,4), %ebx
movl (%r9,%r12,4), %r14d
jmp 0x33321
movq (%rsp), %rdi
movl (%rdi,%r14,4), %r9d
testl %eax, %eax
je 0x33340
movl (%rsi,%rbp,4), %edx
movl (%rdi,%rbp,4), %ecx
movl (%rsi,%r12,4), %r10d
movl (%rdi,%r12,4), %ebx
jmp 0x3334f
movq (%rsp), %rdi
movl (%rdi,%r14,4), %ecx
movq %rcx, 0x38(%rsp)
testl %eax, %eax
je 0x33374
movl (%rsi,%rbp,4), %edx
movl (%r9,%rbp,4), %ecx
movl (%rdi,%rbp,4), %r8d
movl (%rsi,%r12,4), %ebx
movl (%r9,%r12,4), %r14d
movl (%rdi,%r12,4), %r12d
jmp 0x3338b
movl (%rsi,%r12,4), %edx
movl (%rsi,%rbp,4), %r8d
movq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
leaq 0xa74b1(%rip), %rsi     # 0xda7b4
movl %r11d, %ecx
xorl %eax, %eax
callq 0x21d80
jmp 0x333ba
movl (%rsi,%r12,4), %edx
movl (%r9,%r12,4), %ecx
movl (%rsi,%rbp,4), %ebx
movl (%r9,%rbp,4), %r14d
movq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
leaq 0xa7454(%rip), %rsi     # 0xda787
movl %r11d, %r8d
movl %r10d, %r9d
xorl %eax, %eax
pushq %r14
pushq %rbx
jmp 0x33369
movl (%rsi,%r12,4), %edx
movl (%rdi,%r12,4), %ecx
movl (%rsi,%rbp,4), %r10d
movl (%rdi,%rbp,4), %ebx
movq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
leaq 0xa743b(%rip), %rsi     # 0xda79c
movl %r11d, %r8d
xorl %eax, %eax
pushq %rbx
pushq %r10
callq 0x21d80
addq $0x10, %rsp
jmp 0x333ba
movl (%rsi,%r12,4), %edx
movl (%r9,%r12,4), %ecx
movl (%rdi,%r12,4), %r8d
movl (%rsi,%rbp,4), %ebx
movl (%r9,%rbp,4), %r14d
movl (%rdi,%rbp,4), %r12d
subq $0x8, %rsp
movq 0x1c0(%rsp), %rbp
movq %rbp, %rdi
leaq 0xa73c8(%rip), %rsi     # 0xda769
movl %r11d, %r9d
xorl %eax, %eax
pushq %r12
pushq %r14
pushq %rbx
pushq 0x58(%rsp)
pushq %r10
callq 0x21d80
addq $0x30, %rsp
incq %r15
movslq 0x24(%r13), %rax
cmpq %rax, %r15
jl 0x33213
movq 0x20(%rsp), %rdi
callq 0x21c20
movq (%rsp), %rdi
callq 0x21c20
movq 0x8(%rsp), %rdi
callq 0x21c20
movq $-0x1, %r15
movq 0x58(%rsp), %r14
movq 0x60(%rsp), %r12
incq %r12
movslq 0x1c(%r14), %rax
cmpq %rax, %r12
jl 0x32ff8
movq 0x50(%rsp), %rdx
incq %rdx
movq 0x18(%rsp), %rcx
movslq 0x3c(%rcx), %rax
cmpq %rax, %rdx
jl 0x32f9f
cmpl $0x0, 0x2c(%rcx)
jle 0x334fe
xorl %r12d, %r12d
leaq 0x105d12(%rip), %r14    # 0x139148
addq $0x10, %r14
leaq 0x11223f(%rip), %rax    # 0x145680
leaq 0x8(%rax), %r15
movl $0x88, %r13d
addq 0x109b86(%rip), %r13    # 0x13cfd8
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 0x71cc6
leaq 0xf0(%rsp), %rdi
movq %r13, %rsi
callq 0x71cc6
leaq 0x1121f8(%rip), %rax    # 0x145680
movq 0x108(%rax), %rax
movq %rax, 0x170(%rsp)
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x6fb9e
leaq 0x58(%rbx), %rsi
movq %rbp, %rdi
callq 0x6fb9e
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq 0x48(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x1b8(%rsp)
pushq 0x1b8(%rsp)
callq 0x32f14
addq $0x10, %rsp
movq %rbp, %rdi
callq 0x6fa30
incq %r12
movq 0x18(%rsp), %rcx
movslq 0x2c(%rcx), %rax
cmpq %rax, %r12
jl 0x33457
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 0x700c4
jmp 0x33531
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x6fa30
movq %rbx, %rdi
callq 0x220c0
nop