• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
irreversible

Determining vertex winding order

13 posts in this topic

bool GetFacetWinding(IN IFacet * facet)
{
	TReal area = 0;
	TReal p0x, p0y, p1x, p1y;
	IMesh* m = facet->ownermesh;

	IVector3D vNormal = CalculateFacetNormal(facet);

	int iAxis0, iAxis1;

        //project the triangle
	IMath::GetMinorAxes(vNormal, iAxis0, iAxis1);

	for(int i = 0; i < facet->indexes.size() - 1; i++)
		{
		p0x = m->vertices[facet->indexes[i    ]][iAxis0] - m->vertices[0][iAxis0];
		p0y = m->vertices[facet->indexes[i    ]][iAxis1] - m->vertices[0][iAxis1];

		p1x = m->vertices[facet->indexes[i + 1]][iAxis0] - m->vertices[0][iAxis0];
		p1y = m->vertices[facet->indexes[i + 1]][iAxis1] - m->vertices[0][iAxis1];

		area += (p0x * -p1y) + (p1x * p0y);
		}

	return(area < 0);
}

I'm getting opposite winding for opposite sides of a cube. This can't be right as the cube renders just fine. Either I'm missing something super obvious here or I need to get some rest.

0

Share this post


Link to post
Share on other sites

Are the windings opposite with respect to the face normal, or what do you mean?  Can you give more detail about what you are seeing that is not working?

0

Share this post


Link to post
Share on other sites

Sorry for being vague.

 

After building a box from faces that should be winding the same way (or else they'd get culled), I would expect all faces to have the same winding (either clockwise or counter-clockwise). This function should return either true or false for all of them. It returns true for faces facing in one direction and false for faces facing in the opposite direction. Here's an example:

 

Normal         Winding

(0, 0, -1)            1

(0, 0, +1)            0

(-1, 0, 0)            1

(+1, 0, 0)            0

(0, -1, 0)             0

(0, +1, 0)            1

 

Looking at my code now, I think I see why the results are wrong - the faces facing in opposite directions aren't being affected by the normal while physically the order indeed is opposite. I'm not sure how to fix it, though.

0

Share this post


Link to post
Share on other sites

The way you determine the normal already depends on the winding (order of vertices), since the two triangle-edge vectors you use to get the normal already have a specific order in your "facet", so the normal has nothing to do with it.

Also, I think (please correct me if I'm wrong) the formula you're using determine the winding only works for 2d triangles but your code also allows higher-order polygons, so your m->vertices should already be transformed into projection space or reference plane, and it should contain only triangles.

In fact, in your case you don't even need to use the area formula, as the sign of the z from the normal will give you the winding (assuming that the original vectors used in the normal cross-product calculation had x and y set to the pre-transformed 2d coordinates and z=0) - i.e, the normal will be pointing either towards positive z or negative z, and the x and y components of the normal will be 0.

If you do use transformed vertices, keep in mind that using a perspective projection matrix will give you the same winding for left& right faces of of a cube when the cube's front/back faces are perfectly parallel to the projection plane. The same goes for the top and bottom faces. Only the front and back faces will have different winding. In an orthogonal projection, all faces that are opposite to each other will have opposite windings, unless the faces are perpendicular to the projection plane, in which case you can't determine the winding (because the z value of the normal will be 0).

Edited by tonemgub
0

Share this post


Link to post
Share on other sites

bool GetFacetWinding(IN IFacet * facet)

This does not make sense. There is no 'right' winding order for any triangle or polygon.

You could write a function which tests for a facet and a camera whether the facet is visible using that particular camera.

You could also test whether all facets of a whole object are winded consistently (for example because you know the object should be watertight).
0

Share this post


Link to post
Share on other sites

 

bool GetFacetWinding(IN IFacet * facet)
This does not make sense. There is no 'right' winding order for any triangle or polygon.

 

You're right - the name of the function should read as IsFacetWindingClockwise().

 

You could also test whether all facets of a whole object are winded consistently (for example because you know the object should be watertight).

 

This is exactly what I'm trying to accomplish. Point in case - the faces of a cube should be wound consistently. My test fails to show that. Note that I'm aiming to determine the winding of a 3D polygon.

 

In short - if I pass in a set of polygons taken from a closed mesh or polytope - even if it's concave -, I want them all to consistently return the same winding.

 

 
Also, I think (please correct me if I'm wrong) the formula you're using determine the winding only works for 2d triangles but your code also allows higher-order polygons, so your m->vertices should already be transformed into projection space or reference plane, and it should contain only triangles.
 

The lines:

 

int iAxis0, iAxis1;

//project the triangle

IMath::GetMinorAxes(vNormal, iAxis0, iAxis1);

 

do exactly that. They use the normal to identify the minor axes (eg the two non-major axes) and the rest of the code uses the triangle that is projected onto this plane as baseline.

 

In fact, in your case you don't even need to use the area formula, as the sign of the z from the normal will give you the winding (assuming that the original vectors used in the normal cross-product calculation had x and y set to the pre-transformed 2d coordinates and z=0) - i.e, the normal will be pointing either towards positive z or negative z, and the x and y components of the normal will be 0.
 

Hm - unless I'm misunderstanding you, you're suggesting the following code should basically give me the winding?

 

bClockwise = vNormal[iMajorAxis] > 0;

 

Where iMajorAxis is the predominant axis of the vector, eg z for (0, 0, -1). I'll be honest, my brain's a little garbled at the moment but at least one case I can see where it fails is for faces that are diagonal with respect to all major planes, eg faces with (unnormalized) normals like (1, 1, 1), (-1, 1, -1) etc. That is, cases where no major axis exists (come to think of it my original code doesn't account for these either).

0

Share this post


Link to post
Share on other sites

You're right - the name of the function should read as IsFacetWindingClockwise().


That function could only make sense if the facet comes with a normal, so it would check if the normal and the winding are consistent or not. If you think that given three points in space there is a notion of "clockwise", you need to revisit your thinking.

One way to convince yourself of this is that you can start with an isosceles triangle and rotate it around the bisector of the angle that's different. When you rotate 180 degrees, you'll get to the same vertices but with different winding. The question is, at what point did the winding change? There is no good answer: You need a normal vector to answer it, which tells you something like where you are looking at the situation from.
0

Share this post


Link to post
Share on other sites

In short - if I pass in a set of polygons taken from a closed mesh or polytope - even if it's concave -, I want them all to consistently return the same winding.


That is somewhat more promising: You can determine if two polygons that share an edge have the same winding or not, just by checking that the common edge is mentioned in reverse order. You can then start with a given winding for one of the polygons and propagate it to neighboring polygons. If your polygons form a closed mesh, you'll get your desired consistency.
0

Share this post


Link to post
Share on other sites

That function could only make sense if the facet comes with a normal, so it would check if the normal and the winding are consistent or not. If you think that given three points in space there is a notion of "clockwise", you need to revisit your thinking.

It looks like he is projecting the triangles onto the xy-plane, so in that sense you could say that one winding order or the other is "clockwise" - but in general I agree with you that you can't define a winding order with just three points.  The usual way to find the winding is to calculate the normal using the cross product of the first two edges and compare that with the face normal vector.  Depending on if they point in the same direction, you can figure out what the winding order is.

 

Since you apparently don't have normal vectors in your facets, you will have to make some assumptions.  If you can guarantee that the mesh is watertight and that all vertex locations that occupy the same position will be the same vertex (i.e. not allow multiple vertices in the same place) then you can check the order of the vertices on each *edge* and compare it to the vertex edge order of the other primitives that share those vertices.  If it is a watertight mesh, you can assume that only one other face will share the vertices of an edge, and that they should be the opposite order in the other face.

 

I think that would give you a good indication of the "sameness" of the winding of the faces, but no information about an absolute winding order.  Hope that helps...

Edited by Jason Z
0

Share this post


Link to post
Share on other sites

 

 

The usual way to find the winding is to calculate the normal using the cross product of the first two edges and compare that with the face normal vector.  Depending on if they point in the same direction, you can figure out what the winding order is.

Yes, without explicit normal info, the winding consistancy is just interpretation open.

Consider a mesh consisting of two planes, not connected, with one plane facing opposite than the other. Will you consider the winding broken? Or will you consider it a correct mesh?

0

Share this post


Link to post
Share on other sites


Consider a mesh consisting of two planes, not connected, with one plane facing opposite than the other. Will you consider the winding broken? Or will you consider it a correct mesh?

Strictly speaking, if you don't specify normal vectors, then there is no way to tell if they are facing opposite directions :)  However, the test that the OP is doing would tell you if the windings were opposite of one another in that test case...

0

Share this post


Link to post
Share on other sites


Just for the record, your last two quotations are wrong. tonemgub said that, not me.

Yup -but  I was talking only about the 2d case, where the winding is relative to the 2d plane, so the normal.z value can be easily interpreted as winding.

Because in OP code snippet, it seems that only 2d values are used in the area calculationL p0x and p0y - there's no p0z.

And in that case, there is no need to use the area at all. He can just use the z value from the normal, assuming that the normal is computed using the dot product, and that the order of vertices used in the vector-subtraction operations to get the two vectors that are passed to the dot product is always the same, for each triangle.

0

Share this post


Link to post
Share on other sites

 

I'm getting opposite winding for opposite sides of a cube. This can't be right as the cube renders just fine

Cube is an enclosed mesh, and as for enclosed meshes, it makes sance to detect winding consistancy. If winding would be inconsistant, you would not have an enclosed mesh, but a spike-open edge mesh. Your algorithm returns different results becouse it projects the cube. You should go the way that Alvaro suggested, that would perfectly work for enclosed meshes.

 

You can determine if two polygons that share an edge have the same winding or not, just by checking that the common edge is mentioned in reverse order.

it does not matter then wheather you start with an hypothetical inverse triangle or correct one, since you check consistancy. This would be a robust function that would return surface validity, wheather it was a cube, or inverted cube-room. But for open meshes without normal info, it is pointless to check on winding.

 

 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0