// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::ParseSUNCGMaterials(gaps::R3Scene*, gaps::RNSymbolTable<gaps::R2Texture*>&, gaps::RNArray<gaps::R3Material*> const&, gaps::RNArray<gaps::R3Material*>&, Json::Value*)
static int
ParseSUNCGMaterials(R3Scene *scene,
  RNSymbolTable<R2Texture *>& texture_symbol_table,
  const RNArray<R3Material *>& input_materials,
  RNArray<R3Material *>& output_materials,
  Json::Value *json_materials)
{
  // Parse JSON array of materials
  Json::Value *json_material, *json_value;
  for (Json::ArrayIndex index = 0; index < json_materials->size(); index++) {
    char material_name[1024] = { '\0' };
    char texture_name[1024] = { '\0' };
    char diffuse_string[1024] = { '\0' };
    if (!GetJsonArrayEntry(json_material, json_materials, index)) continue; 
    if (json_material->type() != Json::objectValue) continue;
    if (GetJsonObjectMember(json_value, json_material, "name"))
      strncpy(material_name, json_value->asString().c_str(), 1023);
    if (GetJsonObjectMember(json_value, json_material, "texture")) 
      strncpy(texture_name, json_value->asString().c_str(), 1023);
    if (GetJsonObjectMember(json_value, json_material, "diffuse")) 
      strncpy(diffuse_string, json_value->asString().c_str(), 1023);

    // Get input material
    R3Material *input_material = (input_materials.NEntries() > (int) index) ? input_materials.Kth(index) : NULL;
    const R3Brdf *input_brdf = (input_material) ? input_material->Brdf() : NULL;
    const R2Texture *input_texture = (input_material) ? input_material->Texture() : NULL;
    
    // Get/create output material
    R3Material *output_material = NULL;
    if (*diffuse_string || *texture_name) {
      // Create output brdf
      R3Brdf *output_brdf = NULL;
      if (input_brdf) output_brdf = new R3Brdf(*input_brdf);
      else { output_brdf = new R3Brdf(); output_brdf->SetDiffuse(RNwhite_rgb); }
      scene->InsertBrdf(output_brdf);
      if (*diffuse_string) {
        long int b = strtol(&diffuse_string[5], NULL, 16); diffuse_string[5] = '\0';
        long int g = strtol(&diffuse_string[3], NULL, 16); diffuse_string[3] = '\0';
        long int r = strtol(&diffuse_string[1], NULL, 16); diffuse_string[1] = '\0';
        RNRgb diffuse_rgb(r / 255.0, g / 255.0, b / 255.0);
        output_brdf->SetDiffuse(diffuse_rgb);
      }

      // Create output texture
      R2Texture *output_texture = NULL;
      if (*texture_name) {
        char texture_filename[2048];
        const char *texture_directory = "../../texture";
        sprintf(texture_filename, "%s/%s.png", texture_directory, texture_name);
        if (!RNFileExists(texture_filename)) sprintf(texture_filename, "%s/%s.jpg", texture_directory, texture_name);
        if (!texture_symbol_table.Find(texture_filename, &output_texture)) {
          if (input_texture) output_texture = new R2Texture(*input_texture);
          else output_texture = new R2Texture();
          R2Image *image = new R2Image();
          if (!image->Read(texture_filename)) return 0;
          output_texture->SetImage(image);
          output_texture->SetFilename(texture_filename);
          output_texture->SetName(texture_name);
          scene->InsertTexture(output_texture);
          texture_symbol_table.Insert(texture_filename, output_texture);
        }
      }
      else if (input_texture) {
        const char *texture_filename = input_texture->Filename();
        if (!texture_symbol_table.Find(texture_filename, &output_texture)) {
          output_texture = new R2Texture(*input_texture);
          scene->InsertTexture(output_texture);
          texture_symbol_table.Insert(texture_filename, output_texture);
        }
      }

      // Create output material
      output_material = new R3Material(output_brdf, output_texture);
      output_material->SetName(material_name);
      scene->InsertMaterial(output_material);
    }

    // Insert material into array of results
    output_materials.Insert(output_material);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x16a8, %rsp           # imm = 0x16A8
movq %rdi, 0x1698(%rsp)
movq %rsi, 0x1690(%rsp)
movq %rdx, 0x1688(%rsp)
movq %rcx, 0x1680(%rsp)
movq %r8, 0x1678(%rsp)
movl $0x0, 0x1664(%rsp)
movl 0x1664(%rsp), %eax
movl %eax, 0xd8(%rsp)
movq 0x1678(%rsp), %rdi
callq 0xff4b0
movl %eax, %ecx
movl 0xd8(%rsp), %eax
cmpl %ecx, %eax
jae 0x3f4e5
leaq 0x1260(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x224f0
leaq 0xe60(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x224f0
leaq 0xa60(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x224f0
movq 0x1678(%rsp), %rsi
movl 0x1664(%rsp), %edx
leaq 0x1670(%rsp), %rdi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x3e810
cmpl $0x0, %eax
jne 0x3e9ad
jmp 0x3f4cf
movq 0x1670(%rsp), %rdi
callq 0x101940
cmpl $0x7, %eax
je 0x3e9c4
jmp 0x3f4cf
movq 0x1670(%rsp), %rsi
leaq 0x1668(%rsp), %rdi
leaq 0x154042(%rip), %rdx    # 0x192a1d
xorl %ecx, %ecx
callq 0x3e6f0
cmpl $0x0, %eax
je 0x3ea3b
leaq 0x1260(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x1668(%rsp), %rsi
leaq 0xa40(%rsp), %rdi
callq 0xf7000
leaq 0xa40(%rsp), %rdi
callq 0x223a0
movq 0xd0(%rsp), %rdi
movq %rax, %rsi
movl $0x3ff, %edx            # imm = 0x3FF
callq 0x224b0
leaq 0xa40(%rsp), %rdi
callq 0x22610
movq 0x1670(%rsp), %rsi
leaq 0x1668(%rsp), %rdi
leaq 0x150fca(%rip), %rdx    # 0x18fa1c
xorl %ecx, %ecx
callq 0x3e6f0
cmpl $0x0, %eax
je 0x3eab2
leaq 0xe60(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x1668(%rsp), %rsi
leaq 0xa20(%rsp), %rdi
callq 0xf7000
leaq 0xa20(%rsp), %rdi
callq 0x223a0
movq 0xc8(%rsp), %rdi
movq %rax, %rsi
movl $0x3ff, %edx            # imm = 0x3FF
callq 0x224b0
leaq 0xa20(%rsp), %rdi
callq 0x22610
movq 0x1670(%rsp), %rsi
leaq 0x1668(%rsp), %rdi
leaq 0x150f45(%rip), %rdx    # 0x18fa0e
xorl %ecx, %ecx
callq 0x3e6f0
cmpl $0x0, %eax
je 0x3eb29
leaq 0xa60(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x1668(%rsp), %rsi
leaq 0xa00(%rsp), %rdi
callq 0xf7000
leaq 0xa00(%rsp), %rdi
callq 0x223a0
movq 0xc0(%rsp), %rdi
movq %rax, %rsi
movl $0x3ff, %edx            # imm = 0x3FF
callq 0x224b0
leaq 0xa00(%rsp), %rdi
callq 0x22610
movq 0x1688(%rsp), %rdi
callq 0x2b8b0
cmpl 0x1664(%rsp), %eax
jle 0x3eb5d
movq 0x1688(%rsp), %rdi
movl 0x1664(%rsp), %esi
callq 0x49ca0
movq %rax, 0xb8(%rsp)
jmp 0x3eb69
xorl %eax, %eax
movq %rax, 0xb8(%rsp)
jmp 0x3eb69
movq 0xb8(%rsp), %rax
movq %rax, 0x9f8(%rsp)
cmpq $0x0, 0x9f8(%rsp)
je 0x3eb9b
movq 0x9f8(%rsp), %rdi
callq 0x2be70
movq %rax, 0xb0(%rsp)
jmp 0x3eba7
xorl %eax, %eax
movq %rax, 0xb0(%rsp)
jmp 0x3eba7
movq 0xb0(%rsp), %rax
movq %rax, 0x9f0(%rsp)
cmpq $0x0, 0x9f8(%rsp)
je 0x3ebd9
movq 0x9f8(%rsp), %rdi
callq 0x48b20
movq %rax, 0xa8(%rsp)
jmp 0x3ebe5
xorl %eax, %eax
movq %rax, 0xa8(%rsp)
jmp 0x3ebe5
movq 0xa8(%rsp), %rax
movq %rax, 0x9e8(%rsp)
movq $0x0, 0x9e0(%rsp)
cmpb $0x0, 0xa60(%rsp)
jne 0x3ec19
cmpb $0x0, 0xe60(%rsp)
je 0x3f4ba
movq $0x0, 0x9d8(%rsp)
cmpq $0x0, 0x9f0(%rsp)
je 0x3ec9b
movl $0xb8, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xa0(%rsp)
movq 0x9f0(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x645c0
jmp 0x3ec5b
movq 0xa0(%rsp), %rax
movq %rax, 0x9d8(%rsp)
jmp 0x3ed0f
movq 0xa0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
movl $0xb8, %esi
callq 0x22a00
jmp 0x3f4ff
movl $0xb8, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x98(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x644b0
jmp 0x3ecbe
movq 0x98(%rsp), %rax
movq %rax, 0x9d8(%rsp)
movq 0x9d8(%rsp), %rdi
leaq 0x1c0f6b(%rip), %rsi    # 0x1ffc48
callq 0x64e20
jmp 0x3ed0f
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
movl $0xb8, %esi
callq 0x22a00
jmp 0x3f4ff
movq 0x1698(%rsp), %rdi
movq 0x9d8(%rsp), %rsi
callq 0x2ccd0
cmpb $0x0, 0xa60(%rsp)
je 0x3ee14
leaq 0xa60(%rsp), %rdi
addq $0x5, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x22ed0
movq %rax, 0x9c0(%rsp)
movb $0x0, 0xa65(%rsp)
leaq 0xa60(%rsp), %rdi
addq $0x3, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x22ed0
movq %rax, 0x9b8(%rsp)
movb $0x0, 0xa63(%rsp)
leaq 0xa60(%rsp), %rdi
addq $0x1, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x22ed0
movq %rax, 0x9b0(%rsp)
movb $0x0, 0xa61(%rsp)
cvtsi2sdq 0x9b0(%rsp), %xmm0
movsd 0x14f81e(%rip), %xmm1   # 0x18e5e0
divsd %xmm1, %xmm0
cvtsi2sdq 0x9b8(%rsp), %xmm1
movsd 0x14f808(%rip), %xmm2   # 0x18e5e0
divsd %xmm2, %xmm1
cvtsi2sdq 0x9c0(%rsp), %xmm2
movsd 0x14f7f2(%rip), %xmm3   # 0x18e5e0
divsd %xmm3, %xmm2
leaq 0x998(%rsp), %rdi
callq 0xef340
movq 0x9d8(%rsp), %rdi
leaq 0x998(%rsp), %rsi
callq 0x64e20
movq $0x0, 0x990(%rsp)
cmpb $0x0, 0xe60(%rsp)
je 0x3f1e4
leaq 0x150be1(%rip), %rax    # 0x18fa16
movq %rax, 0x188(%rsp)
leaq 0x190(%rsp), %rdi
movq 0x188(%rsp), %rdx
leaq 0xe60(%rsp), %rcx
leaq 0x150bc8(%rip), %rsi    # 0x18fa24
movb $0x0, %al
callq 0x22160
leaq 0x190(%rsp), %rdi
callq 0xf28e0
cmpl $0x0, %eax
jne 0x3ee9b
leaq 0x190(%rsp), %rdi
movq 0x188(%rsp), %rdx
leaq 0xe60(%rsp), %rcx
leaq 0x150b9a(%rip), %rsi    # 0x18fa2e
movb $0x0, %al
callq 0x22160
movq 0x1690(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x167(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x232a0
movq 0x90(%rsp), %rdx
leaq 0x168(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x485b0
jmp 0x3eedf
movq 0x88(%rsp), %rdi
leaq 0x168(%rsp), %rsi
leaq 0x990(%rsp), %rdx
callq 0x4a440
movl %eax, 0x84(%rsp)
jmp 0x3ef05
movl 0x84(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x83(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x22610
leaq 0x167(%rsp), %rdi
callq 0x22ab0
movb 0x83(%rsp), %al
testb $0x1, %al
jne 0x3ef45
jmp 0x3f1df
cmpq $0x0, 0x9e8(%rsp)
je 0x3efff
movl $0x50, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x78(%rsp)
movq 0x9e8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x657c0
jmp 0x3ef7c
movq 0x78(%rsp), %rax
movq %rax, 0x990(%rsp)
jmp 0x3f052
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
jmp 0x3efc5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x22610
leaq 0x167(%rsp), %rdi
callq 0x22ab0
jmp 0x3f4ff
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
movl $0x50, %esi
callq 0x22a00
jmp 0x3f4ff
movl $0x50, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x70(%rsp)
callq 0x65740
jmp 0x3f01b
movq 0x70(%rsp), %rax
movq %rax, 0x990(%rsp)
jmp 0x3f052
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
movl $0x50, %esi
callq 0x22a00
jmp 0x3f4ff
movl $0x18, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x68(%rsp)
callq 0xe5730
jmp 0x3f06e
movq 0x68(%rsp), %rax
movq %rax, 0x158(%rsp)
movq 0x158(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x49390
cmpl $0x0, %eax
jne 0x3f0cd
movl $0x0, 0x16a4(%rsp)
jmp 0x3f4f0
movq 0x68(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
movl $0x18, %esi
callq 0x22a00
jmp 0x3f4ff
movq 0x990(%rsp), %rdi
movq 0x158(%rsp), %rsi
callq 0x65ce0
movq 0x990(%rsp), %rdi
leaq 0x190(%rsp), %rsi
movq %rsi, 0x58(%rsp)
callq 0x65c80
movq 0x990(%rsp), %rdi
leaq 0xe60(%rsp), %rsi
callq 0x65c20
movq 0x1698(%rsp), %rdi
movq 0x990(%rsp), %rsi
callq 0x2cdd0
movq 0x1690(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x137(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x232a0
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
leaq 0x138(%rsp), %rdi
callq 0x485b0
jmp 0x3f15e
movq 0x50(%rsp), %rdi
movq 0x990(%rsp), %rdx
leaq 0x138(%rsp), %rsi
callq 0x4a4d0
jmp 0x3f17a
leaq 0x138(%rsp), %rdi
callq 0x22610
leaq 0x137(%rsp), %rdi
callq 0x22ab0
jmp 0x3f1df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
jmp 0x3f1cd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x22610
leaq 0x137(%rsp), %rdi
callq 0x22ab0
jmp 0x3f4ff
jmp 0x3f42c
cmpq $0x0, 0x9e8(%rsp)
je 0x3f42a
movq 0x9e8(%rsp), %rdi
callq 0x48b30
movq %rax, 0x128(%rsp)
movq 0x1690(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x107(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x232a0
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x108(%rsp), %rdi
callq 0x485b0
jmp 0x3f24d
movq 0x38(%rsp), %rdi
leaq 0x108(%rsp), %rsi
leaq 0x990(%rsp), %rdx
callq 0x4a440
movl %eax, 0x34(%rsp)
jmp 0x3f26d
movl 0x34(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x33(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x22610
leaq 0x107(%rsp), %rdi
callq 0x22ab0
movb 0x33(%rsp), %al
testb $0x1, %al
jne 0x3f2a4
jmp 0x3f428
movl $0x50, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq 0x9e8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x657c0
jmp 0x3f2cc
movq 0x28(%rsp), %rax
movq %rax, 0x990(%rsp)
movq 0x1698(%rsp), %rdi
movq 0x990(%rsp), %rsi
callq 0x2cdd0
movq 0x1690(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xdf(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x232a0
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
leaq 0xe0(%rsp), %rdi
callq 0x485b0
jmp 0x3f333
movq 0x10(%rsp), %rdi
movq 0x990(%rsp), %rdx
leaq 0xe0(%rsp), %rsi
callq 0x4a4d0
jmp 0x3f34f
leaq 0xe0(%rsp), %rdi
callq 0x22610
leaq 0xdf(%rsp), %rdi
callq 0x22ab0
jmp 0x3f428
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
jmp 0x3f3a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x22610
leaq 0x107(%rsp), %rdi
callq 0x22ab0
jmp 0x3f4ff
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
movl $0x50, %esi
callq 0x22a00
jmp 0x3f4ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
jmp 0x3f416
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x22610
leaq 0xdf(%rsp), %rdi
callq 0x22ab0
jmp 0x3f4ff
jmp 0x3f42a
jmp 0x3f42c
movl $0x30, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
movq 0x9d8(%rsp), %rsi
movq 0x990(%rsp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x63f30
jmp 0x3f45c
movq 0x8(%rsp), %rax
movq %rax, 0x9e0(%rsp)
movq 0x9e0(%rsp), %rdi
leaq 0x1260(%rsp), %rsi
callq 0x64260
movq 0x1698(%rsp), %rdi
movq 0x9e0(%rsp), %rsi
callq 0x2cbd0
jmp 0x3f4ba
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9d0(%rsp)
movl %eax, 0x9cc(%rsp)
movl $0x30, %esi
callq 0x22a00
jmp 0x3f4ff
movq 0x1680(%rsp), %rdi
movq 0x9e0(%rsp), %rsi
callq 0x48420
movl 0x1664(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1664(%rsp)
jmp 0x3e91a
movl $0x1, 0x16a4(%rsp)
movl 0x16a4(%rsp), %eax
addq $0x16a8, %rsp           # imm = 0x16A8
retq
movq 0x9d0(%rsp), %rdi
callq 0x23290
nopl (%rax)