// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebp
movq %rcx, (%rsp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %rbx
movabsq $0x100010000, %rax      # imm = 0x100010000
movq %rax, (%rdi)
movl $0x3f800000, 0xc(%rdi)  # imm = 0x3F800000
movl %r8d, 0x10(%rdi)
callq 0x6192
movq 0x30(%rbx), %rdi
movslq %ebp, %r15
leaq (,%r15,4), %rax
leaq (%rax,%rax,2), %r13
movq %r12, %rsi
movq %r13, %rdx
callq 0x2140
movq 0x38(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2140
leal (%r15,%r15), %eax
cltq
shlq $0x2, %rax
testl %r15d, %r15d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %r14
movq 0x28(%rbx), %rdi
movq %r15, %rdx
shlq $0x3, %rdx
movq (%rsp), %rsi
callq 0x2140
testl %r15d, %r15d
jle 0x6663
movq 0x30(%rbx), %rax
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rdx
movq 0x38(%rbx), %rsi
movq 0x28(%rbx), %rdi
movl %ebp, %r8d
shlq $0x2, %r8
leaq (%r8,%r8,2), %r8
movl $0x4, %r9d
xorl %r10d, %r10d
movss 0x3d3c(%rip), %xmm0     # 0xa2d4
xorl %r11d, %r11d
movss (%rax,%r11), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
movw %bp, -0x4(%rcx,%r9)
movss 0x4(%rax,%r11), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
movw %bp, -0x2(%rcx,%r9)
movss 0x8(%rax,%r11), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
movw %bp, (%rcx,%r9)
movw %r10w, 0x2(%rcx,%r9)
movss (%rsi,%r11), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
movw %bp, -0x4(%rdx,%r9)
movss 0x4(%rsi,%r11), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
movw %bp, -0x2(%rdx,%r9)
movss 0x8(%rsi,%r11), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
movw %bp, (%rdx,%r9)
movw %r10w, 0x2(%rdx,%r9)
movss -0x4(%r14,%r9), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
movw %bp, -0x4(%rdi,%r9)
movss (%r14,%r9), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
movw %bp, -0x2(%rdi,%r9)
movl $0xffffffff, (%rdi,%r9) # imm = 0xFFFFFFFF
addq $0xc, %r11
addq $0x8, %r9
cmpq %r11, %r8
jne 0x659b
movq %r14, %rdi
callq 0x2250
movq 0x58(%rbx), %rdi
testq %rdi, %rdi
je 0x6681
callq 0x2250
movq $0x0, 0x58(%rbx)
cmpq $0x0, 0x60(%rbx)
je 0x66d0
cmpl $0x0, 0x50(%rbx)
je 0x66ba
xorl %r14d, %r14d
movq 0x60(%rbx), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x66af
callq 0x2250
movq 0x60(%rbx), %rax
movq $0x0, (%rax,%r14,8)
incq %r14
movl 0x50(%rbx), %eax
cmpq %rax, %r14
jb 0x6691
movq 0x60(%rbx), %rdi
testq %rdi, %rdi
je 0x66c8
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)
movabsq $0x3f80000000000000, %rcx # imm = 0x3F80000000000000
movq %rcx, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq