// Saves the given mesh to the given file. void SaveMesh(const utf8_ntri &file, const Mesh &mesh, uint4 flags) { const MeshImpl &meshImpl = static_cast<const MeshImpl&>(mesh); lean::raw_file meshFile(file, lean::file::readwrite, lean::file::overwrite); // Mesh ChunkInfo meshChunk; BeginChunk(meshFile, beScene::MeshDataChunk::Header, meshChunk); // Mesh name { const aiNode &rootNode = *meshImpl.GetScene()->mRootNode; if (rootNode.mName.length > 0) meshChunk.Size += WriteStringChunk(meshFile, beScene::MeshDataChunk::Name, rootNode.mName.data, static_cast<uint4>(rootNode.mName.length)); } // Mesh subsets { meshChunk.Size += SaveSubsets(meshFile, *meshImpl.GetScene(), flags); } EndChunk(meshFile, meshChunk); }
shared_ptr<SceneNode> BLOCO_API ModelChildObjectParams::VCreateSceneNode( shared_ptr<Scene> pScene ) { switch(m_Type) { case AT_ModelChild_Mesh: { shared_ptr<MeshSceneNode> meshFile(DEBUG_CLIENTBLOCK MeshSceneNode( m_pModelElement, m_Id, m_pRootNode, m_pActor->VGetMat() )); return meshFile; } case AT_ModelChild_Bone: { shared_ptr<BoneSceneNode> meshFile(DEBUG_CLIENTBLOCK BoneSceneNode( m_pModelElement, m_Id, m_pRootNode, m_pActor->VGetMat() )); return meshFile; } } return nullptr; }
void WavefrontObj::Load(const path_type& filename) { std::vector<char> objText; std::ifstream meshFile(filename); if( !meshFile ) return; objText.assign( std::istreambuf_iterator<char>(meshFile), std::istreambuf_iterator<char>()); std::vector<ObjPosition> pos; std::vector<ObjTexCoords> tex; std::vector<ObjNormal> norm; std::vector<ObjFace> face; // // Read in vertex data // auto it = objText.begin(); while (it != objText.end()) { switch (*it) { case 'v': switch (*++it) { case 't': // vt = texture coord tex.push_back(ParseObjTexCoords(++it, objText.end())); break; case 'n': // vn = normal norm.push_back(ParseObjNormal(++it, objText.end())); break; case 'p': // vp = parametrized coords -- unhandled EndLine(it, objText.end()); break; default: // v = vertex position pos.push_back(ParseObjPosition(it, objText.end())); break; } break; case 'f': // f = face face.push_back(ParseObjFace(++it, objText.end())); break; default: // skip this line EndLine(it, objText.end()); break; }; } m_pos.swap(pos); m_tex.swap(tex); m_norm.swap(norm); m_face.swap(face); }
void Shader::_readShaderSource(const char* filename, std::string& contents) { std::ifstream meshFile(filename, std::ios::in | std::ios::binary); if (meshFile) { meshFile.seekg(0, std::ios::end); contents.resize(meshFile.tellg()); meshFile.seekg(0, std::ios::beg); meshFile.read(&contents[0], contents.size()); meshFile.close(); } else { printf("ERROR: Cannot find mesh file!"); exit(1); } }
int main( int argc, char **argv ) { if( 2 != argc ) { std::cout << "readMSH <file>" << std::endl; return 0; } std::ifstream meshFile( argv[1], std::ios_base::binary ); if( !meshFile.is_open() ) { std::cout << "Unable to open file: " << argv[1] << std::endl; exit( 0 ); } ml::msh mesh; mesh.readMSH( meshFile ); meshFile.close(); return 0; }
shared_ptr<SceneNode> VoxelObjectParams::VCreateSceneNode( shared_ptr<Scene> pScene ) { shared_ptr<VoxelSceneNode> meshFile(DEBUG_CLIENTBLOCK VoxelSceneNode(m_width, m_height, m_depth, m_ChunkSize, m_sName, m_Id, m_pActor->VGetMat())); return meshFile; }
shared_ptr<SceneNode> BLOCO_API ModelObjectParams::VCreateSceneNode(shared_ptr<Scene> pScene) { shared_ptr<ModelSceneNode> meshFile(DEBUG_CLIENTBLOCK ModelSceneNode(m_Id, m_sName, m_ModellFilename, m_pActor->VGetMat())); return meshFile; }
template<> unique_ptr<Model3D> ResourceService::LoadResource<Model3D>(const std::tstring& filePath) { auto pModel = new Model3D(); BinaryReader meshFile(filePath); unsigned int inputLayoutID = 0; auto versionNumber = meshFile.Read<unsigned short>(); auto vertexFormat = meshFile.Read<unsigned char>(); auto nrOfTexCoordChannels = meshFile.Read<unsigned char>(); unsigned int* nrOfTexCoordsArr; unsigned int nrOfPositions=0, nrOfNormals=0, nrOfTangents=0, nrOfBinormals=0, nrOfVertexColors=0, nrOfAnimData=0; nrOfPositions = meshFile.Read<unsigned int>(); if(nrOfTexCoordChannels > 0){ nrOfTexCoordsArr = new unsigned int[nrOfTexCoordChannels]; meshFile.ReadArray<unsigned int>(nrOfTexCoordsArr, nrOfTexCoordChannels); } if(vertexFormat & 1) nrOfNormals = meshFile.Read<unsigned int>(); if(vertexFormat & 2) nrOfTangents = meshFile.Read<unsigned int>(); if(vertexFormat & 4) nrOfBinormals = meshFile.Read<unsigned int>(); if(vertexFormat & 8) nrOfVertexColors = meshFile.Read<unsigned int>(); if(vertexFormat & 16) nrOfAnimData = meshFile.Read<unsigned int>(); auto nrOfVertices = meshFile.Read<unsigned int>(); auto nrOfIndices = meshFile.Read<unsigned int>(); //Read vertex positions for(unsigned int i=0; i < nrOfPositions; ++i){ auto x = meshFile.Read<float>(); auto y = meshFile.Read<float>(); auto z = meshFile.Read<float>(); pModel->m_Positions.data.push_back(D3DXVECTOR3(x,y,z)); } //Read texture coordinates if(nrOfTexCoordChannels > 0){ pModel->m_TexCoords.resize(nrOfTexCoordChannels); for(unsigned short channel = 0; channel < nrOfTexCoordChannels; ++channel){ for(unsigned int i = 0; i < nrOfTexCoordsArr[channel]; ++i){ auto u = meshFile.Read<float>(); auto v = meshFile.Read<float>(); pModel->m_TexCoords[channel].data.push_back(D3DXVECTOR2(u,v)); } } delete[] nrOfTexCoordsArr; } //Read vertex normals if(vertexFormat & 1){ for(unsigned int i=0; i < nrOfNormals; ++i){ auto x = meshFile.Read<float>(); auto y = meshFile.Read<float>(); auto z = meshFile.Read<float>(); pModel->m_Normals.data.push_back(D3DXVECTOR3(x,y,z)); } } //Read vertex tangents if(vertexFormat & 2){ for(unsigned int i=0; i < nrOfTangents; ++i){ auto x = meshFile.Read<float>(); auto y = meshFile.Read<float>(); auto z = meshFile.Read<float>(); pModel->m_Tangents.data.push_back(D3DXVECTOR3(x,y,z)); } } //Read vertex binormals if(vertexFormat & 4){ for(unsigned int i=0; i < nrOfBinormals; ++i){ auto x = meshFile.Read<float>(); auto y = meshFile.Read<float>(); auto z = meshFile.Read<float>(); pModel->m_Binormals.data.push_back(D3DXVECTOR3(x,y,z)); } } //Read vertex color if(vertexFormat & 8){ for(unsigned int i=0; i < nrOfVertexColors; ++i){ auto r = meshFile.Read<float>(); auto g = meshFile.Read<float>(); auto b = meshFile.Read<float>(); auto a = meshFile.Read<float>(); pModel->m_Colors.data.push_back(D3DXCOLOR(r,g,b,a)); } } if(vertexFormat & 16){ //Read blend indices & blend weights for(unsigned int i=0; i < nrOfAnimData; ++i){ auto nrOfIndices = meshFile.Read<unsigned int>(); auto x = nrOfIndices > 0 ? static_cast<float>( meshFile.Read<unsigned int>() ) : -1.0f; auto y = nrOfIndices > 1 ? static_cast<float>( meshFile.Read<unsigned int>() ) : -1.0f; auto z = nrOfIndices > 2 ? static_cast<float>( meshFile.Read<unsigned int>() ) : -1.0f; auto w = nrOfIndices > 3 ? static_cast<float>( meshFile.Read<unsigned int>() ) : -1.0f; pModel->m_BlendIndices.data.push_back(D3DXVECTOR4(x,y,z,w)); x = nrOfIndices > 0 ? meshFile.Read<float>() : 0.0f; y = nrOfIndices > 1 ? meshFile.Read<float>() : 0.0f; z = nrOfIndices > 2 ? meshFile.Read<float>() : 0.0f; w = nrOfIndices > 3 ? meshFile.Read<float>() : 0.0f; pModel->m_BlendWeights.data.push_back(D3DXVECTOR4(x,y,z,w)); } } //Read vertices for(unsigned int i=0; i < nrOfVertices; ++i) { auto iPos = meshFile.Read<unsigned int>(); pModel->m_Positions.indices.push_back(iPos); if(nrOfTexCoordChannels > 0){ auto iTexCoordArr = new unsigned int[nrOfTexCoordChannels]; meshFile.ReadArray<unsigned int>(iTexCoordArr, nrOfTexCoordChannels); for(unsigned int channel=0; channel < nrOfTexCoordChannels; ++channel) pModel->m_TexCoords[channel].indices.push_back(iTexCoordArr[channel]); delete[] iTexCoordArr; } if(nrOfNormals > 0){ auto iNormal = meshFile.Read<unsigned int>(); pModel->m_Normals.indices.push_back(iNormal); } if(nrOfTangents > 0){ auto iTangent = meshFile.Read<unsigned int>(); pModel->m_Tangents.indices.push_back(iTangent); } if(nrOfBinormals > 0){ auto iBinormal = meshFile.Read<unsigned int>(); pModel->m_Binormals.indices.push_back(iBinormal); } if(nrOfVertexColors > 0){ auto iVertexColor = meshFile.Read<unsigned int>(); pModel->m_Colors.indices.push_back(iVertexColor); } if(nrOfAnimData > 0){ auto iAnimData = meshFile.Read<unsigned int>(); pModel->m_BlendIndices.indices.push_back(iAnimData); pModel->m_BlendWeights.indices.push_back(iAnimData); } } //Read indices for(unsigned int i=0; i < nrOfIndices; ++i) pModel->m_Indices.push_back(meshFile.Read<unsigned int>()); if(nrOfAnimData > 0){ //Read bones auto nrOfBones = meshFile.Read<unsigned int>(); for(unsigned int i=0; i < nrOfBones; ++i){ Bone newBone; newBone.Name = meshFile.ReadString(); /* for(unsigned int row=0; row < 4; ++row) for(unsigned int col=0; col < 3; ++col) newBone.BindPose.m[row][col] = meshFile.Read<float>(); newBone.BindPose._14 = newBone.BindPose._24 = newBone.BindPose._34 = 0; newBone.BindPose._44 = 1; */ newBone.BindPose.Data[0].x = meshFile.Read<float>(); newBone.BindPose.Data[0].y = meshFile.Read<float>(); newBone.BindPose.Data[0].z = meshFile.Read<float>(); newBone.BindPose.Data[0].w = meshFile.Read<float>(); newBone.BindPose.Data[1].x = meshFile.Read<float>(); newBone.BindPose.Data[1].y = meshFile.Read<float>(); newBone.BindPose.Data[1].z = meshFile.Read<float>(); newBone.BindPose.Data[1].w = meshFile.Read<float>(); pModel->m_Skeleton.push_back(newBone); } //Read AnimClips auto nrOfAnimClips = meshFile.Read<unsigned int>(); for(unsigned int i=0; i < nrOfAnimClips; ++i){ AnimationClip newClip; newClip.Name = meshFile.ReadString(); newClip.KeysPerSecond = meshFile.Read<float>(); auto nrOfKeys = meshFile.Read<unsigned int>(); for(unsigned int iKey=0; iKey < nrOfKeys; ++iKey){ AnimationKey newKey; newKey.KeyTime = meshFile.Read<float>(); for(unsigned int iBone=0; iBone < nrOfBones; ++iBone){ /* D3DXMATRIX transform; for(unsigned int row=0; row < 4; ++row) for(unsigned int col=0; col < 3; ++col) transform.m[row][col] = meshFile.Read<float>(); transform._14 = transform._24 = transform._34 = 0; transform._44 = 1; newKey.BoneTransforms.push_back(transform); */ tt::DualQuaternion newDQ; newDQ.Data[0].x = meshFile.Read<float>(); newDQ.Data[0].y = meshFile.Read<float>(); newDQ.Data[0].z = meshFile.Read<float>(); newDQ.Data[0].w = meshFile.Read<float>(); newDQ.Data[1].x = meshFile.Read<float>(); newDQ.Data[1].y = meshFile.Read<float>(); newDQ.Data[1].z = meshFile.Read<float>(); newDQ.Data[1].w = meshFile.Read<float>(); newKey.BoneTransforms.push_back(newDQ); } newClip.Keys.push_back(newKey); } pModel->m_AnimClips.push_back(newClip); } } //Build bounding box pModel->m_BoundingBox.Initialize(pModel->m_Positions.data); return std::unique_ptr<Model3D>(pModel); }
// FIXME Some problem should occur with gmsh because nodes are sometimes duplicated. void TetraMesh::read_gmsh (const char* name) { ifstream meshFile(name); if(!meshFile) { cerr << "TetraMesh::read_gmsh, cannot open file " << name << endl; return; } string token; char not_read[150]; meshFile >> token; while(meshFile) { if (token == "$Nodes") { unsigned int nbnodes; unsigned int num_node; Node N; meshFile >> nbnodes; for (unsigned int n = 0 ; n < nbnodes ; ++n) { meshFile >> num_node >> N.x >> N.y >> N.z; // cout << num_node << " " << N.x << "" << N.y << " " << N.z << endl; node.push_back(N); } } if (token == "$Elements") { unsigned int nbElements; unsigned int num_element, element_type, nbTags ; Tetraedre T; unsigned int t = 0; meshFile >> nbElements; for (unsigned int e = 0 ; e < nbElements ; ++e) { meshFile >> num_element >> element_type; // 4-node tetrahedron if (element_type != 4) { meshFile.getline(not_read,150); continue; } meshFile >> nbTags; // the third tag is the number of a mesh partition to which the element belongs unsigned int tag; for (unsigned int tg = 0 ; tg < nbTags ; ++(tg)) { meshFile >> tag; } meshFile >> T.nodeId[0] >> T.nodeId[1] >> T.nodeId[2] >> T.nodeId[3]; // nodeId has 0-offset // (0 in C/C++ corresponds to 1 in the file) T.nodeId[0] -= 1; T.nodeId[1] -= 1; T.nodeId[2] -= 1; T.nodeId[3] -= 1; node[T.nodeId[0]].tetraOwner.push_back(t); node[T.nodeId[1]].tetraOwner.push_back(t); node[T.nodeId[2]].tetraOwner.push_back(t); node[T.nodeId[3]].tetraOwner.push_back(t); tetraedre.push_back(T); ++t; } }