// File: /workspace/github_cpp_makefile_star2_O2/Crawler995[P]Cpp-Ray-Tracing/libs/render_engine.cpp
// Function: RenderEngine::render(PerspectiveCamera&, GeometryUnion&, _IO_FILE*, unsigned char*)
void RenderEngine::render(
    PerspectiveCamera &camera, 
    GeometryUnion &geo_union,
    FILE *f, 
    unsigned char *output_img_data
) {
    int index = 0;
    int two_percent_num = image_width * image_height / 50;
    unsigned char *p = output_img_data;
    clock_t start, end;
    start = clock();

    std::string progress_char;
    std::cout << "Start rendering..." << std::endl;

    for(int i = image_height - 1; i >= 0; i--) {
        for(int j = 0; j < image_width; j++) {
            Color color(0, 0, 0);

            for(int k = 0; k < sample_times; k++) {
                double u = 1.0 * (j + rand_num_0_to_1()) / image_width;
                double v = 1.0 * (i + rand_num_0_to_1()) / image_height;

                Ray3 ray = camera.generate_ray(u, v);
                color = color.add(ray_tracing_recursive(ray, geo_union, 0));
            }

            color = color.multiply(1.0 / sample_times);

            *p++ = (unsigned char)color.get_r();
            *p++ = (unsigned char)color.get_g();
            *p++ = (unsigned char)color.get_b();

            index++;
            if(index % two_percent_num == 0) {
                update_progress(progress_char);
            }
        }
    }

    end = clock();
    double run_time = (double)(end - start) / CLOCKS_PER_SEC;

    svpng(f, image_width, image_height, output_img_data, 0);
    
    std::cout << std::endl << "Render done! Spend " << run_time << "s." << std::endl;

    fclose(f);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r8, -0x48(%rbp)
movq %rcx, -0x58(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdi, %r12
movl 0x4(%rdi), %eax
imull (%rdi), %eax
pushq $0x32
popq %rcx
cltd
idivl %ecx
movl %eax, -0x40(%rbp)
callq 0x20b0
movq %rax, -0x50(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x3cf1(%rip), %rdi      # 0x8fd8
leaq 0xf3d(%rip), %rsi       # 0x622b
callq 0x20f0
movq %rax, %rdi
callq 0x2090
movl 0x4(%r12), %ebx
movl $0x0, -0x3c(%rbp)
leaq -0x38(%rbp), %rdi
movq -0x48(%rbp), %r15
testl %ebx, %ebx
jle 0x5474
decl %ebx
cvtsi2sd %ebx, %xmm0
movsd %xmm0, -0x78(%rbp)
xorl %r14d, %r14d
cmpl (%r12), %r14d
jge 0x530f
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x35d8
movq %r15, -0x60(%rbp)
cvtsi2sd %r14d, %xmm0
movsd %xmm0, -0x80(%rbp)
xorl %r15d, %r15d
movl 0x8(%r12), %eax
cmpl %eax, %r15d
jge 0x53fc
callq 0x370f
movsd %xmm0, -0x88(%rbp)
movl %ebx, %r13d
movl (%r12), %ebx
callq 0x370f
movapd %xmm0, %xmm1
movsd -0x88(%rbp), %xmm0
addsd -0x80(%rbp), %xmm0
cvtsi2sd %ebx, %xmm2
divsd %xmm2, %xmm0
addsd -0x78(%rbp), %xmm1
cvtsi2sdl 0x4(%r12), %xmm2
divsd %xmm2, %xmm1
leaq -0xd8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x308a
movupd -0xd8(%rbp), %xmm0
movupd -0xc8(%rbp), %xmm1
movupd -0xb8(%rbp), %xmm2
movupd %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq %r12, %rdi
movq -0x68(%rbp), %rsi
xorl %edx, %edx
callq 0x5036
leaq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x3600
movq %rax, -0x38(%rbp)
movl %edx, -0x30(%rbp)
incl %r15d
movl %r13d, %ebx
jmp 0x5347
cvtsi2sd %eax, %xmm1
movsd 0xc90(%rip), %xmm0      # 0x6098
divsd %xmm1, %xmm0
leaq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x361c
movq %rax, -0x38(%rbp)
movl %edx, -0x30(%rbp)
movq %r15, %rdi
callq 0x36a2
movq -0x60(%rbp), %r15
movb %al, (%r15)
leaq -0x38(%rbp), %rdi
callq 0x36b4
movb %al, 0x1(%r15)
leaq -0x38(%rbp), %rdi
callq 0x36aa
movb %al, 0x2(%r15)
movl -0x3c(%rbp), %eax
incl %eax
movl %eax, -0x3c(%rbp)
cltd
idivl -0x40(%rbp)
testl %edx, %edx
jne 0x5464
leaq -0xa8(%rbp), %rdi
callq 0x36dc
addq $0x3, %r15
incl %r14d
leaq -0x38(%rbp), %rdi
jmp 0x5325
callq 0x20b0
movq %rax, %rbx
movl (%r12), %esi
movl 0x4(%r12), %edx
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq -0x48(%rbp), %rcx
xorl %r8d, %r8d
callq 0x4830
movq 0x3b39(%rip), %rdi      # 0x8fd8
callq 0x2090
leaq 0xd93(%rip), %rsi       # 0x623e
movq %rax, %rdi
callq 0x20f0
subq -0x50(%rbp), %rbx
cvtsi2sd %rbx, %xmm0
divsd 0xcf4(%rip), %xmm0      # 0x61b8
movq %rax, %rdi
callq 0x2160
leaq 0xd7f(%rip), %rsi       # 0x6252
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
callq 0x2090
movq %r14, %rdi
callq 0x2100
leaq -0xa8(%rbp), %rdi
callq 0x2080
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5517
jmp 0x5517
jmp 0x5517
jmp 0x5517
jmp 0x5517
jmp 0x5517
jmp 0x5517
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x21a0