From b53a425fe4424a45bf52fb40e0ebdbd2f753ee4e Mon Sep 17 00:00:00 2001 From: Gargaj Date: Thu, 7 Aug 2014 09:28:50 +0200 Subject: [PATCH] make templates gcc compatible --- code/AssbinExporter.cpp | 445 ++++++++++++++++++++-------------------- 1 file changed, 220 insertions(+), 225 deletions(-) diff --git a/code/AssbinExporter.cpp b/code/AssbinExporter.cpp index 2f1c403e9..bca9a3975 100644 --- a/code/AssbinExporter.cpp +++ b/code/AssbinExporter.cpp @@ -58,6 +58,154 @@ using namespace Assimp; namespace Assimp { +template +size_t Write(IOStream * stream, const T& v) +{ + return stream->Write( &v, sizeof(T), 1 ); +} + + +// ----------------------------------------------------------------------------------- +// Serialize an aiString +template <> +inline size_t Write(IOStream * stream, const aiString& s) +{ + const size_t s2 = (uint32_t)s.length; + stream->Write(&s,4,1); + stream->Write(s.data,s2,1); + return s2+4; +} + +// ----------------------------------------------------------------------------------- +// Serialize an unsigned int as uint32_t +template <> +inline size_t Write(IOStream * stream, const unsigned int& w) +{ + const uint32_t t = (uint32_t)w; + if (w > t) { + // this shouldn't happen, integers in Assimp data structures never exceed 2^32 + throw new DeadlyExportError("loss of data due to 64 -> 32 bit integer conversion"); + } + + stream->Write(&t,4,1); + return 4; +} + +// ----------------------------------------------------------------------------------- +// Serialize an unsigned int as uint16_t +template <> +inline size_t Write(IOStream * stream, const uint16_t& w) +{ + stream->Write(&w,2,1); + return 2; +} + +// ----------------------------------------------------------------------------------- +// Serialize a float +template <> +inline size_t Write(IOStream * stream, const float& f) +{ + BOOST_STATIC_ASSERT(sizeof(float)==4); + stream->Write(&f,4,1); + return 4; +} + +// ----------------------------------------------------------------------------------- +// Serialize a double +template <> +inline size_t Write(IOStream * stream, const double& f) +{ + BOOST_STATIC_ASSERT(sizeof(double)==8); + stream->Write(&f,8,1); + return 8; +} + +// ----------------------------------------------------------------------------------- +// Serialize a vec3 +template <> +inline size_t Write(IOStream * stream, const aiVector3D& v) +{ + size_t t = Write(stream,v.x); + t += Write(stream,v.y); + t += Write(stream,v.z); + return t; +} + +// ----------------------------------------------------------------------------------- +// Serialize a color value +template <> +inline size_t Write(IOStream * stream, const aiColor4D& v) +{ + size_t t = Write(stream,v.r); + t += Write(stream,v.g); + t += Write(stream,v.b); + t += Write(stream,v.a); + return t; +} + +// ----------------------------------------------------------------------------------- +// Serialize a quaternion +template <> +inline size_t Write(IOStream * stream, const aiQuaternion& v) +{ + size_t t = Write(stream,v.w); + t += Write(stream,v.x); + t += Write(stream,v.y); + t += Write(stream,v.z); + return 16; +} + + +// ----------------------------------------------------------------------------------- +// Serialize a vertex weight +template <> +inline size_t Write(IOStream * stream, const aiVertexWeight& v) +{ + uint32_t t = Write(stream,v.mVertexId); + return t+Write(stream,v.mWeight); +} + +// ----------------------------------------------------------------------------------- +// Serialize a mat4x4 +template <> +inline size_t Write(IOStream * stream, const aiMatrix4x4& m) +{ + for (unsigned int i = 0; i < 4;++i) { + for (unsigned int i2 = 0; i2 < 4;++i2) { + Write(stream,m[i][i2]); + } + } + return 64; +} + +// ----------------------------------------------------------------------------------- +// Serialize an aiVectorKey +template <> +inline size_t Write(IOStream * stream, const aiVectorKey& v) +{ + const size_t t = Write(stream,v.mTime); + return t + Write(stream,v.mValue); +} + +// ----------------------------------------------------------------------------------- +// Serialize an aiQuatKey +template <> +inline size_t Write(IOStream * stream, const aiQuatKey& v) +{ + const size_t t = Write(stream,v.mTime); + return t + Write(stream,v.mValue); +} + +template +inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) +{ + T minc,maxc; + ArrayBounds(in,size,minc,maxc); + + const size_t t = Write(stream,minc); + return t + Write(stream,maxc); +} + // ---------------------------------------------------------------------------------- /** @class AssbinChunkWriter * @brief Chunk writer mechanism for the .assbin file structure @@ -138,154 +286,6 @@ namespace Assimp { return pCount; } - template - size_t Write(const T& v) - { - return Write( &v, sizeof(T), 1 ); - } - - - // ----------------------------------------------------------------------------------- - // Serialize an aiString - template <> - inline uint32_t Write(const aiString& s) - { - const uint32_t s2 = (uint32_t)s.length; - Write(&s,4,1); - Write(s.data,s2,1); - return s2+4; - } - - // ----------------------------------------------------------------------------------- - // Serialize an unsigned int as uint32_t - template <> - inline uint32_t Write(const unsigned int& w) - { - const uint32_t t = (uint32_t)w; - if (w > t) { - // this shouldn't happen, integers in Assimp data structures never exceed 2^32 - throw new DeadlyExportError("loss of data due to 64 -> 32 bit integer conversion"); - } - - Write(&t,4,1); - return 4; - } - - // ----------------------------------------------------------------------------------- - // Serialize an unsigned int as uint16_t - template <> - inline uint32_t Write(const uint16_t& w) - { - Write(&w,2,1); - return 2; - } - - // ----------------------------------------------------------------------------------- - // Serialize a float - template <> - inline uint32_t Write(const float& f) - { - BOOST_STATIC_ASSERT(sizeof(float)==4); - Write(&f,4,1); - return 4; - } - - // ----------------------------------------------------------------------------------- - // Serialize a double - template <> - inline uint32_t Write(const double& f) - { - BOOST_STATIC_ASSERT(sizeof(double)==8); - Write(&f,8,1); - return 8; - } - - // ----------------------------------------------------------------------------------- - // Serialize a vec3 - template <> - inline uint32_t Write(const aiVector3D& v) - { - uint32_t t = Write(v.x); - t += Write(v.y); - t += Write(v.z); - return t; - } - - // ----------------------------------------------------------------------------------- - // Serialize a color value - template <> - inline uint32_t Write(const aiColor4D& v) - { - uint32_t t = Write(v.r); - t += Write(v.g); - t += Write(v.b); - t += Write(v.a); - return t; - } - - // ----------------------------------------------------------------------------------- - // Serialize a quaternion - template <> - inline uint32_t Write(const aiQuaternion& v) - { - uint32_t t = Write(v.w); - t += Write(v.x); - t += Write(v.y); - t += Write(v.z); - return 16; - } - - - // ----------------------------------------------------------------------------------- - // Serialize a vertex weight - template <> - inline uint32_t Write(const aiVertexWeight& v) - { - uint32_t t = Write(v.mVertexId); - return t+Write(v.mWeight); - } - - // ----------------------------------------------------------------------------------- - // Serialize a mat4x4 - template <> - inline uint32_t Write(const aiMatrix4x4& m) - { - for (unsigned int i = 0; i < 4;++i) { - for (unsigned int i2 = 0; i2 < 4;++i2) { - Write(m[i][i2]); - } - } - return 64; - } - - // ----------------------------------------------------------------------------------- - // Serialize an aiVectorKey - template <> - inline uint32_t Write(const aiVectorKey& v) - { - const uint32_t t = Write(v.mTime); - return t + Write(v.mValue); - } - - // ----------------------------------------------------------------------------------- - // Serialize an aiQuatKey - template <> - inline uint32_t Write(const aiQuatKey& v) - { - const uint32_t t = Write(v.mTime); - return t + Write(v.mValue); - } - - template - inline uint32_t WriteBounds(const T* in, unsigned int size) - { - T minc,maxc; - ArrayBounds(in,size,minc,maxc); - - const uint32_t t = Write(minc); - return t + Write(maxc); - } - }; // ---------------------------------------------------------------------------------- @@ -301,24 +301,19 @@ namespace Assimp { bool compressed; protected: - template - size_t Write( IOStream * container, const T& v) - { - return container->Write( &v, sizeof(T), 1 ); - } // ----------------------------------------------------------------------------------- void WriteBinaryNode( IOStream * container, const aiNode* node) { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AINODE ); - chunk.Write(node->mName); - chunk.Write(node->mTransformation); - chunk.Write(node->mNumChildren); - chunk.Write(node->mNumMeshes); + Write(&chunk,node->mName); + Write(&chunk,node->mTransformation); + Write(&chunk,node->mNumChildren); + Write(&chunk,node->mNumMeshes); for (unsigned int i = 0; i < node->mNumMeshes;++i) { - chunk.Write(node->mMeshes[i]); + Write(&chunk,node->mMeshes[i]); } for (unsigned int i = 0; i < node->mNumChildren;++i) { @@ -331,8 +326,8 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AITEXTURE ); - chunk.Write(tex->mWidth); - chunk.Write(tex->mHeight); + Write(&chunk,tex->mWidth); + Write(&chunk,tex->mHeight); chunk.Write( tex->achFormatHint, sizeof(char), 4 ); if(!shortened) { @@ -351,14 +346,14 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AIBONE ); - chunk.Write(b->mName); - chunk.Write(b->mNumWeights); - chunk.Write(b->mOffsetMatrix); + Write(&chunk,b->mName); + Write(&chunk,b->mNumWeights); + Write(&chunk,b->mOffsetMatrix); // for the moment we write dumb min/max values for the bones, too. // maybe I'll add a better, hash-like solution later if (shortened) { - chunk.WriteBounds(b->mWeights,b->mNumWeights); + WriteBounds(&chunk,b->mWeights,b->mNumWeights); } // else write as usual else chunk.Write(b->mWeights,1,b->mNumWeights*sizeof(aiVertexWeight)); } @@ -368,11 +363,11 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AIMESH ); - chunk.Write(mesh->mPrimitiveTypes); - chunk.Write(mesh->mNumVertices); - chunk.Write(mesh->mNumFaces); - chunk.Write(mesh->mNumBones); - chunk.Write(mesh->mMaterialIndex); + Write(&chunk,mesh->mPrimitiveTypes); + Write(&chunk,mesh->mNumVertices); + Write(&chunk,mesh->mNumFaces); + Write(&chunk,mesh->mNumBones); + Write(&chunk,mesh->mMaterialIndex); // first of all, write bits for all existent vertex components unsigned int c = 0; @@ -397,25 +392,25 @@ namespace Assimp { } c |= ASSBIN_MESH_HAS_COLOR(n); } - chunk.Write(c); + Write(&chunk,c); aiVector3D minVec, maxVec; if (mesh->mVertices) { if (shortened) { - chunk.WriteBounds(mesh->mVertices,mesh->mNumVertices); + WriteBounds(&chunk,mesh->mVertices,mesh->mNumVertices); } // else write as usual else chunk.Write(mesh->mVertices,1,12*mesh->mNumVertices); } if (mesh->mNormals) { if (shortened) { - chunk.WriteBounds(mesh->mNormals,mesh->mNumVertices); + WriteBounds(&chunk,mesh->mNormals,mesh->mNumVertices); } // else write as usual else chunk.Write(mesh->mNormals,1,12*mesh->mNumVertices); } if (mesh->mTangents && mesh->mBitangents) { if (shortened) { - chunk.WriteBounds(mesh->mTangents,mesh->mNumVertices); - chunk.WriteBounds(mesh->mBitangents,mesh->mNumVertices); + WriteBounds(&chunk,mesh->mTangents,mesh->mNumVertices); + WriteBounds(&chunk,mesh->mBitangents,mesh->mNumVertices); } // else write as usual else { chunk.Write(mesh->mTangents,1,12*mesh->mNumVertices); @@ -427,7 +422,7 @@ namespace Assimp { break; if (shortened) { - chunk.WriteBounds(mesh->mColors[n],mesh->mNumVertices); + WriteBounds(&chunk,mesh->mColors[n],mesh->mNumVertices); } // else write as usual else chunk.Write(mesh->mColors[n],16*mesh->mNumVertices,1); } @@ -436,10 +431,10 @@ namespace Assimp { break; // write number of UV components - chunk.Write(mesh->mNumUVComponents[n]); + Write(&chunk,mesh->mNumUVComponents[n]); if (shortened) { - chunk.WriteBounds(mesh->mTextureCoords[n],mesh->mNumVertices); + WriteBounds(&chunk,mesh->mTextureCoords[n],mesh->mNumVertices); } // else write as usual else chunk.Write(mesh->mTextureCoords[n],12*mesh->mNumVertices,1); } @@ -464,7 +459,7 @@ namespace Assimp { hash = SuperFastHash(reinterpret_cast(&tmp),sizeof tmp,hash); } } - chunk.Write(hash); + Write(&chunk,hash); } } else // else write as usual @@ -474,13 +469,13 @@ namespace Assimp { const aiFace& f = mesh->mFaces[i]; BOOST_STATIC_ASSERT(AI_MAX_FACE_INDICES <= 0xffff); - chunk.Write(f.mNumIndices); + Write(&chunk,f.mNumIndices); for (unsigned int a = 0; a < f.mNumIndices;++a) { if (mesh->mNumVertices < (1u<<16)) { - chunk.Write(f.mIndices[a]); + Write(&chunk,f.mIndices[a]); } - else chunk.Write(f.mIndices[a]); + else Write(&chunk,f.mIndices[a]); } } } @@ -499,12 +494,12 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AIMATERIALPROPERTY ); - chunk.Write(prop->mKey); - chunk.Write(prop->mSemantic); - chunk.Write(prop->mIndex); + Write(&chunk,prop->mKey); + Write(&chunk,prop->mSemantic); + Write(&chunk,prop->mIndex); - chunk.Write(prop->mDataLength); - chunk.Write((unsigned int)prop->mType); + Write(&chunk,prop->mDataLength); + Write(&chunk,(unsigned int)prop->mType); chunk.Write(prop->mData,1,prop->mDataLength); } @@ -513,7 +508,7 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AIMATERIAL); - chunk.Write(mat->mNumProperties); + Write(&chunk,mat->mNumProperties); for (unsigned int i = 0; i < mat->mNumProperties;++i) { WriteBinaryMaterialProperty( &chunk, mat->mProperties[i]); } @@ -524,30 +519,30 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AINODEANIM ); - chunk.Write(nd->mNodeName); - chunk.Write(nd->mNumPositionKeys); - chunk.Write(nd->mNumRotationKeys); - chunk.Write(nd->mNumScalingKeys); - chunk.Write(nd->mPreState); - chunk.Write(nd->mPostState); + Write(&chunk,nd->mNodeName); + Write(&chunk,nd->mNumPositionKeys); + Write(&chunk,nd->mNumRotationKeys); + Write(&chunk,nd->mNumScalingKeys); + Write(&chunk,nd->mPreState); + Write(&chunk,nd->mPostState); if (nd->mPositionKeys) { if (shortened) { - chunk.WriteBounds(nd->mPositionKeys,nd->mNumPositionKeys); + WriteBounds(&chunk,nd->mPositionKeys,nd->mNumPositionKeys); } // else write as usual else chunk.Write(nd->mPositionKeys,1,nd->mNumPositionKeys*sizeof(aiVectorKey)); } if (nd->mRotationKeys) { if (shortened) { - chunk.WriteBounds(nd->mRotationKeys,nd->mNumRotationKeys); + WriteBounds(&chunk,nd->mRotationKeys,nd->mNumRotationKeys); } // else write as usual else chunk.Write(nd->mRotationKeys,1,nd->mNumRotationKeys*sizeof(aiQuatKey)); } if (nd->mScalingKeys) { if (shortened) { - chunk.WriteBounds(nd->mScalingKeys,nd->mNumScalingKeys); + WriteBounds(&chunk,nd->mScalingKeys,nd->mNumScalingKeys); } // else write as usual else chunk.Write(nd->mScalingKeys,1,nd->mNumScalingKeys*sizeof(aiVectorKey)); @@ -560,10 +555,10 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AIANIMATION ); - chunk.Write (anim->mName); - chunk.Write (anim->mDuration); - chunk.Write (anim->mTicksPerSecond); - chunk.Write(anim->mNumChannels); + Write(&chunk,anim->mName); + Write(&chunk,anim->mDuration); + Write(&chunk,anim->mTicksPerSecond); + Write(&chunk,anim->mNumChannels); for (unsigned int a = 0; a < anim->mNumChannels;++a) { const aiNodeAnim* nd = anim->mChannels[a]; @@ -576,22 +571,22 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AILIGHT ); - chunk.Write(l->mName); - chunk.Write(l->mType); + Write(&chunk,l->mName); + Write(&chunk,l->mType); if (l->mType != aiLightSource_DIRECTIONAL) { - chunk.Write(l->mAttenuationConstant); - chunk.Write(l->mAttenuationLinear); - chunk.Write(l->mAttenuationQuadratic); + Write(&chunk,l->mAttenuationConstant); + Write(&chunk,l->mAttenuationLinear); + Write(&chunk,l->mAttenuationQuadratic); } - chunk.Write((const aiVector3D&)l->mColorDiffuse); - chunk.Write((const aiVector3D&)l->mColorSpecular); - chunk.Write((const aiVector3D&)l->mColorAmbient); + Write(&chunk,(const aiVector3D&)l->mColorDiffuse); + Write(&chunk,(const aiVector3D&)l->mColorSpecular); + Write(&chunk,(const aiVector3D&)l->mColorAmbient); if (l->mType == aiLightSource_SPOT) { - chunk.Write(l->mAngleInnerCone); - chunk.Write(l->mAngleOuterCone); + Write(&chunk,l->mAngleInnerCone); + Write(&chunk,l->mAngleOuterCone); } } @@ -601,14 +596,14 @@ namespace Assimp { { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AICAMERA ); - chunk.Write(cam->mName); - chunk.Write(cam->mPosition); - chunk.Write(cam->mLookAt); - chunk.Write(cam->mUp); - chunk.Write(cam->mHorizontalFOV); - chunk.Write(cam->mClipPlaneNear); - chunk.Write(cam->mClipPlaneFar); - chunk.Write(cam->mAspect); + Write(&chunk,cam->mName); + Write(&chunk,cam->mPosition); + Write(&chunk,cam->mLookAt); + Write(&chunk,cam->mUp); + Write(&chunk,cam->mHorizontalFOV); + Write(&chunk,cam->mClipPlaneNear); + Write(&chunk,cam->mClipPlaneFar); + Write(&chunk,cam->mAspect); } // ----------------------------------------------------------------------------------- @@ -617,13 +612,13 @@ namespace Assimp { AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AISCENE ); // basic scene information - chunk.Write(scene->mFlags); - chunk.Write(scene->mNumMeshes); - chunk.Write(scene->mNumMaterials); - chunk.Write(scene->mNumAnimations); - chunk.Write(scene->mNumTextures); - chunk.Write(scene->mNumLights); - chunk.Write(scene->mNumCameras); + Write(&chunk,scene->mFlags); + Write(&chunk,scene->mNumMeshes); + Write(&chunk,scene->mNumMaterials); + Write(&chunk,scene->mNumAnimations); + Write(&chunk,scene->mNumTextures); + Write(&chunk,scene->mNumLights); + Write(&chunk,scene->mNumCameras); // write node graph WriteBinaryNode( &chunk, scene->mRootNode );