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

  // Write default material
  fprintf(fp, "newmtl _DEFAULT_\n");
  fprintf(fp, "Kd 0 0 0\n");
  fprintf(fp, "\n");

  // Write materials
  for (int i = 0; i < this->NMaterials(); i++) {
    R3Material *material = this->Material(i);  

    // ??? CHANGE NAME OF MATERIAL SO THAT IT IS UNIQUE ???
    char matname[1024];
    if (!material->Name()) sprintf(matname, "m%d", i);
    else sprintf(matname, "m%d_%s", i, material->Name());
    material->SetName(matname);
    
    // Write new material command
    fprintf(fp, "newmtl %s\n", material->Name());

    // Write brdf
    if (material->Brdf()) {
      const R3Brdf *brdf = material->Brdf();
      if (brdf->IsAmbient()) fprintf(fp, "Ka %g %g %g\n", brdf->Ambient().R(), brdf->Ambient().G(), brdf->Ambient().B());
      if (brdf->IsDiffuse()) fprintf(fp, "Kd %g %g %g\n", brdf->Diffuse().R(), brdf->Diffuse().G(), brdf->Diffuse().B());
      if (brdf->IsSpecular()) fprintf(fp, "Ks %g %g %g\n", brdf->Specular().R(), brdf->Specular().G(), brdf->Specular().B());
      if (brdf->IsEmissive()) fprintf(fp, "Ke %g %g %g\n", brdf->Emission().R(), brdf->Emission().G(), brdf->Emission().B());
      if (brdf->IsTransparent()) fprintf(fp, "Tf %g %g %g\n", brdf->Transmission().R(), brdf->Transmission().G(), brdf->Transmission().B());
      if (RNIsNotEqual(brdf->Opacity(), 1.0)) fprintf(fp, "Tr %g\n", 1.0 - brdf->Opacity());
      if (RNIsNotEqual(brdf->Opacity(), 1.0)) fprintf(fp, "d %g\n", brdf->Opacity());
      if (RNIsNotZero(brdf->Shininess())) fprintf(fp, "Ns %g\n", brdf->Shininess());
      if (RNIsNotZero(brdf->IndexOfRefraction())) fprintf(fp, "Ni %g\n", brdf->IndexOfRefraction());
    }

    // Write texture
    if (material->Texture()) {
      const R2Texture *texture = material->Texture();

      // Check if texture has a filename associated with it 
      if (texture->Filename()) {
        // Write texture command to material file (USE SAME FILE AS READ WITHOUT RE-WRITING IT)
        fprintf(fp, "map_Kd %s\n", texture->Filename());
      }
      else {
        // Get texture filename
        char texture_filename[1024];
        const char *texture_extension = "jpg";
        if (dirname) sprintf(texture_filename, "%s/%s.%s", dirname, material->Name(), texture_extension);
        else sprintf(texture_filename, "%s.%s", material->Name(), texture_extension);
        ((R2Texture *) texture)->SetFilename(texture_filename);
        
        // Write texture file
        const R2Image *texture_image = texture->Image();
        texture_image->Write(texture_filename);
        
        // Write texture command to material file
        fprintf(fp, "map_Kd %s.%s\n", material->Name(), texture_extension);
      }
    }

    // Write new line after each material
    fprintf(fp, "\n");
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1018, %rsp           # imm = 0x1018
movq %rdx, %rcx
movq %rdi, %r14
testq %rsi, %rsi
movq %rsi, 0x8(%rsp)
je 0x32ab9
movq %rsi, %rax
leaq 0xa7bab(%rip), %rsi     # 0xda650
leaq 0x810(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21140
jmp 0x32ace
leaq 0x810(%rsp), %rdi
movl $0x7ff, %edx            # imm = 0x7FF
movq %rcx, %rsi
callq 0x21430
leaq 0xab367(%rip), %rsi     # 0xdde3c
leaq 0x810(%rsp), %rdi
callq 0x21ab0
testq %rax, %rax
je 0x32ee8
movq %rax, %r15
leaq 0xa7b78(%rip), %rdi     # 0xda66d
movl $0x11, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x22290
leaq 0xa7b71(%rip), %rdi     # 0xda67f
movl $0x9, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22290
movl $0xa, %edi
movq %r15, %rsi
callq 0x21bb0
cmpl $0x0, 0x34(%r14)
jle 0x32ed9
leaq 0x410(%rsp), %r13
xorl %r12d, %r12d
movq %r14, (%rsp)
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rbx
movq 0x10(%rbx), %rcx
movq %r13, %rdi
testq %rcx, %rcx
je 0x32b6e
leaq 0xa7b2b(%rip), %rsi     # 0xda68d
movl %r12d, %edx
xorl %eax, %eax
callq 0x21140
jmp 0x32b7f
leaq 0xa7b14(%rip), %rsi     # 0xda689
movl %r12d, %edx
xorl %eax, %eax
callq 0x21140
movq %rbx, %rdi
movq %r13, %rsi
callq 0x43c9c
movq 0x10(%rbx), %rdx
movq %r15, %rdi
leaq 0xa7afc(%rip), %rsi     # 0xda694
xorl %eax, %eax
callq 0x21d80
movq 0x18(%rbx), %rbp
testq %rbp, %rbp
je 0x32e0a
movq 0xa8(%rbp), %rax
testb $0x1, %al
je 0x32bde
movsd 0x20(%rbp), %xmm0
movsd 0x28(%rbp), %xmm1
movsd 0x30(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa7acf(%rip), %rsi     # 0xda69f
movb $0x3, %al
callq 0x21d80
movq 0xa8(%rbp), %rax
testb $0x2, %al
je 0x32c09
movsd 0x38(%rbp), %xmm0
movsd 0x40(%rbp), %xmm1
movsd 0x48(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa7ab1(%rip), %rsi     # 0xda6ac
movb $0x3, %al
callq 0x21d80
movq 0xa8(%rbp), %rax
testb $0x4, %al
je 0x32c34
movsd 0x50(%rbp), %xmm0
movsd 0x58(%rbp), %xmm1
movsd 0x60(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa7a93(%rip), %rsi     # 0xda6b9
movb $0x3, %al
callq 0x21d80
movq 0xa8(%rbp), %rax
testb $0x8, %al
je 0x32c68
movsd 0x80(%rbp), %xmm0
movsd 0x88(%rbp), %xmm1
movsd 0x90(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa7a6c(%rip), %rsi     # 0xda6c6
movb $0x3, %al
callq 0x21d80
movq 0xa8(%rbp), %rax
testb $0x20, %al
je 0x32c8c
movsd 0x68(%rbp), %xmm0
movsd 0x70(%rbp), %xmm1
movsd 0x78(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa7a4e(%rip), %rsi     # 0xda6d3
movb $0x3, %al
callq 0x21d80
movsd 0x70(%rbp), %xmm0
mulsd 0xa787f(%rip), %xmm0    # 0xda518
movsd 0x68(%rbp), %xmm1
mulsd 0xa787a(%rip), %xmm1    # 0xda520
addsd %xmm0, %xmm1
movsd 0x78(%rbp), %xmm2
mulsd 0xa7871(%rip), %xmm2    # 0xda528
addsd %xmm1, %xmm2
movsd 0xa7355(%rip), %xmm0    # 0xda018
subsd %xmm2, %xmm0
leaq 0x10b40a(%rip), %rax    # 0x13e0d8
movsd (%rax), %xmm1
movapd %xmm0, %xmm3
addsd 0xa77f2(%rip), %xmm3    # 0xda4d0
ucomisd %xmm1, %xmm3
ja 0x32cf6
movapd %xmm1, %xmm2
xorpd 0xa7850(%rip), %xmm2    # 0xda540
ucomisd %xmm3, %xmm2
jbe 0x32d75
movsd 0xa731a(%rip), %xmm1    # 0xda018
subsd %xmm0, %xmm1
movq %r15, %rdi
leaq 0xa79d4(%rip), %rsi     # 0xda6e0
movapd %xmm1, %xmm0
movb $0x1, %al
callq 0x21d80
leaq 0x10b3ba(%rip), %rax    # 0x13e0d8
movsd (%rax), %xmm1
movsd 0x70(%rbp), %xmm0
mulsd 0xa77e9(%rip), %xmm0    # 0xda518
movsd 0x68(%rbp), %xmm2
mulsd 0xa77e4(%rip), %xmm2    # 0xda520
addsd %xmm0, %xmm2
movsd 0x78(%rbp), %xmm3
mulsd 0xa77db(%rip), %xmm3    # 0xda528
addsd %xmm2, %xmm3
movsd 0xa72bf(%rip), %xmm0    # 0xda018
subsd %xmm3, %xmm0
movapd %xmm0, %xmm3
addsd 0xa7767(%rip), %xmm3    # 0xda4d0
movapd %xmm1, %xmm2
xorpd 0xa77cb(%rip), %xmm2    # 0xda540
ucomisd %xmm1, %xmm3
ja 0x32d81
ucomisd %xmm3, %xmm2
jbe 0x32da9
movq %r15, %rdi
leaq 0xa795c(%rip), %rsi     # 0xda6e7
movb $0x1, %al
callq 0x21d80
leaq 0x10b33f(%rip), %rax    # 0x13e0d8
movsd (%rax), %xmm1
movapd %xmm1, %xmm2
xorpd 0xa7797(%rip), %xmm2    # 0xda540
movsd 0x98(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x32dbd
ucomisd %xmm0, %xmm2
jbe 0x32de5
movq %r15, %rdi
leaq 0xa7926(%rip), %rsi     # 0xda6ed
movb $0x1, %al
callq 0x21d80
leaq 0x10b303(%rip), %rax    # 0x13e0d8
movsd (%rax), %xmm1
movapd %xmm1, %xmm2
xorpd 0xa775b(%rip), %xmm2    # 0xda540
movsd 0xa0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x32df9
ucomisd %xmm0, %xmm2
jbe 0x32e0a
movq %r15, %rdi
leaq 0xa78f1(%rip), %rsi     # 0xda6f4
movb $0x1, %al
callq 0x21d80
movq 0x20(%rbx), %rbp
testq %rbp, %rbp
je 0x32ebc
movq 0x18(%rbp), %rdx
testq %rdx, %rdx
je 0x32e36
movq %r15, %rdi
leaq 0xa78d1(%rip), %rsi     # 0xda6fb
xorl %eax, %eax
callq 0x21d80
jmp 0x32ebc
movq 0x10(%rbx), %rcx
movq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %r14
movq %r14, %rdi
testq %rdx, %rdx
je 0x32e66
leaq 0xa78b3(%rip), %rsi     # 0xda706
leaq 0xa8b07(%rip), %r13     # 0xdb961
movq %r13, %r8
xorl %eax, %eax
callq 0x21140
jmp 0x32e81
leaq 0xa789c(%rip), %rsi     # 0xda709
movq %rcx, %rdx
leaq 0xa8aea(%rip), %r13     # 0xdb961
movq %r13, %rcx
xorl %eax, %eax
callq 0x21140
movq %rbp, %rdi
movq %r14, %rsi
callq 0x44c6c
movq 0x20(%rbp), %rdi
movq %r14, %rsi
callq 0x843f0
movq 0x10(%rbx), %rdx
movq %r15, %rdi
leaq 0xa7869(%rip), %rsi     # 0xda70f
movq %r13, %rcx
xorl %eax, %eax
callq 0x21d80
leaq 0x410(%rsp), %r13
movq (%rsp), %r14
movl $0xa, %edi
movq %r15, %rsi
callq 0x21bb0
incq %r12
movslq 0x34(%r14), %rax
cmpq %rax, %r12
jl 0x32b47
movq %r15, %rdi
callq 0x218b0
movl $0x1, %ebx
jmp 0x32f00
leaq 0xa7767(%rip), %rdi     # 0xda656
xorl %ebx, %ebx
leaq 0x810(%rsp), %rsi
xorl %eax, %eax
callq 0x8a0fe
movl %ebx, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq