// File: ticky[P]ps2iconsys/src/ps2_ps2icon.cpp
// Function: PS2Icon::SetGeometry(OBJ_Mesh const&, float)
void PS2Icon::SetGeometry(OBJ_Mesh const& mesh, float scale_factor)
{
	//rewrite header:
	header.file_id = 0x010000;	header.reserved = 0x3F800000;
	header.animation_shapes = 1;
	header.n_vertices = mesh.GetNFaces() * 3;

	//copy animation data:
	AllocateVertexMemory();
	float* tmptexture = new float[mesh.GetNFaces() * 9];

	mesh.GetMeshGeometryUnindexed(fvertices, fnormals, tmptexture, scale_factor);
	for(int i=0; i<mesh.GetNFaces() * 3; 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*3]);
		vert_texture[i].f16_v = convert_f32_to_f16(tmptexture[i*3 + 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)
movss %xmm0, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x10000, (%rax)        # imm = 0x10000
movl $0x3f800000, 0xc(%rax)  # imm = 0x3F800000
movl $0x1, 0x4(%rax)
movq -0x10(%rbp), %rdi
callq 0x45d0
movq -0x30(%rbp), %rdi
imull $0x3, %eax, %eax
movl %eax, 0x10(%rdi)
callq 0xa830
movq -0x10(%rbp), %rdi
callq 0x45d0
imull $0x9, %eax, %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 -0x30(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq 0x30(%rax), %rsi
movq 0x38(%rax), %rdx
movq -0x20(%rbp), %rcx
movss -0x14(%rbp), %xmm0
callq 0xc730
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
callq 0x45d0
movl %eax, %ecx
movl -0x34(%rbp), %eax
imull $0x3, %ecx, %ecx
cmpl %ecx, %eax
jge 0xaccf
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rdi
imull $0x3, -0x24(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movslq -0x24(%rbp), %rdx
movw %si, (%rcx,%rdx,8)
movq 0x30(%rax), %rdi
imull $0x3, -0x24(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movslq -0x24(%rbp), %rdx
movw %si, 0x2(%rcx,%rdx,8)
movq 0x30(%rax), %rdi
imull $0x3, -0x24(%rbp), %eax
addl $0x2, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movslq -0x24(%rbp), %rdx
movw %si, 0x4(%rcx,%rdx,8)
movq 0x18(%rax), %rcx
movslq -0x24(%rbp), %rdx
movw $0x0, 0x6(%rcx,%rdx,8)
movq 0x38(%rax), %rdi
imull $0x3, -0x24(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0x24(%rbp), %rdx
movw %si, (%rcx,%rdx,8)
movq 0x38(%rax), %rdi
imull $0x3, -0x24(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0x24(%rbp), %rdx
movw %si, 0x2(%rcx,%rdx,8)
movq 0x38(%rax), %rdi
imull $0x3, -0x24(%rbp), %eax
addl $0x2, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0x24(%rbp), %rdx
movw %si, 0x4(%rcx,%rdx,8)
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
movw $0x0, 0x6(%rax,%rcx,8)
movq -0x20(%rbp), %rdi
imull $0x3, -0x24(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %dx
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x24(%rbp), %rcx
movw %dx, (%rax,%rcx,8)
movq -0x20(%rbp), %rdi
imull $0x3, -0x24(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcef0
movw %ax, %si
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rcx
movslq -0x24(%rbp), %rdx
movw %si, 0x2(%rcx,%rdx,8)
movq 0x28(%rax), %rax
movslq -0x24(%rbp), %rcx
movl $0xffffffff, 0x4(%rax,%rcx,8) # imm = 0xFFFFFFFF
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xab14
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0xace6
movq -0x40(%rbp), %rdi
callq 0x2270
movq -0x30(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0xad18
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0xad0c
movq -0x48(%rbp), %rdi
callq 0x2270
movq -0x30(%rbp), %rax
movq $0x0, 0x58(%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0xadb5
movl $0x0, -0x28(%rbp)
movq -0x30(%rbp), %rcx
movl -0x28(%rbp), %eax
cmpl 0x50(%rcx), %eax
jae 0xad8e
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x28(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0xad81
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0xad6e
movq -0x50(%rbp), %rdi
callq 0x2270
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x28(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
jmp 0xad83
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xad2e
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0xada9
movq -0x58(%rbp), %rdi
callq 0x2270
movq -0x30(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x30(%rbp), %rax
movl $0x1, 0x50(%rax)
movl $0x8, %edi
callq 0x2040
movq %rax, %rcx
movq -0x30(%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 -0x30(%rbp), %rax
movq %rcx, 0x60(%rax)
movl $0x8, %edi
callq 0x2040
movq %rax, %rdx
movq -0x30(%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 0x6338(%rip), %xmm0     # 0x1116c
movss %xmm0, 0x4(%rax)
addq $0x60, %rsp
popq %rbp
retq
nop