_new_horizon

06-12-2006, 06:02 PM

I'm working on some code to generate vertex normals based on a cutoff-angle (ie. the current facet normal won't be included in the average if the dot product between it and previous facet normal are beyond a certain angle, say 90 degrees). But my code is producing basic 'averaged' normals for a cube which is no good.

Can someone point out what's wrong with this? It should be fairly self-explanatory:

// first calculate all facet normals

for (int i = 0; i < mTriangles.size(); i++) {

mTriangles[i]->computeNormal();

}

// calculate vertex normals

for (unsigned short i = 0; i < mVertices.size(); i++)

{

Vertex::Vertex *vert = mVertices[i];

int avgCount = 1;

const float cos_angle = cos(90.0f * kDegToRad);

Poly *pp = vert->getAdjTriangle(0); // get first triangle

Vec3 lastPolyNormal = pp->getNormal(); // get first normal

vert->mNormal = lastPolyNormal; // set to first facet normal

// loop through all adjacent faces to this vertex

for (int j = 0; j < vert->getAdjTriangleCount(); j++)

{

Poly *ppp = vert->getAdjTriangle(j); // get next face

Vec3 currentNormal = ppp->getNormal(); // get face normal

// is angle between this and previous face more than angle

if (currentNormal.dot(lastPolyNormal) > cos_angle)

{

vert->mNormal += currentNormal;

avgCount++;

// record last face normal used

lastPolyNormal = currentNormal;

}

else {

vert->mNormal = currentNormal;

}

}

if (avgCount > 0) {

vert->mNormal /= avgCount;

}

}

Can someone point out what's wrong with this? It should be fairly self-explanatory:

// first calculate all facet normals

for (int i = 0; i < mTriangles.size(); i++) {

mTriangles[i]->computeNormal();

}

// calculate vertex normals

for (unsigned short i = 0; i < mVertices.size(); i++)

{

Vertex::Vertex *vert = mVertices[i];

int avgCount = 1;

const float cos_angle = cos(90.0f * kDegToRad);

Poly *pp = vert->getAdjTriangle(0); // get first triangle

Vec3 lastPolyNormal = pp->getNormal(); // get first normal

vert->mNormal = lastPolyNormal; // set to first facet normal

// loop through all adjacent faces to this vertex

for (int j = 0; j < vert->getAdjTriangleCount(); j++)

{

Poly *ppp = vert->getAdjTriangle(j); // get next face

Vec3 currentNormal = ppp->getNormal(); // get face normal

// is angle between this and previous face more than angle

if (currentNormal.dot(lastPolyNormal) > cos_angle)

{

vert->mNormal += currentNormal;

avgCount++;

// record last face normal used

lastPolyNormal = currentNormal;

}

else {

vert->mNormal = currentNormal;

}

}

if (avgCount > 0) {

vert->mNormal /= avgCount;

}

}