Пример #1
0
Resource* ASEImporter::Import( const String& pFilename, const String& /*pParams*/ )
{
    ASEFile aseFile;

    try
    {
        ASE::MeshReader   reader(aseFile);
        reader.Read( pFilename );
    }
    catch( Exception& /*e*/ )
    {
        return NULL;
    }    

    if(aseFile.mGeomObjects.size() == 0)
        return NULL;
	
    Map<const ASEFile::Material*, ASESubMesh> mSubMeshes; 
    GenerateSubMeshes(aseFile, mSubMeshes);

    // Create mesh instance
    Mesh* newMesh = Cast<Mesh>(GraphicSubsystem::Instance()->Create( Mesh::StaticClass() ));
    
    // Create all submeshes
    Map<const ASEFile::Material*, ASESubMesh>::const_iterator itSubMesh;
    Map<const ASEFile::Material*, ASESubMesh>::const_iterator itSubMeshBegin = mSubMeshes.begin();
    Map<const ASEFile::Material*, ASESubMesh>::const_iterator itSubMeshEnd = mSubMeshes.end();
    for(itSubMesh = itSubMeshBegin; itSubMesh != itSubMeshEnd; ++itSubMesh)
    {
        const ASEFile::Material& mat = *(*itSubMesh).first;
        const ASESubMesh& aseSubMesh = (*itSubMesh).second;

        // Create the sub mesh
        Mesh* subMesh = Cast<Mesh>(GraphicSubsystem::Instance()->Create( Mesh::StaticClass() ));
        newMesh->AddChild(subMesh);

        // Create VB
	    subMesh->GetVertexList().Allocate( aseSubMesh.mVertices.size(), (VertexFormat::Component) (VertexFormat::Position3 | VertexFormat::Normal3 | VertexFormat::TexCoord2 ));
        Vector3f* ptrPosition = subMesh->GetVertexList().GetPositions();
        Vector3f* ptrNormal   = subMesh->GetVertexList().GetNormals();
        Vector2f* ptrTexCoord = subMesh->GetVertexList().GetTextureCoords();

        UInt32 i = 0;
        for(Vector<ASEVertex>::const_iterator itVertex = aseSubMesh.mVertices.begin(); itVertex != aseSubMesh.mVertices.end(); ++itVertex, ++i)
        {
            ptrPosition[i].x = (*itVertex).mPosition.x;
            ptrPosition[i].y = (*itVertex).mPosition.z;
            ptrPosition[i].z = (*itVertex).mPosition.y;
            
            ptrNormal[i].x = (*itVertex).mNormal.x; 
            ptrNormal[i].y = (*itVertex).mNormal.z; 
            ptrNormal[i].z = (*itVertex).mNormal.y; 

            ptrTexCoord[i] = (*itVertex).mUV; 
        }

        // Create IB
        {
            subMesh->GetTriangles().Allocate( TriangleBatch::TriangleList, aseSubMesh.mIndices.size());
            UInt16* ptrIndices = subMesh->GetTriangles().GetIndices();
        
            UInt32 i = 0;
            for(Vector<UInt32>::const_iterator itIndex = aseSubMesh.mIndices.begin(); itIndex != aseSubMesh.mIndices.end(); ++itIndex, ++i)
                ptrIndices[i] = *itIndex;
        }

        // Shader
        {
            TextureShader* shader = GD_NEW(TextureShader, this, "TextureShader");

            if(mat.mMapDiffuse.mBitmap.size() != 0)
            {
                HTexture2D hdl( mat.mMapDiffuse.mBitmap );
                hdl->GetImage().FlipY();
                hdl->Update();
                shader->mTexture = hdl;
            }
            else if(mat.mMapGeneric.mBitmap.size() != 0)
            {
                HTexture2D hdl( mat.mMapGeneric.mBitmap );
                hdl->GetImage().FlipY();
                hdl->Update();
                shader->mTexture = hdl;
            }

            shader->mMaterial.mAmbient   = Color4f(mat.mAmbient.R, mat.mAmbient.G, mat.mAmbient.B);
            shader->mMaterial.mDiffuse   = Color4f(mat.mDiffuse.R, mat.mDiffuse.G, mat.mDiffuse.B);
            //shader->mMaterial.mSpecular  = Color4f(mat.mSpecular.R, mat.mSpecular.G, mat.mSpecular.B);
            shader->mMaterial.mShininess = mat.mShine;
            subMesh->mShader = shader;
        }
    }    

	return newMesh;
}