// File: /workspace/github_c_makefile_star2_O1/Nealsbo[P]C-Software-Renderer/src/objloader.c
// Function: Model_LoadOBJ
obj_model_t *Model_LoadOBJ( const char *file_name ){
    char line[MAX_NAME_LENGTH];
    FILE *fp;
    if( ( fp = fopen(file_name, "r") ) == NULL ){
        printf( "Error: Cannot open file '%s'!\n", file_name );
        return NULL;
    }
    
    obj_model_t *model = Model_Create(file_name);
    if( model == NULL ){
        printf("Error: Cannot create model for file '%s'!\n", file_name);
    }

    node_t *tmpnode = NULL;

    List *vertList  = (List *)malloc(sizeof(List));
    List *coordList = (List *)malloc(sizeof(List));
    List *normList  = (List *)malloc(sizeof(List));
    List *faceList  = (List *)malloc(sizeof(List));

    List_Init(vertList,  &vec3_printp, &free);
    List_Init(coordList, &vec2_printp, &free);
    List_Init(normList,  &vec3_printp, &free);
    List_Init(faceList,  &Face_Printp, &free);

    int fa[9] = {0};
    int i;
    int vertCount  = 0;
    int coordCount = 0;
    int normCount  = 0;
    int faceCount  = 0;

    while(!feof(fp)){
        if(fgets( line, MAX_NAME_LENGTH, fp )){
            if( line == NULL || line[0] == '#' )
                continue;

            if( line[0] == 'v' && line[1] == ' ' ){
                vertCount++;
                vec3 *tmpvec3 = (vec3 *)malloc( sizeof( vec3 ) );
                sscanf(&line[2], "%f %f %f", &tmpvec3->x, &tmpvec3->y, &tmpvec3->z);
                List_Push(vertList, tmpvec3);
            }

            if( line[0] == 'v' && line[1] == 't' ){
                coordCount++;
                vec2 *tmpvec2 = (vec2 *)malloc( sizeof( vec2 ) );
                sscanf(&line[3], "%f %f", &tmpvec2->x, &tmpvec2->y);
                List_Push(coordList, tmpvec2);
            }

            if( line[0] == 'v' && line[1] == 'n' ){
                normCount++;
                vec3 *tmpvec3 = (vec3 *)malloc( sizeof( vec3 ) );
                sscanf(&line[3], "%f %f %f", &tmpvec3->x, &tmpvec3->y, &tmpvec3->z);
                List_Push(normList, tmpvec3);
            }

            if( line[0] == 'f' && line[1] == ' ' ){
                faceCount++;
                obj_face_t *tmpface = (obj_face_t *)malloc( sizeof( obj_face_t ) );
                sscanf(&line[2], "%i/%i/%i %i/%i/%i %i/%i/%i", &fa[0], &fa[1], &fa[2], &fa[3], &fa[4], &fa[5], &fa[6], &fa[7], &fa[8]);
                //*tmpface = Face_Create( 3, vec3i_arrayToVec3ip(9, fa) );
                tmpface->vcount = 3;
                tmpface->indexes = vec3i_arrayToVec3ip(9, fa);
                List_Push(faceList, tmpface);
            }
        }
    }

    fclose(fp);

    vec3 *tmpv3 = NULL;
    vec2 *tmpv2 = NULL;
    obj_face_t *tmpf = NULL;

    model->vertCount = vertCount;
    model->vertices = (vec3 *)malloc(sizeof(vec3) * vertCount);
    tmpnode = List_Head(vertList);
    tmpv3 = (vec3 *)List_Data(tmpnode);
    for(i = 0; i < vertCount; i++){
        tmpv3 = List_Data(tmpnode);
        model->vertices[i] = *tmpv3;
        tmpnode = List_Next(tmpnode);
    } tmpv3 = NULL;

    model->coordCount = coordCount;
    model->textcoords = (vec2 *)malloc(sizeof(vec2) * coordCount);
    tmpnode = List_Head(coordList);
    tmpv2 = (vec2 *)List_Data(tmpnode);
    for(i = 0; i < coordCount; i++){
        tmpv2 = List_Data(tmpnode);
        model->textcoords[i] = *tmpv2;
        tmpnode = List_Next(tmpnode);
    } tmpv2 = NULL;

    model->normCount = normCount;
    model->normals = (vec3 *)malloc(sizeof(vec3) * normCount);
    tmpnode = List_Head(normList);
    tmpv3 = (vec3 *)List_Data(tmpnode);
    for(i = 0; i < normCount; i++){
        tmpv3 = List_Data(tmpnode);
        model->normals[i] = *tmpv3;
        tmpnode = List_Next(tmpnode);
    }

    model->trisCount = faceCount;
    model->faces = (obj_face_t *)malloc(sizeof(obj_face_t) * faceCount);
    tmpnode = List_Head(faceList);
    tmpf = (obj_face_t *)List_Data(tmpnode);
    for(i = 0; i < faceCount; i++){
        tmpf = List_Data(tmpnode);
        model->faces[i] = *tmpf;
        tmpnode = List_Next(tmpnode);
    }
    
    model->position    = vec3_create(0.0, 0.0, 0.0);
    model->rotation    = vec3_create(0.0, 0.0, 0.0);
    model->scale       = vec3_create(1.0, 1.0, 1.0);

    model->diffmap = (texture_t *)malloc(sizeof(texture_t));
    model->diffmap->bitmap = Bitmap_LoadPPM6("./assets/texture.ppm");
    
    model->normalmap = (texture_t *)malloc(sizeof(texture_t));
    model->normalmap->bitmap = Bitmap_LoadPPM6("./assets/texture_n.ppm");

    List_Destroy(vertList);
    List_Destroy(coordList);
    List_Destroy(normList);
    List_Destroy(faceList);

    return model;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdi, %r14
leaq 0x2d19(%rip), %rsi      # 0x808d
callq 0x1180
testq %rax, %rax
je 0x586f
movq %rax, %rbp
movq %r14, %rdi
callq 0x49c4
movq %rax, 0x58(%rsp)
testq %rax, %rax
jne 0x53a8
leaq 0x3757(%rip), %rdi      # 0x8af5
movq %r14, %rsi
xorl %eax, %eax
callq 0x1040
movl $0x28, %edi
callq 0x11a0
movq %rax, %r15
movl $0x28, %edi
callq 0x11a0
movq %rax, %r12
movl $0x28, %edi
callq 0x11a0
movq %rax, %rbx
movl $0x28, %edi
callq 0x11a0
movq %rax, %r13
leaq -0x2688(%rip), %rsi     # 0x2d5b
movq 0x6bf6(%rip), %r14      # 0xbfe0
movq %r15, 0x18(%rsp)
movq %r15, %rdi
movq %rsi, %r15
movq %r14, %rdx
callq 0x1d00
leaq -0x2af2(%rip), %rsi     # 0x2912
movq %r12, 0x28(%rsp)
movq %r12, %rdi
movq %r14, %rdx
callq 0x1d00
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1d00
leaq -0xad8(%rip), %rsi      # 0x4956
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %r14, %rdx
callq 0x1d00
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, -0x10(%rax)
movl $0x0, 0x10(%rax)
leaq 0x60(%rsp), %r13
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
movq 0x58(%rsp), %rbx
movq %rbp, %rdi
callq 0x1200
testl %eax, %eax
jne 0x5644
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdx
callq 0x1220
testq %rax, %rax
je 0x546f
movb 0x60(%rsp), %al
cmpb $0x23, %al
je 0x546f
cmpb $0x76, %al
jne 0x54f1
cmpb $0x20, 0x61(%rsp)
jne 0x54f1
incl 0xc(%rsp)
movl $0xc, %edi
callq 0x11a0
movl %r12d, (%rsp)
movl %r14d, %r12d
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x8(%rax), %r8
leaq 0x62(%rsp), %rdi
leaq 0x364d(%rip), %rsi      # 0x8b20
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movl %r12d, %r14d
movl (%rsp), %r12d
callq 0x1da8
cmpb $0x76, 0x60(%rsp)
jne 0x5544
cmpb $0x74, 0x61(%rsp)
jne 0x5544
incl %r12d
movl %r12d, (%rsp)
movl $0x8, %edi
callq 0x11a0
movl %r14d, %r12d
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x63(%rsp), %rdi
leaq 0x35fd(%rip), %rsi      # 0x8b23
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movl %r12d, %r14d
movl (%rsp), %r12d
callq 0x1da8
cmpb $0x76, 0x60(%rsp)
jne 0x559b
cmpb $0x6e, 0x61(%rsp)
jne 0x559b
incl %r14d
movl $0xc, %edi
callq 0x11a0
movl %r12d, (%rsp)
movl %r14d, %r12d
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x8(%rax), %r8
leaq 0x63(%rsp), %rdi
leaq 0x35a3(%rip), %rsi      # 0x8b20
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movl %r12d, %r14d
movl (%rsp), %r12d
callq 0x1da8
cmpb $0x66, 0x60(%rsp)
jne 0x546f
cmpb $0x20, 0x61(%rsp)
jne 0x546f
incq %r15
movl $0x10, %edi
callq 0x11a0
movq %rax, (%rsp)
subq $0x8, %rsp
leaq 0x6a(%rsp), %rdi
leaq 0x3557(%rip), %rsi      # 0x8b29
leaq 0x38(%rsp), %rdx
leaq 0x3c(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x44(%rsp), %r9
xorl %eax, %eax
leaq 0x58(%rsp), %r10
pushq %r10
leaq 0x5c(%rsp), %r10
pushq %r10
leaq 0x60(%rsp), %r10
pushq %r10
leaq 0x64(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
callq 0x1140
addq $0x30, %rsp
movq (%rsp), %rax
movl $0x3, (%rax)
movl $0x9, %edi
leaq 0x30(%rsp), %rsi
callq 0x2939
movq (%rsp), %rsi
movq %rax, 0x8(%rsi)
movq 0x10(%rsp), %rdi
callq 0x1da8
jmp 0x546f
movl %r14d, %r13d
movq %rbp, %rdi
callq 0x1150
movl 0xc(%rsp), %ebp
movl %ebp, 0x120(%rbx)
movslq %ebp, %r14
leaq (,%r14,4), %rax
leaq (%rax,%rax,2), %rdi
callq 0x11a0
movq %rax, 0x100(%rbx)
testl %r14d, %r14d
movq 0x18(%rsp), %r9
jle 0x56b4
movq %r9, %rcx
addq $0x18, %rcx
movl %ebp, %edx
shlq $0x2, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movq (%rcx), %rcx
movq (%rcx), %rdi
movl 0x8(%rdi), %r8d
movl %r8d, 0x8(%rax,%rsi)
movq (%rdi), %rdi
movq %rdi, (%rax,%rsi)
addq $0x8, %rcx
addq $0xc, %rsi
cmpq %rsi, %rdx
jne 0x5691
movl %r12d, 0x124(%rbx)
movslq %r12d, %r14
leaq (,%r14,8), %rdi
callq 0x11a0
movq %rax, 0x118(%rbx)
testl %r14d, %r14d
movl %r12d, %edx
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %rbp
jle 0x5708
movq %r12, %rcx
addq $0x18, %rcx
movl %edx, %edx
xorl %esi, %esi
movq (%rcx), %rcx
movq (%rcx), %rdi
movq (%rdi), %rdi
movq %rdi, (%rax,%rsi,8)
addq $0x8, %rcx
incq %rsi
cmpq %rsi, %rdx
jne 0x56ef
movl %r13d, 0x128(%rbx)
movslq %r13d, %r14
leaq (,%r14,4), %rax
leaq (%rax,%rax,2), %rdi
callq 0x11a0
movq %rax, 0x110(%rbx)
testl %r14d, %r14d
jle 0x5766
movq %rbp, %rcx
addq $0x18, %rcx
movl %r13d, %edx
shlq $0x2, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movq (%rcx), %rcx
movq (%rcx), %rdi
movl 0x8(%rdi), %r8d
movl %r8d, 0x8(%rax,%rsi)
movq (%rdi), %rdi
movq %rdi, (%rax,%rsi)
addq $0x8, %rcx
addq $0xc, %rsi
cmpq %rsi, %rdx
jne 0x5743
movl %r15d, 0x12c(%rbx)
movl %r15d, %edi
shlq $0x4, %rdi
callq 0x11a0
movq %rax, 0x108(%rbx)
testl %r15d, %r15d
je 0x57a7
movq 0x10(%rsp), %rcx
addq $0x18, %rcx
movq (%rcx), %rcx
movq (%rcx), %rdx
movups (%rdx), %xmm0
movups %xmm0, (%rax)
addq $0x8, %rcx
addq $0x10, %rax
decq %r15
jne 0x578e
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x2a5a
movlps %xmm0, 0x130(%rbx)
movss %xmm1, 0x138(%rbx)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x2a5a
movlps %xmm0, 0x13c(%rbx)
movss %xmm1, 0x144(%rbx)
movss 0x282b(%rip), %xmm0     # 0x8014
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x2a5a
movlps %xmm0, 0x148(%rbx)
movss %xmm1, 0x150(%rbx)
movl $0x8, %edi
callq 0x11a0
movq %rax, %r14
movq %rax, 0x158(%rbx)
leaq 0x328c(%rip), %rdi      # 0x8aaa
callq 0x223a
movq %rax, (%r14)
movl $0x8, %edi
callq 0x11a0
movq %rax, %r14
movq %rax, 0x160(%rbx)
leaq 0x327e(%rip), %rdi      # 0x8abf
callq 0x223a
movq %rax, (%r14)
movq 0x18(%rsp), %rdi
callq 0x1d16
movq %r12, %rdi
callq 0x1d16
movq %rbp, %rdi
callq 0x1d16
movq 0x10(%rsp), %rdi
callq 0x1d16
jmp 0x5882
leaq 0x3260(%rip), %rdi      # 0x8ad6
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x1040
movq %rbx, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq