// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movss %xmm0, 0x4(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movabsq $0x100010000, %rax      # imm = 0x100010000
movq %rax, (%rdi)
movl $0x3f800000, 0xc(%rdi)  # imm = 0x3F800000
movq %rsi, %rdi
callq 0x3e30
leal (%rax,%rax,2), %eax
movl %eax, 0x10(%rbx)
movq %rbx, %rdi
callq 0x5fee
movq %r14, %rdi
callq 0x3e30
leal (%rax,%rax,8), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, %r15
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
movq %r14, %rdi
movq %rax, %rcx
movd 0x4(%rsp), %xmm0
callq 0x7094
movq %r14, %rdi
callq 0x3e30
testl %eax, %eax
jle 0x6230
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x18(%rbx), %rax
movq 0x30(%rbx), %rcx
movsd (%rcx,%r13), %xmm0
movaps 0x419c(%rip), %xmm1     # 0xa320
mulps %xmm1, %xmm0
cvttps2dq %xmm0, %xmm0
pshuflw $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3,4,5,6,7]
movd %xmm0, (%rax,%rbp,8)
movss 0x8(%rcx,%r13), %xmm0
movss 0x4150(%rip), %xmm2     # 0xa2f4
mulss %xmm2, %xmm0
cvttss2si %xmm0, %ecx
movw %cx, 0x4(%rax,%rbp,8)
movw %r12w, 0x6(%rax,%rbp,8)
movq 0x20(%rbx), %rax
movq 0x38(%rbx), %rcx
movsd (%rcx,%r13), %xmm0
mulps %xmm1, %xmm0
cvttps2dq %xmm0, %xmm0
pshuflw $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3,4,5,6,7]
movd %xmm0, (%rax,%rbp,8)
movss 0x8(%rcx,%r13), %xmm0
mulss %xmm2, %xmm0
cvttss2si %xmm0, %ecx
movw %cx, 0x4(%rax,%rbp,8)
movw %r12w, 0x6(%rax,%rbp,8)
movq 0x28(%rbx), %rax
movsd (%r15,%r13), %xmm0
mulps %xmm1, %xmm0
cvttps2dq %xmm0, %xmm0
pshuflw $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3,4,5,6,7]
movd %xmm0, (%rax,%rbp,8)
movl $0xffffffff, 0x4(%rax,%rbp,8) # imm = 0xFFFFFFFF
incq %rbp
movq %r14, %rdi
callq 0x3e30
leal (%rax,%rax,2), %eax
cltq
addq $0xc, %r13
cmpq %rax, %rbp
jl 0x616f
movq %r15, %rdi
callq 0x2250
movq 0x58(%rbx), %rdi
testq %rdi, %rdi
je 0x624e
callq 0x2250
movq $0x0, 0x58(%rbx)
movq 0x60(%rbx), %rdi
testq %rdi, %rdi
je 0x62a2
movl 0x50(%rbx), %eax
testl %eax, %eax
je 0x6295
xorl %r14d, %r14d
movq 0x60(%rbx), %rcx
movq (%rcx,%r14,8), %rdi
testq %rdi, %rdi
je 0x6282
callq 0x2250
movq 0x60(%rbx), %rax
movq $0x0, (%rax,%r14,8)
movl 0x50(%rbx), %eax
incq %r14
movl %eax, %ecx
cmpq %rcx, %r14
jb 0x6261
movq 0x60(%rbx), %rdi
testq %rdi, %rdi
je 0x629a
callq 0x2250
movq $0x0, 0x60(%rbx)
movl $0x1, 0x50(%rbx)
movl $0x8, %edi
callq 0x2030
movq %rax, 0x58(%rbx)
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, (%rax)
movl $0x8, %edi
callq 0x2030
movq %rax, %r14
movq %rax, 0x60(%rbx)
movl $0x8, %edi
callq 0x2030
movq %rax, (%r14)
movsd 0x4046(%rip), %xmm0     # 0xa330
movsd %xmm0, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop