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

  // Determine output directory
  const char *output_directory = ".";

  // Determine if need to remove references
  RNBoolean remove_references = FALSE;
  for (int i = 0; i < NReferencedScenes(); i++) {
    R3Scene *referenced_scene = ReferencedScene(i);
    if (referenced_scene->Filename()) continue;
    remove_references = TRUE; break;
  }

  // Remove references if necessary (this will change the scene structure :( )
  if (remove_references) ((R3Scene *) this)->RemoveReferences();

  // Write color and camera 
  fprintf(fp, "background %g %g %g\n", Background().R(), Background().G(), Background().B());
  fprintf(fp, "ambient %g %g %g\n", Ambient().R(), Ambient().G(), Ambient().B());
  fprintf(fp, "camera %g %g %g  %g %g %g  %g %g %g  %g   %g %g  \n",
    Camera().Origin().X(), Camera().Origin().Y(), Camera().Origin().Z(),
    Camera().Towards().X(), Camera().Towards().Y(), Camera().Towards().Z(),
    Camera().Up().X(), Camera().Up().Y(), Camera().Up().Z(),
    Camera().XFOV(), Camera().Near(), Camera().Far());

  // Create textures files
  for (int i = 0; i < NTextures(); i++) {
    R2Texture *texture = Texture(i);
    if (!CreateTextureFile(this, texture, output_directory)) return 0;
  }

  // Write lights
  fprintf(fp, "\n");
  for (int i = 0; i < NLights(); i++) {
    R3Light *light = Light(i);
    if (!WritePrincetonLight(this, light, fp)) return 0;
  }

  // Write materials
  fprintf(fp, "\n");
  for (int i = 0; i < NMaterials(); i++) {
    R3Material *material = Material(i);
    if (!WritePrincetonMaterial(this, material, fp)) return 0;
  }

  // Write nodes recursively
  int status = WritePrincetonNode(this, root, R3identity_affine, fp, "");
  
  // Close file
  fclose(fp);

  // Return status
  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0xad952(%rip), %rsi     # 0xdde3c
