我想使用Maya Api复制Open GL中的形状,包括法线。 我们将一个立方体作为源形状。
Maya的MFnMesh :: getPoints将返回8分 Maya的MFnMesh :: getNormals将返回24个法线(每个顶点3个法线,或每个顶点每个顶点1个法线)。
glDrawElements需要顶点列表和法线列表对应,这不是这里的情况。如何对齐两个列表以便glDrawElement正确地将法线分配给顶点?
我相信顶点列表中的顶点需要重复才能成为24个点,而MFnMesh :: getPolygonVertices需要指向重复的顶点。有什么办法吗?
答案 0 :(得分:4)
顶点法线与顶点不是1:1:它们与顶点面顶点只有1:1。显示硬边需要maya为定义边的每个顶点设置两个法线值。您进入给定顶点的边缘越多,顶点可能支持的法线越多:它将始终至少有一个,但每个传入边可以有多达1个法线。你的24个法线反映了它们每个立方体顶点有3个硬边进入
的事实要正确获得法线,你应该使用顶点面顶点,而不是普通顶点。
from maya.api.OpenMaya import MFnMesh, MGlobal, MSpace, MVector
mobj = MGlobal.getSelectionListByName('pCubeShape1').getDagPath(0)
mesh = MFnMesh(mobj)
space = MSpace.kWorld
def vertices_and_normals(mesh_fn, face, space):
'''
mesh_fn = an MFnMesh, face = an integer, space = an MSpace enum valu
'''
normals = [MVector(m) for m in mesh_fn.getFaceVertexNormals(face, space=space)]
verts = [mesh_fn.getPoint(f, space=space) for f in mesh_fn.getPolygonVertices(face)]
return zip(verts, normals)
for f in range(6):
for v, n in vertices_and_normals(mesh,f, space):
print "v", v, "n", n
v (-0.5, -0.5, 0.5, 1) n (0, 0, 1)
v (0.5, -0.5, 0.5, 1) n (0, 0, 1)
v (0.5, 0.5, 0.5, 1) n (0, 0, 1)
v (-0.5, 0.5, 0.5, 1) n (0, 0, 1)
v (-0.5, 0.5, 0.5, 1) n (0, 1, 0)
v (0.5, 0.5, 0.5, 1) n (0, 1, 0)
v (0.5, 0.5, -0.5, 1) n (0, 1, 0)
v (-0.5, 0.5, -0.5, 1) n (0, 1, 0)
v (-0.5, 0.5, -0.5, 1) n (0, 0, -1)
v (0.5, 0.5, -0.5, 1) n (0, 0, -1)
v (0.5, -0.5, -0.5, 1) n (0, 0, -1)
v (-0.5, -0.5, -0.5, 1) n (0, 0, -1)
v (-0.5, -0.5, -0.5, 1) n (0, -1, 0)
v (0.5, -0.5, -0.5, 1) n (0, -1, 0)
v (0.5, -0.5, 0.5, 1) n (0, -1, 0)
v (-0.5, -0.5, 0.5, 1) n (0, -1, 0)
v (0.5, -0.5, 0.5, 1) n (1, 0, 0)
v (0.5, -0.5, -0.5, 1) n (1, 0, 0)
v (0.5, 0.5, -0.5, 1) n (1, 0, 0)
v (0.5, 0.5, 0.5, 1) n (1, 0, 0)
v (-0.5, -0.5, -0.5, 1) n (-1, 0, 0)
v (-0.5, -0.5, 0.5, 1) n (-1, 0, 0)
v (-0.5, 0.5, 0.5, 1) n (-1, 0, 0)
v (-0.5, 0.5, -0.5, 1) n (-1, 0, 0)