// File: 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:
subq $0x1148, %rsp           # imm = 0x1148
movq %rdi, 0x1138(%rsp)
movq %rsi, 0x1130(%rsp)
movq %rdx, 0x1128(%rsp)
movq 0x1138(%rsp), %rax
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0x1130(%rsp)
je 0x3abb2
leaq 0x920(%rsp), %rdi
movq 0x1130(%rsp), %rdx
movq 0x1128(%rsp), %rcx
leaq 0x153b1f(%rip), %rsi    # 0x18e6c8
movb $0x0, %al
callq 0x22160
jmp 0x3abcc
leaq 0x920(%rsp), %rdi
movq 0x1128(%rsp), %rsi
movl $0x7ff, %edx            # imm = 0x7FF
callq 0x224b0
leaq 0x920(%rsp), %rdi
leaq 0x15699f(%rip), %rsi    # 0x19157a
callq 0x22c10
movq %rax, 0x918(%rsp)
cmpq $0x0, 0x918(%rsp)
jne 0x3ac19
leaq 0x920(%rsp), %rsi
leaq 0x153acc(%rip), %rdi    # 0x18e6ce
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x1144(%rsp)
jmp 0x3b3c4
movq 0x918(%rsp), %rdi
leaq 0x153abd(%rip), %rsi    # 0x18e6e5
movb $0x0, %al
callq 0x22f30
movq 0x918(%rsp), %rdi
leaq 0x153ab9(%rip), %rsi    # 0x18e6f7
movb $0x0, %al
callq 0x22f30
movq 0x918(%rsp), %rdi
leaq 0x15a9ee(%rip), %rsi    # 0x195642
movb $0x0, %al
callq 0x22f30
movl $0x0, 0x914(%rsp)
movq 0xd8(%rsp), %rdi
movl 0x914(%rsp), %eax
movl %eax, 0xd4(%rsp)
callq 0x2c020
movl %eax, %ecx
movl 0xd4(%rsp), %eax
cmpl %ecx, %eax
jge 0x3b3ac
movq 0xd8(%rsp), %rdi
movl 0x914(%rsp), %esi
callq 0x48220
movq %rax, 0x908(%rsp)
movq 0x908(%rsp), %rdi
callq 0x48250
cmpq $0x0, %rax
jne 0x3ace0
leaq 0x500(%rsp), %rdi
movl 0x914(%rsp), %edx
leaq 0x153a2a(%rip), %rsi    # 0x18e701
movb $0x0, %al
callq 0x22160
jmp 0x3ad2b
leaq 0x500(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl 0x914(%rsp), %eax
movl %eax, 0xd0(%rsp)
movq 0x908(%rsp), %rdi
callq 0x48250
movq 0xc8(%rsp), %rdi
movl 0xd0(%rsp), %edx
movq %rax, %rcx
leaq 0x1539e1(%rip), %rsi    # 0x18e705
movb $0x0, %al
callq 0x22160
movq 0x908(%rsp), %rdi
leaq 0x500(%rsp), %rsi
callq 0x64260
movq 0x918(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x908(%rsp), %rdi
callq 0x48250
movq 0xc0(%rsp), %rdi
movq %rax, %rdx
leaq 0x15399d(%rip), %rsi    # 0x18e70c
movb $0x0, %al
callq 0x22f30
movq 0x908(%rsp), %rdi
callq 0x2be70
cmpq $0x0, %rax
je 0x3b1f7
movq 0x908(%rsp), %rdi
callq 0x2be70
movq %rax, 0x4f8(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x488a0
cmpl $0x0, %eax
je 0x3ae44
movq 0x918(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x488e0
movq %rax, %rdi
callq 0x2b4a0
movsd %xmm0, 0xb8(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x488e0
movq %rax, %rdi
callq 0x2b4b0
movsd %xmm0, 0xb0(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x488e0
movq %rax, %rdi
callq 0x2b4c0
movq 0xa8(%rsp), %rdi
movsd 0xb0(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0xb8(%rsp), %xmm0
leaq 0x1538da(%rip), %rsi    # 0x18e717
movb $0x3, %al
callq 0x22f30
movq 0x4f8(%rsp), %rdi
callq 0x488f0
cmpl $0x0, %eax
je 0x3aee6
movq 0x918(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48930
movq %rax, %rdi
callq 0x2b4a0
movsd %xmm0, 0xa0(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48930
movq %rax, %rdi
callq 0x2b4b0
movsd %xmm0, 0x98(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48930
movq %rax, %rdi
callq 0x2b4c0
movq 0x90(%rsp), %rdi
movsd 0x98(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0xa0(%rsp), %xmm0
leaq 0x153845(%rip), %rsi    # 0x18e724
movb $0x3, %al
callq 0x22f30
movq 0x4f8(%rsp), %rdi
callq 0x48940
cmpl $0x0, %eax
je 0x3af82
movq 0x918(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48980
movq %rax, %rdi
callq 0x2b4a0
movsd %xmm0, 0x88(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48980
movq %rax, %rdi
callq 0x2b4b0
movsd %xmm0, 0x80(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48980
movq %rax, %rdi
callq 0x2b4c0
movq 0x78(%rsp), %rdi
movsd 0x80(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x88(%rsp), %xmm0
leaq 0x1537b6(%rip), %rsi    # 0x18e731
movb $0x3, %al
callq 0x22f30
movq 0x4f8(%rsp), %rdi
callq 0x48990
cmpl $0x0, %eax
je 0x3b00e
movq 0x918(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x2bea0
movq %rax, %rdi
callq 0x2b4a0
movsd %xmm0, 0x70(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x2bea0
movq %rax, %rdi
callq 0x2b4b0
movsd %xmm0, 0x68(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x2bea0
movq %rax, %rdi
callq 0x2b4c0
movq 0x60(%rsp), %rdi
movsd 0x68(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x70(%rsp), %xmm0
leaq 0x153737(%rip), %rsi    # 0x18e73e
movb $0x3, %al
callq 0x22f30
movq 0x4f8(%rsp), %rdi
callq 0x489d0
cmpl $0x0, %eax
je 0x3b09a
movq 0x918(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48a10
movq %rax, %rdi
callq 0x2b4a0
movsd %xmm0, 0x58(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48a10
movq %rax, %rdi
callq 0x2b4b0
movsd %xmm0, 0x50(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48a10
movq %rax, %rdi
callq 0x2b4c0
movq 0x48(%rsp), %rdi
movsd 0x50(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x58(%rsp), %xmm0
leaq 0x1536b8(%rip), %rsi    # 0x18e74b
movb $0x3, %al
callq 0x22f30
movq 0x4f8(%rsp), %rdi
callq 0x48a50
leaq 0x1bb1fa(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm2
movsd 0x152f56(%rip), %xmm1   # 0x18e010
callq 0x48a20
cmpl $0x0, %eax
je 0x3b100
movq 0x918(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48a50
movq 0x40(%rsp), %rdi
movaps %xmm0, %xmm1
movsd 0x152f22(%rip), %xmm0   # 0x18e010
subsd %xmm1, %xmm0
leaq 0x15365f(%rip), %rsi    # 0x18e758
movb $0x1, %al
callq 0x22f30
movq 0x4f8(%rsp), %rdi
callq 0x48a50
leaq 0x1bb194(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm2
movsd 0x152ef0(%rip), %xmm1   # 0x18e010
callq 0x48a20
cmpl $0x0, %eax
je 0x3b157
movq 0x918(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48a50
movq 0x38(%rsp), %rdi
leaq 0x15360f(%rip), %rsi    # 0x18e75f
movb $0x1, %al
callq 0x22f30
movq 0x4f8(%rsp), %rdi
callq 0x48ae0
leaq 0x1bb13d(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x48a80
cmpl $0x0, %eax
je 0x3b1a6
movq 0x918(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48ae0
movq 0x30(%rsp), %rdi
leaq 0x1535c6(%rip), %rsi    # 0x18e765
movb $0x1, %al
callq 0x22f30
movq 0x4f8(%rsp), %rdi
callq 0x48b00
leaq 0x1bb0ee(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x48a80
cmpl $0x0, %eax
je 0x3b1f5
movq 0x918(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x48b00
movq 0x28(%rsp), %rdi
leaq 0x15357e(%rip), %rsi    # 0x18e76c
movb $0x1, %al
callq 0x22f30
jmp 0x3b1f7
movq 0x908(%rsp), %rdi
callq 0x48b20
cmpq $0x0, %rax
je 0x3b380
movq 0x908(%rsp), %rdi
callq 0x48b20
movq %rax, 0x4f0(%rsp)
movq 0x4f0(%rsp), %rdi
callq 0x48b30
cmpq $0x0, %rax
je 0x3b26b
movq 0x918(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x4f0(%rsp), %rdi
callq 0x48b30
movq 0x20(%rsp), %rdi
movq %rax, %rdx
leaq 0x153514(%rip), %rsi    # 0x18e773
movb $0x0, %al
callq 0x22f30
jmp 0x3b37e
leaq 0x1547c2(%rip), %rax    # 0x18fa34
movq %rax, 0xe8(%rsp)
cmpq $0x0, 0x1130(%rsp)
je 0x3b2d1
leaq 0xf0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x1130(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x908(%rsp), %rdi
callq 0x48250
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
movq 0xe8(%rsp), %r8
leaq 0x1534b6(%rip), %rsi    # 0x18e77e
movb $0x0, %al
callq 0x22160
jmp 0x3b309
leaq 0xf0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x908(%rsp), %rdi
callq 0x48250
movq 0x8(%rsp), %rdi
movq %rax, %rdx
movq 0xe8(%rsp), %rcx
leaq 0x15347f(%rip), %rsi    # 0x18e781
movb $0x0, %al
callq 0x22160
movq 0x4f0(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x65c80
movq 0x4f0(%rsp), %rdi
callq 0x48b40
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x2b6a0
movq 0x918(%rsp), %rax
movq %rax, (%rsp)
movq 0x908(%rsp), %rdi
callq 0x48250
movq (%rsp), %rdi
movq %rax, %rdx
movq 0xe8(%rsp), %rcx
leaq 0x153410(%rip), %rsi    # 0x18e787
movb $0x0, %al
callq 0x22f30
jmp 0x3b380
movq 0x918(%rsp), %rdi
leaq 0x15a2b3(%rip), %rsi    # 0x195642
movb $0x0, %al
callq 0x22f30
movl 0x914(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x914(%rsp)
jmp 0x3ac66
movq 0x918(%rsp), %rdi
callq 0x229d0
movl $0x1, 0x1144(%rsp)
movl 0x1144(%rsp), %eax
addq $0x1148, %rsp           # imm = 0x1148
retq
nopw %cs:(%rax,%rax)