// File: /workspace/github_cpp_makefile_star2_O2/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 $0x188, %rsp            # imm = 0x188
movq %r9, 0x38(%rsp)
movq %r8, 0x40(%rsp)
movq %rdx, 0x20(%rsp)
movq %rdi, 0x48(%rsp)
movq 0x1c8(%rsp), %rbp
movq %rsi, 0x28(%rsp)
movq 0x1a0(%rsi), %rdx
testq %rdx, %rdx
movq %rcx, 0x50(%rsp)
je 0x30cc4
leaq 0x95a97(%rip), %rsi     # 0xc674f
movq %rbp, %rdi
xorl %eax, %eax
callq 0x21da0
jmp 0x30cdb
movl (%rcx), %edx
incl %edx
movl %edx, (%rcx)
leaq 0x94a1c(%rip), %rsi     # 0xc56ed
movq %rbp, %rdi
xorl %eax, %eax
callq 0x21da0
leaq 0x78(%rsp), %r14
xorl %edx, %edx
movq 0x28(%rsp), %rcx
movslq 0x3c(%rcx), %rax
cmpq %rax, %rdx
jge 0x31177
movq 0x30(%rcx), %rax
movq %rdx, 0x58(%rsp)
movq (%rax,%rdx,8), %rbx
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x30d21
movq 0x10(%rax), %rdx
movq %rbp, %rdi
leaq 0x949e1(%rip), %rsi     # 0xc56f9
xorl %eax, %eax
callq 0x21da0
jmp 0x30d36
leaq 0x949dc(%rip), %rdi     # 0xc5704
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
movq %rbp, %rcx
callq 0x222c0
xorl %ecx, %ecx
movq %rbx, 0x60(%rsp)
movslq 0x1c(%rbx), %rax
cmpq %rax, %rcx
jge 0x3116a
movq 0x10(%rbx), %rax
movq %rcx, 0x68(%rsp)
movq (%rax,%rcx,8), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0xb8(%rax)
movl %eax, %ebx
callq 0x5f048
cmpl %eax, %ebx
jne 0x31158
movslq 0x14(%r13), %rax
testq %rax, %rax
je 0x31158
movq %rax, %rdi
shlq $0x2, %rdi
testl %eax, %eax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x21040
movq %rax, 0x8(%rsp)
xorl %eax, %eax
movslq 0x14(%r13), %rcx
cmpq %rcx, %rax
jge 0x30db3
movq 0x8(%rsp), %rcx
orl $-0x1, (%rcx,%rax,4)
incq %rax
jmp 0x30d9c
movq %rcx, %rdi
shlq $0x2, %rdi
testl %ecx, %ecx
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x21040
xorl %edx, %edx
movslq 0x14(%r13), %rcx
cmpq %rcx, %rdx
jge 0x30ddc
orl $-0x1, (%rax,%rdx,4)
incq %rdx
jmp 0x30dca
movq %rax, 0x18(%rsp)
movq %rcx, %rdi
shlq $0x2, %rdi
testl %ecx, %ecx
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x21040
movq %rax, 0x10(%rsp)
xorl %ecx, %ecx
movslq 0x14(%r13), %rax
cmpq %rax, %rcx
jge 0x30e14
movq 0x10(%rsp), %rax
orl $-0x1, (%rax,%rcx,4)
incq %rcx
jmp 0x30dfd
xorl %r12d, %r12d
cltq
cmpq %rax, %r12
jge 0x30f3f
movq 0x8(%r13), %rax
movq (%rax,%r12,8), %r15
movl %r12d, 0x60(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x6b884
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x6beba
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
movq %rbp, %rdi
leaq 0x948ae(%rip), %rsi     # 0xc5716
movb $0x3, %al
callq 0x21da0
movq 0x40(%rsp), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
movq 0x58(%r15), %rax
btl $0x8, %eax
jae 0x30ee7
leaq 0x18(%r15), %rsi
movq %r14, %rdi
callq 0x6c044
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x6c8e0
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
movq %rbp, %rdi
leaq 0x9485a(%rip), %rsi     # 0xc5722
movb $0x3, %al
callq 0x21da0
movq 0x38(%rsp), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
movq 0x18(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
movq 0x58(%r15), %rax
btl $0x9, %eax
jae 0x30f33
addq $0x48, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x76d9e
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movq %rbp, %rdi
leaq 0x9481a(%rip), %rsi     # 0xc572f
movb $0x2, %al
callq 0x21da0
movq 0x1c0(%rsp), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
movq 0x10(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movl 0x14(%r13), %eax
jmp 0x30e17
xorl %ecx, %ecx
movslq 0x24(%r13), %rax
cmpq %rax, %rcx
jge 0x3113a
movq %rbp, %r15
movq 0x18(%r13), %rax
movq %rcx, 0x70(%rsp)
movq (%rax,%rcx,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 0x20(%rsp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
btl $0x9, %ebx
movq 0x8(%rsp), %rcx
movl (%rcx,%r14,4), %r11d
jb 0x30fb7
btl $0x8, %ebx
jb 0x30feb
testl %eax, %eax
leaq 0x78(%rsp), %r14
je 0x3104f
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %edx
movl (%rax,%r12,4), %r8d
jmp 0x3105c
btl $0x8, %ebx
movq 0x10(%rsp), %rcx
movl (%rcx,%r14,4), %r10d
jb 0x31014
testl %eax, %eax
je 0x31078
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %edx
movq 0x10(%rsp), %rsi
movl (%rsi,%rbp,4), %ecx
movl (%rax,%r12,4), %ebx
movl (%rsi,%r12,4), %r14d
jmp 0x31091
movq 0x18(%rsp), %rsi
movl (%rsi,%r14,4), %r9d
testl %eax, %eax
je 0x310ab
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %edx
movl (%rsi,%rbp,4), %ecx
movl (%rax,%r12,4), %r10d
movl (%rsi,%r12,4), %ebx
jmp 0x310bf
movq 0x18(%rsp), %rsi
movl (%rsi,%r14,4), %ecx
movq %rcx, 0x30(%rsp)
testl %eax, %eax
je 0x310dd
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %edx
movq 0x10(%rsp), %rdi
movl (%rdi,%rbp,4), %ecx
movl (%rsi,%rbp,4), %r8d
movl (%rax,%r12,4), %ebx
movl (%rdi,%r12,4), %r14d
movl (%rsi,%r12,4), %r12d
jmp 0x310fe
movq 0x8(%rsp), %rax
movl (%rax,%r12,4), %edx
movl (%rax,%rbp,4), %r8d
movq %r15, %rbp
movq %r15, %rdi
leaq 0x9471b(%rip), %rsi     # 0xc5784
movl %r11d, %ecx
xorl %eax, %eax
callq 0x21da0
jmp 0x3112d
movq 0x8(%rsp), %rax
movl (%rax,%r12,4), %edx
movq 0x10(%rsp), %rsi
movl (%rsi,%r12,4), %ecx
movl (%rax,%rbp,4), %ebx
movl (%rsi,%rbp,4), %r14d
movq %r15, %rbp
movq %r15, %rdi
leaq 0x946b9(%rip), %rsi     # 0xc5757
movl %r11d, %r8d
movl %r10d, %r9d
xorl %eax, %eax
pushq %r14
pushq %rbx
jmp 0x310d4
movq 0x8(%rsp), %rax
movl (%rax,%r12,4), %edx
movl (%rsi,%r12,4), %ecx
movl (%rax,%rbp,4), %r10d
movl (%rsi,%rbp,4), %ebx
movq %r15, %rbp
movq %r15, %rdi
leaq 0x946a0(%rip), %rsi     # 0xc576c
movl %r11d, %r8d
xorl %eax, %eax
pushq %rbx
pushq %r10
callq 0x21da0
popq %rcx
popq %rdx
jmp 0x31128
movq 0x8(%rsp), %rax
movl (%rax,%r12,4), %edx
movq 0x10(%rsp), %rdi
movl (%rdi,%r12,4), %ecx
movl (%rsi,%r12,4), %r8d
movl (%rax,%rbp,4), %ebx
movl (%rdi,%rbp,4), %r14d
movl (%rsi,%rbp,4), %r12d
subq $0x8, %rsp
movq %r15, %rbp
movq %r15, %rdi
leaq 0x9462a(%rip), %rsi     # 0xc5739
movl %r11d, %r9d
xorl %eax, %eax
pushq %r12
pushq %r14
pushq %rbx
pushq 0x50(%rsp)
pushq %r10
callq 0x21da0
addq $0x30, %rsp
leaq 0x78(%rsp), %r14
movq 0x70(%rsp), %rcx
incq %rcx
jmp 0x30f41
movq 0x8(%rsp), %rdi
callq 0x21c20
movq 0x18(%rsp), %rdi
callq 0x21c20
movq 0x10(%rsp), %rdi
callq 0x21c20
movq 0x68(%rsp), %rcx
incq %rcx
movq 0x60(%rsp), %rbx
jmp 0x30d3d
movq 0x58(%rsp), %rdx
incq %rdx
jmp 0x30ce2
xorl %r12d, %r12d
leaq 0x10551f(%rip), %rbx    # 0x1366a0
leaq 0x78(%rsp), %r14
movq 0x28(%rsp), %rcx
movslq 0x2c(%rcx), %rax
cmpq %rax, %r12
jge 0x311f6
movq 0x20(%rcx), %rax
movq (%rax,%r12,8), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x34464
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x669c2
leaq 0x58(%r15), %rsi
movq %r14, %rdi
callq 0x669c2
movq 0x48(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq 0x50(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %r9
pushq %rbp
pushq 0x1c8(%rsp)
callq 0x30c6e
popq %rcx
popq %rdx
movq %r14, %rdi
callq 0x66856
incq %r12
jmp 0x31186
pushq $0x1
popq %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x66856
movq %rbx, %rdi
callq 0x220e0