@CuteAlien
Ok, i firstly just want to go "easy" route and made it just works by the way i did with first structure. Once i will made whole things compiles with -O3 and works as expected, then probabaly we can go futher with reducing code and make it all looks cleaner and better.
Now i have some problem (beware as i not programmer just some slasher who put pieces in one place, so some lame stuff very possible come out from me). Problem is that i want to fix that structure now:
Code: Select all
struct MS3DTriangle
{
u16 Flags;
u16 VertexIndices[3];
float VertexNormals[3][3];
float S[3], T[3];
u8 SmoothingGroup;
u8 GroupIndex;
}
So i create copy of it (just for make it all works of course, dind't worry that its all ugly for now):
Code: Select all
union HybridFloat {
float fvalue;
char cvalue[4];
};
// Triangle information
struct MS3DTrianglePacked
{
u16 Flags;
u16 VertexIndices[3];
HybridFloat VertexNormals[3][3];
HybridFloat S[3], T[3];
u8 SmoothingGroup;
u8 GroupIndex;
} PACK_STRUCT;
struct MS3DTriangle
{
u16 Flags;
u16 VertexIndices[3];
float VertexNormals[3][3];
float S[3], T[3];
u8 SmoothingGroup;
u8 GroupIndex;
}
Now, there is original code which use that structure:
Code: Select all
pPtr += sizeof(u16);
MS3DTriangle *triangles = (MS3DTriangle*)pPtr;
pPtr += sizeof(MS3DTriangle) * numTriangles;
if (pPtr > buffer+fileSize)
{
delete [] buffer;
os::Printer::log("Loading failed. Corrupted data found.", file->getFileName(), ELL_ERROR);
return false;
}
#ifdef __BIG_ENDIAN__
for (u16 tmp=0; tmp<numTriangles; ++tmp)
{
triangles[tmp].Flags = os::Byteswap::byteswap(triangles[tmp].Flags);
for (u16 j=0; j<3; ++j)
{
triangles[tmp].VertexIndices[j] = os::Byteswap::byteswap(triangles[tmp].VertexIndices[j]);
triangles[tmp].VertexNormals[j][0] = os::Byteswap::byteswap(triangles[tmp].VertexNormals[j][0]);
triangles[tmp].VertexNormals[j][1] = os::Byteswap::byteswap(triangles[tmp].VertexNormals[j][1]);
triangles[tmp].VertexNormals[j][2] = -os::Byteswap::byteswap(triangles[tmp].VertexNormals[j][2]);
triangles[tmp].S[j] = os::Byteswap::byteswap(triangles[tmp].S[j]);
triangles[tmp].T[j] = os::Byteswap::byteswap(triangles[tmp].T[j]);
}
}
#endif
And that how i tried to "align" it , but which is just crashes on me currently:
Code: Select all
pPtr += sizeof(u16);
HybridFloat tmpfloat2;
char *pVPtr2 = (char *)pPtr; // address of the first packed vertex in memory
MS3DTriangle *triangles = new MS3DTriangle[numTriangles];
pPtr += sizeof(MS3DTrianglePacked) * numTriangles;
if (pPtr > buffer+fileSize)
{
delete [] buffer;
os::Printer::log("Loading failed. Corrupted data found.", file->getFileName(), ELL_ERROR);
return false;
}
#ifdef __BIG_ENDIAN__
for (u16 tmp=0; tmp<numTriangles; ++tmp)
{
// because fist filed in structure "u16 Flags", do nothing new there:
triangles[tmp].Flags = os::Byteswap::byteswap(triangles[tmp].Flags);
// because second field in structure "u16 VertexIndices[3];", do also nothing new (just repeat it 3 times, as we get rid of "for (u16 j=0; j<3; ++j)" loop:
triangles[tmp].VertexIndices[0] = os::Byteswap::byteswap(triangles[tmp].VertexIndices[0]);
triangles[tmp].VertexIndices[1] = os::Byteswap::byteswap(triangles[tmp].VertexIndices[1]);
triangles[tmp].VertexIndices[2] = os::Byteswap::byteswap(triangles[tmp].VertexIndices[2]);
// Our floats start, let's align them.
//
// Read per byte with swapping and fill 9 VertexNormals ([3][3]), 3 S and 3 T , which mean 15 floats in whole
// pVPtr[0] and pVPtr[1] is ignored, as it contains u16 Flags;
// pVPtr[2] and pVPtr[3], pVPtr[4] and pVPtr[5], pVPtr[6] and pVPtr[7] is ignored, as it "u16 VertexIndices[3]"
// So we start from pVPtr[8]
// hybridfloat VertexNormals
// x.[0]
tmpfloat2.cvalue[3] = pVPtr2[8];
tmpfloat2.cvalue[2] = pVPtr2[9];
tmpfloat2.cvalue[1] = pVPtr2[10];
tmpfloat2.cvalue[0] = pVPtr2[11];
triangles[tmp].VertexNormals[0][0] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[12];
tmpfloat2.cvalue[2] = pVPtr2[13];
tmpfloat2.cvalue[1] = pVPtr2[14];
tmpfloat2.cvalue[0] = pVPtr2[15];
triangles[tmp].VertexNormals[0][1] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[16];
tmpfloat2.cvalue[2] = pVPtr2[17];
tmpfloat2.cvalue[1] = pVPtr2[18];
tmpfloat2.cvalue[0] = pVPtr2[19];
triangles[tmp].VertexNormals[0][2] = - tmpfloat2.fvalue;
// x.[1]
tmpfloat2.cvalue[3] = pVPtr2[20];
tmpfloat2.cvalue[2] = pVPtr2[21];
tmpfloat2.cvalue[1] = pVPtr2[22];
tmpfloat2.cvalue[0] = pVPtr2[23];
triangles[tmp].VertexNormals[1][0] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[24];
tmpfloat2.cvalue[2] = pVPtr2[25];
tmpfloat2.cvalue[1] = pVPtr2[26];
tmpfloat2.cvalue[0] = pVPtr2[27];
triangles[tmp].VertexNormals[1][1] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[28];
tmpfloat2.cvalue[2] = pVPtr2[29];
tmpfloat2.cvalue[1] = pVPtr2[30];
tmpfloat2.cvalue[0] = pVPtr2[31];
triangles[tmp].VertexNormals[1][2] = - tmpfloat2.fvalue;
// x.[2]
tmpfloat2.cvalue[3] = pVPtr2[32];
tmpfloat2.cvalue[2] = pVPtr2[33];
tmpfloat2.cvalue[1] = pVPtr2[34];
tmpfloat2.cvalue[0] = pVPtr2[35];
triangles[tmp].VertexNormals[2][0] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[36];
tmpfloat2.cvalue[2] = pVPtr2[37];
tmpfloat2.cvalue[1] = pVPtr2[38];
tmpfloat2.cvalue[0] = pVPtr2[39];
triangles[tmp].VertexNormals[2][1] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[40];
tmpfloat2.cvalue[2] = pVPtr2[41];
tmpfloat2.cvalue[1] = pVPtr2[42];
tmpfloat2.cvalue[0] = pVPtr2[43];
triangles[tmp].VertexNormals[2][2] = - tmpfloat2.fvalue;
// hybridfloat S
tmpfloat2.cvalue[3] = pVPtr2[44];
tmpfloat2.cvalue[2] = pVPtr2[45];
tmpfloat2.cvalue[1] = pVPtr2[46];
tmpfloat2.cvalue[0] = pVPtr2[47];
triangles[tmp].S[0] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[48];
tmpfloat2.cvalue[2] = pVPtr2[49];
tmpfloat2.cvalue[1] = pVPtr2[50];
tmpfloat2.cvalue[0] = pVPtr2[51];
triangles[tmp].S[1] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[52];
tmpfloat2.cvalue[2] = pVPtr2[53];
tmpfloat2.cvalue[1] = pVPtr2[54];
tmpfloat2.cvalue[0] = pVPtr2[55];
triangles[tmp].S[2] = tmpfloat2.fvalue;
// hybridfloat T
tmpfloat2.cvalue[3] = pVPtr2[56];
tmpfloat2.cvalue[2] = pVPtr2[57];
tmpfloat2.cvalue[1] = pVPtr2[58];
tmpfloat2.cvalue[0] = pVPtr2[59];
triangles[tmp].T[0] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[60];
tmpfloat2.cvalue[2] = pVPtr2[61];
tmpfloat2.cvalue[1] = pVPtr2[62];
tmpfloat2.cvalue[0] = pVPtr2[63];
triangles[tmp].T[1] = tmpfloat2.fvalue;
tmpfloat2.cvalue[3] = pVPtr2[64];
tmpfloat2.cvalue[2] = pVPtr2[65];
tmpfloat2.cvalue[1] = pVPtr2[66];
tmpfloat2.cvalue[0] = pVPtr2[67];
triangles[tmp].T[2] = tmpfloat2.fvalue;
pVPtr2 += sizeof(struct MS3DTrianglePacked);
}
#endif
Did you see something very weird and wrong there ? I probabaly can mess with sizes which i should skip from begining (probabaly it can be not 8, but 4 , and i maye very well suck in whole logic of all that alignment .. Is there something obviously wrong ?:)
Maybe for those [x][x] things it should be like pVptr2[x][x] too, not just +1 for each one ?