// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::WriteObjFile(char const*) const
int R3Mesh::
WriteObjFile(const char *filename) const
{
  // Open file
  FILE *fp;
  if (!(fp = fopen(filename, "w"))) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Check if have texcoords
  RNBoolean has_texcoords = FALSE;
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    const R2Point& t = VertexTextureCoords(vertex);
    if (!t.IsZero()) { has_texcoords = TRUE; break; }
  }

  // Write vertices
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    const R3Point& p = VertexPosition(vertex);
    const R2Point& t = VertexTextureCoords(vertex);
    if (has_texcoords) fprintf(fp, "vt %g %g\n", t.X(), t.Y());
    fprintf(fp, "v %g %g %g\n", p.X(), p.Y(), p.Z());
  }

  // Write Faces
  for (int i = 0; i < NFaces(); i++) {
    R3MeshFace *face = Face(i);
    R3MeshVertex *v0 = VertexOnFace(face, 0);
    R3MeshVertex *v1 = VertexOnFace(face, 1);
    R3MeshVertex *v2 = VertexOnFace(face, 2);
    if (has_texcoords) {
      fprintf(fp, "f %d/%d %d/%d %d/%d\n",
        v0->id + 1, v0->id + 1,
        v1->id + 1, v1->id + 1,
        v2->id + 1, v2->id + 1);
    }
    else {
      fprintf(fp, "f %d %d %d\n",
        v0->id + 1, v1->id + 1, v2->id + 1);
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xc8(%rsp)
movq %rsi, 0xc0(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xc0(%rsp), %rdi
leaq 0xff9c0(%rip), %rsi     # 0x1342f3
callq 0xf5e0
movq %rax, 0xb8(%rsp)
cmpq $0x0, %rax
jne 0x3496c
movq 0xc0(%rsp), %rsi
leaq 0xfea8b(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0xd4(%rsp)
jmp 0x34ca9
movl $0x0, 0xb4(%rsp)
movl $0x0, 0xb0(%rsp)
movq 0x50(%rsp), %rdi
movl 0xb0(%rsp), %eax
movl %eax, 0x4c(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x34a0b
movq 0x50(%rsp), %rdi
movl 0xb0(%rsp), %esi
callq 0x13630
movq 0x50(%rsp), %rdi
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x368f0
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x3a3f0
cmpl $0x0, %eax
jne 0x349f3
movl $0x1, 0xb4(%rsp)
jmp 0x34a0b
jmp 0x349f5
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x34982
movl $0x0, 0x9c(%rsp)
movq 0x50(%rsp), %rdi
movl 0x9c(%rsp), %eax
movl %eax, 0x48(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0x48(%rsp), %eax
cmpl %ecx, %eax
jge 0x34b51
movq 0x50(%rsp), %rdi
movl 0x9c(%rsp), %esi
callq 0x13630
movq 0x50(%rsp), %rdi
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rsi
callq 0x13660
movq 0x50(%rsp), %rdi
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rsi
callq 0x368f0
movq %rax, 0x80(%rsp)
cmpl $0x0, 0xb4(%rsp)
je 0x34ad9
movq 0xb8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x80(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x40(%rsp)
movq 0x80(%rsp), %rdi
callq 0x38c60
movq 0x38(%rsp), %rdi
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
leaq 0xfef5d(%rip), %rsi     # 0x133a2f
movb $0x2, %al
callq 0xf730
movq 0xb8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x88(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x30(%rsp)
movq 0x88(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x28(%rsp)
movq 0x88(%rsp), %rdi
callq 0x378c0
movq 0x20(%rsp), %rdi
movsd 0x28(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x30(%rsp), %xmm0
leaq 0xfef05(%rip), %rsi     # 0x133a39
movb $0x3, %al
callq 0xf730
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x34a16
movl $0x0, 0x7c(%rsp)
movq 0x50(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0x135a0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x34c91
movq 0x50(%rsp), %rdi
movl 0x7c(%rsp), %esi
callq 0x136d0
movq 0x50(%rsp), %rdi
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rsi
xorl %edx, %edx
callq 0x13700
movq 0x50(%rsp), %rdi
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rsi
movl $0x1, %edx
callq 0x13700
movq 0x50(%rsp), %rdi
movq %rax, 0x60(%rsp)
movq 0x70(%rsp), %rsi
movl $0x2, %edx
callq 0x13700
movq %rax, 0x58(%rsp)
cmpl $0x0, 0xb4(%rsp)
je 0x34c46
movq 0xb8(%rsp), %rdi
movq 0x68(%rsp), %rax
movl 0x70(%rax), %edx
addl $0x1, %edx
movq 0x68(%rsp), %rax
movl 0x70(%rax), %ecx
addl $0x1, %ecx
movq 0x60(%rsp), %rax
movl 0x70(%rax), %r8d
addl $0x1, %r8d
movq 0x60(%rsp), %rax
movl 0x70(%rax), %r9d
addl $0x1, %r9d
movq 0x58(%rsp), %rax
movl 0x70(%rax), %r10d
addl $0x1, %r10d
movq 0x58(%rsp), %rax
movl 0x70(%rax), %eax
addl $0x1, %eax
leaq 0xfee10(%rip), %rsi     # 0x133a45
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0xf730
jmp 0x34c7f
movq 0xb8(%rsp), %rdi
movq 0x68(%rsp), %rax
movl 0x70(%rax), %edx
addl $0x1, %edx
movq 0x60(%rsp), %rax
movl 0x70(%rax), %ecx
addl $0x1, %ecx
movq 0x58(%rsp), %rax
movl 0x70(%rax), %r8d
addl $0x1, %r8d
leaq 0xfede2(%rip), %rsi     # 0x133a5a
movb $0x0, %al
callq 0xf730
jmp 0x34c81
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x34b59
movq 0xb8(%rsp), %rdi
callq 0xf4b0
movl $0x1, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
addq $0xd8, %rsp
retq
nopl (%rax,%rax)