Created
November 5, 2011 07:57
-
-
Save eliangcs/1341253 to your computer and use it in GitHub Desktop.
Creating a Triangle Mesh with 3ds Max SDK
This file contains hidden or 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
TriObject *createTriangleMesh(const std::vector<Point3> &points, | |
const std::vector<Point3> &normals, | |
const std::vector<Point2> &uvs, | |
const std::vector<int> &triangleVertIndices) | |
{ | |
TriObject *triobj = CreateNewTriObject(); | |
if (triobj == NULL) | |
return NULL; | |
assert(points.size() == normals.size() && normals.size() == uvs.size()); | |
assert(triangleVertIndices.size() % 3 == 0); | |
int numVertices = (int) points.size(); | |
int numTriangles = (int) triangleVertIndices.size() / 3; | |
Mesh &mesh = triobj->GetMesh(); | |
// set vertex positions | |
mesh.setNumVerts(numVertices); | |
for (int i = 0; i < numVertices; i++) | |
mesh.setVert(i, points[i]); | |
// set vertex normals | |
mesh.SpecifyNormals(); | |
MeshNormalSpec *normalSpec = mesh.GetSpecifiedNormals(); | |
normalSpec->ClearNormals(); | |
normalSpec->SetNumNormals(numVertices); | |
for (int i = 0; i < numVertices; i++) | |
{ | |
normalSpec->Normal(i) = normals[i].Normalize(); | |
normalSpec->SetNormalExplicit(i, true); | |
} | |
// set UVs | |
// TODO: multiple map channels? | |
// channel 0 is reserved for vertex color, channel 1 is the default texture mapping | |
mesh.setNumMaps(2); | |
mesh.setMapSupport(1, TRUE); // enable map channel | |
MeshMap &map = mesh.Map(1); | |
map.setNumVerts(numVertices); | |
for (int i = 0; i < numVertices; i++) | |
{ | |
UVVert &texVert = map.tv[i]; | |
texVert.x = uvs[i].x; | |
texVert.y = uvs[i].y; | |
texVert.z = 0.0f; | |
} | |
// set triangles | |
mesh.setNumFaces(numTriangles); | |
normalSpec->SetNumFaces(numTriangles); | |
map.setNumFaces(numTriangles); | |
for (int i = 0, j = 0; i < numTriangles; i++, j += 3) | |
{ | |
// three vertex indices of a triangle | |
int v0 = triangleVertIndices[j]; | |
int v1 = triangleVertIndices[j+1]; | |
int v2 = triangleVertIndices[j+2]; | |
// vertex positions | |
Face &face = mesh.faces[i]; | |
face.setMatID(1); | |
face.setEdgeVisFlags(1, 1, 1); | |
face.setVerts(v0, v1, v2); | |
// vertex normals | |
MeshNormalFace &normalFace = normalSpec->Face(i); | |
normalFace.SpecifyAll(); | |
normalFace.SetNormalID(0, v0); | |
normalFace.SetNormalID(1, v1); | |
normalFace.SetNormalID(2, v2); | |
// vertex UVs | |
TVFace &texFace = map.tf[i]; | |
texFace.setTVerts(v0, v1, v2); | |
} | |
mesh.InvalidateGeomCache(); | |
mesh.InvalidateTopologyCache(); | |
return triobj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment