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,68 +702,73 @@ 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));
{ const Vector3 gradFloor = volIter.getSobelGradient();
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ)); if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
const Vector3 gradFloor = volIter.getSobelGradient(); {
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 volIter.setPosition(static_cast<uint>(posX+1.0),static_cast<uint>(posY),static_cast<uint>(posZ));
{
volIter.setPosition(static_cast<uint>(posX+1.0),static_cast<uint>(posY),static_cast<uint>(posZ));
}
if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY+1.0),static_cast<uint>(posZ));
}
if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ+1.0));
}
const Vector3 gradCeil = volIter.getSobelGradient();
result = ((gradFloor + gradCeil) * -1.0);
break;
} }
case CENTRAL_DIFFERENCE: if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{ {
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ)); volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY+1.0),static_cast<uint>(posZ));
const Vector3 gradFloor = volIter.getCentralDifferenceGradient();
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint>(posX+1.0),static_cast<uint>(posY),static_cast<uint>(posZ));
}
if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY+1.0),static_cast<uint>(posZ));
}
if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ+1.0));
}
const Vector3 gradCeil = volIter.getCentralDifferenceGradient();
result = ((gradFloor + gradCeil) * -1.0);
break;
} }
case SIMPLE: if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
default: {
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ+1.0));
}
const Vector3 gradCeil = volIter.getSobelGradient();
result = ((gradFloor + gradCeil) * -1.0);
if(result.squaredLength() < 0.0001)
{ {
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ)); //Operation failed - fall back on simple gradient estimation
const uchar uFloor = volIter.getVoxel() > 0 ? 1 : 0; normalGenerationMethod = SIMPLE;
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 }
{ }
uchar uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0; if(normalGenerationMethod == CENTRAL_DIFFERENCE)
result = Vector3(uFloor - uCeil,0.0,0.0); {
} volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ));
else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 const Vector3 gradFloor = volIter.getCentralDifferenceGradient();
{ if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
uchar uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0; {
result = Vector3(0.0,uFloor - uCeil,0.0); volIter.setPosition(static_cast<uint>(posX+1.0),static_cast<uint>(posY),static_cast<uint>(posZ));
} }
else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{ {
uchar uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0; volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY+1.0),static_cast<uint>(posZ));
result = Vector3(0.0, 0.0,uFloor - uCeil); }
} if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ+1.0));
}
const Vector3 gradCeil = volIter.getCentralDifferenceGradient();
result = ((gradFloor + gradCeil) * -1.0);
if(result.squaredLength() < 0.0001)
{
//Operation failed - fall back on simple gradient estimation
normalGenerationMethod = SIMPLE;
}
}
if(normalGenerationMethod == SIMPLE)
{
volIter.setPosition(static_cast<uint>(posX),static_cast<uint>(posY),static_cast<uint>(posZ));
const uchar uFloor = volIter.getVoxel() > 0 ? 1 : 0;
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
{
uchar uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0;
result = Vector3(uFloor - uCeil,0.0,0.0);
}
else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
uchar uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0;
result = Vector3(0.0,uFloor - uCeil,0.0);
}
else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
uchar uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0;
result = Vector3(0.0, 0.0,uFloor - uCeil);
} }
} }
return result; return result;