// File: /workspace/github_c_makefile_star2_O2/Nealsbo[P]C-Software-Renderer/src/render.c
// Function: Renderer_DrawObject
void Renderer_DrawObject( scene_t *scene, renderer_t *renderer, obj_model_t *model, SDL_Surface *Surface ) {
    int i, j;
    obj_face_t face;
    vec4 rast_verts[3];
    vertex_t vertex[3], vertexx[3];
    mat4 camMat = {0.0f}, lookAtMat = {0.0f}, tmp = {0.0f};
    mat4 translation = {0.0f}, rotation = {0.0f}, scale = {0.0f};
    mat4 modelMat = {0.0f}, viewMat = {0.0f}, projMat = {0.0f};
    
    renderer_       = renderer;
    camera_t *cam   = scene->mainCamera;
    shader_t shader = { 0 };
    
    Shader_SetLight( &shader, scene->directLight, scene->directLightColor, scene->ambientLightColor );

    mat4_lookAt( lookAtMat,
                 Camera_GetPosition( cam ),
                 vec3_add( Camera_GetPosition( cam ), Camera_GetFront( cam ) ),
                 vec3_create( 0.0, 1.0, 0.0 ) );

    mat4_translate( translation, model->position.x, model->position.y, model->position.z );
    mat4_rotation(  rotation,    model->rotation.x, model->rotation.y, model->rotation.z );
    mat4_scale(     scale,       model->scale.x,    model->scale.y,    model->scale.z );
    
    mat4_mlt( translation, rotation, tmp );
    mat4_mlt( tmp, scale, modelMat );
    mat4_mlt( lookAtMat, modelMat, viewMat );
    mat4_mlt( scene->mainCamera->projection, viewMat, projMat );

    Shader_SetMatrices( &shader, modelMat, lookAtMat, projMat );
    Shader_SetCamera( &shader, Camera_GetPosition( cam ) );
    Shader_InvertNormal( &shader, TRUE );

    for( i = 0; i < model->trisCount; i++ ) {
        face = model->faces[i];
        for( j = 0; j < face.vcount; j++ ) {
            vertex[j]     = Vertex_Init( vec3_toVec4( model->vertices  [face.indexes[j].x - 1] ),
                                         vec3_toVec4( model->normals   [face.indexes[j].z - 1] ),
                                         model->textcoords[face.indexes[j].y - 1] );

            rast_verts[j] = Shader_Vertex( &shader, &vertex[j], j );
        }
        
        if( Renderer_ClipTriangle( rast_verts ) )
			continue;

        switch( renderer->flagState ) {
            case RENDER_STATE_WIREFRAME:
                Raster_DrawLine( rast_verts[0], rast_verts[1], Surface, model->baseColor );
                Raster_DrawLine( rast_verts[1], rast_verts[2], Surface, model->baseColor );
                Raster_DrawLine( rast_verts[0], rast_verts[2], Surface, model->baseColor );
                break;
                
            case RENDER_STATE_UNLIT:
				Shader_UseLight( &shader, FALSE );
				Raster_DrawTriangle( rast_verts, Surface, model, &shader );
				break;

            case RENDER_STATE_LIT:
				Shader_UseLight( &shader, TRUE );
                Raster_DrawTriangle( rast_verts, Surface, model, &shader );
                break;
                
            case RENDER_STATE_Z_BUFFER:
				Raster_DrawTriangle( rast_verts, Surface, model, &shader );
				break;
				
            default:
                break;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
movq %rcx, 0x8(%rsp)
movq %rdx, %r14
movq %rdi, %rbp
xorps %xmm0, %xmm0
leaq 0x220(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rax, %r15
leaq 0x1e0(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0x1a0(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0x260(%rsp), %r12
movaps %xmm0, 0x30(%r12)
movaps %xmm0, 0x20(%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
leaq 0x160(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0x120(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0xe0(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0xa0(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rsi, 0x70(%rsp)
movq %rsi, 0x656f(%rip)      # 0xc1b0
movq (%rdi), %r13
leaq 0x2a0(%rsp), %rbx
movl $0x1f0, %edx            # imm = 0x1F0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10b0
movsd 0x8(%rbp), %xmm0
movss 0x10(%rbp), %xmm1
movsd 0x14(%rbp), %xmm2
movss 0x1c(%rbp), %xmm3
movsd 0x20(%rbp), %xmm4
movss 0x28(%rbp), %xmm5
movq %rbx, %rdi
callq 0x684d
movq %r13, %rdi
callq 0x1b30
movaps %xmm0, 0x60(%rsp)
movss %xmm1, 0x50(%rsp)
movq %r13, %rdi
callq 0x1b30
movaps %xmm0, 0x10(%rsp)
movss %xmm1, 0x4(%rsp)
movq %r13, %rdi
callq 0x1b3a
movaps %xmm0, %xmm2
movaps %xmm1, %xmm3
movaps 0x10(%rsp), %xmm0
movss 0x4(%rsp), %xmm1
callq 0x2919
movaps %xmm0, 0x10(%rsp)
movss %xmm1, 0x4(%rsp)
movss 0x133c(%rip), %xmm1     # 0x7014
xorps %xmm0, %xmm0
xorps %xmm2, %xmm2
callq 0x2911
movaps %xmm0, %xmm4
movaps %xmm1, %xmm5
movq %r15, %rdi
movaps 0x60(%rsp), %xmm0
movss 0x50(%rsp), %xmm1
movaps 0x10(%rsp), %xmm2
movss 0x4(%rsp), %xmm3
callq 0x3ed3
movss 0x130(%r14), %xmm0
movss 0x134(%r14), %xmm1
movss 0x138(%r14), %xmm2
leaq 0x1a0(%rsp), %rbx
movq %rbx, %rdi
callq 0x397e
movss 0x13c(%r14), %xmm0
movss 0x140(%r14), %xmm1
movss 0x144(%r14), %xmm2
movq %r12, %rdi
callq 0x3b6a
movss 0x148(%r14), %xmm0
movss 0x14c(%r14), %xmm1
movss 0x150(%r14), %xmm2
leaq 0x160(%rsp), %r15
movq %r15, %rdi
callq 0x39d8
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x1e0(%rsp), %rbx
movq %rbx, %rdx
callq 0x2e4a
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x120(%rsp), %r15
movq %r15, %rdx
callq 0x2e4a
leaq 0x220(%rsp), %rdi
movq %rdi, %r12
movq %r15, %rsi
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdx
callq 0x2e4a
movq (%rbp), %rdi
addq $0x3c, %rdi
movq %rbx, %rsi
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdx
callq 0x2e4a
leaq 0x2a0(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x67db
movq %r13, %rdi
callq 0x1b30
movq %rbx, %rdi
callq 0x683d
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x67d4
xorl %ecx, %ecx
movslq 0x12c(%r14), %rax
cmpq %rax, %rcx
jge 0x602b
movq 0x108(%r14), %rax
movq %rcx, 0x10(%rsp)
shlq $0x4, %rcx
movl (%rax,%rcx), %ebx
movq 0x8(%rax,%rcx), %r12
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
addq $0x8, %r12
leaq 0x490(%rsp), %r13
leaq 0x28(%rsp), %rbp
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x5f32
movq 0x100(%r14), %rax
movslq -0x8(%r12), %rcx
imulq $0xc, %rcx, %rcx
movsd -0xc(%rax,%rcx), %xmm0
movss -0x4(%rax,%rcx), %xmm1
callq 0x2a6d
movaps %xmm0, 0x60(%rsp)
movaps %xmm1, 0x50(%rsp)
movq 0x110(%r14), %rax
movslq (%r12), %rcx
imulq $0xc, %rcx, %rcx
movsd -0xc(%rax,%rcx), %xmm0
movss -0x4(%rax,%rcx), %xmm1
callq 0x2a6d
movaps %xmm0, %xmm2
movaps %xmm1, %xmm3
movq 0x118(%r14), %rax
movslq -0x4(%r12), %rcx
movsd -0x8(%rax,%rcx,8), %xmm4
leaq 0x78(%rsp), %rdi
movaps 0x60(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
callq 0x4154
movq 0x98(%rsp), %rax
movq %rax, 0x20(%r13)
movups 0x78(%rsp), %xmm0
movups 0x88(%rsp), %xmm1
movups %xmm1, 0x10(%r13)
movups %xmm0, (%r13)
leaq 0x2a0(%rsp), %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x6248
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movaps %xmm0, -0x8(%rbp)
incq %r15
addq $0x10, %rbp
addq $0xc, %r12
addq $0x28, %r13
jmp 0x5e61
movq 0x70(%rsp), %rax
movl 0x8(%rax), %eax
cmpq $0x3, %rax
ja 0x601e
leaq 0x1d95(%rip), %rcx      # 0x7ce0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x38(%rsp), %xmm3
movl 0x154(%r14), %esi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5410
movsd 0x30(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movl 0x154(%r14), %esi
movq %rbx, %rdi
callq 0x5410
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movl 0x154(%r14), %esi
movq %rbx, %rdi
callq 0x5410
jmp 0x601e
leaq 0x2a0(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x6004
leaq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
leaq 0x2a0(%rsp), %rcx
jmp 0x6019
leaq 0x2a0(%rsp), %rbx
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0x67cd
leaq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x55bb
movq 0x10(%rsp), %rcx
incq %rcx
jmp 0x5e1b
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq