// File: /workspace/github_c_makefile_star2_O3/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 $0x178, %rsp            # imm = 0x178
movq %rdi, %r14
leaq 0x2052(%rip), %rsi      # 0x708d
callq 0x1180
testq %rax, %rax
je 0x5538
movq %rax, %rbp
movq %r14, %rdi
callq 0x46c1
movq %rax, 0x68(%rsp)
testq %rax, %rax
jne 0x506f
leaq 0x2ab0(%rip), %rdi      # 0x7b15
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 -0x2422(%rip), %rsi     # 0x2c88
movq 0x5f2f(%rip), %r14      # 0xafe0
movq %r15, 0x18(%rsp)
movq %r15, %rdi
movq %rsi, %r15
movq %r14, %rdx
callq 0x1cf0
leaq -0x27ef(%rip), %rsi     # 0x28dc
movq %r12, 0x28(%rsp)
movq %r12, %rdi
movq %r14, %rdx
callq 0x1cf0
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1cf0
leaq -0xaa2(%rip), %rsi      # 0x4653
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %r14, %rdx
callq 0x1cf0
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, -0x10(%rax)
movl $0x0, 0x10(%rax)
leaq 0x70(%rsp), %r13
xorl %r15d, %r15d
xorl %eax, %eax
movl $0x0, 0xc(%rsp)
xorl %r12d, %r12d
movq 0x68(%rsp), %rbx
movl %eax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x1200
testl %eax, %eax
jne 0x530a
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdx
callq 0x1220
testq %rax, %rax
je 0x5139
movb 0x70(%rsp), %al
cmpb $0x23, %al
je 0x5139
movl %eax, %edx
xorb $0x76, %dl
movb 0x71(%rsp), %cl
movl %ecx, %esi
xorb $0x20, %sil
orb %dl, %sil
jne 0x51bd
incl %r12d
movl $0xc, %edi
callq 0x11a0
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x8(%rax), %r8
leaq 0x72(%rsp), %rdi
leaq 0x29a2(%rip), %rsi      # 0x7b40
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x1d98
movb 0x70(%rsp), %al
movb 0x71(%rsp), %cl
movl %eax, %edx
xorb $0x76, %dl
movl %ecx, %esi
xorb $0x74, %sil
orb %dl, %sil
jne 0x520d
incl 0xc(%rsp)
movl $0x8, %edi
callq 0x11a0
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x73(%rsp), %rdi
leaq 0x2955(%rip), %rsi      # 0x7b43
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x1d98
movb 0x70(%rsp), %al
movb 0x71(%rsp), %cl
movl 0x8(%rsp), %edx
cmpb $0x76, %al
jne 0x5260
cmpb $0x6e, %cl
jne 0x5260
incl %edx
movl %edx, 0x8(%rsp)
movl $0xc, %edi
callq 0x11a0
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x8(%rax), %r8
leaq 0x73(%rsp), %rdi
leaq 0x28ff(%rip), %rsi      # 0x7b40
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x1d98
movb 0x70(%rsp), %al
movb 0x71(%rsp), %cl
cmpb $0x66, %al
jne 0x5139
cmpb $0x20, %cl
jne 0x5139
incq %r15
movl $0x10, %edi
callq 0x11a0
movq %rax, 0x60(%rsp)
subq $0x8, %rsp
leaq 0x7a(%rsp), %rdi
leaq 0x28b6(%rip), %rsi      # 0x7b49
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 0x60(%rsp), %r14
movl $0x3, (%r14)
movl $0x9, %edi
leaq 0x30(%rsp), %rsi
callq 0x2903
movq %rax, 0x8(%r14)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1d98
movl 0x8(%rsp), %eax
jmp 0x5135
movq %rbp, %rdi
callq 0x1150
movl %r12d, 0x120(%rbx)
movslq %r12d, %r14
leaq (,%r14,4), %rax
leaq (%rax,%rax,2), %rdi
callq 0x11a0
movq %rax, 0x100(%rbx)
testl %r14d, %r14d
movq 0x18(%rsp), %r9
jle 0x5375
movq %r9, %rcx
addq $0x18, %rcx
movl %r12d, %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 0x5352
movl 0xc(%rsp), %r13d
movl %r13d, 0x124(%rbx)
movslq %r13d, %r14
leaq (,%r14,8), %rdi
callq 0x11a0
movq %rax, 0x118(%rbx)
testl %r14d, %r14d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %rbp
jle 0x53cc
movq %r12, %rcx
addq $0x18, %rcx
movl %r13d, %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 0x53b3
movl 0x8(%rsp), %r13d
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 0x542f
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 0x540c
movl %r15d, 0x12c(%rbx)
movl %r15d, %edi
shlq $0x4, %rdi
callq 0x11a0
movq %rax, 0x108(%rbx)
testl %r15d, %r15d
je 0x5470
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 0x5457
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x2a24
movlps %xmm0, 0x130(%rbx)
movss %xmm1, 0x138(%rbx)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x2a24
movlps %xmm0, 0x13c(%rbx)
movss %xmm1, 0x144(%rbx)
movss 0x1b62(%rip), %xmm0     # 0x7014
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x2a24
movlps %xmm0, 0x148(%rbx)
movss %xmm1, 0x150(%rbx)
movl $0x8, %edi
callq 0x11a0
movq %rax, %r14
movq %rax, 0x158(%rbx)
leaq 0x25e3(%rip), %rdi      # 0x7aca
callq 0x2238
movq %rax, (%r14)
movl $0x8, %edi
callq 0x11a0
movq %rax, %r14
movq %rax, 0x160(%rbx)
leaq 0x25d5(%rip), %rdi      # 0x7adf
callq 0x2238
movq %rax, (%r14)
movq 0x18(%rsp), %rdi
callq 0x1d06
movq %r12, %rdi
callq 0x1d06
movq %rbp, %rdi
callq 0x1d06
movq 0x10(%rsp), %rdi
callq 0x1d06
jmp 0x554b
leaq 0x25b7(%rip), %rdi      # 0x7af6
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x1040
movq %rbx, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq