# how can i add “vertex normal” these md5anim model codes

i am trying to do skeletal animation for adding my project. i added one tutorial but there is no vertex normal. Therefore i can’t make lighting,bumpMapping etc. how can i add vertex normals into these codes.

i am beginner for this reason can you write code for us

tutorial doesn’t use “glm” for math process and doesn’t use “assimp” to read.

i will show you suspected parts of tutorial if these parts not enough i can show all code but this way will be too long. sorry for bad english and thank you too much for your interest

MD5MODEL.h

struct ogl_vertex_t
{
float3 position;
float2 texCoord;
float4 indices;
float4 weights;
};

struct ogl_mesh_t
{
std::string texture;
ogl_vertex_t* vertexArray;
unsigned int* indexArray;
unsigned int numVertices;
unsigned int numIndices;
};

struct md5_vertex_t
{
float2 texCoord;
int startWeight;
int numWeights;
};

MD5MODEL.cpp

void MD5Model::BuildBindPose()
{
mat4 rotation, rotationInv, translationInv, finalmatrix;
for(int i = 0; i < m_iNumJoints; i++)
{
md5_joint_t& joint = m_pJointArray[i];
mlQuaternionToMat4(rotation, joint.orientation);
mlTransposeMat4(rotationInv, rotation);
mlTranslationMat4(translationInv, -joint.position[0], - joint.position[1], -joint.position[2]);

``````    mlMultiplyMat4_2(finalmatrix, rotationInv, translationInv);
memcpy(m_pInvBindPose[i], finalmatrix, sizeof(mat4));
}
``````

}

void MD5Model::ComputeVertexPositionsFromWeights(md5_mesh_t* mesh, ogl_mesh_t* finalmesh)
{
for(int i = 0; i < mesh->numverts; i++)
{
md5_vertex_t& vert = mesh->vertexArray[i];
ogl_vertex_t& finalvert = finalmesh->vertexArray[i];
memset(finalvert.position, 0, sizeof(float3));
memcpy(finalvert.texCoord, vert.texCoord, sizeof(float2));
memset(finalvert.indices, 0, sizeof(float4));
memset(finalvert.weights, 0, sizeof(float4));

``````    for(int j = 0; j &lt; vert.numWeights; j++)
{
md5_weight_t& weight = mesh-&gt;weightArray[vert.startWeight + j];
md5_joint_t& joint = m_pJointArray[weight.jointId];
float3 rotpos;
mlRotateVec3ByQuaternion(rotpos, joint.orientation, weight.position);
``````
``````        finalvert.position[0] += (joint.position[0] + rotpos[0]) * weight.bias;
finalvert.position[1] += (joint.position[1] + rotpos[1]) * weight.bias;
finalvert.position[2] += (joint.position[2] + rotpos[2]) * weight.bias;
finalvert.indices[j] = weight.jointId;
finalvert.weights[j] = weight.bias;
}
}
``````

}

bool MD5Model::PrepareMeshes()
{
for(int i = 0; i < m_iNumMeshes; i++)
{
md5_mesh_t& mesh = m_pMeshArray[i];
ogl_mesh_t& finalmesh = m_pFinalMeshArray[i];

``````    finalmesh.texture = mesh.shader;
finalmesh.numVertices = mesh.numverts;
finalmesh.numIndices = mesh.numtris * 3;
``````
``````    finalmesh.vertexArray = new ogl_vertex_t[finalmesh.numVertices];
if(!finalmesh.vertexArray)
return false;
``````
``````    finalmesh.indexArray = new unsigned int[finalmesh.numIndices];
if(!finalmesh.indexArray)
return false;
``````
``````    ComputeVertexPositionsFromWeights(&mesh, &finalmesh);
``````
``````    for(int j = 0; j &lt; mesh.numtris; j++)
{
finalmesh.indexArray[j * 3 + 0] = mesh.triangleArray[j].indices[0];
finalmesh.indexArray[j * 3 + 1] = mesh.triangleArray[j].indices[1];
finalmesh.indexArray[j * 3 + 2] = mesh.triangleArray[j].indices[2];
}
}
``````
``````return true;
``````

}

i think i started add “float3 normal” inside “struct ogl_vertex_t” after i have no idea

and please try to use this code because tutorial used them and therefore i could do only with these. don’t use “glm”

void mlCrossVec3(float* out, float* a, float* b)
{
out[X] = a[Y] * b[Z] - a[Z] * b[Y];
out[Y] = a[Z] * b[X] - a[X] * b[Z];
out[Z] = a[X] * b[Y] - a[Y] * b[X];
}

void mlNormalizeVec3(float* out, float* a)
{
float invLength = 1.0f / mlLengthVec3(a);

``````out[0] = a[0] * invLength;
out[1] = a[1] * invLength;
out[2] = a[2] * invLength;
``````

}

i hope these enough for this process