// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
testq %rsi, %rsi
movq %rsi, 0x8(%rsp)
je 0x30821
movq %rsi, %rax
leaq 0x94e13(%rip), %rsi     # 0xc5620
leaq 0x810(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21140
jmp 0x30836
leaq 0x810(%rsp), %rdi
movl $0x7ff, %edx            # imm = 0x7FF
movq %rcx, %rsi
callq 0x21440
leaq 0x98587(%rip), %rsi     # 0xc8dc4
leaq 0x810(%rsp), %rdi
callq 0x21ab0
testq %rax, %rax
je 0x30c42
movq %rax, %r15
leaq 0x94de0(%rip), %rdi     # 0xc563d
pushq $0x11
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
movq %rax, %rcx
callq 0x222c0
leaq 0x94dda(%rip), %rdi     # 0xc564f
pushq $0x9
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x222c0
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0x21bb0
leaq 0x410(%rsp), %r14
xorl %ebx, %ebx
movq %r13, (%rsp)
movslq 0x34(%r13), %rax
cmpq %rax, %rbx
jge 0x30c35
movq 0x28(%r13), %rax
movq (%rax,%rbx,8), %r12
movq 0x10(%r12), %rcx
movq %r14, %rdi
testq %rcx, %rcx
je 0x308d0
leaq 0x94d98(%rip), %rsi     # 0xc565d
movl %ebx, %edx
xorl %eax, %eax
callq 0x21140
jmp 0x308e0
leaq 0x94d82(%rip), %rsi     # 0xc5659
movl %ebx, %edx
xorl %eax, %eax
callq 0x21140
movq %r12, %rdi
movq %r14, %rsi
callq 0x3efb0
movq 0x10(%r12), %rdx
movq %r15, %rdi
leaq 0x94d6a(%rip), %rsi     # 0xc5664
xorl %eax, %eax
callq 0x21da0
movq 0x18(%r12), %rbp
testq %rbp, %rbp
je 0x30b6d
movq 0xa8(%rbp), %rax
testb $0x1, %al
je 0x30941
movsd 0x20(%rbp), %xmm0
movsd 0x28(%rbp), %xmm1
movsd 0x30(%rbp), %xmm2
movq %r15, %rdi
leaq 0x94d3c(%rip), %rsi     # 0xc566f
movb $0x3, %al
callq 0x21da0
movq 0xa8(%rbp), %rax
testb $0x2, %al
je 0x3096c
movsd 0x38(%rbp), %xmm0
movsd 0x40(%rbp), %xmm1
movsd 0x48(%rbp), %xmm2
movq %r15, %rdi
leaq 0x94d1e(%rip), %rsi     # 0xc567c
movb $0x3, %al
callq 0x21da0
movq 0xa8(%rbp), %rax
testb $0x4, %al
je 0x30997
movsd 0x50(%rbp), %xmm0
movsd 0x58(%rbp), %xmm1
movsd 0x60(%rbp), %xmm2
movq %r15, %rdi
leaq 0x94d00(%rip), %rsi     # 0xc5689
movb $0x3, %al
callq 0x21da0
movq 0xa8(%rbp), %rax
testb $0x8, %al
je 0x309cb
movsd 0x80(%rbp), %xmm0
movsd 0x88(%rbp), %xmm1
movsd 0x90(%rbp), %xmm2
movq %r15, %rdi
leaq 0x94cd9(%rip), %rsi     # 0xc5696
movb $0x3, %al
callq 0x21da0
movq 0xa8(%rbp), %rax
testb $0x20, %al
je 0x309ef
movsd 0x68(%rbp), %xmm0
movsd 0x70(%rbp), %xmm1
movsd 0x78(%rbp), %xmm2
movq %r15, %rdi
leaq 0x94cbb(%rip), %rsi     # 0xc56a3
movb $0x3, %al
callq 0x21da0
movsd 0x70(%rbp), %xmm0
mulsd 0x94b0c(%rip), %xmm0    # 0xc5508
movsd 0x68(%rbp), %xmm1
mulsd 0x94b07(%rip), %xmm1    # 0xc5510
addsd %xmm0, %xmm1
movsd 0x78(%rbp), %xmm2
mulsd 0x94afe(%rip), %xmm2    # 0xc5518
addsd %xmm1, %xmm2
movsd 0x945f2(%rip), %xmm0    # 0xc5018
subsd %xmm2, %xmm0
leaq 0xfe6c7(%rip), %rax     # 0x12f0f8
movsd (%rax), %xmm1
movapd %xmm0, %xmm3
addsd 0x94a8f(%rip), %xmm3    # 0xc54d0
ucomisd %xmm1, %xmm3
ja 0x30a59
movapd %xmm1, %xmm2
xorpd 0x94add(%rip), %xmm2    # 0xc5530
ucomisd %xmm3, %xmm2
jbe 0x30ad8
movsd 0x945b7(%rip), %xmm1    # 0xc5018
subsd %xmm0, %xmm1
movq %r15, %rdi
leaq 0x94c41(%rip), %rsi     # 0xc56b0
movapd %xmm1, %xmm0
movb $0x1, %al
callq 0x21da0
leaq 0xfe677(%rip), %rax     # 0x12f0f8
movsd (%rax), %xmm1
movsd 0x70(%rbp), %xmm0
mulsd 0x94a76(%rip), %xmm0    # 0xc5508
movsd 0x68(%rbp), %xmm2
mulsd 0x94a71(%rip), %xmm2    # 0xc5510
addsd %xmm0, %xmm2
movsd 0x78(%rbp), %xmm3
mulsd 0x94a68(%rip), %xmm3    # 0xc5518
addsd %xmm2, %xmm3
movsd 0x9455c(%rip), %xmm0    # 0xc5018
subsd %xmm3, %xmm0
movapd %xmm0, %xmm3
addsd 0x94a04(%rip), %xmm3    # 0xc54d0
movapd %xmm1, %xmm2
xorpd 0x94a58(%rip), %xmm2    # 0xc5530
ucomisd %xmm1, %xmm3
ja 0x30ae4
ucomisd %xmm3, %xmm2
jbe 0x30b0c
movq %r15, %rdi
leaq 0x94bc9(%rip), %rsi     # 0xc56b7
movb $0x1, %al
callq 0x21da0
leaq 0xfe5fc(%rip), %rax     # 0x12f0f8
movsd (%rax), %xmm1
movapd %xmm1, %xmm2
xorpd 0x94a24(%rip), %xmm2    # 0xc5530
movsd 0x98(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x30b20
ucomisd %xmm0, %xmm2
jbe 0x30b48
movq %r15, %rdi
leaq 0x94b93(%rip), %rsi     # 0xc56bd
movb $0x1, %al
callq 0x21da0
leaq 0xfe5c0(%rip), %rax     # 0x12f0f8
movsd (%rax), %xmm1
movapd %xmm1, %xmm2
xorpd 0x949e8(%rip), %xmm2    # 0xc5530
movsd 0xa0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x30b5c
ucomisd %xmm0, %xmm2
jbe 0x30b6d
movq %r15, %rdi
leaq 0x94b5e(%rip), %rsi     # 0xc56c4
movb $0x1, %al
callq 0x21da0
movq 0x20(%r12), %rbp
testq %rbp, %rbp
je 0x30c22
movq 0x18(%rbp), %rdx
testq %rdx, %rdx
je 0x30b9a
movq %r15, %rdi
leaq 0x94b3d(%rip), %rsi     # 0xc56cb
xorl %eax, %eax
callq 0x21da0
jmp 0x30c22
movq 0x10(%r12), %rcx
movq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %r14
movq %r14, %rdi
testq %rdx, %rdx
je 0x30bcb
leaq 0x94b1e(%rip), %rsi     # 0xc56d6
leaq 0x95d72(%rip), %r13     # 0xc6931
movq %r13, %r8
xorl %eax, %eax
callq 0x21140
jmp 0x30be6
leaq 0x94b07(%rip), %rsi     # 0xc56d9
movq %rcx, %rdx
leaq 0x95d55(%rip), %r13     # 0xc6931
movq %r13, %rcx
xorl %eax, %eax
callq 0x21140
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3fd78
movq 0x20(%rbp), %rdi
movq %r14, %rsi
callq 0x7955e
movq 0x10(%r12), %rdx
movq %r15, %rdi
leaq 0x94ad3(%rip), %rsi     # 0xc56df
movq %r13, %rcx
xorl %eax, %eax
callq 0x21da0
movq (%rsp), %r13
leaq 0x410(%rsp), %r14
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0x21bb0
incq %rbx
jmp 0x3089c
movq %r15, %rdi
callq 0x218c0
pushq $0x1
popq %rbx
jmp 0x30c5a
leaq 0x949dd(%rip), %rdi     # 0xc5626
xorl %ebx, %ebx
leaq 0x810(%rsp), %rsi
xorl %eax, %eax
callq 0x7e83e
movl %ebx, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq