// File: /workspace/github_c_makefile_star2_O2/Nealsbo[P]C-Software-Renderer/src/objloader.c
// Function: Model_CreateBaseBox
obj_model_t *Model_CreateBaseBox( const char *model_name){
    int i;
    obj_model_t *model = (obj_model_t *)malloc(sizeof(obj_model_t));
    if(model == NULL){
        printf("Error: Cannot create model object '%s'\n", model_name);
        return NULL;
    }

    memcpy(model->name, model_name, MAX_NAME_LENGTH);                   //Set object name by argument

    model->vertCount   = 8;                                             //Allocation prepared data for model, box in that case
    model->trisCount   = 12;

    float    v_arr[8][3]  = {{-1.0, -1.0,  1.0},                        //Vertices data for box model
                             { 1.0, -1.0,  1.0},
                             {-1.0,  1.0,  1.0},
                             { 1.0,  1.0,  1.0},
                             {-1.0,  1.0, -1.0},
                             { 1.0,  1.0, -1.0},
                             {-1.0, -1.0, -1.0},
                             { 1.0, -1.0, -1.0}};

    float   vn_arr[24][3] = {{ 0.0,  0.0,  1.0},
                             { 0.0,  0.0,  1.0},
                             { 0.0,  0.0,  1.0},
                             { 0.0,  0.0,  1.0},
                             { 0.0,  1.0,  0.0},
                             { 0.0,  1.0,  0.0},
                             { 0.0,  1.0,  0.0},
                             { 0.0,  1.0,  0.0},
                             { 0.0,  0.0, -1.0},
                             { 0.0,  0.0, -1.0},
                             { 0.0,  0.0, -1.0},
                             { 0.0,  0.0, -1.0},
                             { 0.0, -1.0,  0.0},
                             { 0.0, -1.0,  0.0},
                             { 0.0, -1.0,  0.0},
                             { 0.0, -1.0,  0.0},
                             { 1.0,  0.0,  0.0},
                             { 1.0,  0.0,  0.0},
                             { 1.0,  0.0,  0.0},
                             { 1.0,  0.0,  0.0},
                             {-1.0,  0.0,  0.0},
                             {-1.0,  0.0,  0.0},
                             {-1.0,  0.0,  0.0},
                             {-1.0,  0.0,  0.0}};

    float   vt_arr[24][2] = {{ 0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             { 0.0, 1.0},
                             {-0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             {-0.0, 1.0},
                             {-0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             {-0.0, 1.0},
                             { 0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             { 0.0, 1.0},
                             { 0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             { 0.0, 1.0},
                             { 0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             { 0.0, 1.0}};

    int      f_arr[12][9] = {{1,  1,  1,  2,  2,  2,  3,  4,  3},       //Face index data for box model
                             {3,  4,  3,  2,  2,  2,  4,  3,  4},
                             {3,  5,  5,  4,  6,  6,  5,  8,  7},
                             {5,  8,  7,  4,  6,  6,  6,  7,  8},
                             {5,  9,  9,  6, 10, 10,  7, 12, 11},
                             {7, 12, 11,  6, 10, 10,  8, 11, 12},
                             {7, 13, 13,  8, 14, 14,  1, 16, 15},
                             {1, 16, 15,  8, 14, 14,  2, 15, 16},
                             {2, 17, 17,  8, 18, 18,  4, 20, 19},
                             {4, 20, 19,  8, 18, 18,  6, 19, 20},
                             {7, 21, 21,  1, 22, 22,  5, 24, 23},
                             {5, 24, 23,  1, 22, 22,  3, 23, 24}};

    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->baseColor   = Color_Init(255, 255, 255, 255);
    model->diffmap     = NULL;
    model->normalmap   = NULL;

    model->vertices    = (vec3 *)malloc(sizeof(vec3) * 8);
    for(i = 0; i < 8; i++){
        model->vertices[i] = (vec3){v_arr[i][0], v_arr[i][1], v_arr[i][2]};
    }

    model->normals     = (vec3 *)malloc(sizeof(vec3) * 24);
    for(i = 0; i < 24; i++){
        model->normals[i] = vec3_create(vn_arr[i][0], vn_arr[i][1], vn_arr[i][2]);
    }

    model->textcoords  = (vec2 *)malloc(sizeof(vec2) * 24);
    for(i = 0; i < 24; i++){
        model->textcoords[i] = vec2_create(vt_arr[i][0], vt_arr[i][1]);
    }

    model->faces       = (obj_face_t *)malloc(sizeof(obj_face_t) * 12);
    for(i = 0; i < 12; i++){
        model->faces[i]  = Face_Create(3, vec3i_arrayToVec3ip(9, f_arr[i]  ));
    }

    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");

    return model;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, %r14
movl $0x170, %edi            # imm = 0x170
callq 0x11b0
movq %rax, %rbx
testq %rax, %rax
je 0x4bff
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1100
movl $0x8, 0x120(%rbx)
movl $0xc, 0x12c(%rbx)
leaq 0x2e58(%rip), %rsi      # 0x7870
movq %rsp, %r14
movl $0x1b0, %edx            # imm = 0x1B0
movq %r14, %rdi
callq 0x1100
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 0x25aa(%rip), %xmm0     # 0x7014
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x2911
movlps %xmm0, 0x148(%rbx)
movss %xmm1, 0x150(%rbx)
movl $0xff, %edi
movl $0xff, %esi
movl $0xff, %edx
movl $0xff, %ecx
callq 0x1e04
movl %eax, 0x154(%rbx)
pushq $0x60
popq %rdi
callq 0x11b0
movq %rax, 0x100(%rbx)
pushq $0x8
popq %rcx
leaq 0x2b74(%rip), %rdx      # 0x7630
cmpq $0x68, %rcx
je 0x4ade
movss (%rcx,%rdx), %xmm0
movsd -0x8(%rcx,%rdx), %xmm1
movsd %xmm1, -0x8(%rax,%rcx)
movss %xmm0, (%rax,%rcx)
addq $0xc, %rcx
jmp 0x4abc
movl $0x120, %edi            # imm = 0x120
callq 0x11b0
movq %rax, %r15
movq %rax, 0x110(%rbx)
pushq $0x8
popq %r12
leaq 0x2b93(%rip), %r13      # 0x7690
cmpq $0x128, %r12            # imm = 0x128
je 0x4b31
movss -0x8(%r12,%r13), %xmm0
movss -0x4(%r12,%r13), %xmm1
movss (%r12,%r13), %xmm2
callq 0x2911
movlps %xmm0, -0x8(%r15,%r12)
movss %xmm1, (%r15,%r12)
addq $0xc, %r12
jmp 0x4afd
movl $0xc0, %edi
callq 0x11b0
movq %rax, %r15
movq %rax, 0x118(%rbx)
xorl %r12d, %r12d
leaq 0x2c61(%rip), %r13      # 0x77b0
cmpq $0x18, %r12
je 0x4b72
movss (%r13,%r12,8), %xmm0
movss 0x4(%r13,%r12,8), %xmm1
callq 0x2736
movlps %xmm0, (%r15,%r12,8)
incq %r12
jmp 0x4b4f
movl $0xc0, %edi
callq 0x11b0
movq %rax, %r15
movq %rax, 0x108(%rbx)
pushq $0x8
popq %r12
pushq $0x9
popq %rbp
cmpq $0xc8, %r12
je 0x4bb7
movl %ebp, %edi
movq %r14, %rsi
callq 0x280d
movl $0x3, -0x8(%r15,%r12)
movq %rax, (%r15,%r12)
addq $0x10, %r12
addq $0x24, %r14
jmp 0x4b8d
pushq $0x8
popq %r14
movq %r14, %rdi
callq 0x11b0
movq %rax, %r15
movq %rax, 0x158(%rbx)
leaq 0x2ef6(%rip), %rdi      # 0x7aca
callq 0x21bd
movq %rax, (%r15)
movq %r14, %rdi
callq 0x11b0
movq %rax, %r14
movq %rax, 0x160(%rbx)
leaq 0x2eea(%rip), %rdi      # 0x7adf
callq 0x21bd
movq %rax, (%r14)
jmp 0x4c10
leaq 0x2e7a(%rip), %rdi      # 0x7a80
movq %r14, %rsi
xorl %eax, %eax
callq 0x1040
movq %rbx, %rax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq