// File: /workspace/github_cpp_makefile_star2_O1/SpiritLooper[P]RDV_OBJRender/obj_render.cpp
// Function: render(Model*, bool)
void render(Model* model, bool _3D) {
   
    float* zbuffer = new float[width * height];
    float* zbuffer_red = new float[width * height];
    float* zbuffer_blue = new float[width * height];
    
    for (int i = 0; i < width * height; i++)
    {
        zbuffer[i] = -std::numeric_limits<float>::max();
        zbuffer_blue[i] = -std::numeric_limits<float>::max();
        zbuffer_red[i] = -std::numeric_limits<float>::max();
    }
    
    Image img(width , height);  
    Image img_red(width , height);  
    Image img_blue(width , height);  

    Camera cam(camera_pos, img, PERSPECTIVE);

    std::cout << "Drawing triangle " ; 

    //Dessin des triangles du model
    for (int i = 0; i < model->nfaces() ; i++)
    {
        std::vector<int> face = model->face(i); 
        Vec3f screen_coords[3];
        Vec3f world_coords[3]; 

        Vec3f screen_coords_left_eye[3];
        Vec3f screen_coords_right_eye[3];

        for (int j=0; j<3; j++) { 
            Vec3f v = model->vert(face[j]); 
            screen_coords[j] = cam.camera_transform(v);

            cam.camera_transform_analygraph(v, screen_coords_left_eye[j], screen_coords_right_eye[j] );

            world_coords[j]  = v; 
        }

        Vec3f norm_triangle = (world_coords[2] - world_coords[0]) ^ (world_coords[1] - world_coords[0]);
        norm_triangle.normalize();
        float intensity = norm_triangle * light_dir;
        
        if(_3D) {
            triangle(screen_coords_left_eye,  zbuffer_red, img_red, Vec3f(intensity, 0, 0));
            triangle(screen_coords_right_eye, zbuffer_blue, img_blue, Vec3f(0, 0, intensity));
        } else {
            triangle(screen_coords, zbuffer, img, Vec3f(intensity, intensity, intensity));
        }
        
    }
    if(_3D) 
        img = img_red.fusionner(img_blue);

    std::cout << "\nFliping image ..." << std::endl; 
    img.flip(VERTICAL_FLIP);

    std::cout << "PPM exporting ..." << std::endl;
    img.exportPPM(); 

    std::cout << "Done" << std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movl %esi, 0xc(%rsp)
movq %rdi, %r14
movl $0x300000, %edi         # imm = 0x300000
callq 0x2030
movq %rax, %rbx
movl $0x300000, %edi         # imm = 0x300000
callq 0x2030
movq %rax, %r15
movl $0x300000, %edi         # imm = 0x300000
callq 0x2030
movq %rax, %r12
xorl %eax, %eax
movl $0xff7fffff, %ecx       # imm = 0xFF7FFFFF
movl %ecx, (%rbx,%rax,4)
movl %ecx, (%r12,%rax,4)
movl %ecx, (%r15,%rax,4)
incq %rax
cmpq $0xc0000, %rax          # imm = 0xC0000
jne 0x6074
leaq 0x60(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3e5c
leaq 0x100(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3e5c
leaq 0xe0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3e5c
movsd 0x40ec(%rip), %xmm0     # 0xa1c0
movaps %xmm0, 0x10(%rsp)
movss 0x40e7(%rip), %xmm0     # 0xa1c8
movss %xmm0, 0x8(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x6874
movq 0x78(%rsp), %rax
leaq 0x178(%rsp), %rsi
movq %rax, 0x18(%rsi)
leaq 0x198(%rsp), %rdi
movaps 0x10(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
movl $0x1, %edx
callq 0x3008
movq %r12, 0xb0(%rsp)
movq %r15, 0xb8(%rsp)
movq %rbx, 0xc0(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x3e8e
movq 0x3e85(%rip), %rdi      # 0x9fd8
leaq 0x1076(%rip), %rsi      # 0x71d0
movl $0x11, %edx
callq 0x21d0
xorl %r15d, %r15d
leaq 0x198(%rsp), %rbp
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x4c7a
cmpl %eax, %ebx
jge 0x641a
leaq 0xc8(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x4c8e
movl %r15d, 0xa0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movl %r15d, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movl %r15d, 0x170(%rsp)
movaps %xmm0, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
movl %r15d, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0xc8(%rsp), %rax
movl (%rax,%r12), %esi
movq %r14, %rdi
callq 0x4cae
movq %rbp, %rdi
movaps %xmm0, 0x10(%rsp)
movss %xmm1, 0x8(%rsp)
callq 0x3860
movlps %xmm0, 0x80(%rsp,%r13)
movss %xmm1, 0x88(%rsp,%r13)
leaq (%rsp,%r13), %rsi
addq $0x150, %rsi            # imm = 0x150
leaq 0x120(%rsp,%r13), %rdx
movq %rbp, %rdi
movaps 0x10(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
callq 0x3a0a
movss 0x8(%rsp), %xmm1
movaps 0x10(%rsp), %xmm0
movlps %xmm0, 0x30(%rsp,%r13)
movss %xmm1, 0x38(%rsp,%r13)
addq $0xc, %r13
addq $0x4, %r12
cmpq $0x24, %r13
jne 0x61f3
movss 0x48(%rsp), %xmm0
movss 0x30(%rsp), %xmm1
movss 0x34(%rsp), %xmm2
subss %xmm1, %xmm0
movss 0x4c(%rsp), %xmm3
subss %xmm2, %xmm3
movss 0x50(%rsp), %xmm7
movss 0x38(%rsp), %xmm4
subss %xmm4, %xmm7
movss 0x3c(%rsp), %xmm5
subss %xmm1, %xmm5
movss 0x40(%rsp), %xmm6
subss %xmm2, %xmm6
movss 0x44(%rsp), %xmm1
subss %xmm4, %xmm1
movaps %xmm6, %xmm2
mulss %xmm7, %xmm2
mulss %xmm5, %xmm7
mulss %xmm3, %xmm5
mulss %xmm1, %xmm3
subss %xmm2, %xmm3
mulss %xmm0, %xmm1
subss %xmm1, %xmm7
mulss %xmm0, %xmm6
subss %xmm5, %xmm6
movaps %xmm7, %xmm0
mulss %xmm7, %xmm0
movaps %xmm3, %xmm1
mulss %xmm3, %xmm1
addss %xmm0, %xmm1
movaps %xmm6, %xmm0
mulss %xmm6, %xmm0
addss %xmm1, %xmm0
ucomiss 0xe16(%rip), %xmm0      # 0x712c
jb 0x631e
sqrtss %xmm0, %xmm0
jmp 0x6345
movaps %xmm6, 0x10(%rsp)
movss %xmm7, 0x8(%rsp)
movss %xmm3, 0x2c(%rsp)
callq 0x22f0
movss 0x2c(%rsp), %xmm3
movss 0x8(%rsp), %xmm7
movaps 0x10(%rsp), %xmm6
movss 0xcb7(%rip), %xmm1      # 0x7004
divss %xmm0, %xmm1
mulss %xmm1, %xmm3
mulss %xmm1, %xmm7
mulss 0x3e57(%rip), %xmm7     # 0xa1b8
mulss %xmm1, %xmm6
mulss 0x3e47(%rip), %xmm3     # 0xa1b4
addss %xmm7, %xmm3
mulss 0x3e43(%rip), %xmm6     # 0xa1bc
addss %xmm3, %xmm6
cmpb $0x0, 0xc(%rsp)
je 0x63d2
xorps %xmm0, %xmm0
movss %xmm6, %xmm0            # xmm0 = xmm6[0],xmm0[1,2,3]
xorps %xmm1, %xmm1
leaq 0x150(%rsp), %rdi
movq 0xb8(%rsp), %rsi
leaq 0x100(%rsp), %rdx
movaps %xmm6, 0x10(%rsp)
callq 0x5d54
xorps %xmm0, %xmm0
movaps 0x10(%rsp), %xmm6
leaq 0x120(%rsp), %rdi
movq 0xb0(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
jmp 0x63ee
movaps %xmm6, %xmm0
shufps $0x0, %xmm6, %xmm0      # xmm0 = xmm0[0,0],xmm6[0,0]
leaq 0x80(%rsp), %rdi
movq 0xc0(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movaps %xmm6, %xmm1
callq 0x5d54
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x6413
movq 0xd8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
incl %ebx
jmp 0x6171
cmpb $0x0, 0xc(%rsp)
je 0x6466
leaq 0x80(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
callq 0x42de
leaq 0x60(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x4422
leaq 0x80(%rsp), %rdi
movq 0x18(%rdi), %rax
movq %rax, 0x78(%rsp)
callq 0x3e8e
movq 0x3b6b(%rip), %rbx      # 0x9fd8
leaq 0xd6e(%rip), %rsi       # 0x71e2
movl $0x12, %edx
movq %rbx, %rdi
callq 0x21d0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x3b39(%rip), %rdi      # 0x9fd8
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x4184
movq 0x3b19(%rip), %rbx      # 0x9fd8
leaq 0xd2f(%rip), %rsi       # 0x71f5
movl $0x11, %edx
movq %rbx, %rdi
callq 0x21d0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x3ae7(%rip), %rdi      # 0x9fd8
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x60(%rsp), %rdi
callq 0x3eae
movq 0x3ac9(%rip), %rbx      # 0x9fd8
leaq 0xcf1(%rip), %rsi       # 0x7207
movl $0x4, %edx
movq %rbx, %rdi
callq 0x21d0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x3a97(%rip), %rdi      # 0x9fd8
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x1e0(%rsp), %rdi
callq 0x2e46
leaq 0x1c0(%rsp), %rdi
callq 0x2e46
leaq 0xe0(%rsp), %rdi
callq 0x3e8e
leaq 0x100(%rsp), %rdi
callq 0x3e8e
leaq 0x60(%rsp), %rdi
callq 0x3e8e
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x3e8e
jmp 0x6600
jmp 0x65d7
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x3e8e
jmp 0x661a
movq %rax, %rbx
jmp 0x661a
movq %rax, %rbx
jmp 0x6627
movq %rax, %rbx
jmp 0x6634
jmp 0x65d7
jmp 0x65d7
movq %rax, %rbx
jmp 0x6600
jmp 0x65e0
jmp 0x65e0
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x6600
movq 0xd8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
leaq 0x1e0(%rsp), %rdi
callq 0x2e46
leaq 0x1c0(%rsp), %rdi
callq 0x2e46
leaq 0xe0(%rsp), %rdi
callq 0x3e8e
leaq 0x100(%rsp), %rdi
callq 0x3e8e
leaq 0x60(%rsp), %rdi
callq 0x3e8e
movq %rbx, %rdi
callq 0x22d0