From 0a0b2a841b61c473fd049bc6d8eaea52b52ada25 Mon Sep 17 00:00:00 2001 From: SpiralP Date: Sun, 5 Jul 2020 02:31:13 -0700 Subject: [PATCH 1/6] Custom Models v2 now allows 4 modifier numbers per animation, and 4 animations per part --- src/Model.c | 313 +++++++++++++++++++++++++++++++++++++++---------- src/Model.h | 25 +++- src/Protocol.c | 32 +++-- 3 files changed, 294 insertions(+), 76 deletions(-) diff --git a/src/Model.c b/src/Model.c index 4d8a8c4bd..d98fbf15b 100644 --- a/src/Model.c +++ b/src/Model.c @@ -551,78 +551,236 @@ static void CustomModel_MakeParts(void) { } } +struct ModelVertex oldVertices[MODEL_BOX_VERTICES]; +static void CustomModel_ApplyAnimation( + struct CustomModelAnim* anim, + struct CustomModelPart* part, + struct CustomModel* cm, + struct Entity* e, + cc_bool* head, + cc_bool* modifiedVertices, + float* rotX, + float* rotY, + float* rotZ +) { + int i; + float value; + + switch (anim->type) { + case CustomModelAnim_None: + return; + + case CustomModelAnim_Head: + *head = true; + *rotX += -e->Pitch * MATH_DEG2RAD; + break; + + case CustomModelAnim_LeftLeg: + *rotX += e->Anim.LeftLegX; + *rotZ += e->Anim.LeftLegZ; + break; + + case CustomModelAnim_RightLeg: + *rotX += e->Anim.RightLegX; + *rotZ += e->Anim.RightLegZ; + break; + + case CustomModelAnim_LeftArm: + /* TODO: we're using 2 different rotation orders here */ + Models.Rotation = ROTATE_ORDER_XZY; + *rotX += e->Anim.LeftArmX; + *rotZ += e->Anim.LeftArmZ; + break; + + case CustomModelAnim_RightArm: + Models.Rotation = ROTATE_ORDER_XZY; + *rotX += e->Anim.RightArmX; + *rotZ += e->Anim.RightArmZ; + break; + + case CustomModelAnim_SpinX: + /* + a: speed + b: shift pos + */ + *rotX += (float)Game.Time * anim->a + anim->b; + break; + + case CustomModelAnim_SpinY: + *rotY += (float)Game.Time * anim->a + anim->b; + break; + + case CustomModelAnim_SpinZ: + *rotZ += (float)Game.Time * anim->a + anim->b; + break; + + case CustomModelAnim_SpinXVelocity: + *rotX += e->Anim.WalkTime * anim->a + anim->b; + break; + + case CustomModelAnim_SpinYVelocity: + *rotY += e->Anim.WalkTime * anim->a + anim->b; + break; + + case CustomModelAnim_SpinZVelocity: + *rotZ += e->Anim.WalkTime * anim->a + anim->b; + break; + + case CustomModelAnim_SinTranslateX: + case CustomModelAnim_SinTranslateY: + case CustomModelAnim_SinTranslateZ: + case CustomModelAnim_SinTranslateXVelocity: + case CustomModelAnim_SinTranslateYVelocity: + case CustomModelAnim_SinTranslateZVelocity: + case CustomModelAnim_SinRotateX: + case CustomModelAnim_SinRotateY: + case CustomModelAnim_SinRotateZ: + case CustomModelAnim_SinRotateXVelocity: + case CustomModelAnim_SinRotateYVelocity: + case CustomModelAnim_SinRotateZVelocity: + /* + a: speed + b: width + c: shift cycle + d: shift pos + */ + switch (anim->type) { + case CustomModelAnim_SinTranslateX: + case CustomModelAnim_SinTranslateY: + case CustomModelAnim_SinTranslateZ: + case CustomModelAnim_SinRotateX: + case CustomModelAnim_SinRotateY: + case CustomModelAnim_SinRotateZ: + value = ( Math_SinF((float)Game.Time * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; + break; + + case CustomModelAnim_SinTranslateXVelocity: + case CustomModelAnim_SinTranslateYVelocity: + case CustomModelAnim_SinTranslateZVelocity: + case CustomModelAnim_SinRotateXVelocity: + case CustomModelAnim_SinRotateYVelocity: + case CustomModelAnim_SinRotateZVelocity: + value = ( Math_SinF(e->Anim.WalkTime * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; + break; + } + + switch (anim->type) { + case CustomModelAnim_SinTranslateX: + case CustomModelAnim_SinTranslateY: + case CustomModelAnim_SinTranslateZ: + case CustomModelAnim_SinTranslateXVelocity: + case CustomModelAnim_SinTranslateYVelocity: + case CustomModelAnim_SinTranslateZVelocity: + if (!*modifiedVertices) { + *modifiedVertices = true; + Mem_Copy( + oldVertices, + &cm->model.vertices[part->modelPart.offset], + sizeof(struct ModelVertex) * MODEL_BOX_VERTICES + ); + } + + for (i = 0; i < MODEL_BOX_VERTICES; i++) { + switch (anim->type) { + case CustomModelAnim_SinTranslateX: + case CustomModelAnim_SinTranslateXVelocity: + cm->model.vertices[part->modelPart.offset + i].X += value; + break; + + case CustomModelAnim_SinTranslateY: + case CustomModelAnim_SinTranslateYVelocity: + cm->model.vertices[part->modelPart.offset + i].Y += value; + break; + + case CustomModelAnim_SinTranslateZ: + case CustomModelAnim_SinTranslateZVelocity: + cm->model.vertices[part->modelPart.offset + i].Z += value; + break; + } + } + break; + + case CustomModelAnim_SinRotateX: + case CustomModelAnim_SinRotateXVelocity: + *rotX += value; + break; + + case CustomModelAnim_SinRotateY: + case CustomModelAnim_SinRotateYVelocity: + *rotY += value; + break; + + case CustomModelAnim_SinRotateZ: + case CustomModelAnim_SinRotateZVelocity: + *rotZ += value; + break; + } + + break; + } +} + static PackedCol oldCols[FACE_COUNT]; +static void CustomModel_DrawPart( + struct CustomModelPart* part, + struct CustomModel* cm, + struct Entity* e +) { + int i, animIndex; + float rotX, rotY, rotZ; + cc_bool head = false; + cc_bool modifiedVertices = false; + + if (part->fullbright) { + for (i = 0; i < FACE_COUNT; i++) { + oldCols[i] = Models.Cols[i]; + Models.Cols[i] = PACKEDCOL_WHITE; + } + } + + /* bbmodels use xyz rotation order */ + Models.Rotation = ROTATE_ORDER_XYZ; + + rotX = part->rotation.X * MATH_DEG2RAD; + rotY = part->rotation.Y * MATH_DEG2RAD; + rotZ = part->rotation.Z * MATH_DEG2RAD; + + for (animIndex = 0; animIndex < MAX_CUSTOM_MODEL_ANIMS; animIndex++) { + struct CustomModelAnim* anim = &part->anims[animIndex]; + CustomModel_ApplyAnimation(anim, part, cm, e, &head, &modifiedVertices, &rotX, &rotY, &rotZ); + } + + if (rotX || rotY || rotZ || head) { + Model_DrawRotate(rotX, rotY, rotZ, &part->modelPart, head); + } else { + Model_DrawPart(&part->modelPart); + } + + if (modifiedVertices) { + Mem_Copy( + &cm->model.vertices[part->modelPart.offset], + oldVertices, + sizeof(struct ModelVertex) * MODEL_BOX_VERTICES + ); + } + + if (part->fullbright) { + for (i = 0; i < FACE_COUNT; i++) { + Models.Cols[i] = oldCols[i]; + } + } +} + static void CustomModel_Draw(struct Entity* e) { - int i, j; struct CustomModel* cm = (struct CustomModel*)Models.Active; + int partIndex; Model_ApplyTexture(e); Models.uScale = 1.0f / cm->uScale; Models.vScale = 1.0f / cm->vScale; - for (i = 0; i < cm->numParts; i++) { - float rotX, rotY, rotZ; - cc_bool head; - struct CustomModelPart* part = &cm->parts[i]; - - if (part->fullbright) { - for (j = 0; j < FACE_COUNT; j++) { - oldCols[j] = Models.Cols[j]; - Models.Cols[j] = PACKEDCOL_WHITE; - } - } - - /* bbmodels use xyz rotation order */ - Models.Rotation = ROTATE_ORDER_XYZ; - - rotX = part->rotation.X * MATH_DEG2RAD; - rotY = part->rotation.Y * MATH_DEG2RAD; - rotZ = part->rotation.Z * MATH_DEG2RAD; - head = false; - - if (part->anim == CustomModelAnim_Head) { - head = true; - rotX += -e->Pitch * MATH_DEG2RAD; - } else if (part->anim == CustomModelAnim_LeftLeg) { - rotX += e->Anim.LeftLegX; - rotZ += e->Anim.LeftLegZ; - } else if (part->anim == CustomModelAnim_RightLeg) { - rotX += e->Anim.RightLegX; - rotZ += e->Anim.RightLegZ; - } else if (part->anim == CustomModelAnim_LeftArm) { - /* TODO: we're using 2 different rotation orders here */ - Models.Rotation = ROTATE_ORDER_XZY; - rotX += e->Anim.LeftArmX; - rotZ += e->Anim.LeftArmZ; - } else if (part->anim == CustomModelAnim_RightArm) { - Models.Rotation = ROTATE_ORDER_XZY; - rotX += e->Anim.RightArmX; - rotZ += e->Anim.RightArmZ; - } else if (part->anim == CustomModelAnim_SpinX) { - rotX += (float)(Game.Time * part->animModifier); - } else if (part->anim == CustomModelAnim_SpinY) { - rotY += (float)(Game.Time * part->animModifier); - } else if (part->anim == CustomModelAnim_SpinZ) { - rotZ += (float)(Game.Time * part->animModifier); - } else if (part->anim == CustomModelAnim_SpinXVelocity) { - rotX += e->Anim.WalkTime * part->animModifier; - } else if (part->anim == CustomModelAnim_SpinYVelocity) { - rotY += e->Anim.WalkTime * part->animModifier; - } else if (part->anim == CustomModelAnim_SpinZVelocity) { - rotZ += e->Anim.WalkTime * part->animModifier; - } - - if (rotX || rotY || rotZ || head) { - Model_DrawRotate(rotX, rotY, rotZ, &cm->parts[i].modelPart, head); - } else { - Model_DrawPart(&cm->parts[i].modelPart); - } - - if (part->fullbright) { - for (j = 0; j < FACE_COUNT; j++) { - Models.Cols[j] = oldCols[j]; - } - } + for (partIndex = 0; partIndex < cm->numParts; partIndex++) { + CustomModel_DrawPart(&cm->parts[partIndex], cm, e); } Model_UpdateVB(); @@ -645,6 +803,31 @@ static void CustomModel_GetPickingBounds(struct Entity* e) { e->ModelAABB = ((struct CustomModel*)e->Model)->pickingBoundsAABB; } +static void CustomModel_DrawArmPart(struct CustomModelPart* part) { + struct Model* model = Models.Active; + struct ModelPart arm = part->modelPart; + arm.rotX = model->armX / 16.0f; + arm.rotY = (model->armY + model->armY / 2) / 16.0f; + + if (Models.ClassicArms) { + Model_DrawRotate( + part->rotation.X * MATH_DEG2RAD + 0, + part->rotation.Y * MATH_DEG2RAD + -90 * MATH_DEG2RAD, + part->rotation.Z * MATH_DEG2RAD + 120 * MATH_DEG2RAD, + &arm, + false + ); + } else { + Model_DrawRotate( + part->rotation.X * MATH_DEG2RAD + -20 * MATH_DEG2RAD, + part->rotation.Y * MATH_DEG2RAD + -70 * MATH_DEG2RAD, + part->rotation.Z * MATH_DEG2RAD + 135 * MATH_DEG2RAD, + &arm, + false + ); + } +} + static void CustomModel_DrawArm(struct Entity* e) { int i; struct CustomModel* cm = (struct CustomModel*)Models.Active; @@ -655,7 +838,7 @@ static void CustomModel_DrawArm(struct Entity* e) { for (i = 0; i < cm->numParts; i++) { struct CustomModelPart* part = &cm->parts[i]; if (part->firstPersonArm) { - Model_DrawArmPart(&part->modelPart); + CustomModel_DrawArmPart(part); } } diff --git a/src/Model.h b/src/Model.h index 2219c1863..a14cde351 100644 --- a/src/Model.h +++ b/src/Model.h @@ -211,8 +211,9 @@ CC_API void BoxDesc_ZQuad2(struct Model* m, float x1, float x2, float y1, float #define MAX_CUSTOM_MODELS 64 #define MAX_CUSTOM_MODEL_PARTS 64 +#define MAX_CUSTOM_MODEL_ANIMS 4 -enum CustomModelAnim { +enum CustomModelAnimType { CustomModelAnim_None = 0, CustomModelAnim_Head = 1, CustomModelAnim_LeftLeg = 2, @@ -224,7 +225,24 @@ enum CustomModelAnim { CustomModelAnim_SpinZ = 8, CustomModelAnim_SpinXVelocity = 9, CustomModelAnim_SpinYVelocity = 10, - CustomModelAnim_SpinZVelocity = 11 + CustomModelAnim_SpinZVelocity = 11, + CustomModelAnim_SinTranslateX = 12, + CustomModelAnim_SinTranslateY = 13, + CustomModelAnim_SinTranslateZ = 14, + CustomModelAnim_SinTranslateXVelocity = 15, + CustomModelAnim_SinTranslateYVelocity = 16, + CustomModelAnim_SinTranslateZVelocity = 17, + CustomModelAnim_SinRotateX = 18, + CustomModelAnim_SinRotateY = 19, + CustomModelAnim_SinRotateZ = 20, + CustomModelAnim_SinRotateXVelocity = 21, + CustomModelAnim_SinRotateYVelocity = 22, + CustomModelAnim_SinRotateZVelocity = 23 +}; + +struct CustomModelAnim { + cc_uint8 type; + float a, b, c, d; }; struct CustomModelPart { @@ -245,8 +263,7 @@ struct CustomModelPart { /* rotation angles */ Vec3 rotation; - float animModifier; - cc_uint8 anim; + struct CustomModelAnim anims[MAX_CUSTOM_MODEL_ANIMS]; cc_bool fullbright; cc_bool firstPersonArm; diff --git a/src/Protocol.c b/src/Protocol.c index 39fdf2f38..c4789095a 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -60,7 +60,7 @@ static struct MapState map2; /* CPE state */ cc_bool cpe_needD3Fix; static int cpe_serverExtensionsCount, cpe_pingTicks; -static int cpe_envMapVer = 2, cpe_blockDefsExtVer = 2; +static int cpe_envMapVer = 2, cpe_blockDefsExtVer = 2, cpe_customModelsVer = 2; static cc_bool cpe_sendHeldBlock, cpe_useMessageTypes, cpe_extEntityPos, cpe_blockPerms, cpe_fastMap; static cc_bool cpe_twoWayPing, cpe_extTextures, cpe_extBlocks; @@ -851,6 +851,7 @@ static void CPE_SendCpeExtInfoReply(void) { if (String_CaselessEqualsConst(&name, "ExtPlayerList")) ver = 2; if (String_CaselessEqualsConst(&name, "EnvMapAppearance")) ver = cpe_envMapVer; if (String_CaselessEqualsConst(&name, "BlockDefinitionsExt")) ver = cpe_blockDefsExtVer; + if (String_CaselessEqualsConst(&name, "CustomModels")) ver = cpe_customModelsVer; if (!Game_AllowCustomBlocks) { if (String_CaselessEqualsConst(&name, "BlockDefinitionsExt")) continue; @@ -924,6 +925,11 @@ static void CPE_ExtEntry(cc_uint8* data) { } else if (String_CaselessEqualsConst(&ext, "FastMap")) { Net_PacketSizes[OPCODE_LEVEL_BEGIN] += 4; cpe_fastMap = true; + } else if (String_CaselessEqualsConst(&ext, "CustomModels")) { + cpe_customModelsVer = min(2, version); + if (version == 2) { + Net_PacketSizes[OPCODE_DEFINE_MODEL_PART] = 167; + } } #ifdef EXTENDED_TEXTURES else if (String_CaselessEqualsConst(&ext, "ExtendedTextures")) { @@ -1521,12 +1527,24 @@ static void CPE_DefineModelPart(cc_uint8* data) { part->rotation.Z = GetFloat(data); data += 4; - /* read anim */ - part->anim = *data++; + if (cpe_customModelsVer == 1) { + part->anims[0].type = *data++; + part->anims[0].a = GetFloat(data); + data += 4; + } else if (cpe_customModelsVer == 2) { + for (i = 0; i < MAX_CUSTOM_MODEL_ANIMS; i++) { + part->anims[i].type = *data++; - /* read animModifier */ - part->animModifier = GetFloat(data); - data += 4; + part->anims[i].a = GetFloat(data); + data += 4; + part->anims[i].b = GetFloat(data); + data += 4; + part->anims[i].c = GetFloat(data); + data += 4; + part->anims[i].d = GetFloat(data); + data += 4; + } + } /* read bool flags */ flags = *data++; @@ -1548,7 +1566,7 @@ static void CPE_UndefineModel(cc_uint8* data) { static void CPE_Reset(void) { cpe_serverExtensionsCount = 0; cpe_pingTicks = 0; cpe_sendHeldBlock = false; cpe_useMessageTypes = false; - cpe_envMapVer = 2; cpe_blockDefsExtVer = 2; + cpe_envMapVer = 2; cpe_blockDefsExtVer = 2; cpe_customModelsVer = 2; cpe_needD3Fix = false; cpe_extEntityPos = false; cpe_twoWayPing = false; cpe_extTextures = false; cpe_fastMap = false; cpe_extBlocks = false; Game_UseCPEBlocks = false; From 18ad212279d63612f297c040ca7bce9938494720 Mon Sep 17 00:00:00 2001 From: SpiralP Date: Mon, 6 Jul 2020 23:43:35 -0700 Subject: [PATCH 2/6] remove CustomModel_DrawArmPart, added by mistake --- src/Model.c | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/src/Model.c b/src/Model.c index d98fbf15b..3ea585850 100644 --- a/src/Model.c +++ b/src/Model.c @@ -803,31 +803,6 @@ static void CustomModel_GetPickingBounds(struct Entity* e) { e->ModelAABB = ((struct CustomModel*)e->Model)->pickingBoundsAABB; } -static void CustomModel_DrawArmPart(struct CustomModelPart* part) { - struct Model* model = Models.Active; - struct ModelPart arm = part->modelPart; - arm.rotX = model->armX / 16.0f; - arm.rotY = (model->armY + model->armY / 2) / 16.0f; - - if (Models.ClassicArms) { - Model_DrawRotate( - part->rotation.X * MATH_DEG2RAD + 0, - part->rotation.Y * MATH_DEG2RAD + -90 * MATH_DEG2RAD, - part->rotation.Z * MATH_DEG2RAD + 120 * MATH_DEG2RAD, - &arm, - false - ); - } else { - Model_DrawRotate( - part->rotation.X * MATH_DEG2RAD + -20 * MATH_DEG2RAD, - part->rotation.Y * MATH_DEG2RAD + -70 * MATH_DEG2RAD, - part->rotation.Z * MATH_DEG2RAD + 135 * MATH_DEG2RAD, - &arm, - false - ); - } -} - static void CustomModel_DrawArm(struct Entity* e) { int i; struct CustomModel* cm = (struct CustomModel*)Models.Active; @@ -838,7 +813,7 @@ static void CustomModel_DrawArm(struct Entity* e) { for (i = 0; i < cm->numParts; i++) { struct CustomModelPart* part = &cm->parts[i]; if (part->firstPersonArm) { - CustomModel_DrawArmPart(part); + Model_DrawArmPart(&part->modelPart); } } From 79e3e0beed6fe6fd005505d43f4b382637bef493 Mon Sep 17 00:00:00 2001 From: SpiralP Date: Tue, 7 Jul 2020 00:14:37 -0700 Subject: [PATCH 3/6] change protocol for anims: anim type byte now contains which axis to apply to --- src/Model.c | 151 +++++++++++++++++++------------------------------ src/Model.h | 43 +++++++------- src/Protocol.c | 8 ++- 3 files changed, 83 insertions(+), 119 deletions(-) diff --git a/src/Model.c b/src/Model.c index 3ea585850..3bb230d63 100644 --- a/src/Model.c +++ b/src/Model.c @@ -567,110 +567,96 @@ static void CustomModel_ApplyAnimation( float value; switch (anim->type) { - case CustomModelAnim_None: + case CustomModelAnimType_None: return; - case CustomModelAnim_Head: + case CustomModelAnimType_Head: *head = true; *rotX += -e->Pitch * MATH_DEG2RAD; break; - case CustomModelAnim_LeftLeg: + case CustomModelAnimType_LeftLeg: *rotX += e->Anim.LeftLegX; *rotZ += e->Anim.LeftLegZ; break; - case CustomModelAnim_RightLeg: + case CustomModelAnimType_RightLeg: *rotX += e->Anim.RightLegX; *rotZ += e->Anim.RightLegZ; break; - case CustomModelAnim_LeftArm: + case CustomModelAnimType_LeftArm: /* TODO: we're using 2 different rotation orders here */ Models.Rotation = ROTATE_ORDER_XZY; *rotX += e->Anim.LeftArmX; *rotZ += e->Anim.LeftArmZ; break; - case CustomModelAnim_RightArm: + case CustomModelAnimType_RightArm: Models.Rotation = ROTATE_ORDER_XZY; *rotX += e->Anim.RightArmX; *rotZ += e->Anim.RightArmZ; break; - case CustomModelAnim_SpinX: - /* - a: speed - b: shift pos - */ - *rotX += (float)Game.Time * anim->a + anim->b; - break; + case CustomModelAnimType_Spin: + case CustomModelAnimType_SpinVelocity: + case CustomModelAnimType_SinRotate: + case CustomModelAnimType_SinRotateVelocity: + case CustomModelAnimType_SinTranslate: + case CustomModelAnimType_SinTranslateVelocity: - case CustomModelAnim_SpinY: - *rotY += (float)Game.Time * anim->a + anim->b; - break; - - case CustomModelAnim_SpinZ: - *rotZ += (float)Game.Time * anim->a + anim->b; - break; - - case CustomModelAnim_SpinXVelocity: - *rotX += e->Anim.WalkTime * anim->a + anim->b; - break; - - case CustomModelAnim_SpinYVelocity: - *rotY += e->Anim.WalkTime * anim->a + anim->b; - break; - - case CustomModelAnim_SpinZVelocity: - *rotZ += e->Anim.WalkTime * anim->a + anim->b; - break; - - case CustomModelAnim_SinTranslateX: - case CustomModelAnim_SinTranslateY: - case CustomModelAnim_SinTranslateZ: - case CustomModelAnim_SinTranslateXVelocity: - case CustomModelAnim_SinTranslateYVelocity: - case CustomModelAnim_SinTranslateZVelocity: - case CustomModelAnim_SinRotateX: - case CustomModelAnim_SinRotateY: - case CustomModelAnim_SinRotateZ: - case CustomModelAnim_SinRotateXVelocity: - case CustomModelAnim_SinRotateYVelocity: - case CustomModelAnim_SinRotateZVelocity: - /* - a: speed - b: width - c: shift cycle - d: shift pos - */ switch (anim->type) { - case CustomModelAnim_SinTranslateX: - case CustomModelAnim_SinTranslateY: - case CustomModelAnim_SinTranslateZ: - case CustomModelAnim_SinRotateX: - case CustomModelAnim_SinRotateY: - case CustomModelAnim_SinRotateZ: + /* + a: speed + b: shift pos + */ + case CustomModelAnimType_Spin: + value = (float)Game.Time * anim->a + anim->b; + break; + + case CustomModelAnimType_SpinVelocity: + value = e->Anim.WalkTime * anim->a + anim->b; + break; + + /* + a: speed + b: width + c: shift cycle + d: shift pos + */ + case CustomModelAnimType_SinRotate: + case CustomModelAnimType_SinTranslate: value = ( Math_SinF((float)Game.Time * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; break; - case CustomModelAnim_SinTranslateXVelocity: - case CustomModelAnim_SinTranslateYVelocity: - case CustomModelAnim_SinTranslateZVelocity: - case CustomModelAnim_SinRotateXVelocity: - case CustomModelAnim_SinRotateYVelocity: - case CustomModelAnim_SinRotateZVelocity: + case CustomModelAnimType_SinRotateVelocity: + case CustomModelAnimType_SinTranslateVelocity: value = ( Math_SinF(e->Anim.WalkTime * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; break; } switch (anim->type) { - case CustomModelAnim_SinTranslateX: - case CustomModelAnim_SinTranslateY: - case CustomModelAnim_SinTranslateZ: - case CustomModelAnim_SinTranslateXVelocity: - case CustomModelAnim_SinTranslateYVelocity: - case CustomModelAnim_SinTranslateZVelocity: + case CustomModelAnimType_Spin: + case CustomModelAnimType_SpinVelocity: + case CustomModelAnimType_SinRotate: + case CustomModelAnimType_SinRotateVelocity: + switch (anim->axis) { + case CustomModelAnimAxis_X: + *rotX += value; + break; + + case CustomModelAnimAxis_Y: + *rotY += value; + break; + + case CustomModelAnimAxis_Z: + *rotZ += value; + break; + } + break; + + case CustomModelAnimType_SinTranslate: + case CustomModelAnimType_SinTranslateVelocity: if (!*modifiedVertices) { *modifiedVertices = true; Mem_Copy( @@ -681,41 +667,22 @@ static void CustomModel_ApplyAnimation( } for (i = 0; i < MODEL_BOX_VERTICES; i++) { - switch (anim->type) { - case CustomModelAnim_SinTranslateX: - case CustomModelAnim_SinTranslateXVelocity: + switch (anim->axis) { + case CustomModelAnimAxis_X: cm->model.vertices[part->modelPart.offset + i].X += value; break; - case CustomModelAnim_SinTranslateY: - case CustomModelAnim_SinTranslateYVelocity: + case CustomModelAnimAxis_Y: cm->model.vertices[part->modelPart.offset + i].Y += value; break; - case CustomModelAnim_SinTranslateZ: - case CustomModelAnim_SinTranslateZVelocity: + case CustomModelAnimAxis_Z: cm->model.vertices[part->modelPart.offset + i].Z += value; break; } } break; - - case CustomModelAnim_SinRotateX: - case CustomModelAnim_SinRotateXVelocity: - *rotX += value; - break; - - case CustomModelAnim_SinRotateY: - case CustomModelAnim_SinRotateYVelocity: - *rotY += value; - break; - - case CustomModelAnim_SinRotateZ: - case CustomModelAnim_SinRotateZVelocity: - *rotZ += value; - break; } - break; } } diff --git a/src/Model.h b/src/Model.h index a14cde351..639afedd1 100644 --- a/src/Model.h +++ b/src/Model.h @@ -214,34 +214,29 @@ CC_API void BoxDesc_ZQuad2(struct Model* m, float x1, float x2, float y1, float #define MAX_CUSTOM_MODEL_ANIMS 4 enum CustomModelAnimType { - CustomModelAnim_None = 0, - CustomModelAnim_Head = 1, - CustomModelAnim_LeftLeg = 2, - CustomModelAnim_RightLeg = 3, - CustomModelAnim_LeftArm = 4, - CustomModelAnim_RightArm = 5, - CustomModelAnim_SpinX = 6, - CustomModelAnim_SpinY = 7, - CustomModelAnim_SpinZ = 8, - CustomModelAnim_SpinXVelocity = 9, - CustomModelAnim_SpinYVelocity = 10, - CustomModelAnim_SpinZVelocity = 11, - CustomModelAnim_SinTranslateX = 12, - CustomModelAnim_SinTranslateY = 13, - CustomModelAnim_SinTranslateZ = 14, - CustomModelAnim_SinTranslateXVelocity = 15, - CustomModelAnim_SinTranslateYVelocity = 16, - CustomModelAnim_SinTranslateZVelocity = 17, - CustomModelAnim_SinRotateX = 18, - CustomModelAnim_SinRotateY = 19, - CustomModelAnim_SinRotateZ = 20, - CustomModelAnim_SinRotateXVelocity = 21, - CustomModelAnim_SinRotateYVelocity = 22, - CustomModelAnim_SinRotateZVelocity = 23 + CustomModelAnimType_None = 0, + CustomModelAnimType_Head = 1, + CustomModelAnimType_LeftLeg = 2, + CustomModelAnimType_RightLeg = 3, + CustomModelAnimType_LeftArm = 4, + CustomModelAnimType_RightArm = 5, + CustomModelAnimType_Spin = 6, + CustomModelAnimType_SpinVelocity = 7, + CustomModelAnimType_SinRotate = 8, + CustomModelAnimType_SinRotateVelocity = 9, + CustomModelAnimType_SinTranslate = 10, + CustomModelAnimType_SinTranslateVelocity = 11 +}; + +enum CustomModelAnimAxis { + CustomModelAnimAxis_X = 0, + CustomModelAnimAxis_Y = 1, + CustomModelAnimAxis_Z = 2, }; struct CustomModelAnim { cc_uint8 type; + cc_uint8 axis; float a, b, c, d; }; diff --git a/src/Protocol.c b/src/Protocol.c index c4789095a..c32313637 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -1528,12 +1528,14 @@ static void CPE_DefineModelPart(cc_uint8* data) { data += 4; if (cpe_customModelsVer == 1) { - part->anims[0].type = *data++; - part->anims[0].a = GetFloat(data); + /* ignore animations */ + data++; data += 4; } else if (cpe_customModelsVer == 2) { for (i = 0; i < MAX_CUSTOM_MODEL_ANIMS; i++) { - part->anims[i].type = *data++; + cc_uint8 tmp = *data++; + part->anims[i].type = tmp & 0x3F; + part->anims[i].axis = tmp >> 6; part->anims[i].a = GetFloat(data); data += 4; From 331651bf34ff4488a4cb9d3fc5079abdaa36ec4f Mon Sep 17 00:00:00 2001 From: SpiralP Date: Tue, 7 Jul 2020 00:37:35 -0700 Subject: [PATCH 4/6] cleaner code, each anim now chooses which axis to modify --- src/Model.c | 200 +++++++++++++++++++++++----------------------------- src/Model.h | 22 +++--- 2 files changed, 101 insertions(+), 121 deletions(-) diff --git a/src/Model.c b/src/Model.c index 3bb230d63..187d23113 100644 --- a/src/Model.c +++ b/src/Model.c @@ -552,138 +552,65 @@ static void CustomModel_MakeParts(void) { } struct ModelVertex oldVertices[MODEL_BOX_VERTICES]; -static void CustomModel_ApplyAnimation( +static float CustomModel_GetAnimationValue( struct CustomModelAnim* anim, struct CustomModelPart* part, struct CustomModel* cm, - struct Entity* e, - cc_bool* head, - cc_bool* modifiedVertices, - float* rotX, - float* rotY, - float* rotZ + struct Entity* e ) { - int i; - float value; - switch (anim->type) { case CustomModelAnimType_None: return; case CustomModelAnimType_Head: - *head = true; - *rotX += -e->Pitch * MATH_DEG2RAD; - break; + return -e->Pitch * MATH_DEG2RAD; - case CustomModelAnimType_LeftLeg: - *rotX += e->Anim.LeftLegX; - *rotZ += e->Anim.LeftLegZ; - break; + case CustomModelAnimType_LeftLegX: + return e->Anim.LeftLegX; - case CustomModelAnimType_RightLeg: - *rotX += e->Anim.RightLegX; - *rotZ += e->Anim.RightLegZ; - break; + case CustomModelAnimType_RightLegX: + return e->Anim.RightLegX; - case CustomModelAnimType_LeftArm: + case CustomModelAnimType_LeftArmX: /* TODO: we're using 2 different rotation orders here */ Models.Rotation = ROTATE_ORDER_XZY; - *rotX += e->Anim.LeftArmX; - *rotZ += e->Anim.LeftArmZ; - break; + return e->Anim.LeftArmX; - case CustomModelAnimType_RightArm: + case CustomModelAnimType_LeftArmZ: Models.Rotation = ROTATE_ORDER_XZY; - *rotX += e->Anim.RightArmX; - *rotZ += e->Anim.RightArmZ; - break; + return e->Anim.LeftArmZ; + case CustomModelAnimType_RightArmX: + Models.Rotation = ROTATE_ORDER_XZY; + return e->Anim.RightArmX; + + case CustomModelAnimType_RightArmZ: + Models.Rotation = ROTATE_ORDER_XZY; + return e->Anim.RightArmZ; + + /* + a: speed + b: shift pos + */ case CustomModelAnimType_Spin: + return (float)Game.Time * anim->a + anim->b; + case CustomModelAnimType_SpinVelocity: + return e->Anim.WalkTime * anim->a + anim->b; + + /* + a: speed + b: width + c: shift cycle + d: shift pos + */ case CustomModelAnimType_SinRotate: - case CustomModelAnimType_SinRotateVelocity: case CustomModelAnimType_SinTranslate: + return ( Math_SinF((float)Game.Time * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; + + case CustomModelAnimType_SinRotateVelocity: case CustomModelAnimType_SinTranslateVelocity: - - switch (anim->type) { - /* - a: speed - b: shift pos - */ - case CustomModelAnimType_Spin: - value = (float)Game.Time * anim->a + anim->b; - break; - - case CustomModelAnimType_SpinVelocity: - value = e->Anim.WalkTime * anim->a + anim->b; - break; - - /* - a: speed - b: width - c: shift cycle - d: shift pos - */ - case CustomModelAnimType_SinRotate: - case CustomModelAnimType_SinTranslate: - value = ( Math_SinF((float)Game.Time * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; - break; - - case CustomModelAnimType_SinRotateVelocity: - case CustomModelAnimType_SinTranslateVelocity: - value = ( Math_SinF(e->Anim.WalkTime * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; - break; - } - - switch (anim->type) { - case CustomModelAnimType_Spin: - case CustomModelAnimType_SpinVelocity: - case CustomModelAnimType_SinRotate: - case CustomModelAnimType_SinRotateVelocity: - switch (anim->axis) { - case CustomModelAnimAxis_X: - *rotX += value; - break; - - case CustomModelAnimAxis_Y: - *rotY += value; - break; - - case CustomModelAnimAxis_Z: - *rotZ += value; - break; - } - break; - - case CustomModelAnimType_SinTranslate: - case CustomModelAnimType_SinTranslateVelocity: - if (!*modifiedVertices) { - *modifiedVertices = true; - Mem_Copy( - oldVertices, - &cm->model.vertices[part->modelPart.offset], - sizeof(struct ModelVertex) * MODEL_BOX_VERTICES - ); - } - - for (i = 0; i < MODEL_BOX_VERTICES; i++) { - switch (anim->axis) { - case CustomModelAnimAxis_X: - cm->model.vertices[part->modelPart.offset + i].X += value; - break; - - case CustomModelAnimAxis_Y: - cm->model.vertices[part->modelPart.offset + i].Y += value; - break; - - case CustomModelAnimAxis_Z: - cm->model.vertices[part->modelPart.offset + i].Z += value; - break; - } - } - break; - } - break; + return ( Math_SinF(e->Anim.WalkTime * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; } } @@ -697,6 +624,7 @@ static void CustomModel_DrawPart( float rotX, rotY, rotZ; cc_bool head = false; cc_bool modifiedVertices = false; + float value = 0.0f; if (part->fullbright) { for (i = 0; i < FACE_COUNT; i++) { @@ -714,7 +642,57 @@ static void CustomModel_DrawPart( for (animIndex = 0; animIndex < MAX_CUSTOM_MODEL_ANIMS; animIndex++) { struct CustomModelAnim* anim = &part->anims[animIndex]; - CustomModel_ApplyAnimation(anim, part, cm, e, &head, &modifiedVertices, &rotX, &rotY, &rotZ); + if (anim->type == CustomModelAnimType_None) { + continue; + } + + value = CustomModel_GetAnimationValue(anim, part, cm, e); + + if (anim->type == CustomModelAnimType_Head) { + head = true; + } else if ( + anim->type == CustomModelAnimType_SinTranslate || + anim->type == CustomModelAnimType_SinTranslateVelocity + ) { + if (!modifiedVertices) { + modifiedVertices = true; + Mem_Copy( + oldVertices, + &cm->model.vertices[part->modelPart.offset], + sizeof(struct ModelVertex) * MODEL_BOX_VERTICES + ); + } + + for (i = 0; i < MODEL_BOX_VERTICES; i++) { + switch (anim->axis) { + case CustomModelAnimAxis_X: + cm->model.vertices[part->modelPart.offset + i].X += value; + break; + + case CustomModelAnimAxis_Y: + cm->model.vertices[part->modelPart.offset + i].Y += value; + break; + + case CustomModelAnimAxis_Z: + cm->model.vertices[part->modelPart.offset + i].Z += value; + break; + } + } + } else { + switch (anim->axis) { + case CustomModelAnimAxis_X: + rotX += value; + break; + + case CustomModelAnimAxis_Y: + rotY += value; + break; + + case CustomModelAnimAxis_Z: + rotZ += value; + break; + } + } } if (rotX || rotY || rotZ || head) { diff --git a/src/Model.h b/src/Model.h index 639afedd1..ba87fa6d2 100644 --- a/src/Model.h +++ b/src/Model.h @@ -216,16 +216,18 @@ CC_API void BoxDesc_ZQuad2(struct Model* m, float x1, float x2, float y1, float enum CustomModelAnimType { CustomModelAnimType_None = 0, CustomModelAnimType_Head = 1, - CustomModelAnimType_LeftLeg = 2, - CustomModelAnimType_RightLeg = 3, - CustomModelAnimType_LeftArm = 4, - CustomModelAnimType_RightArm = 5, - CustomModelAnimType_Spin = 6, - CustomModelAnimType_SpinVelocity = 7, - CustomModelAnimType_SinRotate = 8, - CustomModelAnimType_SinRotateVelocity = 9, - CustomModelAnimType_SinTranslate = 10, - CustomModelAnimType_SinTranslateVelocity = 11 + CustomModelAnimType_LeftLegX = 2, + CustomModelAnimType_RightLegX = 3, + CustomModelAnimType_LeftArmX = 4, + CustomModelAnimType_LeftArmZ = 5, + CustomModelAnimType_RightArmX = 6, + CustomModelAnimType_RightArmZ = 7, + CustomModelAnimType_Spin = 8, + CustomModelAnimType_SpinVelocity = 9, + CustomModelAnimType_SinRotate = 10, + CustomModelAnimType_SinRotateVelocity = 11, + CustomModelAnimType_SinTranslate = 12, + CustomModelAnimType_SinTranslateVelocity = 13 }; enum CustomModelAnimAxis { From 21b97d2dc47db79f7c306527499bb75013cd106a Mon Sep 17 00:00:00 2001 From: SpiralP Date: Tue, 7 Jul 2020 00:41:07 -0700 Subject: [PATCH 5/6] oop --- src/Model.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Model.c b/src/Model.c index 187d23113..bfc7f64a9 100644 --- a/src/Model.c +++ b/src/Model.c @@ -559,9 +559,6 @@ static float CustomModel_GetAnimationValue( struct Entity* e ) { switch (anim->type) { - case CustomModelAnimType_None: - return; - case CustomModelAnimType_Head: return -e->Pitch * MATH_DEG2RAD; @@ -612,6 +609,8 @@ static float CustomModel_GetAnimationValue( case CustomModelAnimType_SinTranslateVelocity: return ( Math_SinF(e->Anim.WalkTime * anim->a + 2 * MATH_PI * anim->c) + anim->d ) * anim->b; } + + return 0.0f; } static PackedCol oldCols[FACE_COUNT]; From 2faeccd5e83455121cf3a0da9ec9057d62eafafe Mon Sep 17 00:00:00 2001 From: SpiralP Date: Tue, 7 Jul 2020 01:28:50 -0700 Subject: [PATCH 6/6] fix head --- src/Model.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Model.c b/src/Model.c index bfc7f64a9..62de9bf49 100644 --- a/src/Model.c +++ b/src/Model.c @@ -647,9 +647,7 @@ static void CustomModel_DrawPart( value = CustomModel_GetAnimationValue(anim, part, cm, e); - if (anim->type == CustomModelAnimType_Head) { - head = true; - } else if ( + if ( anim->type == CustomModelAnimType_SinTranslate || anim->type == CustomModelAnimType_SinTranslateVelocity ) { @@ -678,6 +676,10 @@ static void CustomModel_DrawPart( } } } else { + if (anim->type == CustomModelAnimType_Head) { + head = true; + } + switch (anim->axis) { case CustomModelAnimAxis_X: rotX += value;