what is a clockwise face in openGL
Asked Answered
D

3

8

In back face culling you may either use the face normals to see if the face is pointing away from the camera of you may do some technique with if the triangle is drawn clock wise or counter clock wise.

I am confused about this technique with the clockwise. It seems like with this method, the ordering of the vertex data that gets sent to the graphics card decides whether it is facing towards or away from the camera. I don't see how this makes sense because the camera can be looking in any direction, it's as if the vertex data would have to change based on the camera position and that is obviously not done on the CPU.

How can I understand this?

Dinsmore answered 23/2, 2013 at 14:20 Comment(1)
related question on math.SE: math.stackexchange.com/questions/297588/…Speedometer
W
13

This is one triangle viewed from opposite sides:

clockwise vs counterclockwise

3 points in 3D space are defining a triangle. In addition, if those points are ordered (like here) they are also determining two sides (faces) of a triangle : clockwise and counter-clockwise (they are determined by the order clock put inside a face would point its vertices)

In above example triangle is textured counter-clockwise

Now imagine this triangle is an element of a dice:

ccw textured box

If you roll the dice (or rotate camera around it) our triangle from CCW becomes CW and we don't want it to be textured any more.

This dice would be in fact built out of 12 such triangles and if we order them properly we will be texturing only 6 that are facing camera at a time.

Whiles answered 23/2, 2013 at 14:55 Comment(4)
Just want to add, that this is also known as chirality, or handedness, and that the chirality of a coordinate system also determines the direction in which a normal points, depending on the order in which you put the vectors in the cross product. The statement a×b = -b×a always holds, but flipping chirality has the same effect.Gangland
so the order, either CW or CCW, tells the GPU which side is front, but I don't understand how that is possible since the camera orientation decides whether or not faces are facing front or away.Dinsmore
exactly. that is the whole point. on left hand side of above image camera is 'behind' the triangle and on right hand site it is 'in front' of it. and you want to texture only one side.Whiles
added extra image for you.Whiles
L
5

I'll show you this on an example quad:

  (0,0)-------(1,0)
    |           |
    |     X     |
    |           |
  (0,1)-------(1,1)

Let's assume we are starting at (0,0). Now, if you wind-up (pass) the vertices in "clockwise" order, the next ones would be (1,0), (1,1) and (0,1). (1,0) lays more or less on 2 o'clock, (1,1) is 4, and so on.

Thus the data in GPU memory: 0,0, 1,0, 1,1, 0,1.

When you correlate the windup direction with your setting, you can easily distinguish what's "front" and "back" - to imagine that better, you could draw it on something transparent, then look from the other side - the wind-up direction would be reversed.

So the specification of wind-up direction is merely to tell the GPU which side is front, and which one is back, as it cannot do it by itself - both sides of a triangle are equally good, and the normal vector of given polygon can either be N or -N. However, GPU can verify which of them is aligned with proper wind-up order.

Chapter 2 of The (Old) Red Book has some nice explanation regarding OpenGL specific stuff, that's still pretty relevant today.

Lima answered 23/2, 2013 at 14:47 Comment(2)
so the order, either CW or CCW, tells the GPU which side is front, but I don't understand how that is possible since the camera orientation decides whether or not faces are facing front or away.Dinsmore
That's the point. If you are "looking" at the faces from the back, they can simply be ignored in drawing, because they will be certainly obscured by the front faces (transparent models are an exception - in this case, both front and back faces have to be drawn).Lima
T
1

That's exactly the point: the vertices order doesn't have to do with camera orientation initially, when you create/draw the face! Note that a camera is merely a transform, not something by itself recognized by openGL.
So, when rendering, after you'll have 'watched' through the camera, openGL simply checks (if culling is enabled) the new order of the face vertices, now according to the new orientation (after all transformations have been applied), to display the face or not.

Tippets answered 15/3, 2014 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.