int SceneLoader::LoadFile(const char* filename)
{
	Assimp::Importer importer;

	const aiScene *scene = importer.ReadFile(filename, 0);
	scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace | aiProcess_MakeLeftHanded | aiProcess_FlipWindingOrder | aiProcess_JoinIdenticalVertices);
	if (!scene)
	{
		std::stringstream oss;
		oss << "ERROR - File: " << filename << " not found." << std::endl;
		std::string debugMsg(oss.str());
		OutputDebugStringA(debugMsg.c_str());
		return false;
	}
	DrawableObject *newObject = new DrawableObject();
	std::vector<Vertex> vertexList;
	std::vector<UINT> indexList;
	std::stringstream oss;
	for (unsigned int i = 0; i < scene->mRootNode->mNumChildren; ++i)
	{
		bool successfulLoad = true;
		aiNode* currentNode = scene->mRootNode->mChildren[i];
		BuildShaders(d3dDevice, *newObject, mShaderManager);
		for (unsigned int j = 0; j < currentNode->mNumMeshes; ++j)
		{
			ProcessMesh(d3dDevice, *scene->mMeshes[currentNode->mMeshes[j]], *newObject, vertexList, indexList, scene->mMeshes[currentNode->mMeshes[j]]->mMaterialIndex - 1);
			//LoadMaterials(d3dDevice, scene->mMeshes[currentNode->mMeshes[j]]->mMaterialIndex, *newObject, scene);
			oss << "MatIndex = " << scene->mMeshes[currentNode->mMeshes[j]]->mMaterialIndex << "\n";
		}
	}
	std::string debugMsg(oss.str());
	OutputDebugStringA(debugMsg.c_str());
	for (unsigned int i = 0; i < scene->mNumMaterials; ++i)
	{
		LoadMaterials(d3dDevice, i, *newObject, scene);
	}
	newObject->GetMeshData()->Initialize(d3dDevice, vertexList, indexList);
	mDrawableObjects.push_back(newObject);

	return mDrawableObjects.size() - 1;
}
Exemplo n.º 2
0
ScenePtr ModelLoader::loadScene(const std::string & path) {
	//Création de la scène
	ScenePtr scene(new Scene);
	SceneObjectPtr &rootObject = scene->getRootObject();

	//Chargement du modèle dans assimp
	Assimp::Importer importer;
	const aiScene *aiScene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
	if (!aiScene) {
		throw std::runtime_error(importer.GetErrorString());
	}
	importer.ApplyPostProcessing(aiProcess_GenNormals);

	if (!aiScene || aiScene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !aiScene->mRootNode) {
		cout << "ERROR::ASSIMP::" << importer.GetErrorString() << endl;
		return ScenePtr(nullptr);
	}
	m_directory = path.substr(0, path.find_last_of('/'));

	processNode(aiScene, scene, aiScene->mRootNode, rootObject, glm::mat4());

	importer.FreeScene();
	return std::move(scene);
}
Exemplo n.º 3
0
MeshPtr Helper::loadMesh(std::string path){
	MeshPtr res = nullptr;


	Assimp::Importer importer;
	const aiScene* scene = importer.ReadFile(path.c_str(),aiProcess_Triangulate|aiProcess_GenSmoothNormals|aiProcess_GenUVCoords|aiProcess_CalcTangentSpace);
	scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace);
	if(!scene){
		cout<<"ERROR LOADING MESH: "<<path<<endl;
		return nullptr;
	}

	for(int i=0;i<scene->mNumMeshes;i++){
		GeometryPtr meshGEO = GeometryPtr(new Geometry());
		MaterialPtr meshMAT = MaterialPtr(new Material());

		const aiMesh* mesh = scene->mMeshes[i];
		const aiVector3D Zero3D(0.0f,0.0f,0.0f);
		const aiColor4D EmptyColor(0.0f,0.0f,0.0f,0.0f);

		for(unsigned int i=0;i<mesh->mNumVertices;i++){

		
			const aiVector3D* pPos = &(mesh->mVertices[i]);
			const aiVector3D* pNormal = mesh->HasNormals()? &(mesh->mNormals[i]):&Zero3D;
			const aiColor4D* pColor =mesh->HasVertexColors(0)? (mesh->mColors[i]):&EmptyColor;
			const aiVector3D* pTexCoord = mesh->HasTextureCoords(0)? &(mesh->mTextureCoords[0][i]):&Zero3D;
			const aiVector3D* pTangent = mesh->HasTangentsAndBitangents()? &(mesh->mTangents[i]):&Zero3D;

			cout<<pTangent->x<<" | "<<pTangent->y<<" | "<<pTangent->z<<endl;

			glm::vec3 pos = glm::vec3(pPos->x,pPos->y,pPos->z);
			glm::vec3 norm = glm::vec3(pNormal->x,pNormal->y,pNormal->z);
			glm::vec3 col = glm::vec3(pColor->r,pColor->g,pColor->b);
			glm::vec2 tex = glm::vec2(pTexCoord->x,pTexCoord->y);
			glm::vec3 tan = glm::vec3(pTangent->x,pTangent->y,pTangent->z);

			meshGEO->pushVertex(Vertex(pos,col,tex,norm,tan));
		}

		for(unsigned int i=0;i<mesh->mNumFaces;i++){
			const aiFace& face = mesh->mFaces[i];
			meshGEO->pushFace(face.mIndices[0],face.mIndices[1],face.mIndices[2]);
		}

		string Dir = Helper::getDirectory(path);
		aiString texPath;
		if(AI_SUCCESS == scene->mMaterials[mesh->mMaterialIndex]->GetTexture(aiTextureType_DIFFUSE,0,&texPath)){
			meshMAT->diffuse = TexturePtr(new Texture(Dir+texPath.data,0));
		}
		if(AI_SUCCESS == scene->mMaterials[mesh->mMaterialIndex]->GetTexture(aiTextureType_HEIGHT,0,&texPath)){
			meshMAT->normal = TexturePtr(new Texture(Dir+texPath.data,0));
		}
		if(res == nullptr){
			res = MeshPtr(new Mesh(meshGEO,meshMAT));
		}else{
			res->addSubMesh(MeshPtr(new Mesh(meshGEO,meshMAT)));
		}
	}

	return res;
}
Exemplo n.º 4
0
bool StaticMesh::loadFromFile(const std::string& infile)
{
    Assimp::Importer imp;

    // Load scene and grab first mesh only
    imp.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE,
                           aiPrimitiveType_LINE |
                           aiPrimitiveType_POINT |
                           aiPrimitiveType_POLYGON, nullptr);

    #ifdef DEBUG
    std::cout << "Calling Assimp::Importer::ReadFile...\n";
    #endif // DEBUG

    const aiScene *scene = imp.ReadFile(infile, aiProcessPreset_TargetRealtime_Quality
                                                & (!aiProcess_SplitLargeMeshes));
    #ifdef DEBUG
    std::cout << "Checking for a mesh...\n";
    #endif // DEBUG
    if(scene == nullptr)
        return false;
    else if(!scene->HasMeshes())
        return false;

    imp.ApplyPostProcessing(aiProcess_JoinIdenticalVertices);

    // Will only load one mesh
    const aiMesh *mesh = scene->mMeshes[0];

    #ifdef DEBUG
    std::cout << "Checking mesh has what we need...\n";
    #endif // DEBUG
    // Don't process special mesh types
    if(!mesh->HasPositions() ||
       !mesh->HasFaces())
        return false;

    vertexCount = mesh->mNumVertices;
    triangleCount = mesh->mNumFaces;

    // Check what attributes are in the mesh and
    // calculate total count of floats
    std::size_t componentCount = components.vertex = 3;
    if(mesh->HasNormals())
    {
        componentCount += components.normal = 3;
        #ifdef DEBUG
        std::cout << "Has normals\n";
        #endif // DEBUG
    }
    if(mesh->HasTangentsAndBitangents())
    {
        componentCount += (components.tangentBitangent = 3) * 2;
        #ifdef DEBUG
        std::cout << "Has tangents and bitangents\n";
        #endif // DEBUG
    }
    if(mesh->HasTextureCoords(0))
    {
        componentCount += components.texture = 3;
        #ifdef DEBUG
        std::cout << "Has texture coordinates\n";
        #endif // DEBUG
    }

    // Scale size for size of float
    std::size_t totalSize = componentCount * sizeof(float) * vertexCount;

    #ifdef DEBUG
    std::cout << totalSize << std::endl;
    std::cout << "Allocating and mapping vertex buffer...\n";
    #endif // DEBUG

    // Set size and map buffer
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, totalSize, nullptr, GL_STATIC_DRAW);
    GLfloat *mapPtr = (GLfloat*) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
//    glNamedBufferDataEXT(buffer, totalSize, nullptr, GL_STATIC_DRAW);
//    GLfloat *mapPtr = (GLfloat*) glMapNamedBufferEXT(buffer, GL_WRITE_ONLY);
    if(mapPtr == nullptr)
        return false;

    #ifdef DEBUG
    std::cout << "Worked!\n";
    #endif // DEBUG

    // Load all attributes into mapped buffer
    for(unsigned int i = 0; i < vertexCount; ++i)
    {
        GLfloat *head = mapPtr + i * componentCount;

        *head++ = mesh->mVertices[i].x;
        *head++ = mesh->mVertices[i].y;
        *head++ = mesh->mVertices[i].z;

        if(mesh->HasNormals())
        {
            *head++ = mesh->mNormals[i].x;
            *head++ = mesh->mNormals[i].y;
            *head++ = mesh->mNormals[i].z;
        }
        if(mesh->HasTextureCoords(0))
        {
            *head++ = mesh->mTextureCoords[0][i].x;
            *head++ = mesh->mTextureCoords[0][i].y;
            *head++ = mesh->mTextureCoords[0][i].z;
        }
        if(mesh->HasTangentsAndBitangents())
        {
            *head++ = mesh->mTangents[i].x;
            *head++ = mesh->mTangents[i].y;
            *head++ = mesh->mTangents[i].z;
            *head++ = mesh->mBitangents[i].x;
            *head++ = mesh->mBitangents[i].y;
            *head++ = mesh->mBitangents[i].z;
        }
    }

    if(!glUnmapBuffer(GL_ARRAY_BUFFER))
        return false;

    #ifdef DEBUG
    std::cout << "Allocating and mapping index buffer...\n";
    #endif // DEBUG

    // Same for index buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangleCount*3*sizeof(GLshort), nullptr, GL_STATIC_DRAW);
    GLushort *indexMapPtr = (GLushort*) glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
//    glNamedBufferDataEXT(indices, triangleCount * 3 * sizeof(GLushort), nullptr, GL_STATIC_DRAW);
//    GLushort *indexMapPtr = (GLushort*) glMapNamedBufferEXT(indices, GL_WRITE_ONLY);
    if(indexMapPtr == nullptr)
        return false;

    #ifdef DEBUG
    std::cout << "Worked!\n";
    #endif // DEBUG

    for(unsigned int i = 0; i < triangleCount; ++i)
    {
        #ifdef DEBUG
        assert(mesh->mFaces[i].mNumIndices == 3);
        #endif // DEBUG
        unsigned int *indexArray = mesh->mFaces[i].mIndices;
        for(unsigned int j = 0; j < 3; ++j)
        {
            #ifdef DEBUG
            assert(*indexArray <= std::numeric_limits<GLushort>::max());
            #endif // DEBUG
            *indexMapPtr++ = *indexArray++;
        }
    }

    if(!glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER))
        return false;;

    #ifdef DEBUG
    std::cout << "Loading mesh successful!\n";
    std::cout << "Setting up vertex array...\n";
    #endif // DEBUG

    glBindVertexArray(vertexArray);

    glBindVertexBuffer(0, buffer, 0, (components.vertex +
                                      components.normal +
                                      components.texture +
                                      components.tangentBitangent * 2)
                                        * sizeof(GLfloat));
    glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
    glVertexAttribBinding(0, 0);
    glEnableVertexAttribArray(0);

    if(components.normal)
    {
        glVertexAttribFormat(1, 3, GL_FLOAT, GL_FALSE, components.vertex * sizeof(float));
        glVertexAttribBinding(1, 0);
        glEnableVertexAttribArray(1);
    }
    if(components.texture)
    {
        glVertexAttribFormat(2, 3, GL_FLOAT, GL_FALSE, (components.vertex +
                                                        components.normal) * sizeof(float));
        glVertexAttribBinding(2, 0);
        glEnableVertexAttribArray(2);
    }
    if(components.tangentBitangent)
    {
        glVertexAttribFormat(3, 3, GL_FLOAT, GL_FALSE, (components.vertex +
                                                        components.normal +
                                                        components.texture) * sizeof(float));
        glVertexAttribBinding(3, 0);
        glEnableVertexAttribArray(3);

        glVertexAttribFormat(4, 3, GL_FLOAT, GL_FALSE, (components.vertex +
                                                        components.normal +
                                                        components.texture +
                                                        components.tangentBitangent) * sizeof(float));
        glVertexAttribBinding(4, 0);
        glEnableVertexAttribArray(4);
    }

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);

    glBindVertexArray(0);

    #ifdef DEBUG
    std::cout << vertexCount << ' ' << triangleCount << std::endl;
    std::cout << +components.vertex << ' ';
    std::cout << +components.normal << ' ';
    std::cout << +components.texture << ' ';
    std::cout << +components.tangentBitangent << std::endl;
    #endif // DEBUG

    return true;
}