movq %r15, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x30521
movq %rax, %rbx
movslq 0x64(%r14), %rax
testq %rax, %rax
jle 0x30541
movq 0x58(%r14), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpq $0x0, 0x148(%rsi)
je 0x30539
incq %rdx
cmpq %rdx, %rax
jne 0x30509
jmp 0x30541
leaq 0xaa12e(%rip), %rdi     # 0xda656
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x8a0fe
jmp 0x30acc
movq %r14, %rdi
callq 0x28a36
movsd 0x130(%r14), %xmm0
movsd 0x138(%r14), %xmm1
movsd 0x140(%r14), %xmm2
leaq 0xaa836(%rip), %rsi     # 0xdad99
movq %rbx, %rdi
movb $0x3, %al
callq 0x21d80
movsd 0x118(%r14), %xmm0
movsd 0x120(%r14), %xmm1
movsd 0x128(%r14), %xmm2
leaq 0xaa81f(%rip), %rsi     # 0xdadae
movq %rbx, %rdi
movb $0x3, %al
callq 0x21d80
movsd 0x78(%r14), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%r14), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x88(%r14), %xmm0
movsd %xmm0, 0x70(%rsp)
leaq 0xc0(%r14), %r15
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x76a98
movsd (%r12), %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x76a98
movsd 0x8(%r12), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x76a98
movsd 0x10(%r12), %xmm5
movsd 0xa8(%r14), %xmm6
movsd 0xb0(%r14), %xmm7
movsd 0xb8(%r14), %xmm0
movsd 0xd8(%r14), %xmm1
movsd 0xe8(%r14), %xmm2
movsd 0xf0(%r14), %xmm3
movsd %xmm3, 0x18(%rsp)
movsd %xmm2, 0x10(%rsp)
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rsp)
leaq 0xaa749(%rip), %rsi     # 0xdadc0
movq %rbx, %rdi
movsd 0x80(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
movsd 0x68(%rsp), %xmm3
movsd 0x60(%rsp), %xmm4
movb $0x8, %al
callq 0x21d80
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21bb0
cmpl $0x0, 0x24(%r14)
jle 0x308e8
xorl %r12d, %r12d
leaq 0x48(%rsp), %r15
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rbp
leaq 0x30(%rbp), %rsi
movq %r15, %rdi
callq 0x873e2
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x42408
cmpl %eax, %r13d
jne 0x30724
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x50(%rbp), %xmm3
movsd 0x58(%rbp), %xmm4
movsd 0x60(%rbp), %xmm5
movq %rbx, %rdi
leaq 0xaaf6b(%rip), %rsi     # 0xdb683
movb $0x6, %al
callq 0x21d80
jmp 0x308d8
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x41b8c
cmpl %eax, %r13d
jne 0x30781
movsd 0x68(%rbp), %xmm6
movsd 0x70(%rbp), %xmm7
movsd 0x78(%rbp), %xmm8
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x50(%rbp), %xmm3
movsd 0x58(%rbp), %xmm4
movsd 0x60(%rbp), %xmm5
movsd %xmm8, (%rsp)
movq %rbx, %rdi
leaq 0xaaf26(%rip), %rsi     # 0xdb6a2
jmp 0x308d1
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x41798
cmpl %eax, %r13d
jne 0x30832
movsd 0x68(%rbp), %xmm8
movsd 0x70(%rbp), %xmm9
movsd 0x78(%rbp), %xmm10
movsd 0xa0(%rbp), %xmm11
movsd 0x98(%rbp), %xmm12
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x50(%rbp), %xmm3
movsd 0x58(%rbp), %xmm4
movsd 0x60(%rbp), %xmm5
movsd 0x80(%rbp), %xmm6
movsd 0x88(%rbp), %xmm7
movsd 0x90(%rbp), %xmm13
movsd %xmm12, 0x28(%rsp)
movsd %xmm11, 0x20(%rsp)
movsd %xmm10, 0x18(%rsp)
movsd %xmm9, 0x10(%rsp)
movsd %xmm8, 0x8(%rsp)
movsd %xmm13, (%rsp)
movq %rbx, %rdi
leaq 0xaaea0(%rip), %rsi     # 0xdb6cd
jmp 0x308d1
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x407cc
cmpl %eax, %r13d
jne 0x308d8
movsd 0x70(%rbp), %xmm8
movsd 0x98(%rbp), %xmm9
movsd 0xa0(%rbp), %xmm10
movsd 0xa8(%rbp), %xmm11
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x58(%rbp), %xmm3
movsd 0x60(%rbp), %xmm4
movsd 0x68(%rbp), %xmm5
movsd 0x78(%rbp), %xmm6
movsd 0x80(%rbp), %xmm7
movsd 0x88(%rbp), %xmm12
movsd %xmm11, 0x20(%rsp)
movsd %xmm10, 0x18(%rsp)
movsd %xmm9, 0x10(%rsp)
movsd %xmm8, 0x8(%rsp)
movsd %xmm12, (%rsp)
movq %rbx, %rdi
leaq 0xaae37(%rip), %rsi     # 0xdb708
movb $0x8, %al
callq 0x21d80
incq %r12
movslq 0x24(%r14), %rax
cmpq %rax, %r12
jl 0x306c2
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21bb0
cmpl $0x0, 0x34(%r14)
jle 0x30aa6
xorl %r12d, %r12d
leaq 0x115cd6(%rip), %r15    # 0x1465e0
movq %rbx, %r13
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x18(%rax), %rbx
movq 0x20(%rax), %rbp
testq %rbx, %rbx
je 0x3096c
leaq 0x20(%rbx), %rsi
leaq 0x48(%rsp), %rdi
callq 0x873e2
leaq 0x38(%rbx), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x873e2
leaq 0x50(%rbx), %rsi
leaq 0x88(%rsp), %rdi
callq 0x873e2
leaq 0x68(%rbx), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x873e2
leaq 0x80(%rbx), %rsi
jmp 0x309ac
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x873e2
leaq 0xa0(%rsp), %rdi
movq %r15, %rsi
callq 0x873e2
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0x873e2
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
callq 0x873e2
movq %r15, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x873e2
movsd 0x98(%rbx), %xmm8
movsd 0xa0(%rbx), %xmm9
leaq 0xb14e5(%rip), %rax     # 0xe1eb7
movq %rax, %rdx
testq %rbp, %rbp
je 0x309e5
movq 0x18(%rbp), %rdx
testq %rdx, %rdx
cmoveq %rax, %rdx
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm3
movsd 0xa8(%rsp), %xmm4
movsd 0xb0(%rsp), %xmm5
movsd 0x88(%rsp), %xmm6
movsd 0x90(%rsp), %xmm7
movsd 0x98(%rsp), %xmm10
movups 0xb8(%rsp), %xmm11
movsd 0xc8(%rsp), %xmm12
movups 0xd0(%rsp), %xmm13
movsd 0xe0(%rsp), %xmm14
movsd %xmm9, 0x40(%rsp)
movsd %xmm8, 0x38(%rsp)
movsd %xmm14, 0x30(%rsp)
movups %xmm13, 0x20(%rsp)
movsd %xmm12, 0x18(%rsp)
movups %xmm11, 0x8(%rsp)
movsd %xmm10, (%rsp)
movq %r13, %rbx
movq %r13, %rdi
leaq 0xaacb1(%rip), %rsi     # 0xdb740
movb $0x8, %al
callq 0x21d80
incq %r12
movslq 0x34(%r14), %rax
cmpq %rax, %r12
jl 0x3090a
movq (%r14), %rsi
leaq 0x114bd0(%rip), %rdx    # 0x145680
leaq 0xaa440(%rip), %r8      # 0xdaef7
movq %r14, %rdi
movq %rbx, %rcx
callq 0x33be4
movl %eax, %ebp
movq %rbx, %rdi
callq 0x218b0
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq