/** * Prepare a mesh for drawing. Compute mesh's final vertex positions * given a skeleton. Put the vertices in vertex arrays. */ void GenerateGPUVertices (md5_mesh_t *mesh, const md5_joint_t *skeleton) { int i, j; const md5_weight_t *weight; const md5_joint_t *joint ; vec3_t tmpNormal,tmpVertex; vec3_t normalAccumulator; #ifdef TANGENT_ENABLED vec3_t tmpTangent; vec3_t tangentAccumulator; #endif /* Setup vertices */ vertex_t* currentVertex = mesh->vertexArray ; for (i = 0; i < mesh->num_verts; ++i) { vectorClear(currentVertex->pos); vectorClear(normalAccumulator); #ifdef TANGENT_ENABLED vectorClear(tangentAccumulator); #endif // Calculate final vertex to draw with weights for (j = 0; j < mesh->vertices[i].count; j++) { weight= &mesh->weights[mesh->vertices[i].start + j]; joint = &skeleton[weight->joint]; // Calculate transformed vertex for this weight Quat_rotatePoint (joint->orient, weight->pos, tmpVertex); currentVertex->pos[0] += (joint->pos[0] + tmpVertex[0]) * weight->bias; currentVertex->pos[1] += (joint->pos[1] + tmpVertex[1]) * weight->bias; currentVertex->pos[2] += (joint->pos[2] + tmpVertex[2]) * weight->bias; // Same thing for normal Quat_rotateShortPoint (joint->orient, weight->normal, tmpNormal); vectorAdd(normalAccumulator,tmpNormal,normalAccumulator); #ifdef TANGENT_ENABLED Quat_rotateShortPoint (joint->orient, weight->tangent, tmpTangent); vectorAdd(tangentAccumulator,tmpTangent,tangentAccumulator); #endif } //Need to normalize normal normalize(normalAccumulator); vectorScale(normalAccumulator,32767,currentVertex->normal); #ifdef TANGENT_ENABLED normalize(tangentAccumulator); vectorScale(tangentAccumulator,32767,currentVertex->tangent); #endif currentVertex++; } }
void MD5_GenerateSkin (md5_mesh_t* mesh, md5_bone_t* bones) { int i, j; md5_weight_t* weight; md5_bone_t* bone ; vec3_t normalAccumulator; vec3_t tangentAccumulator; vertex_t* currentVertex; //printf("\nGenerating weight positions.\n"); // Generate weight position in modelSpace weight = mesh->weights; for (i = 0; i < mesh->numWeights; i++,weight++) { bone = &bones[weight->boneId]; Quat_rotatePoint (bone->orientation, weight->boneSpacePos, weight->modelSpacePos); vectorAdd(weight->modelSpacePos,bone->position,weight->modelSpacePos); // printf("weight[%d].pos=[%.2f,%.2f,%.2f]\n",i,bone->position[0],bone->position[1],bone->position[2]); // printf("weight[%d].pos=[%.2f,%.2f,%.2f]\n",i,weight->modelSpacePos[0],weight->modelSpacePos[1],weight->modelSpacePos[2]); Quat_rotateShortPoint (bone->orientation, weight->boneSpaceNormal, weight->modelSpaceNormal); Quat_rotateShortPoint (bone->orientation, weight->boneSpaceTangent,weight->modelSpaceTangent); } /* Setup vertices */ currentVertex = mesh->vertexArray ; for (i = 0; i < mesh->numVertices; ++i) { vectorClear(currentVertex->pos); vectorClear(normalAccumulator); vectorClear(tangentAccumulator); // Calculate final vertex to draw with weights for (j = 0; j < mesh->vertices[i].count; j++) { weight= &mesh->weights[mesh->vertices[i].start + j]; bone = &bones[weight->boneId]; // Calculate transformed vertex for this weight currentVertex->pos[0] += weight->modelSpacePos[0] * weight->bias; currentVertex->pos[1] += weight->modelSpacePos[1] * weight->bias; currentVertex->pos[2] += weight->modelSpacePos[2] * weight->bias; // Same thing for normal vectorAdd(normalAccumulator,weight->modelSpaceNormal,normalAccumulator); vectorAdd(tangentAccumulator,weight->modelSpaceTangent,tangentAccumulator); } // printf("currentVertex[%d].pos=[%.2f,%.2f,%.2f]\n",i,currentVertex->pos[0],currentVertex->pos[1],currentVertex->pos[2]); //Need to normalize normal normalize(normalAccumulator); vectorScale(normalAccumulator,32767,currentVertex->normal); // printf("currentVertex[%d].normal=[%hu,%hu,%hu]\n",i,currentVertex->normal[0],currentVertex->normal[1],currentVertex->normal[2]); normalize(tangentAccumulator); vectorScale(tangentAccumulator,32767,currentVertex->tangent); // printf("currentVertex[%d].tangent=[%hu,%hu,%hu]\n",i,currentVertex->tangent[0],currentVertex->tangent[1],currentVertex->tangent[2]); currentVertex++; } }