// File: ticky[P]ps2iconsys/src/ps2_ps2icon.cpp
// Function: PS2Icon::SetGeometry(float const*, float const*, float const*, int)
void PS2Icon::SetGeometry(float const* pverts, float const* pnormals, float const* ptexture, int n_vertices) 
{
	//rewrite header:
	header.file_id = 0x010000;	header.reserved = 0x3F800000;
	header.animation_shapes = 1;
	header.n_vertices = n_vertices;

	//copy animation data:
	AllocateVertexMemory();
	memcpy(fvertices, pverts, sizeof(float) * 3 * n_vertices);
	memcpy(fnormals, pnormals, sizeof(float) * 3 * n_vertices);
	float* tmptexture = new float[2*n_vertices];
	memcpy(vert_texture, ptexture, sizeof(float) * 2 * n_vertices);

	for(int i=0; i<n_vertices; i++) {
		vertices[i].f16_x = convert_f32_to_f16(fvertices[i*3]);
		vertices[i].f16_y = convert_f32_to_f16(fvertices[i*3 + 1]);
		vertices[i].f16_z = convert_f32_to_f16(fvertices[i*3 + 2]);
		vertices[i].f16_unknown = 0;

		normals[i].f16_x = convert_f32_to_f16(fnormals[i*3]);
		normals[i].f16_y = convert_f32_to_f16(fnormals[i*3 + 1]);
		normals[i].f16_z = convert_f32_to_f16(fnormals[i*3 + 2]);
		normals[i].f16_unknown = 0;

		vert_texture[i].f16_u = convert_f32_to_f16(tmptexture[i*2]);
		vert_texture[i].f16_v = convert_f32_to_f16(tmptexture[i*2 + 1]);
		vert_texture[i].color = 0xFFFFFFFF;
	}
	delete[] tmptexture;

	//rewrite animation data:
	if(animation)    { delete[] animation;        animation = NULL; }
	if(anim_keys)    {
		for(unsigned int i=0; i<anim_header.n_frames; i++) {
			if(anim_keys[i]) {
				delete[] anim_keys[i];
				anim_keys[i] = NULL;
			}
		}
		delete[] anim_keys;
		anim_keys = NULL;
	}
	//insert default values for no animation:
	anim_header.n_frames = 1;
	animation = new Frame_Data[1];
	animation->n_keys   = 1;
	animation->shape_id = 0;
	anim_keys = new Frame_Key*[1];
	anim_keys[0] = new Frame_Key[1];
	anim_keys[0]->time  = 0.0f;
	anim_keys[0]->value = 1.0f;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movl $0x10000, (%rdi)        # imm = 0x10000
movl $0x3f800000, 0xc(%rdi)  # imm = 0x3F800000
movl $0x1, 0x4(%rdi)
movl -0x24(%rbp), %eax
movl %eax, 0x10(%rdi)
callq 0xa830
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x10(%rbp), %rsi
movslq -0x24(%rbp), %rax
imulq $0xc, %rax, %rdx
callq 0x2170
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x18(%rbp), %rsi
movslq -0x24(%rbp), %rax
imulq $0xc, %rax, %rdx
callq 0x2170
movl -0x24(%rbp), %eax
shll %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, -0x30(%rbp)
movq 0x28(%rax), %rdi
movq -0x20(%rbp), %rsi
movslq -0x24(%rbp), %rdx
shlq $0x3, %rdx
callq 0x2170
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0xb0ae
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rdi
imull $0x3, -0x34(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rcx
movslq -0x34(%rbp), %rdx
movw %si, (%rcx,%rdx,8)
movq 0x30(%rax), %rdi
imull $0x3, -0x34(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rcx
movslq -0x34(%rbp), %rdx
movw %si, 0x2(%rcx,%rdx,8)
movq 0x30(%rax), %rdi
imull $0x3, -0x34(%rbp), %eax
addl $0x2, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rcx
movslq -0x34(%rbp), %rdx
movw %si, 0x4(%rcx,%rdx,8)
movq 0x18(%rax), %rcx
movslq -0x34(%rbp), %rdx
movw $0x0, 0x6(%rcx,%rdx,8)
movq 0x38(%rax), %rdi
imull $0x3, -0x34(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0x34(%rbp), %rdx
movw %si, (%rcx,%rdx,8)
movq 0x38(%rax), %rdi
imull $0x3, -0x34(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0x34(%rbp), %rdx
movw %si, 0x2(%rcx,%rdx,8)
movq 0x38(%rax), %rdi
imull $0x3, -0x34(%rbp), %eax
addl $0x2, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0x34(%rbp), %rdx
movw %si, 0x4(%rcx,%rdx,8)
movq 0x20(%rax), %rax
movslq -0x34(%rbp), %rcx
movw $0x0, 0x6(%rax,%rcx,8)
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %eax
shll %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %dx
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x34(%rbp), %rcx
movw %dx, (%rax,%rcx,8)
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %eax
shll %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rcx
movslq -0x34(%rbp), %rdx
movw %si, 0x2(%rcx,%rdx,8)
movq 0x28(%rax), %rax
movslq -0x34(%rbp), %rcx
movl $0xffffffff, 0x4(%rax,%rcx,8) # imm = 0xFFFFFFFF
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xaf04
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0xb0c5
movq -0x48(%rbp), %rdi
callq 0x2270
movq -0x40(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0xb0f7
movq -0x40(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0xb0eb
movq -0x50(%rbp), %rdi
callq 0x2270
movq -0x40(%rbp), %rax
movq $0x0, 0x58(%rax)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0xb194
movl $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rcx
movl -0x38(%rbp), %eax
cmpl 0x50(%rcx), %eax
jae 0xb16d
movq -0x40(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x38(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0xb160
movq -0x40(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0xb14d
movq -0x58(%rbp), %rdi
callq 0x2270
movq -0x40(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x38(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
jmp 0xb162
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xb10d
movq -0x40(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0xb188
movq -0x60(%rbp), %rdi
callq 0x2270
movq -0x40(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x40(%rbp), %rax
movl $0x1, 0x50(%rax)
movl $0x8, %edi
callq 0x2040
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x58(%rax)
movq 0x58(%rax), %rcx
movl $0x1, 0x4(%rcx)
movq 0x58(%rax), %rax
movl $0x0, (%rax)
movl $0x8, %edi
callq 0x2040
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x60(%rax)
movl $0x8, %edi
callq 0x2040
movq %rax, %rdx
movq -0x40(%rbp), %rax
movq 0x60(%rax), %rcx
movq %rdx, (%rcx)
movq 0x60(%rax), %rcx
movq (%rcx), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rcx)
movq 0x60(%rax), %rax
movq (%rax), %rax
movss 0x5f59(%rip), %xmm0     # 0x1116c
movss %xmm0, 0x4(%rax)
addq $0x60, %rsp
popq %rbp
retq
nop