// File: /workspace/github_c_makefile_star2_O2/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 0x244d(%rip), %rsi      # 0x708d
callq 0x1190
testq %rax, %rax
je 0x5119
movq %rax, %rbp
movq %r14, %rdi
callq 0x430c
movq %rax, 0x68(%rsp)
testq %rax, %rax
jne 0x4c74
leaq 0x2eab(%rip), %rdi      # 0x7b15
movq %r14, %rsi
xorl %eax, %eax
callq 0x1040
pushq $0x28
popq %r13
movq %r13, %rdi
callq 0x11b0
movq %rax, %r15
movq %r13, %rdi
callq 0x11b0
movq %rax, %r12
movq %r13, %rdi
callq 0x11b0
movq %rax, %rbx
movq %r13, %rdi
callq 0x11b0
movq %rax, %r13
leaq -0x2140(%rip), %rsi     # 0x2b6b
movq 0x732e(%rip), %r14      # 0xbfe0
movq %r15, 0x28(%rsp)
movq %r15, %rdi
movq %rsi, %r15
movq %r14, %rdx
callq 0x1cc0
leaq -0x24e6(%rip), %rsi     # 0x27e6
movq %r12, 0x18(%rsp)
movq %r12, %rdi
movq %r14, %rdx
callq 0x1cc0
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1cc0
leaq -0xa51(%rip), %rsi      # 0x42a5
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %r14, %rdx
callq 0x1cc0
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
andl $0x0, 0x10(%rax)
movaps %xmm0, (%rax)
movaps %xmm0, -0x10(%rax)
leaq 0x70(%rsp), %r13
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
movq $0x0, (%rsp)
movq 0x68(%rsp), %rbx
movq %rbp, %rdi
callq 0x1210
testl %eax, %eax
jne 0x4f05
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdx
callq 0x1230
testq %rax, %rax
je 0x4d3a
movb 0x70(%rsp), %al
cmpb $0x23, %al
je 0x4d3a
movl %eax, %edx
xorb $0x76, %dl
movb 0x71(%rsp), %cl
movl %ecx, %esi
xorb $0x20, %sil
orb %dl, %sil
jne 0x4dc3
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
pushq $0xc
popq %rdi
callq 0x11b0
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x8(%rax), %r8
leaq 0x72(%rsp), %rdi
leaq 0x2d9c(%rip), %rsi      # 0x7b40
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x1d5c
movb 0x70(%rsp), %al
movb 0x71(%rsp), %cl
movl %eax, %edx
xorb $0x76, %dl
movl %ecx, %esi
xorb $0x74, %sil
orb %dl, %sil
jne 0x4e10
incl %r12d
pushq $0x8
popq %rdi
callq 0x11b0
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x73(%rsp), %rdi
leaq 0x2d52(%rip), %rsi      # 0x7b43
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x1d5c
movb 0x70(%rsp), %al
movb 0x71(%rsp), %cl
cmpb $0x76, %al
jne 0x4e63
cmpb $0x6e, %cl
jne 0x4e63
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
pushq $0xc
popq %rdi
callq 0x11b0
movq %rax, %r14
leaq 0x4(%rax), %rcx
leaq 0x8(%rax), %r8
leaq 0x73(%rsp), %rdi
leaq 0x2cfc(%rip), %rsi      # 0x7b40
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x1d5c
movb 0x70(%rsp), %al
movb 0x71(%rsp), %cl
cmpb $0x66, %al
jne 0x4d3a
cmpb $0x20, %cl
jne 0x4d3a
incq %r15
pushq $0x10
popq %rdi
callq 0x11b0
movq %rax, 0x60(%rsp)
subq $0x8, %rsp
leaq 0x7a(%rsp), %rdi
leaq 0x2cb5(%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)
pushq $0x9
popq %rdi
leaq 0x30(%rsp), %rsi
callq 0x280d
movq %rax, 0x8(%r14)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1d5c
jmp 0x4d3a
movq %rbp, %rdi
callq 0x1150
movq (%rsp), %r13
movl %r13d, 0x120(%rbx)
movslq %r13d, %r14
imulq $0xc, %r14, %rdi
callq 0x11b0
movq %rax, 0x100(%rbx)
movq 0x28(%rsp), %rbp
movq %rbp, %rcx
addq $0x18, %rcx
xorl %edx, %edx
testl %r14d, %r14d
cmovlel %edx, %r13d
imulq $0xc, %r13, %rsi
cmpq %rdx, %rsi
je 0x4f69
movq (%rcx), %rcx
movq (%rcx), %rdi
movl 0x8(%rdi), %r8d
movl %r8d, 0x8(%rax,%rdx)
movq (%rdi), %rdi
movq %rdi, (%rax,%rdx)
addq $0x8, %rcx
addq $0xc, %rdx
jmp 0x4f44
movl %r12d, 0x124(%rbx)
movslq %r12d, %r14
leaq (,%r14,8), %rdi
callq 0x11b0
movq %rax, 0x118(%rbx)
movq 0x18(%rsp), %rcx
addq $0x18, %rcx
xorl %edx, %edx
testl %r14d, %r14d
cmovlel %edx, %r12d
cmpq %rdx, %r12
je 0x4fb4
movq (%rcx), %rcx
movq (%rcx), %rsi
movq (%rsi), %rsi
movq %rsi, (%rax,%rdx,8)
addq $0x8, %rcx
incq %rdx
jmp 0x4f99
movq 0x8(%rsp), %r13
movl %r13d, 0x128(%rbx)
movslq %r13d, %r14
imulq $0xc, %r14, %rdi
callq 0x11b0
movq %rax, 0x110(%rbx)
movq 0x20(%rsp), %r12
movq %r12, %rcx
addq $0x18, %rcx
xorl %edx, %edx
testl %r14d, %r14d
cmovlel %edx, %r13d
imulq $0xc, %r13, %rsi
cmpq %rdx, %rsi
je 0x5011
movq (%rcx), %rcx
movq (%rcx), %rdi
movl 0x8(%rdi), %r8d
movl %r8d, 0x8(%rax,%rdx)
movq (%rdi), %rdi
movq %rdi, (%rax,%rdx)
addq $0x8, %rcx
addq $0xc, %rdx
jmp 0x4fec
movl %r15d, 0x12c(%rbx)
movl %r15d, %edi
shlq $0x4, %rdi
callq 0x11b0
movq %rax, 0x108(%rbx)
movq 0x10(%rsp), %rcx
addq $0x18, %rcx
subq $0x1, %r15
jb 0x5050
movq (%rcx), %rcx
movq (%rcx), %rdx
movups (%rdx), %xmm0
movups %xmm0, (%rax)
addq $0x8, %rcx
addq $0x10, %rax
jmp 0x5034
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x2911
movlps %xmm0, 0x130(%rbx)
movss %xmm1, 0x138(%rbx)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x2911
movlps %xmm0, 0x13c(%rbx)
movss %xmm1, 0x144(%rbx)
movss 0x1f82(%rip), %xmm0     # 0x7014
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x2911
movlps %xmm0, 0x148(%rbx)
movss %xmm1, 0x150(%rbx)
pushq $0x8
popq %r14
movq %r14, %rdi
callq 0x11b0
movq %rax, %r13
movq %rax, 0x158(%rbx)
leaq 0x2a01(%rip), %rdi      # 0x7aca
callq 0x21bd
movq %rax, (%r13)
movq %r14, %rdi
callq 0x11b0
movq %rax, %r14
movq %rax, 0x160(%rbx)
leaq 0x29f4(%rip), %rdi      # 0x7adf
callq 0x21bd
movq %rax, (%r14)
movq %rbp, %rdi
callq 0x1cd3
movq 0x18(%rsp), %rdi
callq 0x1cd3
movq %r12, %rdi
callq 0x1cd3
movq 0x10(%rsp), %rdi
callq 0x1cd3
jmp 0x512c
leaq 0x29d6(%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