gmseed

11-22-2011, 07:38 AM

Hi

When reading about OpenGL we see that it supports face culling; eg:

@Override

public void init(GLAutoDrawable gLDrawable)

{

GL gl = gLDrawable.getGL();

//...

// enable face culling

gl.glEnable(GL.GL_CULL_FACE);

gl.glCullFace(GL.GL_BACK);

gl.glFrontFace(GL.GL_CW);

}

This is easy to understand for a 2D triangle with vertices [v0,v1,v2] ordered either [v0,v1,v2] for an anticlockwise vertex ordering or ordered [v0,v2,v1] for a clockwise ordering.

However, the texts always fail to discuss in detail how to handle the 3D case. For example, how are we to handle the following list of triangle vertices if the ordering is not specified:

class CubeTriangles

{

public static int vertexCount = 12 * 3;

public static final float vertexData[] =

{

0.25f, 0.25f, -1.25f, 1.0f,

0.25f, -0.25f, -1.25f, 1.0f,

-0.25f, 0.25f, -1.25f, 1.0f,

0.25f, -0.25f, -1.25f, 1.0f,

-0.25f, -0.25f, -1.25f, 1.0f,

-0.25f, 0.25f, -1.25f, 1.0f,

0.25f, 0.25f, -2.75f, 1.0f,

-0.25f, 0.25f, -2.75f, 1.0f,

0.25f, -0.25f, -2.75f, 1.0f,

0.25f, -0.25f, -2.75f, 1.0f,

-0.25f, 0.25f, -2.75f, 1.0f,

-0.25f, -0.25f, -2.75f, 1.0f,

-0.25f, 0.25f, -1.25f, 1.0f,

-0.25f, -0.25f, -1.25f, 1.0f,

-0.25f, -0.25f, -2.75f, 1.0f,

//...

If the vertices are to be correctly ordered when filling a vertex buffer then how is the clockwise/anticlockwise ordering resolved? Let's say a triangle has vertices [v0,v1,v2] and the cross-product of v0v1 x v0v2 gives a normal n about which the vertices are ordered anti-clockwise. Viewed from the opposite side [-n] then the vertices are ordered clockwise. Thus, I can't see how the ordering is resolved unless a fixed reference vector is specified. This may be possible for triangles making up a sphere but what about twisted and convoluted surfaces?

If anyone knows the established and robust way of handling 3D face ordering/culling I'd appreciate hearing from you.

Thanks

Graham

When reading about OpenGL we see that it supports face culling; eg:

@Override

public void init(GLAutoDrawable gLDrawable)

{

GL gl = gLDrawable.getGL();

//...

// enable face culling

gl.glEnable(GL.GL_CULL_FACE);

gl.glCullFace(GL.GL_BACK);

gl.glFrontFace(GL.GL_CW);

}

This is easy to understand for a 2D triangle with vertices [v0,v1,v2] ordered either [v0,v1,v2] for an anticlockwise vertex ordering or ordered [v0,v2,v1] for a clockwise ordering.

However, the texts always fail to discuss in detail how to handle the 3D case. For example, how are we to handle the following list of triangle vertices if the ordering is not specified:

class CubeTriangles

{

public static int vertexCount = 12 * 3;

public static final float vertexData[] =

{

0.25f, 0.25f, -1.25f, 1.0f,

0.25f, -0.25f, -1.25f, 1.0f,

-0.25f, 0.25f, -1.25f, 1.0f,

0.25f, -0.25f, -1.25f, 1.0f,

-0.25f, -0.25f, -1.25f, 1.0f,

-0.25f, 0.25f, -1.25f, 1.0f,

0.25f, 0.25f, -2.75f, 1.0f,

-0.25f, 0.25f, -2.75f, 1.0f,

0.25f, -0.25f, -2.75f, 1.0f,

0.25f, -0.25f, -2.75f, 1.0f,

-0.25f, 0.25f, -2.75f, 1.0f,

-0.25f, -0.25f, -2.75f, 1.0f,

-0.25f, 0.25f, -1.25f, 1.0f,

-0.25f, -0.25f, -1.25f, 1.0f,

-0.25f, -0.25f, -2.75f, 1.0f,

//...

If the vertices are to be correctly ordered when filling a vertex buffer then how is the clockwise/anticlockwise ordering resolved? Let's say a triangle has vertices [v0,v1,v2] and the cross-product of v0v1 x v0v2 gives a normal n about which the vertices are ordered anti-clockwise. Viewed from the opposite side [-n] then the vertices are ordered clockwise. Thus, I can't see how the ordering is resolved unless a fixed reference vector is specified. This may be possible for triangles making up a sphere but what about twisted and convoluted surfaces?

If anyone knows the established and robust way of handling 3D face ordering/culling I'd appreciate hearing from you.

Thanks

Graham