void DiMeshSerializerImpl::ReadGeometryVertexDeclaration( DiDataStreamPtr& stream, DiSubMesh* pMesh) { DI_SERIAL_LOG("Reading vertex declaration info..."); uint32 elementsNum = (mCurChunkSize - MSTREAM_OVERHEAD_SIZE)/(sizeof(uint16)*4); DI_SERIAL_LOG("Number of elements: %d", elementsNum); if (elementsNum > 0 && !stream->Eof()) { pMesh->GetVertexElements().ClearElements(); for (uint32 i=0; i<elementsNum; i++) { unsigned short source, tmp; DiVertexType vType; DiVertexUsage vSemantic; ReadShorts(stream, &source, 1); ReadShorts(stream, &tmp, 1); vType = static_cast<DiVertexType>(tmp); ReadShorts(stream, &tmp, 1); vSemantic = static_cast<DiVertexUsage>(tmp); ReadShorts(stream, &tmp, 1); uint8 usageID = static_cast<uint8>(tmp); pMesh->GetVertexElements().AddElement(source,vType,vSemantic,usageID); } } }
void DiMotionSerializerImpl::ReadBonesParents( DiDataStreamPtr& stream,DiSkeleton* skeleton ) { uint16 handleSon = 0; ReadShorts(stream,&handleSon, 1); uint16 handleParent = 0; ReadShorts(stream,&handleParent, 1); DiBone* son = skeleton->GetBone(handleSon); DiBone* parent = skeleton->GetBone(handleParent); parent->AddChild(son); }
void DiMeshSerializerImpl::ReadGeometryVertexBuffer( DiDataStreamPtr& stream, DiSubMesh* pMesh ) { DI_SERIAL_LOG("Reading vertex buffer..."); uint16 bindIndex, vertexSize; ReadShorts(stream, &bindIndex, 1); ReadShorts(stream, &vertexSize, 1); uint32 size = vertexSize * pMesh->GetVerticeNum(); void* buf = pMesh->CreateSourceData(bindIndex,pMesh->GetVerticeNum(),vertexSize); stream->Read(buf, size); DI_SERIAL_LOG("%d bytes readed", size); }
void DiMeshSerializerImpl::ReadSubMesh( DiDataStreamPtr& stream, DiMesh* pMesh ) { DI_SERIAL_LOG("Reading submesh.."); DiSubMesh* sm = pMesh->CreateSubMesh(); unsigned short streamID = 0; DiString material = ReadString(stream); sm->SetMaterialName(material); DI_SERIAL_LOG("Liking material: %s", material.c_str()); unsigned int indexCount = 0; ReadInts(stream, &indexCount, 1); DI_SERIAL_LOG("Indeices count: %d", indexCount); bool idx32bit; ReadBools(stream, &idx32bit, 1); DI_SERIAL_LOG("Index size: %d", idx32bit?32:16); uint16 primitive; ReadShorts(stream,&primitive,1); sm->SetPrimitiveType((DiPrimitiveType)primitive); DI_SERIAL_LOG("Primitive type: %d", primitive); if (indexCount > 0) { void* indexdata = sm->CreateIndexData(indexCount,idx32bit?TRUE:FALSE); int size = indexCount * (sm->GetIndexSize() / 8); stream->Read(indexdata, size); DI_SERIAL_LOG("%d bytes readed", size); } streamID = ReadChunk(stream); if (streamID != DI_GEOMETRY) { DI_ERROR("Bad stream ID"); return; } ReadGeometry(stream, sm); if (!stream->Eof()) { streamID = ReadChunk(stream); if (streamID == DI_MESH_WEIGHTS) { ReadSubMeshBoneWeights(stream,sm); } else { if (!stream->Eof()) stream->Skip(-MSTREAM_OVERHEAD_SIZE); } } }
void DiMotionSerializerImpl::ReadAttachClips( DiDataStreamPtr& stream, DiAnimation* motion ) { DiString strName = ReadString(stream); DiNodeClip* nodeClip = motion->CreateAttachClip(strName); uint16 keyFrameNum = 0; ReadShorts(stream,&keyFrameNum,1); for (uint16 i = 0; i < keyFrameNum; ++i) { float time = 0; ReadFloats(stream,&time, 1); DiVec3 pos; ReadObject(stream,pos); DiQuat quat; ReadObject(stream,quat); bool hasScale = false; ReadBools(stream,&hasScale,1); DiVec3 scale = DiVec3::UNIT_SCALE; if (hasScale) { ReadObject(stream,scale); } DiTransformKeyFrame* key = nodeClip->CreateNodeKeyFrame(time); key->SetTranslate(pos); key->SetRotation(quat); key->SetScale(scale); } }
void DiMotionSerializerImpl::ReadAttachNodes( DiDataStreamPtr& stream,DiAttachSet* attachset ) { uint16 numAttaches; ReadShorts(stream,&numAttaches,1); for (size_t i = 0; i < numAttaches; ++i) { DiString name = ReadString(stream); DiVec3 position; ReadObject(stream,position); DiQuat quat; ReadObject(stream,quat); bool hasscale; ReadBools(stream,&hasscale,1); DiVec3 scale = DiVec3::UNIT_SCALE; if (hasscale) { ReadObject(stream,scale); } DiAttachNode * pkAttachNode = attachset->CreateAttachNode(name); pkAttachNode->SetPosition(position); pkAttachNode->SetOrientation(quat); if (hasscale) { pkAttachNode->SetScale(scale); } } }
uint16 DiSerializer::ReadChunk( DiDataStreamPtr& stream ) { unsigned short id; ReadShorts(stream, &id, 1); ReadInts(stream, &mCurChunkSize, 1); return id; }
void DiMotionSerializerImpl::ReadBones( DiDataStreamPtr& stream,DiSkeleton* skeleton ) { uint16 numBones; ReadShorts(stream,&numBones,1); for (size_t i = 0; i < numBones; ++i) { // name DiString name = ReadString(stream); // handle uint16 handle; ReadShorts(stream,&handle,1); // position DiVec3 position; ReadObject(stream,position); // orientation DiQuat quat; ReadObject(stream,quat); // scale bool hasscale; ReadBools(stream,&hasscale,1); DiVec3 scale = DiVec3::UNIT_SCALE; if (hasscale) { // scale ReadObject(stream,scale); } DiBone* bone = skeleton->CreateBone(name,handle); bone->SetPosition(position); bone->SetOrientation(quat); if (hasscale) { bone->SetScale(scale); } } }
void DiMeshSerializerImpl::ReadSubMeshBoneWeights( DiDataStreamPtr& stream, DiSubMesh* pMesh ) { DI_SERIAL_LOG("Reading bone weights..."); uint32 size = 0; ReadInts(stream,&size,1); DI_SERIAL_LOG("Size: %d",size); for (uint16 i=0; i<size; i++) { DiBoneWeight weight; ReadInts(stream,&weight.vertexIndex,1); ReadShorts(stream,&weight.boneIndex,1); ReadFloats(stream,&weight.weight,1); pMesh->mBoneWeights.insert(DiSubMesh::BoneWeightList::value_type(weight.vertexIndex, weight)); } pMesh->SetupBoneWeights(); }
void DiMotionSerializerImpl::ReadSkeleton( DiDataStreamPtr& stream,DiMotion* motion ) { DiSkeleton* skel = motion->CreateSkeleton(); uint16 blendmode; ReadShorts(stream,&blendmode,1); skel->SetBlendMode(SkeletonBlendMode(blendmode)); if (!stream->Eof()) { uint16 streamID = ReadChunk(stream); while(!stream->Eof() && (streamID == DI_BONES || streamID == DI_BONES_PARENT)) { switch(streamID) { case DI_BONES: ReadBones(stream,skel); break; case DI_BONES_PARENT: ReadBonesParents(stream,skel); break; } if (!stream->Eof()) { streamID = ReadChunk(stream); } } if (!stream->Eof()) { stream->Skip(-MSTREAM_OVERHEAD_SIZE); } } }