// File: /workspace/github_c_makefile_star2_O1/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 $0x168, %rsp            # imm = 0x168
movq %rcx, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdi, %r13
leaq 0x120(%rsp), %r12
movss 0x2f14(%rip), %xmm0     # 0x8c80
movss 0x2f10(%rip), %xmm1     # 0x8c84
movq %r12, %rdi
callq 0x4400
movss 0x2290(%rip), %xmm1     # 0x8014
movaps %xmm1, %xmm0
divss 0xc(%r13), %xmm0
movss %xmm0, 0x20(%rsp)
movaps %xmm1, %xmm0
divss 0x1c(%r13), %xmm0
movss %xmm0, 0x1c(%rsp)
movaps %xmm1, %xmm0
divss 0x2c(%r13), %xmm0
movss %xmm0, 0x18(%rsp)
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movq %r12, %rdi
callq 0x2f94
callq 0x2d16
callq 0x2d15
movaps %xmm0, 0x60(%rsp)
movss %xmm1, 0x30(%rsp)
movsd 0x10(%r13), %xmm0
movsd 0x18(%r13), %xmm1
movq %r12, %rdi
callq 0x2f94
callq 0x2d16
callq 0x2d15
movaps %xmm0, 0x50(%rsp)
movss %xmm1, 0x2c(%rsp)
movsd 0x20(%r13), %xmm0
movsd 0x28(%r13), %xmm1
movq %r12, %rdi
callq 0x2f94
callq 0x2d16
callq 0x2d15
movss 0x21eb(%rip), %xmm6     # 0x8014
movaps 0x50(%rsp), %xmm5
movaps %xmm5, %xmm3
maxss %xmm0, %xmm3
movaps 0x60(%rsp), %xmm4
movaps %xmm4, %xmm2
maxss %xmm3, %xmm2
minss 0x2e3f(%rip), %xmm2     # 0x8c88
movaps %xmm1, %xmm7
cvttss2si %xmm2, %ecx
movaps %xmm5, %xmm2
movaps %xmm0, 0x90(%rsp)
minss %xmm0, %xmm2
movaps %xmm4, %xmm1
minss %xmm2, %xmm1
xorps %xmm8, %xmm8
maxss %xmm8, %xmm1
cvttss2si %xmm1, %eax
movl %ecx, (%rsp)
cmpl %ecx, %eax
jg 0x6213
movaps 0x60(%rsp), %xmm3
movaps %xmm3, %xmm9
shufps $0x55, %xmm3, %xmm9     # xmm9 = xmm9[1,1],xmm3[1,1]
movaps 0x50(%rsp), %xmm4
movaps %xmm4, %xmm10
shufps $0x55, %xmm4, %xmm10    # xmm10 = xmm10[1,1],xmm4[1,1]
movaps 0x90(%rsp), %xmm5
movaps %xmm5, %xmm11
shufps $0x55, %xmm5, %xmm11    # xmm11 = xmm11[1,1],xmm5[1,1]
movaps %xmm5, %xmm12
subss %xmm3, %xmm12
movaps %xmm10, %xmm1
subss %xmm9, %xmm1
movaps %xmm11, %xmm0
subss %xmm9, %xmm0
movaps %xmm4, %xmm13
subss %xmm3, %xmm13
mulss %xmm13, %xmm0
movaps %xmm1, 0xa0(%rsp)
mulss %xmm1, %xmm12
movaps %xmm10, %xmm1
minss %xmm11, %xmm1
movaps %xmm9, %xmm2
minss %xmm1, %xmm2
maxss %xmm8, %xmm2
cvttss2si %xmm2, %ebx
movaps %xmm10, %xmm1
maxss %xmm11, %xmm1
movaps %xmm9, %xmm2
maxss %xmm1, %xmm2
minss 0x2d78(%rip), %xmm2     # 0x8c8c
subss %xmm0, %xmm12
cvttss2si %xmm2, %edx
movaps %xmm11, %xmm0
subss %xmm10, %xmm0
movss %xmm0, 0xc(%rsp)
movaps %xmm9, %xmm0
subss %xmm11, %xmm0
movss %xmm0, 0x8(%rsp)
movaps %xmm3, %xmm14
subss %xmm5, %xmm14
movaps %xmm5, %xmm15
subss %xmm4, %xmm15
movslq %ebx, %rcx
movq %rdx, 0x70(%rsp)
leal 0x1(%rdx), %r14d
movslq %eax, %rdx
incl (%rsp)
leaq (%rcx,%rcx,4), %rax
shlq $0xa, %rax
leaq (%rax,%rdx,4), %r15
leaq 0x6236(%rip), %rbp      # 0xc1a8
movl %ebx, 0x10(%rsp)
movss %xmm7, 0x14(%rsp)
movaps %xmm9, 0x110(%rsp)
movaps %xmm10, 0x100(%rsp)
movaps %xmm11, 0xf0(%rsp)
movaps %xmm12, 0xe0(%rsp)
movaps %xmm13, 0xd0(%rsp)
movaps %xmm14, 0xc0(%rsp)
movaps %xmm15, 0xb0(%rsp)
cmpl 0x70(%rsp), %ebx
movq %rdx, 0x80(%rsp)
movq %r15, 0x78(%rsp)
jg 0x61f2
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movaps %xmm0, %xmm4
subss 0x50(%rsp), %xmm4
mulss 0xc(%rsp), %xmm4
movaps %xmm0, %xmm1
subss 0x90(%rsp), %xmm1
mulss 0x8(%rsp), %xmm1
movss %xmm1, 0x3c(%rsp)
cvttss2si %xmm0, %eax
movl %eax, 0x24(%rsp)
subss 0x60(%rsp), %xmm0
mulss 0xa0(%rsp), %xmm0
movss %xmm0, 0x38(%rsp)
movss %xmm4, 0x4(%rsp)
xorps %xmm5, %xmm5
cvtsi2ss %ebx, %xmm5
movaps %xmm5, %xmm1
subss %xmm10, %xmm1
mulss %xmm15, %xmm1
movaps %xmm4, %xmm0
subss %xmm1, %xmm0
ucomiss %xmm8, %xmm0
jb 0x61e0
movaps %xmm5, %xmm1
subss %xmm11, %xmm1
mulss %xmm14, %xmm1
movss 0x3c(%rsp), %xmm3
subss %xmm1, %xmm3
ucomiss %xmm8, %xmm3
jb 0x61e0
movaps %xmm5, %xmm2
subss %xmm9, %xmm2
mulss %xmm13, %xmm2
movss 0x38(%rsp), %xmm1
subss %xmm2, %xmm1
ucomiss %xmm8, %xmm1
jb 0x61e0
movss %xmm5, 0x34(%rsp)
divss %xmm12, %xmm0
divss %xmm12, %xmm3
divss %xmm12, %xmm1
movss 0x2c(%rsp), %xmm2
mulss %xmm3, %xmm2
movss 0x30(%rsp), %xmm4
mulss %xmm0, %xmm4
addss %xmm2, %xmm4
movaps %xmm7, %xmm5
mulss %xmm1, %xmm5
addss %xmm4, %xmm5
movaps %xmm6, %xmm2
divss %xmm5, %xmm2
movq (%rbp), %rax
movq 0x18(%rax), %rax
movss (%rax,%r15), %xmm4
ucomiss %xmm2, %xmm4
jbe 0x61da
movss %xmm2, (%rax,%r15)
mulss 0x20(%rsp), %xmm0
mulss 0x1c(%rsp), %xmm3
mulss 0x18(%rsp), %xmm1
movaps %xmm0, %xmm4
addss %xmm3, %xmm4
addss %xmm1, %xmm4
movaps %xmm6, %xmm2
divss %xmm4, %xmm2
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
callq 0x2a72
movq (%rbp), %rdx
movl 0x8(%rdx), %ecx
cmpl $0x1, %ecx
je 0x614c
cmpl $0x2, %ecx
je 0x614c
cmpl $0x3, %ecx
movl 0x28(%rsp), %eax
jne 0x615b
movq 0x18(%rdx), %rax
movss (%rax,%r15), %xmm0
mulss 0x20ce(%rip), %xmm0     # 0x820c
cvttss2si %xmm0, %eax
movzbl %al, %edi
callq 0x1eb3
jmp 0x615b
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x6a93
movq (%rbp), %rcx
movq 0x28(%rcx), %r12
cvttss2si 0x34(%rsp), %r13d
movl %eax, 0x28(%rsp)
movl %eax, %edi
callq 0x214b
movq 0x88(%rsp), %rdi
movl 0x24(%rsp), %esi
movl %r13d, %edx
movl %eax, %ecx
callq *%r12
movss 0x1e83(%rip), %xmm6     # 0x8014
movss 0x14(%rsp), %xmm7
xorps %xmm8, %xmm8
movaps 0x110(%rsp), %xmm9
movaps 0x100(%rsp), %xmm10
movaps 0xf0(%rsp), %xmm11
movaps 0xe0(%rsp), %xmm12
movaps 0xd0(%rsp), %xmm13
movaps 0xc0(%rsp), %xmm14
movaps 0xb0(%rsp), %xmm15
movss 0x4(%rsp), %xmm4
incl %ebx
addq $0x1400, %r15           # imm = 0x1400
cmpl %ebx, %r14d
jne 0x6023
movq 0x80(%rsp), %rdx
incq %rdx
movq 0x78(%rsp), %r15
addq $0x4, %r15
cmpl %edx, (%rsp)
movl 0x10(%rsp), %ebx
jne 0x5fbb
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)