// File: /workspace/github_c_makefile_star2_O3/Nealsbo[P]C-Software-Renderer/src/raster.c
// Function: Raster_DrawTriangle
void Raster_DrawTriangle( vec4 *v, SDL_Surface *Surface, obj_model_t *model, shader_t *shader ) {
	float w0, w1, w2, z, area;
	int index, x ,y, srcX, srcY;
	
	mat4 sst;
	color_t color;
	
	mat4_screen( sst, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 );

	//vec3 p;
	vec3 overW = { 1.0f / v[0].w, 1.0f / v[1].w, 1.0f / v[2].w };
	vec3 pts[3] = {vec4_toVec3( vec4_pdiv( vec4_byMat4(v[0], sst) ) ),
				   vec4_toVec3( vec4_pdiv( vec4_byMat4(v[1], sst) ) ),
				   vec4_toVec3( vec4_pdiv( vec4_byMat4(v[2], sst) ) )};
				   
	vec2i bboxmin = { 0, 0 };
	vec2i bboxmax = { WINDOW_WIDTH - 1, WINDOW_HEIGHT - 1 };
	vec2i clamp   = { WINDOW_WIDTH - 1, WINDOW_HEIGHT - 1 };
	
	area = EdgeFunc( &pts[0], &pts[1], &pts[2] );
	
	bboxmax.x = MIN( MAX( pts[0].x, MAX( pts[1].x, pts[2].x) ), clamp.x );
	bboxmin.x = MAX( MIN( pts[0].x, MIN( pts[1].x, pts[2].x) ), 0 );
	bboxmax.y = MIN( MAX( pts[0].y, MAX( pts[1].y, pts[2].y) ), clamp.y );
	bboxmin.y = MAX( MIN( pts[0].y, MIN( pts[1].y, pts[2].y) ), 0 );

	for( x = bboxmin.x; x <= bboxmax.x; x++ ) {
		for( y = bboxmin.y; y <= bboxmax.y; y++ ) {
			int index = y * WINDOW_WIDTH + x;
			vec3 p = { x, y, 0.0f };
			w0 = EdgeFunc( &pts[1], &pts[2], &p );
			w1 = EdgeFunc( &pts[2], &pts[0], &p );
			w2 = EdgeFunc( &pts[0], &pts[1], &p );
			
			if( w0 >= 0.0f && w1 >= 0.0f && w2 >= 0.0f ) {
				w0 /= area;
				w1 /= area;
				w2 /= area;
				z = 1.0f / ( pts[0].z * w0 + pts[1].z * w1 + pts[2].z * w2 );
				
				if( z < renderer_->z_Buffer[index] ) {
					renderer_->z_Buffer[index] = z;
					
					vec3 d = { w0 * overW.x, w1 * overW.y, w2 * overW.z };
					float f = 1.0f/ (d.x + d.y + d.z);
					d = vec3_mlt(d, f);
					
					switch( renderer_->flagState ) {
						case RENDER_STATE_Z_BUFFER:
							color = Color_Gray( (unsigned char)( 255.0f * renderer_->z_Buffer[index] ) );
							break;
						case RENDER_STATE_LIT:
							color = Shader_Fragment( shader, model, d );
							break;
						case RENDER_STATE_UNLIT:
							color = Shader_Fragment( shader, model, d );
							break;
					}
					renderer_->PutPixel( Surface, p.x, p.y, Color_ToUInt32( color ) );
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdi, %r13
leaq 0x140(%rsp), %r12
movss 0x226c(%rip), %xmm0     # 0x7ca0
movss 0x2268(%rip), %xmm1     # 0x7ca4
movq %r12, %rdi
callq 0x41e9
movss 0x1c(%r13), %xmm0
movaps %xmm0, 0x50(%rsp)
movss 0xc(%r13), %xmm0
movaps %xmm0, 0x60(%rsp)
movss 0x15b2(%rip), %xmm0     # 0x7014
divss 0x2c(%r13), %xmm0
movss %xmm0, 0x10(%rsp)
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movq %r12, %rdi
callq 0x2e0f
callq 0x2c3f
callq 0x2c3e
movaps %xmm0, 0x40(%rsp)
movss %xmm1, 0x1c(%rsp)
movsd 0x10(%r13), %xmm0
movsd 0x18(%r13), %xmm1
movq %r12, %rdi
callq 0x2e0f
callq 0x2c3f
callq 0x2c3e
movaps %xmm0, 0x30(%rsp)
movss %xmm1, 0x18(%rsp)
movsd 0x20(%r13), %xmm0
movsd 0x28(%r13), %xmm1
movq %r12, %rdi
callq 0x2e0f
callq 0x2c3f
callq 0x2c3e
movss 0x152e(%rip), %xmm5     # 0x7014
movaps 0x30(%rsp), %xmm8
movaps %xmm8, %xmm2
maxss %xmm0, %xmm2
movaps 0x40(%rsp), %xmm4
movaps %xmm4, %xmm3
maxss %xmm2, %xmm3
minss 0x21a0(%rip), %xmm3     # 0x7ca8
movaps %xmm1, %xmm7
cvttss2si %xmm3, %ecx
movaps %xmm8, %xmm6
movaps %xmm8, %xmm1
minss %xmm0, %xmm1
movaps %xmm4, %xmm2
minss %xmm1, %xmm2
xorps %xmm8, %xmm8
maxss %xmm8, %xmm2
cvttss2si %xmm2, %eax
movl %ecx, 0x4(%rsp)
cmpl %ecx, %eax
jg 0x5ebe
movaps 0x60(%rsp), %xmm2
unpcklps 0x50(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
movaps 0x2164(%rip), %xmm1     # 0x7cb0
divps %xmm2, %xmm1
movaps %xmm1, 0x110(%rsp)
movaps 0x40(%rsp), %xmm4
movaps %xmm4, %xmm9
shufps $0x55, %xmm4, %xmm9     # xmm9 = xmm9[1,1],xmm4[1,1]
movaps %xmm6, %xmm1
shufps $0x55, %xmm6, %xmm1     # xmm1 = xmm1[1,1],xmm6[1,1]
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
movaps %xmm0, %xmm10
subss %xmm4, %xmm10
movaps %xmm1, %xmm11
subss %xmm9, %xmm11
movaps %xmm1, %xmm3
minss %xmm2, %xmm3
maxss %xmm2, %xmm1
subss %xmm9, %xmm2
movaps %xmm6, %xmm12
subss %xmm4, %xmm12
mulss %xmm12, %xmm2
movaps %xmm11, 0xa0(%rsp)
mulss %xmm11, %xmm10
subss %xmm2, %xmm10
movaps %xmm9, %xmm2
minss %xmm3, %xmm2
maxss %xmm8, %xmm2
cvttss2si %xmm2, %ebx
movaps %xmm9, %xmm2
maxss %xmm1, %xmm2
minss 0x20d5(%rip), %xmm2     # 0x7cac
cvttss2si %xmm2, %edx
movaps %xmm0, %xmm11
unpcklps %xmm4, %xmm11           # xmm11 = xmm11[0],xmm4[0],xmm11[1],xmm4[1]
shufps $0x11, %xmm0, %xmm4     # xmm4 = xmm4[1,0],xmm0[1,0]
shufps $0xe2, %xmm0, %xmm4     # xmm4 = xmm4[2,0],xmm0[2,3]
movaps %xmm0, %xmm13
shufps $0x11, %xmm6, %xmm13    # xmm13 = xmm13[1,0],xmm6[1,0]
shufps $0xe2, %xmm6, %xmm13    # xmm13 = xmm13[2,0],xmm6[2,3]
subps %xmm13, %xmm4
movaps %xmm4, 0x90(%rsp)
unpcklps %xmm0, %xmm6            # xmm6 = xmm6[0],xmm0[0],xmm6[1],xmm0[1]
movaps %xmm6, 0x30(%rsp)
subps %xmm6, %xmm11
movslq %ebx, %rcx
movq %rdx, 0x70(%rsp)
leal 0x1(%rdx), %r14d
movslq %eax, %rdx
incl 0x4(%rsp)
movaps %xmm10, %xmm6
shufps $0x0, %xmm10, %xmm6     # xmm6 = xmm6[0,0],xmm10[0,0]
leaq (%rcx,%rcx,4), %rax
shlq $0xa, %rax
leaq (%rax,%rdx,4), %r15
leaq 0x5568(%rip), %rbp      # 0xb1a8
movl %ebx, 0x8(%rsp)
movss %xmm7, 0xc(%rsp)
movaps %xmm9, 0x100(%rsp)
movaps %xmm10, 0xf0(%rsp)
movaps %xmm12, 0xe0(%rsp)
movaps %xmm11, 0xd0(%rsp)
movaps %xmm13, 0xc0(%rsp)
movaps %xmm6, 0xb0(%rsp)
cmpl 0x70(%rsp), %ebx
movq %rdx, 0x80(%rsp)
movq %r15, 0x78(%rsp)
jg 0x5e9c
xorps %xmm14, %xmm14
cvtsi2ss %edx, %xmm14
movaps %xmm14, %xmm15
shufps $0x0, %xmm14, %xmm15    # xmm15 = xmm15[0,0],xmm14[0,0]
subps 0x30(%rsp), %xmm15
mulps 0x90(%rsp), %xmm15
cvttss2si %xmm14, %eax
movl %eax, 0x14(%rsp)
subss 0x40(%rsp), %xmm14
mulss 0xa0(%rsp), %xmm14
movaps %xmm14, 0x130(%rsp)
movaps %xmm15, 0x120(%rsp)
xorps %xmm3, %xmm3
cvtsi2ss %ebx, %xmm3
movaps %xmm3, %xmm1
shufps $0x0, %xmm3, %xmm1      # xmm1 = xmm1[0,0],xmm3[0,0]
subps %xmm13, %xmm1
mulps %xmm11, %xmm1
movaps %xmm15, %xmm0
subps %xmm1, %xmm0
ucomiss %xmm8, %xmm0
jb 0x5e8a
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
ucomiss %xmm8, %xmm1
jb 0x5e8a
movaps %xmm3, %xmm2
subss %xmm9, %xmm2
mulss %xmm12, %xmm2
movaps %xmm14, %xmm1
subss %xmm2, %xmm1
ucomiss %xmm8, %xmm1
jb 0x5e8a
movaps %xmm3, 0x60(%rsp)
divps %xmm6, %xmm0
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
divss %xmm10, %xmm1
mulss 0x18(%rsp), %xmm2
movss 0x1c(%rsp), %xmm3
mulss %xmm0, %xmm3
addss %xmm2, %xmm3
movaps %xmm7, %xmm4
mulss %xmm1, %xmm4
addss %xmm3, %xmm4
movaps %xmm5, %xmm2
divss %xmm4, %xmm2
movq (%rbp), %rax
movq 0x18(%rax), %rax
movss (%rax,%r15), %xmm3
ucomiss %xmm2, %xmm3
jbe 0x5e8a
movss %xmm2, (%rax,%r15)
mulps 0x110(%rsp), %xmm0
mulss 0x10(%rsp), %xmm1
movaps %xmm0, %xmm3
shufps $0x55, %xmm0, %xmm3     # xmm3 = xmm3[1,1],xmm0[1,1]
addss %xmm0, %xmm3
addss %xmm1, %xmm3
movaps %xmm5, %xmm2
divss %xmm3, %xmm2
callq 0x2a3c
movq (%rbp), %rdx
movl 0x8(%rdx), %ecx
cmpl $0x1, %ecx
je 0x5df4
cmpl $0x2, %ecx
je 0x5df4
cmpl $0x3, %ecx
movl 0x50(%rsp), %eax
jne 0x5e03
movq 0x18(%rdx), %rax
movss (%rax,%r15), %xmm0
mulss 0x1426(%rip), %xmm0     # 0x720c
cvttss2si %xmm0, %eax
movzbl %al, %edi
callq 0x1ea3
jmp 0x5e03
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x6747
movq (%rbp), %rcx
movq 0x28(%rcx), %r12
cvttss2si 0x60(%rsp), %r13d
movl %eax, 0x50(%rsp)
movl %eax, %edi
callq 0x213a
movq 0x88(%rsp), %rdi
movl 0x14(%rsp), %esi
movl %r13d, %edx
movl %eax, %ecx
callq *%r12
movss 0x11db(%rip), %xmm5     # 0x7014
movss 0xc(%rsp), %xmm7
xorps %xmm8, %xmm8
movaps 0x100(%rsp), %xmm9
movaps 0xf0(%rsp), %xmm10
movaps 0xe0(%rsp), %xmm12
movaps 0xd0(%rsp), %xmm11
movaps 0xc0(%rsp), %xmm13
movaps 0xb0(%rsp), %xmm6
movaps 0x130(%rsp), %xmm14
movaps 0x120(%rsp), %xmm15
incl %ebx
addq $0x1400, %r15           # imm = 0x1400
cmpl %ebx, %r14d
jne 0x5ce3
movq 0x80(%rsp), %rdx
incq %rdx
movq 0x78(%rsp), %r15
addq $0x4, %r15
cmpl %edx, 0x4(%rsp)
movl 0x8(%rsp), %ebx
jne 0x5c7f
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq