[Maya] to return vertex from a triangle in CW or CCW?

Hi!

I am doing an tools to export my 3D from Maya.

This purely a challenge, I am already using FBX or Collada for my work.

But I would like to understand more about 3D data and practice another language than Mel-Script then…

I currently have a problem under OpenGL.
When I import my vertex list with the GL_Triangle method and using the glCullFace(GL_Back) function to delete the faces on the backside, it creates lot of holes in my model…

Problem seems to me the order of my vertex for each triangle. :sigh:

In Maya I get my 3 vertex list from a triangle with the polyListComponentConversion function.
But I think Maya doesn’t mind of the right order… seems to be random…:x:

Then I would like to know if there is another way to get my vertex list in the Clock Way (CW) or Counter Clock Way (CCW). This could resolve my problem…

If they is no script for it yet under Maya with Mel or Python, maybe a simple explanation how we usually do that could help me to find a solution…

Really thank you by advance.:):

The Maya API MFnMesh’s getTriangles and getVertices will get you there a lot faster.

If you really want to do it in script though, you can get your Face Normal, then take the Cross Product of two of the edges of your triangle. If the dot product of the Normal and the result of your Cross are the same sign, you have the correct winding. Otherwise, you need to reverse it. I may be wrong about which sign is correct and which one is wrong, depending on how you structure your Cross Product. AxB is different from BxA.

Wow really thank you! I will try it in this way!!

About Maya API I didn’t start yet…
I wait to have more experience with C++ under OpenGL…
And then to come back on Maya!

Sorry, I was tired. “Same Sign” is wrong. I meant “negative” In Pseudo:

// For triangle verts A, B, and C
E1 = B - A
E2 = C - A
test = dot( Normal, cross( E1, E2 ) )
if test > 0
CW!
else if test < 0
CCW!
else // test == 0
Degenerate Triangle!

Just found this page too: http://xyz2.net/mel/mel.055.htm
I guess when all you have is a Linear Algebra shaped hammer, everything starts to look like a dot product shaped nail. I am still quite confident in the dot(cross()) but those methods may be more clear.

wowo the last url is amazing!

I tried it on the fly with a triangle I had the problem and it works well!!

But really… I understand why some colleague asked to me to definitely stop MelScript for Python…

In my current code I have the following line :

mc.ls(mc.polyListComponentConversion(triangle, tv=1, ff=1), fl=1)

And it replaces half of the explanation from the url…

Now I will try to understand a little bit more the last part with the right vtx order with an edge… look pretty interesting!

Really thank you one more time!

I will come back with the python code to get the right vtx order from a triangle! :lookdown:

[QUOTE=Lithium;6144]
// For triangle verts A, B, and C
E1 = B - A
E2 = C - A
test = dot( Normal, cross( E1, E2 ) )
if test > 0
CW!
else if test < 0
CCW!
else // test == 0
Degenerate Triangle![/QUOTE]

I finally choose this way!
Because of 2 reasons :

  • Mel Script and Python don’t return always the same datas…
  • Your vector method is better for understanding how it works than using Maya`s stuff… and can also use it for something else ^_^…

Here is the Python code to get the CW vertex list from a triangle:

def samaSetVtxCW (triangle):
	vtx = mc.ls(mc.polyListComponentConversion(triangle, tv=1), fl=1)
	way = samaGetVtxWay (vtx, triangle)
	if way=="CCW":vtx = [vtx[1], vtx[0], vtx[2]]
	return vtx;

def samaGetVtxWay (vtx, triangle):
	normal = 	samaGetTriangleNormal (triangle)
	A = mc.xform(vtx[0], q=1, a=1, ws=1, t=1)
	B =mc.xform(vtx[1], q=1, a=1, ws=1, t=1)
	C = mc.xform(vtx[2], q=1, a=1, ws=1, t=1)
	
	E1 = samaSubstractVector (B, A)
	E2 = samaSubstractVector (C, A)
	
	test = samaDotProduct(normal, samaCrossProduct( E1, E2 ) )
	
	way =""
	if test>0:way = "CW"
	elif test<0:way = "CCW"
	
	return way;	

def samaCrossProduct (u, v):
	A = (u[1]*v[2]) - (u[2]*v[1])
	B = (u[2]*v[0]) - (u[0]*v[2])
	C = (u[0]*v[1]) - (u[1]*v[0])
	
	return [A, B, C];
	
def samaDotProduct (u, v):
	return u[0]*v[0]+u[1]*v[1]+u[2]*v[2];


def samaSubstractVector (u, v):
	A = u[0] - v[0]
	B = u[1] - v[1]
	C = u[2] - v[2]
	
	return [A, B, C];

def samaGetTriangleNormal (triangle):
	tmp = mc.polyInfo(triangle, fn=1)
	ls = tmp[0].split(" ", tmp[0].count(" "))
	
	return [float(ls[len(ls)-3]), float(ls[len(ls)-2]), float(ls[len(ls)-1])];

One more time, really thank you for your help, backface culling is working perfectly!
… but now I need to modify the Uvs info… because it doesn’t match anymor ewith the old triangle with CCW… :curses::D:

It works perfectly!!

http://a.samavan.com/openGL/SamaTest_C.exe

One more time thank you!!