// File: /workspace/github_cpp_makefile_star2_O2/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 $0x228, %rsp            # imm = 0x228
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
cmpq $0xc0000, %rax          # imm = 0xC0000
je 0x5aa9
movl %ecx, (%rbx,%rax,4)
movl %ecx, (%r12,%rax,4)
movl %ecx, (%r15,%rax,4)
incq %rax
jmp 0x5a91
leaq 0xc0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3b28
leaq 0x100(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3b28
leaq 0xe0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x300, %edx            # imm = 0x300
callq 0x3b28
movsd 0x56ba(%rip), %xmm0     # 0xb1b0
movaps %xmm0, 0x20(%rsp)
movss 0x56b5(%rip), %xmm0     # 0xb1b8
movss %xmm0, 0x8(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x60e8
leaq 0x1b8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
pushq $0x1
popq %rdx
movaps 0x20(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
callq 0x2eb8
movq %r12, 0x88(%rsp)
movq %r15, 0x90(%rsp)
movq %rbx, 0x98(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x3b5a
movq 0x546b(%rip), %rdi      # 0xafd8
leaq 0x16e4(%rip), %rsi      # 0x7258
callq 0x21a0
xorl %ebp, %ebp
leaq 0x180(%rsp), %r15
leaq 0x1b8(%rsp), %rbx
movq %r14, %rdi
callq 0x47b0
cmpl %eax, %ebp
jge 0x5dca
movq %r15, %r13
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x47c2
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
andl $0x0, 0x80(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
andl $0x0, 0x50(%rsp)
movaps %xmm0, 0x160(%rsp)
andl $0x0, 0x170(%rsp)
movaps %xmm0, 0x150(%rsp)
andl $0x0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq $0x24, %r12
je 0x5c8a
movq 0x180(%rsp), %rax
movl (%rax,%r15), %esi
movq %r14, %rdi
callq 0x47de
movq %rbx, %rdi
movaps %xmm0, 0x20(%rsp)
movss %xmm1, 0x8(%rsp)
callq 0x348a
movlps %xmm0, 0x60(%rsp,%r12)
movss %xmm1, 0x68(%rsp,%r12)
leaq (%rsp,%r12), %rsi
addq $0x150, %rsi            # imm = 0x150
leaq 0x120(%rsp,%r12), %rdx
movq %rbx, %rdi
movaps 0x20(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
callq 0x35f0
movss 0x8(%rsp), %xmm1
movaps 0x20(%rsp), %xmm0
movlps %xmm0, 0x30(%rsp,%r12)
movss %xmm1, 0x38(%rsp,%r12)
addq $0xc, %r12
addq $0x4, %r15
jmp 0x5c05
movsd 0x48(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
subps %xmm1, %xmm0
movss 0x50(%rsp), %xmm2
movss 0x38(%rsp), %xmm3
subss %xmm3, %xmm2
movlps %xmm0, 0xb0(%rsp)
movss %xmm2, 0xb8(%rsp)
movsd 0x3c(%rsp), %xmm0
subps %xmm1, %xmm0
movss 0x44(%rsp), %xmm1
subss %xmm3, %xmm1
movlps %xmm0, 0xa0(%rsp)
movss %xmm1, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x399c
movlps %xmm0, 0x10(%rsp)
movss %xmm1, 0x18(%rsp)
leaq 0x10(%rsp), %rdi
movss 0x12f9(%rip), %xmm0     # 0x7004
callq 0x395c
movq %r13, %r15
movss 0x10(%rsp), %xmm0
movss 0x14(%rsp), %xmm1
mulss 0x547d(%rip), %xmm1     # 0xb1a4
mulss 0x5471(%rip), %xmm0     # 0xb1a0
addss %xmm1, %xmm0
movss 0x18(%rsp), %xmm1
mulss 0x5467(%rip), %xmm1     # 0xb1a8
addss %xmm0, %xmm1
cmpb $0x0, 0xc(%rsp)
je 0x5d9a
xorps %xmm0, %xmm0
movss %xmm1, %xmm0            # xmm0 = xmm1[0],xmm0[1,2,3]
movaps %xmm1, 0x20(%rsp)
xorps %xmm1, %xmm1
leaq 0x150(%rsp), %rdi
movq 0x90(%rsp), %rsi
leaq 0x100(%rsp), %rdx
callq 0x570d
xorps %xmm0, %xmm0
movaps 0x20(%rsp), %xmm1
leaq 0x120(%rsp), %rdi
movq 0x88(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
jmp 0x5db6
movaps %xmm1, %xmm0
shufps $0x0, %xmm1, %xmm0      # xmm0 = xmm0[0,0],xmm1[0,0]
leaq 0x60(%rsp), %rdi
movq 0x98(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x570d
movq %r15, %rdi
callq 0x5152
incl %ebp
jmp 0x5b8b
cmpb $0x0, 0xc(%rsp)
je 0x5e07
leaq 0x60(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
callq 0x3ebc
leaq 0xc0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x6108
leaq 0x60(%rsp), %rdi
callq 0x3b5a
movq 0x51ca(%rip), %rdi      # 0xafd8
leaq 0x1455(%rip), %rsi      # 0x726a
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
callq 0x3d7c
movq 0x51a0(%rip), %rdi      # 0xafd8
leaq 0x143e(%rip), %rsi      # 0x727d
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
leaq 0xc0(%rsp), %rdi
callq 0x3b68
movq 0x5178(%rip), %rdi      # 0xafd8
leaq 0x1428(%rip), %rsi      # 0x728f
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
leaq 0x1b8(%rsp), %rdi
callq 0x612a
leaq 0xe0(%rsp), %rdi
callq 0x3b5a
leaq 0x100(%rsp), %rdi
callq 0x3b5a
leaq 0xc0(%rsp), %rdi
callq 0x3b5a
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3b5a
jmp 0x5f0d
jmp 0x5ef4
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x3b5a
jmp 0x5f1a
movq %rax, %rbx
jmp 0x5f1a
movq %rax, %rbx
jmp 0x5f27
movq %rax, %rbx
jmp 0x5f34
jmp 0x5ef4
jmp 0x5efd
jmp 0x5efd
jmp 0x5ef4
movq %rax, %rbx
jmp 0x5f0d
jmp 0x5efd
jmp 0x5efd
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
callq 0x5152
leaq 0x1b8(%rsp), %rdi
callq 0x612a
leaq 0xe0(%rsp), %rdi
callq 0x3b5a
leaq 0x100(%rsp), %rdi
callq 0x3b5a
leaq 0xc0(%rsp), %rdi
callq 0x3b5a
movq %rbx, %rdi
callq 0x22b0