Page MenuHomePhorge

MeshManager.cpp
No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None

MeshManager.cpp

#include <echo/Resource/3dsReader.h>
#include <echo/Resource/MeshManager.h>
#include <echo/Resource/MeshReader.h>
#include <echo/Util/StringUtils.h>
#include <echo/Graphics/SubMesh.h>
#include <echo/Graphics/Mesh.h>
#include <echo/FileSystem/FileSystem.h>
#include <boost/bind.hpp>
namespace Echo
{
MeshManager::MeshManager(FileSystem& fileSystem, MaterialManager& materialManager, SkeletonManager& skeletonManager) :
ResourceManager<Mesh>("mesh"),
mFileSystem(fileSystem),
mMaterialManager(materialManager),
mSkeletonManager(skeletonManager),
mMeshNames(0)
{
RegisterLoader("3ds", boost::bind(&Load3DS,_1,_2));
RegisterLoader("mesh", boost::bind(&LoadMesh,_1,_2, boost::ref(mMaterialManager),boost::ref(mSkeletonManager)));
}
MeshManager::~MeshManager()
{
}
bool MeshManager::RegisterLoader(const std::string& fileExtension, MeshLoaderFunc loaderFunction)
{
if(mMeshLoaders.find(fileExtension)!=mMeshLoaders.end())
{
ECHO_LOG_ERROR_LOCATION("MeshManager::RegisterLoader() loader for extension \"" << fileExtension << "\" already registered");
return false;
}
mMeshLoaders[fileExtension]=loaderFunction;
return true;
}
shared_ptr<Mesh> MeshManager::LoadResource(const std::string& resourceFile, const std::string& resourceName)
{
//Determine the extension and load accordingly
u16 lastDot=0;
u16 extensionLength=0;
std::string extension;
lastDot=(u16)resourceFile.find_last_of(".");
lastDot++; //Want the first pos past the dot
extensionLength=(u16)resourceFile.size()-lastDot;
extension=resourceFile.substr(lastDot,extensionLength);
//Must load
std::map<std::string, MeshLoaderFunc >::iterator eit=mMeshLoaders.find(extension);
if(eit==mMeshLoaders.end())
{
ECHO_LOG_ERROR_LOCATION("MeshManager::LoadResource() No Loader found for extension \"" << extension << "\" for file \"" << resourceFile << "\"");
return shared_ptr<Mesh>();
}else
{
shared_ptr<Mesh> resource=(eit->second)(resourceFile,mFileSystem);
if(resource)
{
resource->SetName(resourceName);
}else
{
ECHO_LOG_ERROR_LOCATION("MeshManager::LoadResource() Failed to load resource \"" << resourceFile << "\"");
}
return resource;
}
return shared_ptr<Mesh>();
}
shared_ptr<Mesh> MeshManager::CreateMesh(const std::string& resourceName)
{
shared_ptr<Mesh > mesh(new Mesh());
mesh->SetName(resourceName);
ResourceManager<Mesh>::AddResource(resourceName,mesh);
return ResourceManager<Mesh>::GetResource(resourceName);
}
shared_ptr<Mesh> MeshManager::Load3DS( const std::string& resourceFile, FileSystem& fileSystem)
{
File file = fileSystem.Open(resourceFile);
if(!file.IsOpen())
{
return shared_ptr<Mesh>();
}
shared_ptr<Mesh> mesh(new Mesh());
mesh->SetName(resourceFile);
shared_ptr<SubMesh> subMesh=mesh->CreateSubMesh();
shared_ptr< std::vector<Vector3> > vertices(new std::vector<Vector3>);
shared_ptr< std::vector<u16> > indices(new std::vector<u16>);
std::vector< std::vector<TextureUV> > textureCoordinates;
if(!Read3DSFile(file,*vertices,*indices,textureCoordinates))
{
ECHO_LOG_ERROR("Load3DS: Failed to read 3DS file.");
return shared_ptr<Mesh>();
}
shared_ptr<VertexBuffer> vertexBuffer = subMesh->GetVertexBuffer();
vertexBuffer->AddVertexAttribute("Position",VertexAttribute(VertexAttribute::ComponentTypes::VECTOR3, 1)); //Position
vertexBuffer->AddVertexAttribute("Normal",VertexAttribute(VertexAttribute::ComponentTypes::VECTOR3, 1)); //Normal
vertexBuffer->AddVertexAttribute("Colour",VertexAttribute(VertexAttribute::ComponentTypes::COLOUR_8, 1)); //Colour
std::vector< VertexBuffer::Accessor<TextureUV> > uvs;
for(u32 t=0;t<textureCoordinates.size();++t)
{
std::stringstream ss;
ss << "UV" << t;
vertexBuffer->AddVertexAttribute(ss.str(),VertexAttribute(VertexAttribute::ComponentTypes::TEXTUREUV, 1));
}
Size numberOfVertices = vertices->size();
vertexBuffer->Allocate(numberOfVertices);
for(u32 t=0;t<textureCoordinates.size();++t)
{
std::stringstream ss;
ss << "UV" << t;
uvs.push_back(vertexBuffer->GetAccessor<TextureUV>(ss.str()));
}
// TODO: We're copying this now because I'm trying to get a version one of general vertex buffer usage
// but eventually we should pass the buffer in and have the loader determine how many vertices we need.
VertexBuffer::Accessor<Vector3> destVertices = subMesh->GetComponents<Vector3>("Position");
VertexBuffer::Accessor<Vector3> destNormals = subMesh->GetComponents<Vector3>("Normal");
VertexBuffer::Accessor<VertexColour> destColours = subMesh->GetComponents<VertexColour>("Colour");
subMesh->SetIndices(indices);
for(Size i=0;i < numberOfVertices;++i)
{
destVertices[i] = (*vertices)[i];
destColours[i] = Colour(); // Apparently we don't load colours.
for(Size j=0;j<uvs.size();++j)
{
uvs[j][i] = textureCoordinates[j][i];
}
}
// Apparently we also don't load normals..
subMesh->GenerateNormals();
mesh->GetAxisAlignedBox();
return mesh;
}
shared_ptr<Mesh> MeshManager::LoadMesh( const std::string& resourceFile, FileSystem& fileSystem, MaterialManager& materialManager, SkeletonManager& skeletonManager)
{
File file = fileSystem.Open(resourceFile);
if(!file.IsOpen())
{
return shared_ptr<Mesh>();
}
shared_ptr<Mesh> mesh=ReadMeshFile(file, materialManager,skeletonManager);
return mesh;
}
FileSystem* MeshManager::GetFileSystem() const
{
return &mFileSystem;
}
}

File Metadata

Mime Type
text/x-c++
Expires
Wed, Jan 15, 8:43 PM (2 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72047
Default Alt Text
MeshManager.cpp (5 KB)

Event Timeline