Fixed problem with computeNormal() sometimes returning zero length.

This commit is contained in:
David Williams 2007-10-06 00:52:43 +00:00
parent 579c7d379a
commit a2c984408d

View File

@ -702,9 +702,8 @@ namespace Ogre
Vector3 result; Vector3 result;
switch(normalGenerationMethod)
{ if(normalGenerationMethod == SOBEL)
case SOBEL:
{ {
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ)); volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ));
const Vector3 gradFloor = volIter.getSobelGradient(); const Vector3 gradFloor = volIter.getSobelGradient();
@ -722,9 +721,13 @@ namespace Ogre
} }
const Vector3 gradCeil = volIter.getSobelGradient(); const Vector3 gradCeil = volIter.getSobelGradient();
result = ((gradFloor + gradCeil) * -1.0); result = ((gradFloor + gradCeil) * -1.0);
break; if(result.squaredLength() < 0.0001)
{
//Operation failed - fall back on simple gradient estimation
normalGenerationMethod = SIMPLE;
} }
case CENTRAL_DIFFERENCE: }
if(normalGenerationMethod == CENTRAL_DIFFERENCE)
{ {
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ)); volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ));
const Vector3 gradFloor = volIter.getCentralDifferenceGradient(); const Vector3 gradFloor = volIter.getCentralDifferenceGradient();
@ -742,10 +745,13 @@ namespace Ogre
} }
const Vector3 gradCeil = volIter.getCentralDifferenceGradient(); const Vector3 gradCeil = volIter.getCentralDifferenceGradient();
result = ((gradFloor + gradCeil) * -1.0); result = ((gradFloor + gradCeil) * -1.0);
break; if(result.squaredLength() < 0.0001)
{
//Operation failed - fall back on simple gradient estimation
normalGenerationMethod = SIMPLE;
} }
case SIMPLE: }
default: if(normalGenerationMethod == SIMPLE)
{ {
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ)); volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ));
const uchar uFloor = volIter.getVoxel() > 0 ? 1 : 0; const uchar uFloor = volIter.getVoxel() > 0 ? 1 : 0;
@ -765,7 +771,6 @@ namespace Ogre
result = Vector3(0.0, 0.0,uFloor - uCeil); result = Vector3(0.0, 0.0,uFloor - uCeil);
} }
} }
}
return result; return result;
} }