// File: /workspace/github_cpp_makefile_star2_O2/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 0x9a72c(%rip), %rsi     # 0xc8dc4
movq %r15, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x2ec47
movq %rax, %rbx
movl 0x64(%r14), %eax
movq 0x58(%r14), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
cmpq %rdx, %rax
je 0x2e6d9
movq (%rcx,%rdx,8), %rsi
incq %rdx
cmpq $0x0, 0x148(%rsi)
jne 0x2e6bb
movq %r14, %rdi
callq 0x27a90
movsd 0x130(%r14), %xmm0
movsd 0x138(%r14), %xmm1
movsd 0x140(%r14), %xmm2
leaq 0x9766e(%rip), %rsi     # 0xc5d69
movq %rbx, %rdi
movb $0x3, %al
callq 0x21da0
movsd 0x118(%r14), %xmm0
movsd 0x120(%r14), %xmm1
movsd 0x128(%r14), %xmm2
leaq 0x97657(%rip), %rsi     # 0xc5d7e
movq %rbx, %rdi
movb $0x3, %al
callq 0x21da0
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), %r12
leaq 0x48(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x6c99a
movsd (%r15), %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x6c99a
movsd 0x8(%r13), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x88(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x6c99a
movsd 0x10(%r13), %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 0x97584(%rip), %rsi     # 0xc5d90
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 0x21da0
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21bb0
xorl %r12d, %r12d
movslq 0x24(%r14), %rax
cmpq %rax, %r12
jge 0x2ea70
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rbp
leaq 0x30(%rbp), %rsi
movq %r15, %rdi
callq 0x7c020
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x3dfa4
cmpl %eax, %r13d
jne 0x2e8b4
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 0x97dab(%rip), %rsi     # 0xc6653
movb $0x6, %al
callq 0x21da0
jmp 0x2ea68
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x3d7e4
cmpl %eax, %r13d
jne 0x2e911
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 0x97d66(%rip), %rsi     # 0xc6672
jmp 0x2ea61
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x3d3fc
cmpl %eax, %r13d
jne 0x2e9c2
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 0x97ce0(%rip), %rsi     # 0xc669d
jmp 0x2ea61
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x78(%rax)
movl %eax, %r13d
callq 0x3c48c
cmpl %eax, %r13d
jne 0x2ea68
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 0x97c77(%rip), %rsi     # 0xc66d8
movb $0x8, %al
callq 0x21da0
incq %r12
jmp 0x2e845
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21bb0
xorl %r12d, %r12d
leaq 0x108b7b(%rip), %r15    # 0x137600
movslq 0x34(%r14), %rax
cmpq %rax, %r12
jge 0x2ec1f
movq %rbx, %r13
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x18(%rax), %rbx
movq 0x20(%rax), %rbp
testq %rbx, %rbx
je 0x2eabe
leaq 0x20(%rbx), %rsi
leaq 0x48(%rsp), %rdi
callq 0x7c020
leaq 0x38(%rbx), %rsi
jmp 0x2eace
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x7c020
movq %r15, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x7c020
testq %rbx, %rbx
je 0x2eaf7
leaq 0x50(%rbx), %rsi
leaq 0x88(%rsp), %rdi
callq 0x7c020
leaq 0x68(%rbx), %rsi
jmp 0x2eb0a
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0x7c020
movq %r15, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x7c020
testq %rbx, %rbx
jne 0x2eb21
movq %r15, %rsi
jmp 0x2eb28
leaq 0x80(%rbx), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x7c020
movsd 0x98(%rbx), %xmm8
movsd 0xa0(%rbx), %xmm9
leaq 0x9e049(%rip), %rdx     # 0xccb97
testq %rbp, %rbp
movq %r13, %rbx
je 0x2eb69
movq 0x18(%rbp), %rax
leaq 0x9e036(%rip), %rdx     # 0xccb97
testq %rax, %rax
je 0x2eb69
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 0x97b00(%rip), %rsi     # 0xc6710
movb $0x8, %al
callq 0x21da0
incq %r12
jmp 0x2ea85
movq (%r14), %rsi
leaq 0x107a77(%rip), %rdx    # 0x1366a0
leaq 0x97297(%rip), %r8      # 0xc5ec7
movq %r14, %rdi
movq %rbx, %rcx
callq 0x31867
movl %eax, %ebp
movq %rbx, %rdi
callq 0x218c0
jmp 0x2ec5a
leaq 0x969d8(%rip), %rdi     # 0xc5626
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x7e83e
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq