Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. atUint16 readAttribute(atUint16& value, atUint32 attributes, atUint32 index, Athena::io::IStreamReader& reader);
  2. void readPrimitives(CMesh& mesh, const CMaterial& material, atUint16 dataSize, Athena::io::IStreamReader& reader)
  3. {
  4.     atUint32 vertexAttributes = material.vertexAttributes();
  5.  
  6.     atUint32 mainAttributes = 0;
  7.     atUint32 subAttributes = 0;
  8.  
  9.     mainAttributes = vertexAttributes & 0x00FFFFFF;
  10.     subAttributes = (vertexAttributes >> 0x18) & 0xFF;
  11.  
  12.     atUint32 readBytes = 0;
  13.  
  14.     while (!reader.atEnd())
  15.     {
  16.         atUint8 primitiveFlags = reader.readUByte();
  17.  
  18.         atUint32 indexCount = 0;
  19.  
  20.         if (primitiveFlags == 0)
  21.         {
  22.             break;
  23.         }
  24.  
  25.         indexCount = reader.readUint16();
  26.         readBytes += 2;
  27.         if (indexCount == 0 || reader.atEnd())
  28.         {
  29.             break;
  30.         }
  31.  
  32.         CPrimitive primitive;
  33.         primitive.primitive = (EPrimitive)(primitiveFlags & 0xF8);
  34.         primitive.vertexFormatIdx = primitiveFlags & 0x07;
  35.  
  36.         atUint32 startReadBytes = readBytes;
  37.         atUint32 currentPos = reader.position();
  38.  
  39.         bool valid = false;
  40.         primitive.indices.resize(indexCount);
  41.         SVertexDescriptor* pFaces = &primitive.indices.front();
  42.  
  43.         if (mainAttributes)
  44.         {
  45.             reader.seek(currentPos, Athena::SeekOrigin::Begin);
  46.             readBytes = startReadBytes;
  47.  
  48.             atUint32 attributes = 0;
  49.             for (atUint32 v = 0; v < indexCount; ++v)
  50.             {
  51.                 // read unknown attribute indices
  52.                 attributes = subAttributes;
  53.                 while (attributes)
  54.                 {
  55.                     if (attributes & 1)
  56.                     {
  57.                         reader.readUByte();
  58.                         readBytes += 1;
  59.                     }
  60.                     attributes >>= 1;
  61.                 }
  62.  
  63.                 // read main attribute indices
  64.                 attributes = mainAttributes;
  65.  
  66.                 readBytes += readAttribute(pFaces[v].position, attributes, 0, reader);
  67.                 readBytes += readAttribute(pFaces[v].normal, attributes, 1, reader);
  68.  
  69.                 for (int iColor = 0; iColor < 2; ++iColor)
  70.                 {
  71.                     readBytes += readAttribute(pFaces[v].clr[iColor], attributes, iColor + 2, reader);
  72.                 }
  73.  
  74.                 for (int iUV = 0; iUV < 8; ++iUV)
  75.                 {
  76.                     readBytes += readAttribute(pFaces[v].texCoord[iUV], attributes, iUV + 4, reader);
  77.                 }
  78.  
  79.                 for (int iUnk = 0; iUnk < 4; ++iUnk)
  80.                 {
  81.                     readBytes += readAttribute(pFaces[v].unkIndex[iUnk], attributes, iUnk + 12, reader);
  82.                 }
  83.             }
  84.  
  85.             valid = true;
  86.         }
  87.  
  88.  
  89.         if (!valid)
  90.         {
  91.             primitive.primitive = (EPrimitive)0;
  92.             break;
  93.         }
  94.         mesh.m_primitives.push_back(primitive);
  95.     }
  96. }
  97.  
  98.  
  99. atUint16 readAttribute(atUint16 & value, atUint32 attributes, atUint32 index, Athena::io::IStreamReader& reader)
  100. {
  101.     atUint16 readCount = 0;
  102.  
  103.     switch ((attributes >> (index << 1)) & 3)
  104.     {
  105.         case 3:
  106.             value = reader.readUint16();
  107.             readCount = 2;
  108.             break;
  109.         case 2:
  110.             value = reader.readUByte();
  111.             readCount = 1;
  112.             break;
  113.         default:
  114.             break;
  115.     }
  116.  
  117.     return readCount;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement