/* * \brief Constructor of the model class. Import the model and the three textures and prepare the buffers. * \param[in] name a string containing the name of the model * \param[in] fModele a const character array containing the path of the file of the model * \param[in] fTex a const character array containing the path of the file of the diffuse color texture * \param[in] fNormalmap a const character array containing the path of the file of the normalmap texture * \param[in] fSpecularmap a const character array containing the path of the file of the specular color texture * \return A well initialized model respecting the parameters */ Model::Model(std::string name, const GLchar * fModele, const GLchar * fTex, const GLchar * fNormalmap, const char * fSpecularmap) : Object() { m_name = name; m_texture_id.push_back(0); m_texture_id.push_back(0); m_texture_id.push_back(0); glGenTextures(3, &m_texture_id[0]); if (!importModel(fModele)) { throw ModelImportException(); } if (!importTexture(fTex, 0)) { throw TextureImportException(); } if (!importTexture(fNormalmap, 1)) { throw NormalmapImportException(); } if (!importTexture(fSpecularmap, 2)) { throw SpecularmapImportException(); } computeTangentSpace(); indexBuffers(); glBindBuffer(GL_ARRAY_BUFFER, m_buffer_id[0]); glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(glm::vec3), &m_vertices[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, m_buffer_id[1]); glBufferData(GL_ARRAY_BUFFER, m_uvs.size() * sizeof(glm::vec2), &m_uvs[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, m_buffer_id[2]); glBufferData(GL_ARRAY_BUFFER, m_normals.size() * sizeof(glm::vec3), &m_normals[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, m_buffer_id[3]); glBufferData(GL_ARRAY_BUFFER, m_tangents.size() * sizeof(glm::vec3), &m_tangents[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, m_buffer_id[4]); glBufferData(GL_ARRAY_BUFFER, m_bitangents.size() * sizeof(glm::vec3), &m_bitangents[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_buffer_id[5]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexes.size() * sizeof(GLushort), &m_indexes[0], GL_STATIC_DRAW); }
//------------------------------------------------------------------------------------------------------- bool Exporter::processMesh(RawMesh& rawMesh, const char* fileName) { rawMesh_ = &rawMesh; meshData_.reset(new(std::nothrow) Mesh::Data); if(!meshData_) { std::cout << "error: not enough memory" << std::endl; return false; } numVertices_ = rawMesh_->positions_.getSize(); meshData_->boundingBox = rawMesh_->boundingBox_; boneIndices_.destroy(); boneWeights_.destroy(); vertices_.clear(); newToOldVertexMapping_.clear(); std::cout << "processing raw mesh..." << std::endl; faces_ = rawMesh_->faces_; if(faces_.getSize() != rawMesh_->faces_.getSize()) { std::cout << "error: not enough memory" << std::endl; return false; } std::cout << "merging faces..." << std::endl; numVertices_ = mergeFaces(faces_, rawMesh_->normalFaces_, faces_, numVertices_); if(numVertices_ == 0) { std::cout << "error: could not merge faces" << std::endl; return false; } numVertices_ = mergeFaces(faces_, rawMesh_->textureFaces_, faces_, numVertices_); if(numVertices_ == 0) { std::cout << "error: could not merge faces" << std::endl; return false; } if(!rawMesh_->bones_.isEmpty()) { std::cout << "reading bone indices and weights..." << std::endl; if(!boneIndices_.create(numVertices_) || !boneWeights_.create(numVertices_)) { std::cout << "error: not enough memory" << std::endl; return false; } uint32_t numFaces = faces_.getSize(); for(uint32_t i = 0; i < numFaces; ++i) { const RawMesh::Face& face = rawMesh_->faces_[i]; for(uint8_t j = 0; j < 3; ++j) { uint32_t vertexIndex = faces_[i].indices[j]; boneIndices_[vertexIndex] = rawMesh_->boneIndices_[face.indices[j]]; boneWeights_[vertexIndex] = rawMesh_->boneWeights_[face.indices[j]]; } } } std::cout << "computing tangent space..." << std::endl; if(!computeTangentSpace()) { std::cout << "error: broken mesh" << std::endl; return false; } std::cout << "preparing vertex streams..." << std::endl; if(!prepareVertexStreams()) { std::cout << "error: not enough memory" << std::endl; return false; } std::cout << "preparing faces..." << std::endl; if(!prepareFaces()) { std::cout << "error: not enough memory" << std::endl; return false; } std::cout << "preparing subsets..." << std::endl; if(!prepareSubsets()) { std::cout << "error: not enough memory" << std::endl; return false; } if(!rawMesh_->bones_.isEmpty()) { std::cout << "preparing skeleton..."; auto& skeleton = meshData_->skeleton; skeleton.reset(new(std::nothrow) Skeleton); if(!skeleton) { std::cout << "error: not enough memory" << std::endl; return false; } skeleton->getBones() = rawMesh_->bones_; if(skeleton->getBones().getSize() != rawMesh_->bones_.getSize()) { std::cout << "error: not enough memory" << std::endl; return false; } } std::cout << "writing mesh to the file..." << std::endl; std::ofstream stream(fileName, std::ios_base::binary); MeshManager meshManager; if(!meshManager.writeMesh(stream, *meshData_)) { std::cout << "error: could not write mesh to the file" << std::endl; return false; } std::cout << "mesh has " << numVertices_; std::cout << " vertices and " << faces_.getSize() << " faces" << std::endl; return true; }