Last active
May 12, 2017 09:13
-
-
Save fwilleke80/21d00376452d88e9127e06918c765e9a to your computer and use it in GitHub Desktop.
[C4D] Get vertex normal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Return the normal vector for a vertex of a polygon object. | |
/// The normal is computed as an average of the normals of all polygons that are neighbors to the specified vertex. | |
/// | |
/// @param[in] op The PolygonObject | |
/// @param[in] neighbor Pointer to a Neighbor object. Must already be initialized, caller owns the pointed object. | |
/// @param[in] pointIndex The index of the vertex we want the normal of | |
/// @return The normal of the point in local object space | |
static Vector GetVertexNormal(PolygonObject *op, Neighbor *neighbor, Int32 pointIndex) | |
{ | |
// Variables | |
CPolygon *pNeighborPoly = nullptr; | |
Int32 *neighborFaceArr = nullptr; | |
Int32 neighborFaceCount = 0; | |
CPolygon *polygonArr = op->GetPolygonW(); | |
Vector *pointArr = op->GetPointW(); | |
Vector resultNormal; | |
// Get polygons attached to point | |
neighbor->GetPointPolys(pointIndex, &neighborFaceArr, &neighborFaceCount); | |
if (neighborFaceCount < 1) | |
return resultNormal; | |
// Iterate the neighboring polygons and compute their face normals | |
for (Int32 faceIndex = 0; faceIndex < neighborFaceCount; ++faceIndex) | |
{ | |
// Get polygon | |
pNeighborPoly = &polygonArr[neighborFaceArr[faceIndex]]; | |
// Compute two vectors along polygon's vertices | |
Vector v1 = pointArr[pNeighborPoly->b] - pointArr[pNeighborPoly->a]; | |
Vector v2 = pointArr[pNeighborPoly->c] - pointArr[pNeighborPoly->a]; | |
// Get cross-product: That's the face normal | |
// Add it to the result normal | |
resultNormal += Cross(v1, v2); | |
} | |
// Devide resultNormal by number of neighboring polygons. | |
// This will get us an average normal of all the normals of the neighboring polygons. | |
resultNormal /= neighborFaceCount; | |
// Return resulting normalized normal | |
return resultNormal.GetNormalized(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment