/** * 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 Quat_rotateShortPoint (const quat4_t q, const vec3short_t in, vec3_t out) { vec3_t inFloat; inFloat[0] = in[0]; inFloat[1] = in[1]; inFloat[2] = in[2]; Quat_rotatePoint(q,inFloat,out); }
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++; } }
/** * Prepare a mesh for drawing. Compute mesh's final vertex positions * given a skeleton. Put the vertices in vertex arrays. */ static void PrepareMesh(const struct md5_mesh_part *mesh, const struct md5_joint_t *skeleton) { unsigned int i, j, k; /* Setup vertex indices */ for (k = 0, i = 0; i < mesh->num_tris; ++i) { for (j = 0; j < 3; ++j, ++k) vertexIndices[k] = mesh->triangles[i].index[j]; } /* Setup vertices */ for (i = 0; i < mesh->num_verts; ++i) { vec3_t vert = { 0.0f, 0.0f, 0.0f }; vec3_t norm = { 0.0f, 0.0f, 0.0f }; // vec3_t tan = { 0.0f, 0.0f, 0.0f }; /* Calculate final vertex to draw with weights */ for (j = 0; j < mesh->vertices[i].count; ++j) { const struct md5_weight_t *weight = &mesh->weights[mesh->vertices[i].start + j]; const struct md5_joint_t *joint = &skeleton[weight->joint]; vec3_t wv; /* Calculate transformed vertex for this weight */ Quat_rotatePoint(joint->orient, weight->pos, wv); /* The sum of all weight->bias should be 1.0 */ vert[0] += (joint->pos[0] + wv[0]) * weight->bias; vert[1] += (joint->pos[1] + wv[1]) * weight->bias; vert[2] += (joint->pos[2] + wv[2]) * weight->bias; Quat_rotatePoint(joint->orient, weight->normal, wv); v_scale(wv, weight->bias); v_add(norm, norm, wv); // Quat_rotatePoint(joint->orient, weight->tangent, wv); // v_scale(wv, weight->bias); // v_add(tan, tan, wv); } v_normalize(norm); // v_normalize(tan); vertexArray[i][0] = vert[0]; vertexArray[i][1] = vert[1]; vertexArray[i][2] = vert[2]; normalArray[i][0] = norm[0]; normalArray[i][1] = norm[1]; normalArray[i][2] = norm[2]; #if 0 tangentArray[i][0] = tan[0]; tangentArray[i][1] = tan[1]; tangentArray[i][2] = tan[2]; #endif texArray[i][0] = mesh->vertices[i].st[0]; texArray[i][1] = 1.0f - mesh->vertices[i].st[1]; } }
/** * Prepare a mesh for drawing. Compute mesh's final vertex positions * given a skeleton. Put the vertices in vertex arrays. */ void PrepareMesh (const struct md5_mesh_t *mesh, const struct md5_joint_t *skeleton) { int i, j, k; /* Setup vertex indices */ for (k = 0, i = 0; i < mesh->num_tris; ++i) { for (j = 0; j < 3; ++j, ++k) vertexIndices[k] = mesh->triangles[i].index[j]; } /* Setup texcoords */ for (i = 0; i < mesh->num_verts; ++i) { texCoords[i*2] = mesh->vertices[i].st[0]; texCoords[i*2+1] = 1-mesh->vertices[i].st[1]; } /* Setup vertices */ for (i = 0; i < mesh->num_verts; ++i) { vec3_t finalVertex = { 0.0f, 0.0f, 0.0f }; /* Calculate final vertex to draw with weights */ for (j = 0; j < mesh->vertices[i].count; ++j) { const struct md5_weight_t *weight = &mesh->weights[mesh->vertices[i].start + j]; const struct md5_joint_t *joint = &skeleton[weight->joint]; /* Calculate transformed vertex for this weight */ vec3_t wv; Quat_rotatePoint (joint->orient, weight->pos, wv); /* The sum of all weight->bias should be 1.0 */ finalVertex[0] += (joint->pos[0] + wv[0]) * weight->bias; finalVertex[1] += (joint->pos[1] + wv[1]) * weight->bias; finalVertex[2] += (joint->pos[2] + wv[2]) * weight->bias; } vertexArray[i][0] = finalVertex[0]; vertexArray[i][1] = finalVertex[1]; vertexArray[i][2] = finalVertex[2]; } /* Setup normals */ for (k = 0, i = 0; i < mesh->num_tris; ++i) { vec3_t *v[3]; for (j = 0; j < 3; ++j, ++k) { v[j] = &vertexArray[ mesh->triangles[i].index[j] ]; } vec3_t in1; vec3_t in2; in1[0] = (*v[1])[0] - (*v[0])[0]; in1[1] = (*v[1])[1] - (*v[0])[1]; in1[2] = (*v[1])[2] - (*v[0])[2]; in2[0] = (*v[2])[0] - (*v[0])[0]; in2[1] = (*v[2])[1] - (*v[0])[1]; in2[2] = (*v[2])[2] - (*v[0])[2]; vec3_CrossProduct(in2, in1, &normals[i*3]); float x = normals[i*3][0]; float y = normals[i*3][1]; float z = normals[i*3][2]; float len = sqrt( x * x + y * y + z * z ); len = (len != 0.0f ? len : 1.0f); float lengthMul = 1.0f / len; x *= lengthMul; y *= lengthMul; z *= lengthMul; normals[i*3][0] = x; normals[i*3][1] = y; normals[i*3][2] = z; normals[i*3+1][0] = normals[i*3][0]; normals[i*3+1][1] = normals[i*3][1]; normals[i*3+1][2] = normals[i*3][2]; normals[i*3+2][0] = normals[i*3][0]; normals[i*3+2][1] = normals[i*3][1]; normals[i*3+2][2] = normals[i*3][2]; } }