13

OpenGL by default determines a triangle to be facing towards the camera if the triangle's vertexes are ordered in a counterclockwise order from the perspective of the camera. This seems to have been the case since OpenGL 1.1 and probably earlier:

void glFrontFace(
    GLenum       mode)

mode
    Specifies the orientation of front-facing polygons. GL_CW
    and GL_CCW are accepted. The initial value is GL_CCW.

Is there any historical reason for why it is specifically counterclockwise instead of clockwise?

8
  • 7
    OpenGL is an open-source reimplementation of the closed-source SGI IRIS GL. What order did IRIS GL use for normals? Jul 28, 2022 at 15:22
  • 8
    I see an analogy to the complex plane there. On the complex plane, the positive direction of the polar angle coordinate is ccw.
    – Leo B.
    Jul 28, 2022 at 15:28
  • 1
    @LeoB. - yes, see e.g., this answer over at Math.SE.
    – davidbak
    Jul 28, 2022 at 15:48
  • 1
    @JacobKrall Counterclockwise, apparently (techpubs.jurassic.nl/library/manuals/1000/007-1680-030/pdf/…), but I cannot find a reason why.
    – virchau13
    Jul 28, 2022 at 16:26
  • 3
    It's mathematical convention that positive angles are counterclockwise. So, starting from a vast library of maths which does it one way, would you want to chance missing inserting/removing a minus sign when you're converting written maths to a computer program? Jul 29, 2022 at 17:41

2 Answers 2

25

Is there any historical reason for this?

Oh yes, there is a real linage of reason :))

Software Tooling

OpenGL is an open source version of the former proprietary IRIS GL library offered by SGI for their graphics workstation. This was done as a move to get a wider acceptance than the the standard PHIGS. The move worked out fine, as IRIS GL was a way lower, more hardware orientated approach, better fitting the less capable nature of back then machines.

Using counterclockwise sequence as mark for orientation was at that time already standard.Both system (IRIS GL and PHIGS) use CCW as default. IRIS GL allows to specify clockwise direction as a system wide setting via pfBboardAxis(). This is offered as a support for customer specific systems with a data set made on assuming CW orientation.

Code based detection works of course equally well for both ways, as it's always about testing the sign, so setting either just defines what to test the sign for. SGI called it 'sense':

sense

An indication of whether a positive angle is interpreted as representing a clockwise or counterclockwise rotation with respect to an axis. All CCW rotations in IRIS Performer are specified by positive (+) angles and negative angles represent CW rotations.

(Excerpt from the IRIS Performer Programming Guide)

Human Tooling

A very common 'tool' when visualizing orientation is the mentioned right-hand-rule (*1). Right hand or three finger rules help remind the relation of laws/deinition based on multiple components set in 90 degree angles of each. Here the axis orientation, as well as rotational angles.

Axis Orientation

  • Holding the right hand up, with the thumb up, pointer finger straight and middle finger 90 degrees to the left shows the orientation of each component. Nicely pictured on the Swiss 200 Franks note:

enter image description here

(Image Taken from the Swiss National Bank site)

Angle direction

  • Build a fist with your right hand and extend your thumb upward. When orientating the thumb an axis' orientation, then the fingers describe the 'direction' of the angles(*1). That's counterclockwise due the way our hands are constructed. Sometimes also called 'Winding Direction' as the fingers 'wind' around the axis.

Using that picture (the right-hand rule), counterclockwise is what gives positive numbers, usually more readable than having most with a prefixed minus. Likewise for programmers.

Mathematics Tooling

This all goes back to Euler Angles, a system devised by Swiss mathematician Leonhard Euler, who defined essentially the way we write mathematics today, like the use of Greek letters (*2) for lot of stuff including naming the angles of a triangle as α, β and γ (Alfa, Beta, Gamma) when defining his theories about triangles. Likewise he used upper case Latin for the points (A, B, C) and lower case for a triangles sides (a, b, c) (*3).

All of them in counterclockwise sequence.

Using Eulers terminology and applying the Right Hand Rule results in nice positive numbers for counterclockwise orientation (*4).

Bottom line

It's the result of historic developed nomenclature, going back 300 years, nicely fitting the way humans think and computer work.


P.S.: Thanks to the versatility of RC.SE patrons, like Jacob Krall, Leo B. and Davidbak, most of that was already pointed out, in comment form, just minutes after the question was posted.


*1 - We seem to be very handy creatures, considering that hand based rules come up in next to everything from Ampere's (the guy with the AMPs) right hand rule defining the north pole of an electromagnet to naming an particles quantum spin as up and down according to the right hand rule.

*2 - He influenced everyday language so deep, that most people think first of 3.14 when hearing Pi or of a difference when saying Delta, that is, maybe except Greeks :)

*3 - That's why today we aren't using Euklid's naming to write the Pythagorean Theorem, but a²+b²=c², as Euler suggested.

*4 - I believe that his work, in structuring the how math is written, may have been the single most important important step to enable later development. Before him defining once and for all (all the way to OpenGL and beyond) how a triangle is named, and what symbols are used in general, each math script started with several pages defining its own language (or worse, mangled that between many pages), to talk about such basic stuff.

His formal way is what brought math a great step ahead, by removing the need to reinvent essentials over and over - much like ALGOL put programming on another level. It is that structuring of how math is written that had, IMHO, way more impact than any of his, otherwise very important, discoveries. He made math talk one language.

9
  • Your first footnote seems like it got left incomplete. It looks like you were going to include some examples of disciplines that have rules based on handedness.
    – trlkly
    Jul 30, 2022 at 3:15
  • 3
    Why is the right hand rule on the 200 franks note? Jul 30, 2022 at 15:53
  • 1
    @ThorbjørnRavnAndersen Is this a serious question? Ok, I bite: Usually bank note designs are about the issuing country, praising some positive aspect of it, its people and history. The new Frank series tries to visualize concepts about Switzerland, the 200 note for example the scientific side. Picturing a hand showing the rule made popular by a mathematician who founded much of the way of today's mathematics seems like a great way to solve this, isn't it? Not to mention what nationality Euler was.
    – Raffzahn
    Jul 30, 2022 at 17:12
  • 1
    In particular, using the right-hand rule means that you can use existing optimized code for cross products and so forth. If you used a left-hand rule, you'd have to rewrite that code, or else spend an extra instruction to negate its output. Jul 30, 2022 at 17:29
  • @Raffzahn no need to be snide. i know euler did a lot of stuff. 1-0=e^ipi and all that . I did not know he did that. Learning every day. Jul 30, 2022 at 17:34
4

Given a convex polyhedron, it's easy to produce a set of triangles, each identified by an ordered list of three vertices on the polyhedron, which will from the camera's point of view be drawn in one direction (clockwise or clockwise) for all faces that would be visible to the camera, and the other direction for all faces that would not. If one maps three points of a triangle to screen coordinates (x1,y1),(x2,y2),(x3,y3), the sign of (x2-x1)(y3-y1)-(y2-y1)(x3-x1) will indicate whether points of the triangle are being drawn clockwise, counterclockwise, or neither (the latter being the case if e.g. all three points are in a straight line). Hidden-surface removal can be accomplished by using that latter calculation to decide which triangles to draw or omit without having to do any kind of sorting of screen distances or other such tricks.

The choice of whether to draw clockwise triangles and omit counter-clockwise ones, or draw counter-clockwise ones while omitting clockwise ones, is essentially arbitrary, but using the clockwise or counterclockwise orientation of a triangle as the determining factor for whether it should be drawn is quick and easy means of excluding about half of the triangles from any further processing.

Note that this approach will be 100% accurate when applied to convex polyhedra, but polyhedra that are not convex may have faces whose outside is toward the camera but are nonetheless obscured by other closer faces. This may be resolved, however, by decomposing such polyhedra into two or more convex ones that don't overlap. If two convex polyhedra do not overlap in 3d space, each will have at least one face whose plane does not intersect the other polyhedron. If one selects such a face and it points away from the camera, then no part of the other polyhedron can be in front of it. Conversely, if that face points toward the camera, it cannot obscure any part of the other polyhedron. This makes it possible to determine which order to draw the polyhedra without having to examine all of the vertices thereof.

1
  • 3
    Good explanation, but my question is more about why it's specifically counterclockwise instead of clockwise. (I stumbled into a bug in my vertex shader where I accidentally rendered the vertices clockwise and wondered why it was CCW instead of CW.) I'll edit the question to make that more specific.
    – virchau13
    Jul 28, 2022 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .