// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1f8, %rsp            # imm = 0x1F8
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
movaps 0x1209(%rip), %xmm0     # 0x7200
movups %xmm0, (%rbx,%rax,4)
movups %xmm0, (%r12,%rax,4)
movups %xmm0, (%r15,%rax,4)
addq $0x4, %rax
cmpq $0xc0000, %rax          # imm = 0xC0000
jne 0x5ff7
leaq 0x50(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3e48
leaq 0xf0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3e48
leaq 0xd0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3e48
movsd 0x4165(%rip), %xmm0     # 0xa1c0
movaps %xmm0, 0x10(%rsp)
movss 0x4160(%rip), %xmm0     # 0xa1c8
movss %xmm0, 0x8(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x67aa
movq 0x68(%rsp), %rax
leaq 0x168(%rsp), %rsi
movq %rax, 0x18(%rsi)
leaq 0x188(%rsp), %rdi
movaps 0x10(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
movl $0x1, %edx
callq 0x3128
movq %r12, 0xa0(%rsp)
movq %r15, 0xa8(%rsp)
movq %rbx, 0xb0(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x3e7a
movq 0x3efe(%rip), %rdi      # 0x9fd8
leaq 0x1147(%rip), %rsi      # 0x7228
movl $0x11, %edx
callq 0x21d0
xorl %r15d, %r15d
leaq 0x188(%rsp), %rbp
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x4c46
cmpl %eax, %ebx
jge 0x6362
leaq 0xb8(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x4c5a
movl %r15d, 0x90(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movl %r15d, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl %r15d, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movl %r15d, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0xb8(%rsp), %rax
movl (%rax,%r12), %esi
movq %r14, %rdi
callq 0x4c7a
movq %rbp, %rdi
movaps %xmm0, 0x10(%rsp)
movss %xmm1, 0x8(%rsp)
callq 0x3856
movlps %xmm0, 0x70(%rsp,%r13)
movss %xmm1, 0x78(%rsp,%r13)
leaq (%rsp,%r13), %rsi
addq $0x140, %rsi            # imm = 0x140
leaq 0x110(%rsp,%r13), %rdx
movq %rbp, %rdi
movaps 0x10(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
callq 0x39fa
movss 0x8(%rsp), %xmm1
movaps 0x10(%rsp), %xmm0
movlps %xmm0, 0x20(%rsp,%r13)
movss %xmm1, 0x28(%rsp,%r13)
addq $0xc, %r13
addq $0x4, %r12
cmpq $0x24, %r13
jne 0x6177
movss 0x38(%rsp), %xmm2
movss 0x20(%rsp), %xmm1
movss 0x24(%rsp), %xmm3
subss %xmm1, %xmm2
movss 0x3c(%rsp), %xmm4
subss %xmm3, %xmm4
movss 0x40(%rsp), %xmm0
movss 0x28(%rsp), %xmm5
subss %xmm5, %xmm0
movss 0x2c(%rsp), %xmm6
subss %xmm1, %xmm6
movss 0x30(%rsp), %xmm1
subss %xmm3, %xmm1
movss 0x34(%rsp), %xmm7
subss %xmm5, %xmm7
movaps %xmm1, %xmm5
mulss %xmm0, %xmm5
mulss %xmm6, %xmm0
mulss %xmm4, %xmm6
movaps %xmm4, %xmm3
mulss %xmm7, %xmm3
subss %xmm5, %xmm3
mulss %xmm2, %xmm7
subss %xmm7, %xmm0
mulss %xmm2, %xmm1
subss %xmm6, %xmm1
movaps %xmm0, %xmm2
mulss %xmm0, %xmm2
movaps %xmm3, %xmm4
mulss %xmm3, %xmm4
addss %xmm2, %xmm4
movaps %xmm1, %xmm2
mulss %xmm1, %xmm2
addss %xmm4, %xmm2
sqrtss %xmm2, %xmm2
movss 0xda8(%rip), %xmm4      # 0x7040
divss %xmm2, %xmm4
mulss %xmm4, %xmm3
mulss %xmm4, %xmm0
mulss 0x3f08(%rip), %xmm0     # 0xa1b4
mulss %xmm1, %xmm4
mulss 0x3ef8(%rip), %xmm3     # 0xa1b0
addss %xmm0, %xmm3
mulss 0x3ef4(%rip), %xmm4     # 0xa1b8
addss %xmm3, %xmm4
cmpb $0x0, 0xc(%rsp)
je 0x631d
xorps %xmm0, %xmm0
movss %xmm4, %xmm0            # xmm0 = xmm4[0],xmm0[1,2,3]
xorps %xmm1, %xmm1
leaq 0x140(%rsp), %rdi
movq 0xa8(%rsp), %rsi
leaq 0xf0(%rsp), %rdx
movaps %xmm4, 0x10(%rsp)
callq 0x5cd9
xorps %xmm0, %xmm0
movaps 0x10(%rsp), %xmm4
leaq 0x110(%rsp), %rdi
movq 0xa0(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
jmp 0x6336
movaps %xmm4, %xmm0
shufps $0x0, %xmm4, %xmm0      # xmm0 = xmm0[0,0],xmm4[0,0]
leaq 0x70(%rsp), %rdi
movq 0xb0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movaps %xmm4, %xmm1
callq 0x5cd9
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x635b
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
incl %ebx
jmp 0x60f8
cmpb $0x0, 0xc(%rsp)
je 0x63a5
leaq 0x70(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0x42c8
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x43f4
leaq 0x70(%rsp), %rdi
movq 0x18(%rdi), %rax
movq %rax, 0x68(%rsp)
callq 0x3e7a
movq 0x3c2c(%rip), %rbx      # 0x9fd8
leaq 0xe87(%rip), %rsi       # 0x723a
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 0x3bfa(%rip), %rdi      # 0x9fd8
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
callq 0x416a
movq 0x3bda(%rip), %rbx      # 0x9fd8
leaq 0xe48(%rip), %rsi       # 0x724d
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 0x3ba8(%rip), %rdi      # 0x9fd8
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x50(%rsp), %rdi
callq 0x3e9a
movq 0x3b8a(%rip), %rbx      # 0x9fd8
leaq 0xe0a(%rip), %rsi       # 0x725f
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 0x3b58(%rip), %rdi      # 0x9fd8
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x1d0(%rsp), %rdi
callq 0x2f62
leaq 0x1b0(%rsp), %rdi
callq 0x2f62
leaq 0xd0(%rsp), %rdi
callq 0x3e7a
leaq 0xf0(%rsp), %rdi
callq 0x3e7a
leaq 0x50(%rsp), %rdi
callq 0x3e7a
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3e7a
jmp 0x653c
jmp 0x6513
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x3e7a
jmp 0x6556
movq %rax, %rbx
jmp 0x6556
movq %rax, %rbx
jmp 0x6563
movq %rax, %rbx
jmp 0x6570
jmp 0x6513
jmp 0x6513
movq %rax, %rbx
jmp 0x653c
jmp 0x651c
jmp 0x651c
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x653c
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
leaq 0x1d0(%rsp), %rdi
callq 0x2f62
leaq 0x1b0(%rsp), %rdi
callq 0x2f62
leaq 0xd0(%rsp), %rdi
callq 0x3e7a
leaq 0xf0(%rsp), %rdi
callq 0x3e7a
leaq 0x50(%rsp), %rdi
callq 0x3e7a
movq %rbx, %rdi
callq 0x22d0