Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include "studiorendercontext.h"
  9. #include "optimize.h"
  10. #include "tier0/vprof.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. void CStudioRenderContext::GetTriangles( const DrawModelInfo_t& info, matrix3x4_t *pBoneToWorld, GetTriangles_Output_t &out )
  14. {
  15. VPROF( "CStudioRender::GetTriangles");
  16. out.m_MaterialBatches.RemoveAll(); // clear out data.
  17. if( !info.m_pStudioHdr || !info.m_pHardwareData ||
  18. !info.m_pHardwareData->m_NumLODs || !info.m_pHardwareData->m_pLODs )
  19. {
  20. return;
  21. }
  22. int lod = info.m_Lod;
  23. int lastlod = info.m_pHardwareData->m_NumLODs - 1;
  24. if ( lod == USESHADOWLOD )
  25. {
  26. lod = lastlod;
  27. }
  28. else
  29. {
  30. lod = clamp( lod, 0, lastlod );
  31. }
  32. // clamp to root lod
  33. if ( lod < info.m_pHardwareData->m_RootLOD)
  34. {
  35. lod = info.m_pHardwareData->m_RootLOD;
  36. }
  37. int nSkin = info.m_Skin;
  38. if ( nSkin >= info.m_pStudioHdr->numskinfamilies )
  39. {
  40. nSkin = 0;
  41. }
  42. short *pSkinRef = info.m_pStudioHdr->pSkinref( nSkin * info.m_pStudioHdr->numskinref );
  43. studiomeshdata_t *pStudioMeshes = info.m_pHardwareData->m_pLODs[lod].m_pMeshData;
  44. IMaterial **ppMaterials = info.m_pHardwareData->m_pLODs[lod].ppMaterials;
  45. // Bone to world must be set before calling this function; it uses it here
  46. int boneMask = BONE_USED_BY_VERTEX_AT_LOD(lod);
  47. ComputePoseToWorld( out.m_PoseToWorld, info.m_pStudioHdr, boneMask, m_RC.m_ViewOrigin, pBoneToWorld );
  48. int i;
  49. for (i=0 ; i < info.m_pStudioHdr->numbodyparts ; i++)
  50. {
  51. mstudiomodel_t *pModel = NULL;
  52. R_StudioSetupModel( i, info.m_Body, &pModel, info.m_pStudioHdr );
  53. // Iterate over all the meshes.... each mesh is a new material
  54. int k;
  55. for ( k = 0; k < pModel->nummeshes; ++k )
  56. {
  57. GetTriangles_MaterialBatch_t &materialBatch = out.m_MaterialBatches[out.m_MaterialBatches.AddToTail()];
  58. mstudiomesh_t *pMesh = pModel->pMesh(k);
  59. if ( !pModel->CacheVertexData( info.m_pStudioHdr ) )
  60. {
  61. // not available yet
  62. continue;
  63. }
  64. const mstudio_meshvertexdata_t *vertData = pMesh->GetVertexData( info.m_pStudioHdr );
  65. Assert( vertData ); // This can only return NULL on X360 for now
  66. // add the verts from this mesh to the materialBatch
  67. materialBatch.m_Verts.SetCount( pMesh->numvertices );
  68. for ( int vertID = 0; vertID < pMesh->numvertices; vertID++ )
  69. {
  70. GetTriangles_Vertex_t& vert = materialBatch.m_Verts[vertID];
  71. vert.m_Position = *vertData->Position( vertID );
  72. vert.m_Normal = *vertData->Normal( vertID );
  73. vert.m_TexCoord = *vertData->Texcoord( vertID );
  74. if (vertData->HasTangentData())
  75. {
  76. vert.m_TangentS = *vertData->TangentS( vertID );
  77. }
  78. #if _DEBUG
  79. else
  80. {
  81. // ensure any unintended access faults
  82. vert.m_TangentS.Init( VEC_T_NAN, VEC_T_NAN, VEC_T_NAN, VEC_T_NAN );
  83. }
  84. #endif
  85. vert.m_NumBones = vertData->BoneWeights( vertID )->numbones;
  86. int j;
  87. for ( j = 0; j < vert.m_NumBones; j++ )
  88. {
  89. vert.m_BoneWeight[j] = vertData->BoneWeights( vertID )->weight[j];
  90. vert.m_BoneIndex[j] = vertData->BoneWeights( vertID )->bone[j];
  91. }
  92. }
  93. IMaterial *pMaterial = ppMaterials[pSkinRef[pMesh->material]];
  94. Assert( pMaterial );
  95. materialBatch.m_pMaterial = pMaterial;
  96. studiomeshdata_t *pMeshData = &pStudioMeshes[pMesh->meshid];
  97. if ( pMeshData->m_NumGroup == 0 )
  98. continue;
  99. // Clear out indices
  100. materialBatch.m_TriListIndices.SetCount( 0 );
  101. // Iterate over all stripgroups
  102. int stripGroupID;
  103. for ( stripGroupID = 0; stripGroupID < pMeshData->m_NumGroup; stripGroupID++ )
  104. {
  105. studiomeshgroup_t *pMeshGroup = &pMeshData->m_pMeshGroup[stripGroupID];
  106. // bool bIsFlexed = ( pMeshGroup->m_Flags & MESHGROUP_IS_FLEXED ) != 0;
  107. // bool bIsHWSkinned = ( pMeshGroup->m_Flags & MESHGROUP_IS_HWSKINNED ) != 0;
  108. // Iterate over all strips. . . each strip potentially changes bones states.
  109. int stripID;
  110. for ( stripID = 0; stripID < pMeshGroup->m_NumStrips; stripID++ )
  111. {
  112. OptimizedModel::StripHeader_t *pStripData = &pMeshGroup->m_pStripData[stripID];
  113. // int boneID;
  114. // for( boneID = 0; boneID < pStripData->numBoneStateChanges; boneID++ )
  115. // {
  116. // OptimizedModel::BoneStateChangeHeader_t *pBoneStateChange = pStripData->pBoneStateChange( boneID );
  117. // hardwareBoneToGlobalBone[pBoneStateChange->hardwareID] = pBoneStateChange->newBoneID;
  118. // }
  119. if ( pStripData->flags & OptimizedModel::STRIP_IS_TRILIST )
  120. {
  121. for ( int i = 0; i < pStripData->numIndices; i += 3 )
  122. {
  123. int idx = pStripData->indexOffset + i;
  124. materialBatch.m_TriListIndices.AddToTail( pMeshGroup->MeshIndex( idx ) );
  125. materialBatch.m_TriListIndices.AddToTail( pMeshGroup->MeshIndex( idx + 1 ) );
  126. materialBatch.m_TriListIndices.AddToTail( pMeshGroup->MeshIndex( idx + 2 ) );
  127. }
  128. }
  129. else
  130. {
  131. Assert( pStripData->flags & OptimizedModel::STRIP_IS_TRISTRIP );
  132. for (int i = 0; i < pStripData->numIndices - 2; ++i)
  133. {
  134. int idx = pStripData->indexOffset + i;
  135. bool ccw = (i & 0x1) == 0;
  136. materialBatch.m_TriListIndices.AddToTail( pMeshGroup->MeshIndex( idx ) );
  137. materialBatch.m_TriListIndices.AddToTail( pMeshGroup->MeshIndex( idx + 1 + ccw ) );
  138. materialBatch.m_TriListIndices.AddToTail( pMeshGroup->MeshIndex( idx + 2 - ccw ) );
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }