Skip to content

Instantly share code, notes, and snippets.

@TiliSleepStealer
Created July 21, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TiliSleepStealer/680692725a531137b019 to your computer and use it in GitHub Desktop.
Save TiliSleepStealer/680692725a531137b019 to your computer and use it in GitHub Desktop.
Matrix4 Md2Model::GetTrianglePos(int frameA, int frameB, float interp, int index, const Vector3& localOffset, const Vector3& localRot) {
Vector3 tri[3];
if(frameA == frameB) {
// not animating
Md2Frame_t *pFrame = &_frames[frameA];
for(int j = 0; j < 3; ++j) {
Md2Vertex_t *pVert = &pFrame->verts[_triangles[index].vertex[j]];
tri[j] = Vector3((pFrame->scale[0] * pVert->v[0] + pFrame->translate[0]) * _scale,
(pFrame->scale[1] * pVert->v[1] + pFrame->translate[1]) * _scale,
(pFrame->scale[2] * pVert->v[2] + pFrame->translate[2]) * _scale);
tri[j] = tri[j] * worldTranform;
tri[j] += worldPos;
}
} else {
for(int j = 0; j < 3; ++j) {
Md2Frame_t *pFrameA = &_frames[frameA];
Md2Frame_t *pFrameB = &_frames[frameB];
Md2Vertex_t *pVertA = &pFrameA->verts[_triangles[index].vertex[j]];
Md2Vertex_t *pVertB = &pFrameB->verts[_triangles[index].vertex[j]];
// Compute final vertex position
vec3_t vecA, vecB;
// First, uncompress vertex positions
vecA[0] = pFrameA->scale[0] * pVertA->v[0] + pFrameA->translate[0];
vecA[1] = pFrameA->scale[1] * pVertA->v[1] + pFrameA->translate[1];
vecA[2] = pFrameA->scale[2] * pVertA->v[2] + pFrameA->translate[2];
vecB[0] = pFrameB->scale[0] * pVertB->v[0] + pFrameB->translate[0];
vecB[1] = pFrameB->scale[1] * pVertB->v[1] + pFrameB->translate[1];
vecB[2] = pFrameB->scale[2] * pVertB->v[2] + pFrameB->translate[2];
// Linear interpolation and scaling
Vector3 temp((vecA[0] + interp * (vecB[0] - vecA[0])) * _scale,
(vecA[1] + interp * (vecB[1] - vecA[1])) * _scale,
(vecA[2] + interp * (vecB[2] - vecA[2])) * _scale);
temp = temp * worldTranform;
temp += worldPos;
tri[j] = temp;
}
}
Vector3 u(tri[1] - tri[0]);
Vector3 v(tri[2] - tri[0]);
Vector3 normal = Vector3::cross(u.normalize(), v.normalize()).normalize();
Vector3 tangent, bitangent;
Vector3 triPos = (tri[0] + tri[1] + tri[2]) / 3.0f;
// find tangent
tangent = Vector3::cross(u, normal).normalize();
bitangent = Vector3::cross(tangent, normal);
return Matrix4::createFromHeadPitchRoll(localRot.x, localRot.y, localRot.z) * Matrix4::createTranslate(localOffset.x, localOffset.y, localOffset.z) * Matrix4::createFromAxes(tangent, bitangent, normal) * Matrix4::createTranslate(triPos.x, triPos.y, triPos.z);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment