Added some normal smoothing code.
This commit is contained in:
parent
a7a90a03a1
commit
ebcf266bc7
@ -54,6 +54,7 @@ namespace PolyVox
|
|||||||
const bool isEmpty(void) const;
|
const bool isEmpty(void) const;
|
||||||
|
|
||||||
void smooth(float fAmount, bool bIncludeEdgeVertices = false);
|
void smooth(float fAmount, bool bIncludeEdgeVertices = false);
|
||||||
|
void sumNearbyNormals(bool bNormalise = true);
|
||||||
|
|
||||||
POLYVOX_SHARED_PTR<IndexedSurfacePatch> extractSubset(std::set<uint8_t> setMaterials);
|
POLYVOX_SHARED_PTR<IndexedSurfacePatch> extractSubset(std::set<uint8_t> setMaterials);
|
||||||
|
|
||||||
|
@ -171,10 +171,53 @@ namespace PolyVox
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IndexedSurfacePatch::sumNearbyNormals(bool bNormalise)
|
||||||
|
{
|
||||||
|
if(m_vecVertices.size() == 0) //FIXME - I don't think we should need this test, but I have seen crashes otherwise...
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Vector3DFloat> summedNormals(m_vecVertices.size());
|
||||||
|
|
||||||
|
//Initialise all normals to zero. Pretty sure this is ok,
|
||||||
|
//as the vector should stoer all elements contiguously.
|
||||||
|
memset(&summedNormals[0], 0, summedNormals.size() * sizeof(Vector3DFloat));
|
||||||
|
|
||||||
|
for(vector<uint32_t>::iterator iterIndex = m_vecTriangleIndices.begin(); iterIndex != m_vecTriangleIndices.end();)
|
||||||
|
{
|
||||||
|
SurfaceVertex& v0 = m_vecVertices[*iterIndex];
|
||||||
|
Vector3DFloat& v0New = summedNormals[*iterIndex];
|
||||||
|
iterIndex++;
|
||||||
|
SurfaceVertex& v1 = m_vecVertices[*iterIndex];
|
||||||
|
Vector3DFloat& v1New = summedNormals[*iterIndex];
|
||||||
|
iterIndex++;
|
||||||
|
SurfaceVertex& v2 = m_vecVertices[*iterIndex];
|
||||||
|
Vector3DFloat& v2New = summedNormals[*iterIndex];
|
||||||
|
iterIndex++;
|
||||||
|
|
||||||
|
Vector3DFloat sumOfNormals = v0.getNormal() + v1.getNormal() + v2.getNormal();
|
||||||
|
|
||||||
|
v0New += sumOfNormals;
|
||||||
|
v1New += sumOfNormals;
|
||||||
|
v2New += sumOfNormals;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(uint32_t uIndex = 0; uIndex < summedNormals.size(); uIndex++)
|
||||||
|
{
|
||||||
|
if(bNormalise)
|
||||||
|
{
|
||||||
|
summedNormals[uIndex].normalise();
|
||||||
|
}
|
||||||
|
m_vecVertices[uIndex].setNormal(summedNormals[uIndex]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void IndexedSurfacePatch::generateAveragedFaceNormals(bool bNormalise, bool bIncludeEdgeVertices)
|
void IndexedSurfacePatch::generateAveragedFaceNormals(bool bNormalise, bool bIncludeEdgeVertices)
|
||||||
{
|
{
|
||||||
Vector3DFloat offset = static_cast<Vector3DFloat>(m_Region.getLowerCorner());
|
Vector3DFloat offset = static_cast<Vector3DFloat>(m_Region.getLowerCorner());
|
||||||
|
|
||||||
|
//Initially zero the normals
|
||||||
for(vector<SurfaceVertex>::iterator iterVertex = m_vecVertices.begin(); iterVertex != m_vecVertices.end(); iterVertex++)
|
for(vector<SurfaceVertex>::iterator iterVertex = m_vecVertices.begin(); iterVertex != m_vecVertices.end(); iterVertex++)
|
||||||
{
|
{
|
||||||
if(m_Region.containsPoint(iterVertex->getPosition() + offset, 0.001))
|
if(m_Region.containsPoint(iterVertex->getPosition() + offset, 0.001))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user