// File: /workspace/github_cpp_makefile_star2_O1/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 0x32e01
movq %rsi, %rax
leaq 0xa9853(%rip), %rsi     # 0xdc640
leaq 0x810(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21140
jmp 0x32e16
leaq 0x810(%rsp), %rdi
movl $0x7ff, %edx            # imm = 0x7FF
movq %rcx, %rsi
callq 0x21430
leaq 0xacfff(%rip), %rsi     # 0xdfe1c
leaq 0x810(%rsp), %rdi
callq 0x21ac0
testq %rax, %rax
je 0x33202
movq %rax, %r15
leaq 0xa9820(%rip), %rdi     # 0xdc65d
movl $0x11, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x222a0
leaq 0xa9819(%rip), %rdi     # 0xdc66f
movl $0x9, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x222a0
movl $0xa, %edi
movq %r15, %rsi
callq 0x21bc0
cmpl $0x0, 0x34(%r14)
jle 0x331f3
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 0x32eb6
leaq 0xa97d3(%rip), %rsi     # 0xdc67d
movl %r12d, %edx
xorl %eax, %eax
callq 0x21140
jmp 0x32ec7
leaq 0xa97bc(%rip), %rsi     # 0xdc679
movl %r12d, %edx
xorl %eax, %eax
callq 0x21140
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4427e
movq 0x10(%rbx), %rdx
movq %r15, %rdi
leaq 0xa97a4(%rip), %rsi     # 0xdc684
xorl %eax, %eax
callq 0x21d90
movq 0x18(%rbx), %rbp
testq %rbp, %rbp
leaq 0x10d1e3(%rip), %r13    # 0x1400d8
je 0x33127
testb $0x1, 0xa8(%rbp)
je 0x32f24
movsd 0x20(%rbp), %xmm0
movsd 0x28(%rbp), %xmm1
movsd 0x30(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa9772(%rip), %rsi     # 0xdc68f
movb $0x3, %al
callq 0x21d90
testb $0x2, 0xa8(%rbp)
je 0x32f4d
movsd 0x38(%rbp), %xmm0
movsd 0x40(%rbp), %xmm1
movsd 0x48(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa9756(%rip), %rsi     # 0xdc69c
movb $0x3, %al
callq 0x21d90
testb $0x4, 0xa8(%rbp)
je 0x32f76
movsd 0x50(%rbp), %xmm0
movsd 0x58(%rbp), %xmm1
movsd 0x60(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa973a(%rip), %rsi     # 0xdc6a9
movb $0x3, %al
callq 0x21d90
testb $0x8, 0xa8(%rbp)
je 0x32fa8
movsd 0x80(%rbp), %xmm0
movsd 0x88(%rbp), %xmm1
movsd 0x90(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa9715(%rip), %rsi     # 0xdc6b6
movb $0x3, %al
callq 0x21d90
testb $0x20, 0xa8(%rbp)
je 0x32fd1
movsd 0x68(%rbp), %xmm0
movsd 0x70(%rbp), %xmm1
movsd 0x78(%rbp), %xmm2
movq %r15, %rdi
leaq 0xa96f9(%rip), %rsi     # 0xdc6c3
movb $0x3, %al
callq 0x21d90
movsd 0x70(%rbp), %xmm0
mulsd 0xa9532(%rip), %xmm0    # 0xdc510
movsd 0x68(%rbp), %xmm1
mulsd 0xa952d(%rip), %xmm1    # 0xdc518
addsd %xmm0, %xmm1
movsd 0x78(%rbp), %xmm0
mulsd 0xa9524(%rip), %xmm0    # 0xdc520
addsd %xmm1, %xmm0
movsd 0xa9010(%rip), %xmm1    # 0xdc018
subsd %xmm0, %xmm1
movsd (%r13), %xmm0
movapd %xmm1, %xmm2
addsd 0xa94b2(%rip), %xmm2    # 0xdc4d0
ucomisd %xmm0, %xmm2
ja 0x33032
xorpd 0xa9504(%rip), %xmm0    # 0xdc530
ucomisd %xmm2, %xmm0
jbe 0x3304f
movsd 0xa8fde(%rip), %xmm0    # 0xdc018
subsd %xmm1, %xmm0
movq %r15, %rdi
leaq 0xa9688(%rip), %rsi     # 0xdc6d0
movb $0x1, %al
callq 0x21d90
movsd 0x70(%rbp), %xmm0
mulsd 0xa94b4(%rip), %xmm0    # 0xdc510
movsd 0x68(%rbp), %xmm1
mulsd 0xa94af(%rip), %xmm1    # 0xdc518
addsd %xmm0, %xmm1
movsd 0x78(%rbp), %xmm2
mulsd 0xa94a6(%rip), %xmm2    # 0xdc520
addsd %xmm1, %xmm2
movsd 0xa8f92(%rip), %xmm0    # 0xdc018
subsd %xmm2, %xmm0
movsd (%r13), %xmm1
movapd %xmm0, %xmm2
addsd 0xa9434(%rip), %xmm2    # 0xdc4d0
ucomisd %xmm1, %xmm2
ja 0x330b0
xorpd 0xa9486(%rip), %xmm1    # 0xdc530
ucomisd %xmm2, %xmm1
jbe 0x330c1
movq %r15, %rdi
leaq 0xa961d(%rip), %rsi     # 0xdc6d7
movb $0x1, %al
callq 0x21d90
movsd 0x98(%rbp), %xmm0
movsd (%r13), %xmm1
ucomisd %xmm1, %xmm0
ja 0x330e3
xorpd 0xa9453(%rip), %xmm1    # 0xdc530
ucomisd %xmm0, %xmm1
jbe 0x330f4
movq %r15, %rdi
leaq 0xa95f0(%rip), %rsi     # 0xdc6dd
movb $0x1, %al
callq 0x21d90
movsd 0xa0(%rbp), %xmm0
movsd (%r13), %xmm1
ucomisd %xmm1, %xmm0
ja 0x33116
xorpd 0xa9420(%rip), %xmm1    # 0xdc530
ucomisd %xmm0, %xmm1
jbe 0x33127
movq %r15, %rdi
leaq 0xa95c4(%rip), %rsi     # 0xdc6e4
movb $0x1, %al
callq 0x21d90
movq 0x20(%rbx), %rbp
testq %rbp, %rbp
je 0x331ce
movq 0x18(%rbp), %rdx
testq %rdx, %rdx
je 0x33150
movq %r15, %rdi
leaq 0xa95a4(%rip), %rsi     # 0xdc6eb
xorl %eax, %eax
callq 0x21d90
jmp 0x331ce
movq 0x10(%rbx), %rcx
movq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %r13
movq %r13, %rdi
testq %rdx, %rdx
je 0x33180
leaq 0xa9589(%rip), %rsi     # 0xdc6f6
leaq 0xaa7dd(%rip), %r14     # 0xdd951
movq %r14, %r8
xorl %eax, %eax
callq 0x21140
jmp 0x3319b
leaq 0xa9572(%rip), %rsi     # 0xdc6f9
movq %rcx, %rdx
leaq 0xaa7c0(%rip), %r14     # 0xdd951
movq %r14, %rcx
xorl %eax, %eax
callq 0x21140
movq %rbp, %rdi
movq %r13, %rsi
callq 0x45252
movq 0x20(%rbp), %rdi
movq %r13, %rsi
callq 0x854e8
movq 0x10(%rbx), %rdx
movq %r15, %rdi
leaq 0xa953f(%rip), %rsi     # 0xdc6ff
movq %r14, %rcx
xorl %eax, %eax
callq 0x21d90
movq (%rsp), %r14
movl $0xa, %edi
movq %r15, %rsi
callq 0x21bc0
incq %r12
movslq 0x34(%r14), %rax
cmpq %rax, %r12
leaq 0x410(%rsp), %r13
jl 0x32e8f
movq %r15, %rdi
callq 0x218b0
movl $0x1, %ebx
jmp 0x3321a
leaq 0xa943d(%rip), %rdi     # 0xdc646
xorl %ebx, %ebx
leaq 0x810(%rsp), %rsi
xorl %eax, %eax
callq 0x8b53a
movl %ebx, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq