_new_horizon
05-24-2007, 03:51 PM
My program can import Obj meshes from files and needs to (a) translate the model to the origin and (b) scale the model to a fit within a unit cube (0-1.0). My code mostly works except the model isn't quite placed at the origin - its offset slightly and I can't see what I'm doing wrong :(
Here's a pic of the problem - you can see the feet of the 'Al' model are outside the bounding box:
http://i118.photobucket.com/albums/o105/deep_blue_82/Al.jpg
mVertices is simply a STL <vector*> array of my vertex objects and 'Vec3' is simply my vector class with overloaded operators.
void Mesh::normalize()
{
using namespace std;
unsigned int i;
Vec3 oSize(0,0,0);
// calculate center point
for (i = 0; i < mVertices.size(); i++) {
oSize += mVertices.at(i)->getLocal();
}
Vec3 oCenter(0,0,0);
oCenter = (oSize / (mVertices.size()));
mCenterPoint.setLocal(oCenter);
Vec3 oVec3;
float fMaxDeviation = 0;
// calculate bounds
for (i = 0; i < mVertices.size(); i++)
{
oVec3 = mVertices.at(i)->getLocal();
// get max
fMaxDeviation = max(fMaxDeviation, oVec3.getX());
fMaxDeviation = max(fMaxDeviation, oVec3.getY());
fMaxDeviation = max(fMaxDeviation, oVec3.getZ());
// move to average position
mVertices.at(i)->setLocal(oVec3 - oCenter);
}
// normalize scale (0 - 1.0)
for (i = 0; i < mVertices.size(); i++)
{
oVec3 = mVertices.at(i)->getLocal();
mVertices.at(i)->setLocal(oVec3 /= fMaxDeviation);
}
}Any help appreciated!!!
Here's a pic of the problem - you can see the feet of the 'Al' model are outside the bounding box:
http://i118.photobucket.com/albums/o105/deep_blue_82/Al.jpg
mVertices is simply a STL <vector*> array of my vertex objects and 'Vec3' is simply my vector class with overloaded operators.
void Mesh::normalize()
{
using namespace std;
unsigned int i;
Vec3 oSize(0,0,0);
// calculate center point
for (i = 0; i < mVertices.size(); i++) {
oSize += mVertices.at(i)->getLocal();
}
Vec3 oCenter(0,0,0);
oCenter = (oSize / (mVertices.size()));
mCenterPoint.setLocal(oCenter);
Vec3 oVec3;
float fMaxDeviation = 0;
// calculate bounds
for (i = 0; i < mVertices.size(); i++)
{
oVec3 = mVertices.at(i)->getLocal();
// get max
fMaxDeviation = max(fMaxDeviation, oVec3.getX());
fMaxDeviation = max(fMaxDeviation, oVec3.getY());
fMaxDeviation = max(fMaxDeviation, oVec3.getZ());
// move to average position
mVertices.at(i)->setLocal(oVec3 - oCenter);
}
// normalize scale (0 - 1.0)
for (i = 0; i < mVertices.size(); i++)
{
oVec3 = mVertices.at(i)->getLocal();
mVertices.at(i)->setLocal(oVec3 /= fMaxDeviation);
}
}Any help appreciated!!!