// File: /workspace/github_cpp_makefile_star2_O1/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 0xaf684(%rip), %rsi     # 0xdfe1c
movq %r15, %rdi
callq 0x21ac0
testq %rax, %rax
je 0x30d74
movq %rax, %rbx
movslq 0x64(%r14), %rax
testq %rax, %rax
jle 0x307ee
movq 0x58(%r14), %rcx
decq %rax
xorl %edx, %edx
movl $0x1, %esi
xorl %edi, %edi
movq (%rcx,%rdx,8), %r8
cmpq $0x0, 0x148(%r8)
cmovel %esi, %edi
je 0x307e2
leaq 0x1(%rdx), %r8
cmpq %rdx, %rax
movq %r8, %rdx
jne 0x307c5
testl %edi, %edi
je 0x307ee
movq %r14, %rdi
callq 0x28b08
movsd 0x130(%r14), %xmm0
movsd 0x138(%r14), %xmm1
movsd 0x140(%r14), %xmm2
leaq 0xac579(%rip), %rsi     # 0xdcd89
movq %rbx, %rdi
movb $0x3, %al
callq 0x21d90
movsd 0x118(%r14), %xmm0
movsd 0x120(%r14), %xmm1
movsd 0x128(%r14), %xmm2
leaq 0xac562(%rip), %rsi     # 0xdcd9e
movq %rbx, %rdi
movb $0x3, %al
callq 0x21d90
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 0x779e8
movsd (%r12), %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x779e8
movsd 0x8(%r12), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x779e8
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 0xac48c(%rip), %rsi     # 0xdcdb0
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 0x21d90
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21bc0
cmpl $0x0, 0x24(%r14)
jle 0x30b95
xorl %r12d, %r12d
leaq 0x48(%rsp), %r15
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rbp
leaq 0x30(%rbp), %rsi
movq %r15, %rdi
callq 0x88512
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x42920
cmpl %eax, %r13d
jne 0x309d1
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 0xaccae(%rip), %rsi     # 0xdd673
movb $0x6, %al
callq 0x21d90
jmp 0x30b85
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x420a4
cmpl %eax, %r13d
jne 0x30a2e
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 0xacc69(%rip), %rsi     # 0xdd692
jmp 0x30b7e
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x41cb0
cmpl %eax, %r13d
jne 0x30adf
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 0xacbe3(%rip), %rsi     # 0xdd6bd
jmp 0x30b7e
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x40c48
cmpl %eax, %r13d
jne 0x30b85
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 0xacb7a(%rip), %rsi     # 0xdd6f8
movb $0x8, %al
callq 0x21d90
incq %r12
movslq 0x24(%r14), %rax
cmpq %rax, %r12
jl 0x3096f
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21bc0
cmpl $0x0, 0x34(%r14)
jle 0x30d4c
xorl %r12d, %r12d
leaq 0x117a29(%rip), %r15    # 0x1485e0
movq %rbx, %r13
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x18(%rax), %rbx
movq 0x20(%rax), %rbp
testq %rbx, %rbx
je 0x30be3
leaq 0x20(%rbx), %rsi
leaq 0x48(%rsp), %rdi
callq 0x88512
leaq 0x38(%rbx), %rsi
jmp 0x30bf3
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x88512
movq %r15, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x88512
testq %rbx, %rbx
je 0x30c1c
leaq 0x50(%rbx), %rsi
leaq 0x88(%rsp), %rdi
callq 0x88512
leaq 0x68(%rbx), %rsi
jmp 0x30c2f
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0x88512
movq %r15, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x88512
testq %rbx, %rbx
jne 0x30c46
movq %r15, %rsi
jmp 0x30c4d
leaq 0x80(%rbx), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x88512
movsd 0x98(%rbx), %xmm8
movsd 0xa0(%rbx), %xmm9
leaq 0xb3204(%rip), %rdx     # 0xe3e77
testq %rbp, %rbp
movq %r13, %rbx
je 0x30c8e
movq 0x18(%rbp), %rax
leaq 0xb31f1(%rip), %rdx     # 0xe3e77
testq %rax, %rax
je 0x30c8e
movq %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 0xd0(%rsp), %xmm11
movsd 0xe0(%rsp), %xmm12
movups 0xb8(%rsp), %xmm13
movsd 0xc8(%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 %rbx, %rdi
leaq 0xac9fb(%rip), %rsi     # 0xdd730
movb $0x8, %al
callq 0x21d90
incq %r12
movslq 0x34(%r14), %rax
cmpq %rax, %r12
jl 0x30bb7
movq (%r14), %rsi
leaq 0x11692a(%rip), %rdx    # 0x147680
leaq 0xac18a(%rip), %r8      # 0xdcee7
movq %r14, %rdi
movq %rbx, %rcx
callq 0x33f5c
movl %eax, %ebp
movq %rbx, %rdi
callq 0x218b0
jmp 0x30d87
leaq 0xab8cb(%rip), %rdi     # 0xdc646
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x8b53a
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop