⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 physique.cpp

📁 Cal3D实现虚拟角色 Cal3D实现虚拟角色
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  int vertexId;  for(vertexId = 0; vertexId < vertexCount; vertexId++)  {    CalCoreSubmesh::TangentSpace& tangentSpace = vectorTangentSpace[vertexId];    // get the vertex    CalCoreSubmesh::Vertex& vertex = vectorVertex[vertexId];    // initialize tangent    float tx, ty, tz;    tx = 0.0f;    ty = 0.0f;    tz = 0.0f;    // blend together all vertex influences    int influenceId;    int influenceCount=(int)vertex.vectorInfluence.size();    for(influenceId = 0; influenceId < influenceCount; influenceId++)    {      // get the influence      CalCoreSubmesh::Influence& influence = vertex.vectorInfluence[influenceId];      // get the bone of the influence vertex      CalBone *pBone;      pBone = vectorBone[influence.boneId];      // transform normal with current state of the bone      CalVector v(tangentSpace.tangent);      v *= pBone->getTransformMatrix();       tx += influence.weight * v.x;      ty += influence.weight * v.y;      tz += influence.weight * v.z;    }    // re-normalize tangent if necessary    if (m_Normalize)    {      float scale;	  tx/= m_axisFactorX;	  ty/= m_axisFactorY;	  tz/= m_axisFactorZ;      scale = (float)( 1.0f / sqrt(tx * tx + ty * ty + tz * tz));      pTangentSpaceBuffer[0] = tx * scale;      pTangentSpaceBuffer[1] = ty * scale;      pTangentSpaceBuffer[2] = tz * scale;	      }    else    {      pTangentSpaceBuffer[0] = tx;      pTangentSpaceBuffer[1] = ty;      pTangentSpaceBuffer[2] = tz;    }    pTangentSpaceBuffer[3] = tangentSpace.crossFactor;    // next vertex position in buffer    pTangentSpaceBuffer = (float *)(((char *)pTangentSpaceBuffer) + stride) ;  }  return vertexCount;} /*****************************************************************************//** Calculates the transformed normal data.  *  * This function calculates and returns the transformed normal data of a  * specific submesh.  *  * @param pSubmesh A pointer to the submesh from which the normal data should  *                 be calculated and returned.  * @param pNormalBuffer A pointer to the user-provided buffer where the normal  *                      data is written to.  *  * @return The number of normals written to the buffer.  *****************************************************************************/int CalPhysique::calculateNormals(CalSubmesh *pSubmesh, float *pNormalBuffer, int stride){  if(stride <= 0)  {	  stride = 3*sizeof(float);  }  // get bone vector of the skeleton  std::vector<CalBone *>& vectorBone = m_pModel->getSkeleton()->getVectorBone();  // get vertex vector of the submesh  std::vector<CalCoreSubmesh::Vertex>& vectorVertex = pSubmesh->getCoreSubmesh()->getVectorVertex();  // get the number of vertices  int vertexCount;  vertexCount = pSubmesh->getVertexCount();  // get the sub morph target vector from the core sub mesh  std::vector<CalCoreSubMorphTarget*>& vectorSubMorphTarget =  pSubmesh->getCoreSubmesh()->getVectorCoreSubMorphTarget();  // calculate the base weight  float baseWeight = pSubmesh->getBaseWeight();  // get the number of morph targets  int morphTargetCount = pSubmesh->getMorphTargetWeightCount();  // calculate normal for all submesh vertices  int vertexId;  for(vertexId = 0; vertexId < vertexCount; ++vertexId)  {    // get the vertex    CalCoreSubmesh::Vertex& vertex = vectorVertex[vertexId];    // blend the morph targets    CalVector normal(0,0,0);    if(baseWeight == 1.0f)    {      normal.x = vertex.normal.x;      normal.y = vertex.normal.y;      normal.z = vertex.normal.z;    }    else    {      normal.x = baseWeight*vertex.normal.x;      normal.y = baseWeight*vertex.normal.y;      normal.z = baseWeight*vertex.normal.z;      int morphTargetId;      for(morphTargetId=0; morphTargetId < morphTargetCount;++morphTargetId)      {        CalCoreSubMorphTarget::BlendVertex& blendVertex =         vectorSubMorphTarget[morphTargetId]->getVectorBlendVertex()[vertexId];        float currentWeight = pSubmesh->getMorphTargetWeight(morphTargetId);        normal.x += currentWeight*blendVertex.normal.x;        normal.y += currentWeight*blendVertex.normal.y;        normal.z += currentWeight*blendVertex.normal.z;      }    }    // initialize normal    float nx, ny, nz;    nx = 0.0f;    ny = 0.0f;    nz = 0.0f;    // blend together all vertex influences    int influenceId;	int influenceCount=(int)vertex.vectorInfluence.size();    if(influenceCount == 0) 	{      nx = normal.x;      ny = normal.y;      nz = normal.z;    } 	else 	{		for(influenceId = 0; influenceId < influenceCount; ++influenceId)		{			// get the influence			CalCoreSubmesh::Influence& influence = vertex.vectorInfluence[influenceId];						// get the bone of the influence vertex			CalBone *pBone;			pBone = vectorBone[influence.boneId];						// transform normal with current state of the bone			CalVector v(normal);			v *= pBone->getTransformMatrix(); 						nx += influence.weight * v.x;			ny += influence.weight * v.y;			nz += influence.weight * v.z;		}	}    // re-normalize normal if necessary    if (m_Normalize)    {	  nx/= m_axisFactorX;	  ny/= m_axisFactorY;	  nz/= m_axisFactorZ;      float scale;      scale = (float)( 1.0f / sqrt(nx * nx + ny * ny + nz * nz));      pNormalBuffer[0] = nx * scale;      pNormalBuffer[1] = ny * scale;      pNormalBuffer[2] = nz * scale;    }    else    {      pNormalBuffer[0] = nx;      pNormalBuffer[1] = ny;      pNormalBuffer[2] = nz;    }     // next vertex position in buffer    pNormalBuffer = (float *)(((char *)pNormalBuffer) + stride) ;  }  return vertexCount;} /*****************************************************************************//** Calculates the transformed vertex data.  *  * This function calculates and returns the transformed vertex and the transformed   * normal datadata of a specific submesh.  *  * @param pSubmesh A pointer to the submesh from which the vertex data should  *                 be calculated and returned.  * @param pVertexBuffer A pointer to the user-provided buffer where the vertex  *                      data is written to.  *  * @return The number of vertices written to the buffer.  *****************************************************************************/int CalPhysique::calculateVerticesAndNormals(CalSubmesh *pSubmesh, float *pVertexBuffer, int stride){  if(stride <= 0)  {	  stride = 6*sizeof(float);  }  // get bone vector of the skeleton  std::vector<CalBone *>& vectorBone = m_pModel->getSkeleton()->getVectorBone();  // get vertex vector of the core submesh  std::vector<CalCoreSubmesh::Vertex>& vectorVertex = pSubmesh->getCoreSubmesh()->getVectorVertex();  // get physical property vector of the core submesh  std::vector<CalCoreSubmesh::PhysicalProperty>& vectorPhysicalProperty = pSubmesh->getCoreSubmesh()->getVectorPhysicalProperty();  // get the number of vertices  int vertexCount;  vertexCount = pSubmesh->getVertexCount();  // get the sub morph target vector from the core sub mesh  std::vector<CalCoreSubMorphTarget*>& vectorSubMorphTarget =  pSubmesh->getCoreSubmesh()->getVectorCoreSubMorphTarget();  // calculate the base weight  float baseWeight = pSubmesh->getBaseWeight();  // get the number of morph targets  int morphTargetCount = pSubmesh->getMorphTargetWeightCount();  // calculate all submesh vertices  int vertexId;  for(vertexId = 0; vertexId < vertexCount; ++vertexId)  {    // get the vertex    CalCoreSubmesh::Vertex& vertex = vectorVertex[vertexId];    // blend the morph targets    CalVector position(0,0,0);    CalVector normal(0,0,0);    if(baseWeight == 1.0f)    {      position.x = vertex.position.x;      position.y = vertex.position.y;      position.z = vertex.position.z;      normal.x = vertex.normal.x;      normal.y = vertex.normal.y;      normal.z = vertex.normal.z;    }    else    {      position.x = baseWeight*vertex.position.x;      position.y = baseWeight*vertex.position.y;      position.z = baseWeight*vertex.position.z;      normal.x = baseWeight*vertex.normal.x;      normal.y = baseWeight*vertex.normal.y;      normal.z = baseWeight*vertex.normal.z;      int morphTargetId;      for(morphTargetId=0; morphTargetId < morphTargetCount;++morphTargetId)      {        CalCoreSubMorphTarget::BlendVertex& blendVertex =        vectorSubMorphTarget[morphTargetId]->getVectorBlendVertex()[vertexId];        float currentWeight = pSubmesh->getMorphTargetWeight(morphTargetId);        position.x += currentWeight*blendVertex.position.x;        position.y += currentWeight*blendVertex.position.y;        position.z += currentWeight*blendVertex.position.z;        normal.x += currentWeight*blendVertex.normal.x;        normal.y += currentWeight*blendVertex.normal.y;        normal.z += currentWeight*blendVertex.normal.z;      }    }    // initialize vertex    float x, y, z;    x = 0.0f;    y = 0.0f;    z = 0.0f;	// initialize normal    float nx, ny, nz;    nx = 0.0f;    ny = 0.0f;    nz = 0.0f;    // blend together all vertex influences    int influenceId;	int influenceCount=(int)vertex.vectorInfluence.size();    if(influenceCount == 0) 	{      x = position.x;      y = position.y;      z = position.z;      nx = normal.x;      ny = normal.y;      nz = normal.z;    } 	else 	{		for(influenceId = 0; influenceId < influenceCount; ++influenceId)		{			// get the influence			CalCoreSubmesh::Influence& influence = vertex.vectorInfluence[influenceId];						// get the bone of the influence vertex			CalBone *pBone;			pBone = vectorBone[influence.boneId];						// transform vertex with current state of the bone			CalVector v(position);			v *= pBone->getTransformMatrix();			v += pBone->getTranslationBoneSpace();						x += influence.weight * v.x;			y += influence.weight * v.y;			z += influence.weight * v.z;						// transform normal with current state of the bone			CalVector n(normal);			n *= pBone->getTransformMatrix();						nx += influence.weight * n.x;			ny += influence.weight * n.y;			nz += influence.weight * n.z;		}	}    // save vertex position    if(pSubmesh->getCoreSubmesh()->getSpringCount() > 0 && pSubmesh->hasInternalData())    {      // get the pgysical property of the vertex

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -