#include "PlatformMesh.h"
#include "FTTUPtr.h"
#include "CFTTRenderHelper.h"
#include "CFTTShaderBuiltInConstants.h"
#include "CFTTMaterialManager.h"
static float g_f309C50[6] = {1.0, 1.0, 1.0, 0.0, 0.0, 0.0};

//---------------------------------------------------------------------------------------------------------------------------------------------
//0021B304 //^_^
//单元测试等级: 游戏运行正常
//单元测试内容: 1-5关
//单元测试结果: 正常
void PlatformMesh_Free(CFTTPlatformMesh *r0_pmesh) {
	r0_pmesh->pCFTTRenderHelper_94=NULL;
	//loc_21B32A
	for(int r5_i=0;r5_i<r0_pmesh->count_40;r5_i++){
		// /loc_21B31C
		FTT_pMtlL->ReleaseMaterial(r0_pmesh->uOPData_44.list_model[r5_i].tStr12_2.us_materialIndex_0);
	}
	delete[] ((char*)r0_pmesh);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021B340 //^_^
void PlatformMesh_PerMeshSetup(CFTTPlatformMesh const *pPlatformMesh1, CFTTVector32x4 const *pFTTVector32x42, CFTTVector32x4 const *pFTTVector32x43) {
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021b370 ^_^
void PlatformMesh_Render(CFTTPlatformMesh* r0_pPlatformMesh1, int r1_i, unsigned short r2_us, unsigned int r3_u, unsigned int sp10_u)
{
	CFTTModel_struct_14* r12_list_model = r0_pPlatformMesh1->uOPData_44.list_model;
	if (r2_us ==0xFFFF )
		r2_us = r12_list_model[r1_i].tStr12_2.us_materialIndex_0;
	if (r3_u == 0xFFFFFFFF)
		r3_u = r12_list_model[r1_i].tStr12_2.field_6;
	if (sp10_u == 0xFFFFFFFF)
		sp10_u = r12_list_model[r1_i].tStr12_2.field_2 *3;
	return ((CFTTRenderHelper*)r0_pPlatformMesh1->pCFTTRenderHelper_94.pType_0)->Render( &CFTTShaderBuiltInConstants::ms_matWorld, r2_us, r3_u,sp10_u);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021b3c8 ^_^
void PlatformMesh_PerMeshRestore(CFTTPlatformMesh const *pMesh) {
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021B3F0 //^_^
void PlatformMesh_CreateVolatileResources(CFTTPlatformMesh *p_cfttplatformesh_r0_arg, bool b_r1_arg) {
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021B44A
void PlatformMesh_DeleteVolatileResources(CFTTPlatformMesh *, bool) {
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021B452
void PlatformMesh_CreateManagedResources(CFTTPlatformMesh *, bool) {
  // empty
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021B454
void PlatformMesh_DeleteManagedResources(CFTTPlatformMesh *, bool) {
  // empty
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021B456 //^_^
void PlatformMesh_SetVerts(CFTTPlatformMesh *p_cfttplatformesh_r0) {
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021b478 ^_^
void PlatformMesh_SetIndices(CFTTPlatformMesh *pPlatformMesh1) {
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0x0021b488 ^_-  测试8关无异常
void PlatformMesh_ToModel(CFTTPlatformMesh* pfTTPlatformMesh1, TFTTModel* pfTTModel2, EMeshModelFlags eMeshModelFlags3)
{
	//LOGE("PlatformMesh_ToModel  ");
	//0021B488
	TFTTVertexDescriptor* pTFTTVertexDescriptor_r6 = pfTTPlatformMesh1->uOPData_3C.pTFTTVertexDescriptor;
	FTTGetFVFFromDescriptor(pTFTTVertexDescriptor_r6);
	pfTTModel2->field_12 = 0;
	//!!!!!!注意这里的除法 field_58 定义需要改为short
	int sp4 = pfTTPlatformMesh1->field_30 / (short)pfTTPlatformMesh1->field_58;

	
	pfTTModel2->dCount_C = sp4;
	void* pr12 = pfTTModel2 + 1;
	pfTTModel2->us_10 = pfTTPlatformMesh1->count_40;
	if (eMeshModelFlags3 & 1)
	{
		pfTTModel2->u_28.pfTTModel = (TFTTModel*)pr12;
		pfTTModel2->u_34.pdat = (ushort*)((int)pr12 + pfTTModel2->us_10 * 12);
		unsigned int u_r12 = (3 + pfTTModel2->us_10 * 2) & 0x3FFFC;

		char b_r7 = 0;
		unsigned int u_r3 = 0;
		//goto loc_21B4F4;
		//loc_21B4E4
		while (pfTTModel2->us_10 > b_r7)
		{
			CFTTModel_struct_14* p_list_model2_r1 = pfTTPlatformMesh1->uOPData_44.list_model;
			u_r3 = p_list_model2_r1[b_r7].tStr12_2.field_2 + u_r3;
			b_r7++;
		}

		unsigned int p_r0 = (int)pfTTModel2->u_34.pdat + u_r12;
		pfTTModel2->field_E = u_r3;
		pfTTModel2->u_24.index = p_r0;
		unsigned int sp10 = (3 + u_r3 * 6) & 0xFFFFFFFC;

		unsigned char b_r10 = 0;
		unsigned int u_r9 = 0;
	//loc_21B57A:
		while (b_r10 < pfTTModel2->us_10)
		{
			//21B522
			CFTTModel_struct_14* plistMode_r2 = pfTTPlatformMesh1->uOPData_44.list_model;
			unsigned short us_r7 = plistMode_r2[b_r10].field_0;
			TFTTModel_struct_C_1* pTFTTModel_struct_C_1_r8 = &pfTTModel2->u_28.list_C[b_r10];//r8
			pTFTTModel_struct_C_1_r8->field_8 = us_r7;
			pTFTTModel_struct_C_1_r8->field_4 = 0;
			int ir2 = plistMode_r2[b_r10].tStr12_2.field_2;
			pTFTTModel_struct_C_1_r8->field_0 = 0;
			pTFTTModel_struct_C_1_r8->field_6 = ir2;
			int i_r1 = plistMode_r2[b_r10].tStr12_2.field_6;
			unsigned short* pus_r0 = pfTTModel2->u_24.pusbuf;
			unsigned  u_r2 =  ((unsigned short)ir2)*6;
			ushort* pus_r3 = pfTTPlatformMesh1->uOPData_34.pusData;
			memcpy((void*) & pus_r0[u_r9], (void*)&pus_r3[i_r1], u_r2 );
			unsigned u_r1 = pTFTTModel_struct_C_1_r8->field_6;
			b_r10++;
			u_r1 *= 3;
			u_r9 += u_r1;
		}
		pr12 = (void*)((int)p_r0 + sp10);
	//loc_21B5B0:
		unsigned char b_lr = 0;
		unsigned short* pus_r1;
		while (b_lr < pfTTModel2->us_10)
		{
			//loc_21B596
			CFTTModel_struct_14* pCFTTModel_struct_14_r3 = pfTTPlatformMesh1->uOPData_44.list_model;
			pus_r1 = pfTTModel2->u_34.pdat;
			pfTTModel2->u_34.pdat[b_lr] = pCFTTModel_struct_14_r3[b_lr].tStr12_2.us_materialIndex_0;
			b_lr++;
		}
	}
	//loc_21B5BE:
	unsigned int u_lr = eMeshModelFlags3 & 2;
	if (u_lr)
	{
		pfTTModel2->u_1C.pusbuf = (ushort*)pr12;
		pr12 = (void*)((int)pr12 + 12 * sp4);
	}
	unsigned int sp8 = eMeshModelFlags3 & 4; //r1
	float f_s0 = 32767.0;
	if (sp8)
	{
		pfTTModel2->u_20.pusbuf =(ushort*)pr12;
		pr12 = (void*)((int)pr12 + 12 * sp4);
	}

	float f_s2 = 65535.0;
	unsigned sp14 = eMeshModelFlags3 & 8;
	float f_s4 = 0.0;
	if (sp14)
	{
		pfTTModel2->u_30.pusbuf = (ushort*)pr12;
		pr12 = (void*)((int)pr12 + 12 * sp4);
	}
	unsigned sp10 = eMeshModelFlags3 & 0x20;
	if (sp10)
	{
		pfTTModel2->u_38.pusbuf = (ushort*)pr12;
		pr12 = (void*)((int)pr12 + 4 * sp4);
	}

	unsigned sp18 = eMeshModelFlags3 & 0x40;
	if (sp18)
	{
		unsigned int u_r1 = (int)pr12 + 2 * sp4;
		pfTTModel2->pmodel_struct_C_2_40 = (TFTTModel_struct_C_2*)u_r1;
		pfTTModel2->pmodel_struct_C_3_3C =(TFTTModel_struct_C_3*) pr12;
		
		pr12 = (void*)((int)u_r1 + 16 * sp4);
	}

	unsigned int u_r2 = eMeshModelFlags3 & 0x10;
	unsigned int spC = u_r2;
	if (spC)
		pfTTModel2->u_2C.index = (int)pr12;
//loc_21BA12:
//LOGE("PlatformMesh_ToModel while b ");
	while (pTFTTVertexDescriptor_r6->field_0 != -1)
	{
		//loc_21B638:

		unsigned int u_r8 = 0;
		unsigned int u_r9 = 0;
		unsigned int u_r10 = 0;
		unsigned int u_r11 = 0;
		//goto loc_21BA0A;
	//loc_21BA0A: 
		for (; u_r11 != sp4; u_r11++)
		{
			unsigned int* p_r12 = (unsigned int*)(pfTTPlatformMesh1->field_58 * u_r11 
				+ pfTTPlatformMesh1->uOPData_2C.pData + pTFTTVertexDescriptor_r6->field_C);

			switch (pTFTTVertexDescriptor_r6->field_4)
			{
			case 0:
			{
			//	LOGE("PlatformMesh_ToModel switch 0 ");
				//loc_21B67C:
				if (u_lr)
				{
					if (pTFTTVertexDescriptor_r6->field_0 == 2)
					{
						//LOGE("PlatformMesh_ToModel switch 0 2 ");
						((int*)(pfTTModel2->u_1C.pmodel_union_struct_2[u_r11].pfbuf))[0] = p_r12[0];
						((int*)(pfTTModel2->u_1C.pmodel_union_struct_2[u_r11].pfbuf))[1] = p_r12[1];
						((int*)(pfTTModel2->u_1C.pmodel_union_struct_2[u_r11].pfbuf))[2] = p_r12[2];
					}
					//loc_21B6B2
					if (pTFTTVertexDescriptor_r6->field_0 == 0x1A)
					{
						//LOGE("PlatformMesh_ToModel switch 0 1a ");
						int icount_r7 = 0;
						pfTTModel2->u_1C.pmodel_union_struct_2[u_r11].pfbuf[0] = (float)(((short*)p_r12)[0]) / (float)32767.0;
						pfTTModel2->u_1C.pmodel_union_struct_2[u_r11].pfbuf[1] = (float)(((short*)p_r12)[1]) / (float)32767.0;
						pfTTModel2->u_1C.pmodel_union_struct_2[u_r11].pfbuf[2] = (float)(((short*)p_r12)[2]) / (float)32767.0;
						//LOGE("PlatformMesh_ToModel switch 0 1a while begin ");
						//loc_21B72C
						while (icount_r7 != 0x3)
						{
							//loc_21B70E 
							pfTTModel2->u_1C.pmodel_union_struct_2[u_r8].pfbuf[icount_r7] =
								pfTTPlatformMesh1->pfloat_array_68[icount_r7]
								+ pfTTPlatformMesh1->pfloat_array_5C[icount_r7]*
								pfTTModel2->u_1C.pmodel_union_struct_2[u_r8].pfbuf[icount_r7];
								icount_r7++;
						}

					//	LOGE("PlatformMesh_ToModel switch 0 1a end  ");
					}
				}
				break;
			}
			case 1:
			{
			//	LOGE("PlatformMesh_ToModel switch 1 ");
				if (sp18)
				{

					pfTTModel2->pmodel_struct_C_2_40[u_r11].pfloatArray[0] = (float)(((unsigned short*)p_r12)[0]) / (float)65535.0;
					pfTTModel2->pmodel_struct_C_2_40[u_r11].pfloatArray[1] = (float)(((unsigned short*)p_r12)[1]) / (float)65535.0;
					pfTTModel2->pmodel_struct_C_2_40[u_r11].pfloatArray[2] = (float)(((unsigned short*)p_r12)[2]) / (float)65535.0;
					pfTTModel2->pmodel_struct_C_2_40[u_r11].pfloatArray[3] = (float)(((unsigned short*)p_r12)[3]) / (float)65535.0;
					int icount_r2 = 0;
					float f_s6 = 0.0;
					//loc_21B7C8
					while (icount_r2 != 4)
					{
						//loc_21B7BC
						f_s6 += pfTTModel2->pmodel_struct_C_2_40[u_r10].pfloatArray[icount_r2++];
					}
					int icount_r7 = 0;
					//loc_21B7E4
					while (icount_r7 != 2)
					{
						//loc_21B7D0 
						pfTTModel2->pmodel_struct_C_2_40[u_r9].pfloatArray[icount_r7] = pfTTModel2->pmodel_struct_C_2_40[u_r9].pfloatArray[icount_r7] / f_s6;
						icount_r7++;
					}
				}

				break;
			}
			case 2:
			{
			//		LOGE("PlatformMesh_ToModel switch 2 ");
				//loc_21B880
				if (sp18!=0)
				{
					pfTTModel2->pmodel_struct_C_3_3C[u_r11].field_0 = ((char*)p_r12)[0];
					pfTTModel2->pmodel_struct_C_3_3C[u_r11].field_1 = ((char*)p_r12)[1];
				}
				break;
			}
			case 3:
			{
				//	LOGE("PlatformMesh_ToModel switch 3 ");
				//loc_21B8A2
				if (sp8)
				{
					if (pTFTTVertexDescriptor_r6->field_0 == 2)
					{
						pfTTModel2->u_20.pmodel_union_struct_2[u_r11].pfbuf[0] = ((float*)p_r12)[0];
						pfTTModel2->u_20.pmodel_union_struct_2[u_r11].pfbuf[1] = ((float*)p_r12)[1];
						pfTTModel2->u_20.pmodel_union_struct_2[u_r11].pfbuf[2] = ((float*)p_r12)[2];
					}
					//loc_21B8D6
					if (pTFTTVertexDescriptor_r6->field_0 == 0x1A)
					{
						pfTTModel2->u_20.pmodel_union_struct_2[u_r11].pfbuf[0] = (float)(((short*)p_r12)[0]) / (float)32767.0;
						pfTTModel2->u_20.pmodel_union_struct_2[u_r11].pfbuf[1] = (float)(((short*)p_r12)[1]) / (float)32767.0;
						pfTTModel2->u_20.pmodel_union_struct_2[u_r11].pfbuf[2] = (float)(((short*)p_r12)[2]) / (float)32767.0;
					}
				}
				break;
			}
			case 5:
			{
				//	LOGE("PlatformMesh_ToModel switch 5 ");
				//loc_21B930
				if (spC)
				{
					int i_r7 = sp4;
					if (pTFTTVertexDescriptor_r6->field_8 != 1)
						i_r7 = 0;

					if (pTFTTVertexDescriptor_r6->field_0 == 1)
					{
						pfTTModel2->u_2C.pmodel_union_struct_3[i_r7 + u_r11].pfbuf[0] = p_r12[0];
						pfTTModel2->u_2C.pmodel_union_struct_3[i_r7 + u_r11].pfbuf[1] = p_r12[1];
					}
					//loc_21B968
					if (pTFTTVertexDescriptor_r6->field_0 == 0x19)
					{
						pfTTModel2->u_2C.pmodel_union_struct_3[i_r7 + u_r11].pfbuf[0]=(float)(((short*)p_r12)[0]) / (float) 32767.0;
						pfTTModel2->u_2C.pmodel_union_struct_3[i_r7 + u_r11].pfbuf[1]= (float)(((short*)p_r12)[1]) / (float)32767.0;

						CFTTVector32x4* pfttVector32x4 = &pfTTPlatformMesh1->pfTTVector32x4_74;
						pfTTModel2->u_2C.pmodel_union_struct_3[i_r7 + u_r11].pfbuf[0] =
							pfttVector32x4[pTFTTVertexDescriptor_r6->field_8].my[2] +
							pfttVector32x4[pTFTTVertexDescriptor_r6->field_8].my[0] *
							pfTTModel2->u_2C.pmodel_union_struct_3[i_r7 + u_r11].pfbuf[0];

						pfTTModel2->u_2C.pmodel_union_struct_3[i_r7 + u_r11].pfbuf[1] =
							pfttVector32x4[pTFTTVertexDescriptor_r6->field_8].my[3] +
							pfttVector32x4[pTFTTVertexDescriptor_r6->field_8].my[1] *
							pfTTModel2->u_2C.pmodel_union_struct_3[i_r7 + u_r11].pfbuf[1];
					}
				}
				break;
			}
			case 6:
			{
					//LOGE("PlatformMesh_ToModel switch 6 ");
				//loc_21B7EA
				if (sp14)
				{
					if (pTFTTVertexDescriptor_r6->field_0 == 2)
					{
						pfTTModel2->u_30.pmodel_union_struct_2[u_r11].pfbuf[0] = ((float*)p_r12)[0];
						pfTTModel2->u_30.pmodel_union_struct_2[u_r11].pfbuf[1] = ((float*)p_r12)[1];
						pfTTModel2->u_30.pmodel_union_struct_2[u_r11].pfbuf[2] = ((float*)p_r12)[2];
					}
					//loc_21B820
					if (pTFTTVertexDescriptor_r6->field_0 == 0x1A)
					{
						pfTTModel2->u_30.pmodel_union_struct_2[u_r11].pfbuf[0] = (float)(((short*)p_r12)[0]) / 32767.0;
						pfTTModel2->u_30.pmodel_union_struct_2[u_r11].pfbuf[1] = (float)(((short*)p_r12)[1]) / 32767.0;
						pfTTModel2->u_30.pmodel_union_struct_2[u_r11].pfbuf[2] = (float)(((short*)p_r12)[2]) / 32767.0;
					}
				}
				break;
			}
			case 10:
			{
				//	LOGE("PlatformMesh_ToModel switch 10 ");
				if (sp10)
					pfTTModel2->u_38.pibuf[u_r11] = p_r12[0];
				break;
			}
			default:
				break;
			}
			//def_21B662
			u_r8 += 1;
			u_r9 +=1;
			u_r10 += 1;//
		}
		pTFTTVertexDescriptor_r6++;
	}
	//LOGE("PlatformMesh_ToModel memcpy ");
	memcpy((void*)&pfTTModel2->float32x2_t_54, (void*)&pfTTPlatformMesh1->tPlatformModel_0.tfloat32x2_24, sizeof(float32x2_t));
	memcpy((void*)&pfTTModel2->float32x2_t_44, (void*)&pfTTPlatformMesh1->tPlatformModel_0.tfloat32x2_14, sizeof(float32x2_t)*2);
	//LOGE("PlatformMesh_ToModel memcpy end ");
	return;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//0021BA38
void PlatformMesh_FromModel(CFTTPlatformMesh *, CFTTPlatformMesh *, TFTTModel *, EMeshModelFlags) {
}
//---------------------------------------------------------------------------------------------------------------------------------------------

/*
0021B304 PlatformMesh_Free
0021BA38 PlatformMesh_FromModel
0021B44A PlatformMesh_DeleteVolatileResources

*/