// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdi, %r13
xorps %xmm0, %xmm0
leaq 0x220(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rax, %rbx
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), %rbp
movaps %xmm0, 0x30(%rbp)
movaps %xmm0, 0x20(%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
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, 0x5daa(%rip)      # 0xc1a8
movq (%rdi), %r12
leaq 0x318(%rsp), %r14
movl $0x1f0, %edx            # imm = 0x1F0
movq %r14, %rdi
xorl %esi, %esi
callq 0x10b0
movsd 0x8(%r13), %xmm0
movss 0x10(%r13), %xmm1
movsd 0x14(%r13), %xmm2
movss 0x1c(%r13), %xmm3
movsd 0x20(%r13), %xmm4
movss 0x28(%r13), %xmm5
movq %r14, %rdi
callq 0x7011
movq %r12, %rdi
callq 0x1b51
movaps %xmm0, 0x60(%rsp)
movss %xmm1, 0x50(%rsp)
movq %r12, %rdi
callq 0x1b51
movaps %xmm0, 0x10(%rsp)
movss %xmm1, 0x4(%rsp)
movq %r12, %rdi
callq 0x1b5b
movaps %xmm0, %xmm2
movaps %xmm1, %xmm3
movaps 0x10(%rsp), %xmm0
movss 0x4(%rsp), %xmm1
callq 0x2a62
movaps %xmm0, 0x10(%rsp)
movss %xmm1, 0x4(%rsp)
movss 0x1b79(%rip), %xmm1     # 0x8014
xorps %xmm0, %xmm0
xorps %xmm2, %xmm2
callq 0x2a5a
movaps %xmm0, %xmm4
movaps %xmm1, %xmm5
movq %rbx, %rdi
movaps 0x60(%rsp), %xmm0
movss 0x50(%rsp), %xmm1
movaps 0x10(%rsp), %xmm2
movss 0x4(%rsp), %xmm3
callq 0x4436
movss 0x130(%r15), %xmm0
movss 0x134(%r15), %xmm1
movss 0x138(%r15), %xmm2
leaq 0x1a0(%rsp), %r14
movq %r14, %rdi
callq 0x3c48
movss 0x13c(%r15), %xmm0
movss 0x140(%r15), %xmm1
movss 0x144(%r15), %xmm2
movq %rbp, %rdi
callq 0x3e35
movss 0x148(%r15), %xmm0
movss 0x14c(%r15), %xmm1
movss 0x150(%r15), %xmm2
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0x3ca2
movq %r14, %rdi
movq %rbp, %rsi
leaq 0x1e0(%rsp), %r14
movq %r14, %rdx
callq 0x30f2
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x120(%rsp), %rbx
movq %rbx, %rdx
callq 0x30f2
leaq 0x220(%rsp), %rdi
movq %rdi, %rbp
movq %rbx, %rsi
leaq 0xe0(%rsp), %r14
movq %r14, %rdx
callq 0x30f2
movq (%r13), %rdi
addq $0x3c, %rdi
movq %r14, %rsi
leaq 0xa0(%rsp), %r13
movq %r13, %rdx
callq 0x30f2
leaq 0x318(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x6f9f
movq %r12, %rdi
callq 0x1b51
movq %r14, %rdi
callq 0x7001
movq %r14, %rdi
movl $0x1, %esi
callq 0x6f98
cmpl $0x0, 0x12c(%r15)
jle 0x67dc
xorl %ecx, %ecx
movq 0x108(%r15), %rax
movq %rcx, 0x10(%rsp)
shlq $0x4, %rcx
movslq (%rax,%rcx), %r14
testq %r14, %r14
movq %r15, %rbx
jle 0x66f2
movq 0x8(%rax,%rcx), %r12
addq $0x8, %r12
leaq 0x2a0(%rsp), %rbp
leaq 0x28(%rsp), %r13
xorl %r15d, %r15d
movq 0x100(%rbx), %rax
movslq -0x8(%r12), %rcx
leaq (%rcx,%rcx,2), %rcx
movsd -0xc(%rax,%rcx,4), %xmm0
movss -0x4(%rax,%rcx,4), %xmm1
callq 0x2c55
movaps %xmm0, 0x60(%rsp)
movaps %xmm1, 0x50(%rsp)
movq 0x110(%rbx), %rax
movslq (%r12), %rcx
leaq (%rcx,%rcx,2), %rcx
movsd -0xc(%rax,%rcx,4), %xmm0
movss -0x4(%rax,%rcx,4), %xmm1
callq 0x2c55
movaps %xmm0, %xmm2
movaps %xmm1, %xmm3
movq 0x118(%rbx), %rax
movslq -0x4(%r12), %rcx
movsd -0x8(%rax,%rcx,8), %xmm4
leaq 0x78(%rsp), %rdi
movaps 0x60(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
callq 0x47fc
movq 0x98(%rsp), %rax
movq %rax, 0x20(%rbp)
movups 0x78(%rsp), %xmm0
movups 0x88(%rsp), %xmm1
movups %xmm1, 0x10(%rbp)
movups %xmm0, (%rbp)
leaq 0x318(%rsp), %rdi
movq %rbp, %rsi
movl %r15d, %edx
callq 0x6a0c
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movaps %xmm0, -0x8(%r13)
incq %r15
addq $0x10, %r13
addq $0xc, %r12
addq $0x28, %rbp
cmpq %r15, %r14
jne 0x6627
movq 0x70(%rsp), %rax
movl 0x8(%rax), %eax
cmpq $0x3, %rax
movq %rbx, %r15
leaq 0x318(%rsp), %rbx
ja 0x67c4
leaq 0x258a(%rip), %rcx      # 0x8ca0
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(%r15), %esi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5b8c
movsd 0x30(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movl 0x154(%r15), %esi
movq %rbx, %rdi
callq 0x5b8c
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movl 0x154(%r15), %esi
movq %rbx, %rdi
callq 0x5b8c
jmp 0x67c4
movq %rbx, %rdi
movl $0x1, %esi
jmp 0x67aa
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6f91
leaq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x5d36
movq 0x10(%rsp), %rcx
incq %rcx
movslq 0x12c(%r15), %rax
cmpq %rax, %rcx
jl 0x65ee
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq