Esempio n. 1
0
void ObjLoader::LoadAssimp( const std::string &FileName )
{
    auto t0 = std::chrono::high_resolution_clock::now();
    Assimp::Importer Importer;
    const aiScene *scene = Importer.ReadFile( FileName,
                           aiProcessPreset_TargetRealtime_Quality );
    if( !scene )
    {
        std::cout << "ASSIMP cos poszlo nie tak\n";
        std::cout << "ASSIMP ERROR: " << Importer.GetErrorString() << std::endl;
    }

    //w petli przez wszystkie meshe
    const aiMesh * mesh = scene->mMeshes[0];
    for( uint t = 0; t < mesh->mNumFaces ; t++ )
    {
        const aiFace *face = &mesh->mFaces[ t ] ;
        mGpuIndices.push_back( face->mIndices[ 0 ] );
        mGpuIndices.push_back( face->mIndices[ 1 ] );
        mGpuIndices.push_back( face->mIndices[ 2 ] );
    }

    // verteksy
    if( mesh->HasPositions() )
    {
        mVertex.resize( mesh->mNumVertices );
        memcpy( &mVertex[0], mesh->mVertices, 3*4* mesh->mNumVertices );
    }
    if( mesh->HasNormals() )
    {
        mNormals.resize( mesh->mNumVertices  );
        memcpy(  &mNormals[0], mesh->mNormals, 3*4* mesh->mNumVertices );
    }
    if( mesh->HasTextureCoords(0) )
    {
        for( uint k = 0; k < mesh->mNumVertices; ++k )
        {
            glm::vec2 tmp;
            tmp.x =	mesh->mTextureCoords[0][k].x;
            tmp.y = mesh->mTextureCoords[0][k].y;
            mTexcoords.push_back( tmp );
        }
    }
    mGpuBuffer.resize( mesh->mNumVertices*8);

    for( uint i = 0; i < mesh->mNumVertices; ++i )
    {
        mGpuBuffer[ i*8 ] = mVertex[ i ].x;
        mGpuBuffer[ i*8 +1 ] = mVertex[ i ].y;
        mGpuBuffer[ i*8 +2] = mVertex[ i ].z;
        mGpuBuffer[ i*8 +3] = mNormals[ i ].x;
        mGpuBuffer[ i*8 +4] = mNormals[ i ].y;
        mGpuBuffer[ i*8 +5] = mNormals[ i ].z;
        if( mesh->HasTextureCoords(0) ) {
            mGpuBuffer[i * 8 + 6] = mTexcoords[i].s;
            mGpuBuffer[i * 8 + 7] = mTexcoords[i].t;
        }
    }
    auto t1 = std::chrono::high_resolution_clock::now();
    std::chrono::milliseconds total_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0);
    std::cout << "Time: " << total_ms.count() << " ms " << std::endl;
    Surface *pSurface = new Surface();
    VertexBufferManager &VBOMan = VertexBufferManager::GetSingleton();
    IndexBuffer *pIndex = VBOMan.CreateIndexBuffer();
    pIndex->SetIndexData( mGpuIndices );
    pIndex->Prepare();
    VertexDeclaration *pDecl = VBOMan.GetVertexDeclaration( "Default" );
    VertexSettings( PT_TRIANGLE, UT_STATIC_DRAW, pIndex, pDecl );
    VertexBuffer *pVertex = VBOMan.CreateVertexBuffer(
                                VertexSettings( PT_TRIANGLE, UT_STATIC_DRAW, pIndex, pDecl ) );
    pVertex->SetVertexData( mGpuBuffer );
    pVertex->Prepare();
    pSurface->SetVertexBuffer( pVertex );
    mSurfaces.push_back( pSurface );


}