Alternate Back face culling

Member
Posts: 72
Joined: 2006.10
Post: #1
Okay, I'd like to have your opinion on this before I go on with its implementation.

I have a data structure to draw models using flat lighting ( mainly for rendering buildings ). So each vertex in the model can have different normals depending on wich triangle is being drawn.

The data structure is composed of a big vertex array, and a number of index arrays.
Each index array is associated with a normal. ( all triangles with a given normal get drawn all at once ). So drawing is done with the follwoing:
Code:
for( unsigned int i = 0 ; i < mNumOfSets ; i++ )
  {
    {
      glNormal3fv( mVertexSets[i].normal ) ;
      glDrawElements( GL_TRIANGLES ,
                                 mVertexSets[i].mNumVertices ,
                                 GL_UNSIGNED_SHORT ,
                                 mVertexSets[i].mVerticesIndexes ) ;
    }
  }

So I figured that , since I know the normal for a ( potentially VERY big ) number of triangles, I could take the normal, tranform it in the ModelView matrix, and if the z component of the result if greater than the cosine of the FOV ( well, that's not exactly mathematicaly correct, but you get the idea ), I can discard all those triangles. Resulting in more efficient back face culling... However, I have a strange feeling that I'm overlooking something hereNinja

So my question is: Is this making sense? Sneaky

Edit: great, I double checked my post, but managed to make a typo in the thread title Rolleyes

- Sohta
Quote this message in a reply
kberg
Unregistered
 
Post: #2
Usually one would just take the dot product between your camera direction and the normal of the polygon. If the result is negative then that face is back-facing and you can throw it out. However much you optimize this operation, letting your video card handle it for you will almost certainly be faster though.
Quote this message in a reply
Member
Posts: 72
Joined: 2006.10
Post: #3
Back face culling ( by the video card ) is done depending in wich direction the poly is wound. That means that GL has to tranform all the poly's vertices before it can determine if the poly is culled or not.

Also, normals are a vertex attribute, not a poly one.

By batch doing the operation on the normal for a bunch of polygons ( in my case, about 500 polys for certain normals ). I save a lot of trouble, or at least I think so.

Note however, that this is meant to complement, not replace, GL's standard back face culling.

Also , your dot-product would work if the only matrix operations were the camera. Rotating an object would likely screw everything up.

It's not a question of how to do it. ( pretty simple actually ), but wether the whole idea makes sense or not.

- Sohta
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #4
This will work provided the triangles you are grouping together are coplanar.
Quote this message in a reply
Member
Posts: 72
Joined: 2006.10
Post: #5
why? I don't see how this would be a problem, since a normal is just a direction.

Ahh, I think I understand what you mean, I forgot to mention, the normals passed are the actual polygon normals.

- Sohta
Quote this message in a reply
kberg
Unregistered
 
Post: #6
I'm sorry, you are correct. One first has to transform the normal by the modelview matrix... And I see that I misinterpreted your original post as well (sorry, was distracted when I first answered). If you can discard a large number of backfacing poly's that would be good, and the method you describe sounds like it will work to me.
Quote this message in a reply
Member
Posts: 72
Joined: 2006.10
Post: #7
Thanks all, I implemented it and it works like a charm.

Edit: for the sake of completeness, the threshold value is:
cos( atan( aspectRatio * tan( FOV ) ) ) - 1.0

- Sohta
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGLES video play back prathap 0 898 May 9, 2014 03:45 AM
Last Post: prathap
  Calculate face direction from bvh or 3d skeleton data harisz 3 2,850 May 29, 2013 10:50 AM
Last Post: OneSadCookie
  OpenGL ES2 matrix setup (humbly crawling back) Fenris 2 6,428 Aug 31, 2011 06:47 AM
Last Post: Fenris
  The tabbing on a word playing back the recorded voice itcreate 0 2,372 Jun 25, 2010 02:06 PM
Last Post: itcreate
  glDrawElements and Face indices Ashford 8 12,278 Nov 11, 2009 03:03 PM
Last Post: Ashford