Page MenuHomePhorge

Mesh.cpp
No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None

Mesh.cpp

#include <echo/Graphics/Mesh.h>
#include <echo/Graphics/RenderTarget.h>
#include <echo/Graphics/SubMesh.h>
#include <echo/Graphics/RenderPass.h>
#include <iostream>
namespace Echo
{
Mesh::Mesh()
{
mMin.SetZero();
mMax.SetZero();
mSubMeshNames = 0;
mUseSkeleton = false;
}
Mesh::~Mesh()
{
Clear();
}
Vector3 Mesh::GetCentre()
{
return mMin + ((mMax - mMin) / 2);
}
BoneBinding* Mesh::CreateBinding(const std::map< size_t, f32 >& bindingMap)
{
//Find an existing binding
for(u32 b = 0; b < mBoneBindings.size(); ++b)
{
if((*mBoneBindings[b]) == bindingMap)
{
return mBoneBindings[b];
}
}
//None? Create a new one
BoneBinding* binding = new BoneBinding();
binding->mBoneWeights = bindingMap;
mBoneBindings.push_back(binding);
return binding;
}
void Mesh::CacheBindings()
{
if(mSkeleton)
{
for(u32 b = 0; b < mBoneBindings.size(); ++b)
{
BoneBinding* binding = mBoneBindings[b];
binding->mPosition.SetZero();
binding->mScale.Set(1, 1, 1);
binding->mOrientation = Quaternion::IDENTITY;
std::map< size_t, f32 >::iterator bit = (*mBoneBindings[b]).mBoneWeights.begin();
std::map< size_t, f32 >::iterator bitEnd = (*mBoneBindings[b]).mBoneWeights.end();
while(bit != bitEnd)
{
Bone* bone = mSkeleton->GetBone(bit->first);
Vector3 pos = Maths::LinearInterpolate(Vector3(0, 0, 0), bone->GetDerivedPosition(), bit->second);
Vector3 scaleInter = Maths::LinearInterpolate(Vector3(1, 1, 1), bone->GetDerivedScale(), bit->second);
Quaternion orientation = Quaternion::IDENTITY.Nlerp(bone->GetDerivedOrientation(), bit->second, true);
Vector3 scale = scaleInter * bone->GetBindDerivedInverseScale();
Quaternion rotate = orientation * bone->GetBindDerivedInverseOrientation();
Vector3 translate = pos + rotate * (scale * bone->GetBindDerivedInversePosition());
binding->mOffsetTransform.MakeTransform(translate, scale, rotate);
++bit;
}
}
}
}
shared_ptr<SubMesh> Mesh::CreateSubMesh()
{
std::stringstream ss;
while(HasSubmesh(ss.str()))
{
ss.clear();
++mSubMeshNames;
ss << mSubMeshNames;
}
shared_ptr<SubMesh> subMesh = shared_ptr<SubMesh>(new SubMesh(ss.str(), *this));
mSubMeshes.push_back(subMesh);
return shared_ptr<SubMesh>(subMesh);
}
shared_ptr<SubMesh> Mesh::CreateSubMesh(const std::string& name)
{
if(HasSubmesh(name))
{
std::cout << "Mesh::CreateSubMesh() mesh with name '" << name << "' already exists" << std::endl;
return shared_ptr<SubMesh>();
}
shared_ptr<SubMesh> subMesh = shared_ptr<SubMesh>(new SubMesh(name, *this));
mSubMeshes.push_back(subMesh);
return subMesh;
}
shared_ptr<SubMesh> Mesh::GetSubMesh(const std::string& name)
{
for(u32 s = 0; s < mSubMeshes.size(); ++s)
{
if(mSubMeshes[s]->GetName() == name)
return mSubMeshes[s];
}
return shared_ptr<SubMesh>();
}
shared_ptr<SubMesh> Mesh::GetSubMesh(u32 index)
{
if(index < mSubMeshes.size())
return mSubMeshes[index];
return shared_ptr<SubMesh>();
}
shared_ptr<Mesh> Mesh::Clone()
{
Mesh* mesh = new Mesh();
for(u32 s = 0; s < mSubMeshes.size(); ++s)
{
mesh->mSubMeshes.push_back(mSubMeshes[s]->Clone(*mesh));
}
return shared_ptr<Mesh>(mesh);
}
bool Mesh::RemoveSubmesh(const std::string& name)
{
for(u32 s = 0; s < mSubMeshes.size(); ++s)
{
if(mSubMeshes[s]->GetName() == name)
{
mSubMeshes[s] = mSubMeshes[mSubMeshes.size() - 1];
mSubMeshes.pop_back();
return true;
}
}
return false;
}
bool Mesh::HasSubmesh(const std::string& name)
{
for(u32 s = 0; s < mSubMeshes.size(); ++s)
{
if(mSubMeshes[s]->GetName() == name)
return true;
}
return false;
}
void Mesh::Clear()
{
mSubMeshes.clear();
}
void Mesh::SetMaterial(shared_ptr<Material> material)
{
for(u32 s = 0; s < mSubMeshes.size(); ++s)
{
mSubMeshes[s]->SetMaterial(material);
}
}
void Mesh::Render(const Matrix4& m, RenderTarget& renderTarget)
{
renderTarget.SetModelViewMatrix(m);
for(size_t i = 0; i < mSubMeshes.size(); ++i)
{
mSubMeshes[i]->Render(m, renderTarget);
}
}
void Mesh::CalculateAABB()
{
if(mSubMeshes.empty())
{
mMin.SetZero();
mMax.SetZero();
}else
{
mMin.Set(9999999000.0f, 9999999000.0f, 9999999000.0f);
mMax.Set(-9999999000.0f, -9999999000.0f, -9999999000.0f);
for(size_t i = 0; i < mSubMeshes.size(); ++i)
{
mSubMeshes[i]->CalculateAABB();
Vector3 subMeshMin = mSubMeshes[i]->GetMin();
Vector3 subMeshMax = mSubMeshes[i]->GetMax();
mMin.x = std::min(mMin.x,subMeshMin.x);
mMin.y = std::min(mMin.y,subMeshMin.y);
mMin.z = std::min(mMin.z,subMeshMin.z);
mMax.x = std::max(mMax.x,subMeshMax.x);
mMax.y = std::max(mMax.y,subMeshMax.y);
mMax.z = std::max(mMax.z,subMeshMax.z);
}
}
}
void Mesh::CentreMeshToOrigin()
{
//Makes sure the min and max are up to date.
CalculateAABB();
Vector3 translation = -GetCentre();
for(size_t i = 0; i < mSubMeshes.size(); ++i)
{
mSubMeshes[i]->TranslateVertices(translation);
}
CalculateAABB();
}
void Mesh::_GenerateTransformBuffers()
{
if(mSkeleton)
{
for(size_t s = 0; s < mSubMeshes.size(); ++s)
{
mSubMeshes[s]->GenerateTransformBuffers();
}
}
}
void Mesh::SetDiffuseColour(const Colour& diffuseColour)
{
for(size_t s = 0; s < mSubMeshes.size(); ++s)
{
shared_ptr<Material> material=mSubMeshes[s]->GetMaterial();
if(material)
{
RenderPass* pass = material->GetPass(0);
if(pass)
{
pass->SetDiffuse(diffuseColour);
}
}
}
}
}

File Metadata

Mime Type
text/x-c++
Expires
Wed, Jan 15, 10:57 PM (11 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72082
Default Alt Text
Mesh.cpp (5 KB)

Event Timeline