Mesh face winding

Discussion about everything. New games, 3d math, development tips...
Post Reply
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Mesh face winding

Post by serengeor »

Today and yesterday I was having trouble understanding why my faces are culled with backface culling enabled in opengl.
I can't figure out how the winding works ( I thought I knew before :| )

Does it matter how vertices are arranged in array?
Or does only indices matter when winding a mesh? If so that would mean that no matter how vertices are arranged in array as long as indices point to same vertices the winding wouldn't differ?
Working on game: Marrbles (Currently stopped).
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Mesh face winding

Post by CuteAlien »

Vertex order shouldn't matter as only the indices describe the order. That's unless you work without indices in which case I think some defaults are used and then vertex order would matter.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Mesh face winding

Post by Mel »

The indices matters. They describe the order of the sides of a triangle and thus its winding. The only way to make sure that nothing is hidden wrong is to disable the backface culling.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Mesh face winding

Post by serengeor »

CuteAlien wrote:Vertex order shouldn't matter as only the indices describe the order. That's unless you work without indices in which case I think some defaults are used and then vertex order would matter.
Mel wrote:The indices matters. They describe the order of the sides of a triangle and thus its winding.
I see, so I knew it correctly from the beginning :)
Then it must be something wrong with indices.

Mel wrote:The only way to make sure that nothing is hidden wrong is to disable the backface culling.
the thing is that front faces get culled and back faces don't with backface culling.

Image

If I wind them CW I get normal results, but I my front faces are set to CCW.

Here's how I wind the cube:
Image

Faces:

Code: Select all

 
faceIndices
({
    {7,6,5,4},///top
    {4,5,1,0},///front
    {5,6,2,1},///right
    {6,7,3,2},///back
    {7,4,0,3},///left
    {0,1,2,3} ///bottom
})
 
winding order:

Code: Select all

 
faceWindingMain({0,3,1,1,3,2})
 
winding code:

Code: Select all

 
for(boost::uint32_t i = 0; i < 6; i++)
    {
        const boost::uint8_t voffset = (i*4) + size;
        const boost::uint8_t ioffset = (i*6) + isize;
 
        const boost::uint8_t * winding = /*invertFaceWinding(this->p_nodeVerts, (ECubeFace)i) ? faceWindingInverse :*/ faceWindingMain ;
 
        vertices[0 + voffset] = (Vertex3d(this->p_nodeVerts[faceIndices[i][0]], p_quadTexCoords[0]));
 
        vertices[1 + voffset] = (Vertex3d(this->p_nodeVerts[faceIndices[i][1]], p_quadTexCoords[1]));
 
        vertices[2 + voffset] = (Vertex3d(this->p_nodeVerts[faceIndices[i][2]], p_quadTexCoords[2]));
 
        vertices[3 + voffset] = (Vertex3d(this->p_nodeVerts[faceIndices[i][3]], p_quadTexCoords[3]));
 
        indices[0 + ioffset] = (winding[0]+voffset);
        indices[1 + ioffset] = (winding[1]+voffset);
        indices[2 + ioffset] = (winding[2]+voffset);
 
        indices[3 + ioffset] = (winding[3]+voffset);
        indices[4 + ioffset] = (winding[4]+voffset);
        indices[5 + ioffset] = (winding[5]+voffset);
 }
 
I've been trying to fix this for two days, so I thought that it wouldn't hurt if some spare eyes would help :)
I hope I've given enough information.
Working on game: Marrbles (Currently stopped).
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Mesh face winding

Post by hybrid »

You chose a hard one to imagine in the current place and orientation of the cube. But talking about the side facing us, the correct index order is 0,4,1,1,4,5. Irrlicht uses clock-wise winding, so drawing the triangles should go in that order when viweing the front-sides.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Mesh face winding

Post by serengeor »

hybrid wrote:But talking about the side facing us, the correct index order is 0,4,1,1,4,5. Irrlicht uses clock-wise winding, so drawing the triangles should go in that order when viweing the front-sides.
That would be clock wise winding? I'm using CCW since that's default on opengl, I think.

P.S. I'm rendering this with my own opengl renderer.
Working on game: Marrbles (Currently stopped).
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Mesh face winding

Post by hybrid »

Ah, ok. OpenGL uses CCW, so counter-clock wise by default.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Mesh face winding

Post by serengeor »

Fixed it all.
Thanks for the help :wink:
Working on game: Marrbles (Currently stopped).
Post Reply