home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / graphics / 14224 < prev    next >
Encoding:
Internet Message Format  |  1993-01-27  |  17.4 KB

  1. Xref: sparky comp.graphics:14224 comp.windows.x.pex:703 comp.graphics.opengl:280
  2. Path: sparky!uunet!ukma!bogus.sura.net!udel!intercon!psinntp!eye!erich
  3. From: erich@eye.com (Eric Haines)
  4. Newsgroups: comp.graphics,comp.windows.x.pex,comp.graphics.opengl
  5. Subject: Re: Row major or column major matrices? - SUMMARY POSTING
  6. Message-ID: <1993Jan27.094023.23238@eye.com>
  7. Date: Wed, 27 Jan 93 14:40:23 GMT
  8. Sender: erich@eye.com (Eric Haines)
  9. Organization: 3D/EYE, Inc.  Ithaca, NY
  10. Lines: 375
  11.  
  12. Once upon a time (like, last week) I wrote:
  13.  
  14. >Someone was telling me that OpenGL allows only post-concatenation of matrices.
  15. >This seemed bass-ackwards, and after a few minutes reconfirming in my slow
  16. >turtle brain that you'd normally want pre-concatenation of matrices, I read
  17. >over the OpenGL manual page carefully.  Aha, they store matrices in
  18. >column-major form:
  19. >
  20. >    a0   a4   a8   a12    a typical    1  0  0 -2
  21. >    a1   a5   a9   a13    translation    0  1  0  5
  22. >    a2   a6   a10  a14    matrix -->    0  0  1  3
  23. >    a3   a7   a11  a15            0  0  0  1
  24. >
  25. >
  26. >Just out of curiosity, I looked at a few computer graphics books and whatnot
  27. >and how they presented 3D matrices.
  28.  
  29. I received a large amount of mail about this topic (thanks, everyone), with
  30. much of it having unexpected angles on the question.  There were a fair number
  31. of responses which said that column major is what mathematicians use (and that
  32. computer graphics people are finally seeing the light), while others said they
  33. learned row major order in mathematics, engineering, and physics classes.  Go
  34. figure.  Actually, Ron Levine's answer has an authoritative sound to it, and
  35. he says that the type of notation used depends on the field of mathematics.
  36.  
  37. As Ron also points out, a source of confusion arose from my propagation of the
  38. term "column major", which is what OpenGL calls the form shown above.  My
  39. question was meant to address the pencil-and-paper notation of matrices, not
  40. the internal representation in the machine.  As Mark Segal (who designed
  41. OpenGL) writes, there is _no_ difference between IRIS GL and OpenGL on the way
  42. matrices are passed via the API's or used internally, so there should be no
  43. porting problems (other than rearranging the neuro-electrical pathways in the
  44. programmer's head!).  The data elements 0 through 15 are in the same order for
  45. a "row major" matrix as for a "column major" matrix.  To beat it to death:
  46. the OpenGL matrix stores translation values in elements 12,13,14, which is
  47. exactly where they are stored in IRIS GL.  There may be other systems in which
  48. the translation elements are stored in locations 3,7, and 11, but this is a
  49. separate computer language issue (can you say "FORTRAN"?).  As Ron Levine
  50. notes, postfix vs. prefix is probably a better way to talk about these
  51. representations, though then we have to remember that "prefix == translation
  52. vector in column".
  53.  
  54. Anyway, here's a new list of who uses what.  I started to list everyone who
  55. replied and anyone they mentioned, but it got too long and pointless.  The
  56. ratio seems to be 2:1 of row major (postfix) to column major (prefix) users.
  57.  
  58. Eric Haines  erich@eye.com
  59.  
  60.  
  61. Row Major users:
  62.  
  63.     Foley & VD (1st edition), Newman & Sproull, Graphics Gems series,
  64.     Rogers books, Watt, Burger & Gillies, the Thalmanns, HP Starbase,
  65.     RenderMan, SGI's IRIS GL, Sun's XGL
  66.  
  67. Column Major users:
  68.  
  69.     Foley & VD & F & H (2nd edition), OpenGL, Phigs (and PEX, I assume),
  70.     Dore'
  71.  
  72.  
  73. Here are various quotes of interest.  If you have too much time on your hands,
  74. write me and I'll send you the entire file of mail message replies.  Some
  75. related "this way or that" issues that people mentioned:
  76.     right handed vs. left handed coordinate systems
  77.     upper left corner vs. lower left corner being the origin of the image
  78.  
  79. Interesting to us amateur psychologists, there seems to be an inferiority
  80. complex in the computer graphics community, e.g.  "mathematicians use column
  81. major, so we should, too".  Reading the replies over, there's a fair mix of
  82. people who learned row major in their engineering and physics and even math
  83. classes.  At this point most computer graphics people seem to use row major,
  84. and I like Robin Forrest's reason of how this began.  So, I don't believe
  85. there's currently a "right" way, but rather it's worth conforming to the usage
  86. of whoever you're talking with (see Jon Leech's quote as a good example of
  87. this).  It'll be interesting to see how this sorts itself out in a decade or
  88. two.  I suspect Gavin Bell's quote is probably right, since the F&VD&F&H book,
  89. OpenGL, Phigs, and PEX are major influences in our field.
  90.  
  91. I wish I could have found a relevant quote from Major Major Major Major in
  92. _Catch-22_, but no such luck.  Anyway, here we go (in some vaguely structured
  93. order):
  94.  
  95.  
  96.  
  97. "`Row-major' and `column-major', as I have understood the terms for years,
  98. refer to how the matrices are stored in memory, not to how they are used to
  99. represent transformations.  That is, the terms distinguish between the Fortran
  100. convention, in which the first subscript varies most rapidly as memory address
  101. increases, and the C convention, in which the second subscript varies most
  102. rapidly as memory address increases.  Although your note refers to how
  103. matrices are stored, I think you are really concerned with quite a different
  104. distinction, namely, whether the matrix represents a linear transformation as
  105. a left multiplier of column vectors or as a right multiplier of row vectors.
  106. Mathematicians sometimes distinguish these two conventions as the `pre-fix'
  107. and `post-fix' conventions.
  108.  
  109. Mathematicians have long been split into two camps, between pre-fix and
  110. post-fix preferences.  Geometers and analysts have tended more to the pre-fix
  111. convention--thus you learned in calculus that f(x) denotes the value of a
  112. function f for a domain element x.  Then, if you compose two functions,
  113. (gf)(x) means g(f(x)), that is, first apply f then apply g.  This corresponds
  114. to interpreting a matrix as a left multiplier of column vectors.
  115.  
  116. But some schools of abstract algebraists logicians, and some authors of
  117. computer graphics books, have insisted on the post-fix (x)f or xf.  I always
  118. work in the pre-fix convention when I have a choice, but this is only because
  119. in the past most of my math professors and books have used it and so it is a
  120. little more natural to me.  I especially prefer it in computer graphics, which
  121. is inspired more by geometry than by abstract algebra.
  122.  
  123. But the two conventions are completely equivalent, and one should have no
  124. difficulty in using either.  Of course it is important to know which
  125. convention is used by any particular system with which you are working.  And
  126. of course, the matrices may be stored in row-major or column-major order for
  127. either convention of writing functional expressions; that distinction tends to
  128. depend on the programming language."
  129.     - levine@netcom.com (Ron Levine)
  130.  
  131.  
  132. "I plead guilty.  As an author of the OpenGL specification, I'm the one
  133. responsible for this foolishness.  I explained it in a message in
  134. comp.graphics.opengl a couple of months ago (you can probably find it if you
  135. look through your archives), but since you seemed to have figured out the
  136. salient points, I'll just explain my reasons.
  137.  
  138. I have always been irritated with computer graphics treatments using rows to
  139. represent vectors, since this is at odds with what is done throughout the
  140. mathematics world, where vectors are represented by columns (there are some
  141. good reasons for this, but I'll spare you).  I wanted to do my part to remedy
  142. this disturbing situation (it had nothing to do with Foley and van Dam; I
  143. wasn't even aware until recently that they had adopted a new convention).  So
  144. I wrote everything in the spec with vectors as columns, with matrices and
  145. application order corresponding.  The only snag was compatibility with
  146. existing GL programs, so I used the subterfuge of saying that the matrices are
  147. stored in column major order.  This was kind of a dirty trick, but I didn't
  148. think it would seriously bother anyone.
  149.  
  150. As you note, of course, there is no actual difference.  You can rewrite
  151. everything the old way, and then your matrices are in row major order.  I'm
  152. sure it's not much consolation, but many numerical analysts and FORTRAN
  153. afficianados actually prefer column major order."
  154.     - segal@spud.asd.sgi.com (Mark Segal)
  155.  
  156.  
  157.  
  158. "I have no strong preference for either, but it would make my life much easier
  159. if real-world systems could *choose one and stick with it*!  I recently had
  160. days of unnecessary skullsweat grafting OpenGL evaluator onto a utility
  161. library that used the opposite ordering."
  162.     - jbulf@balsa.Berkeley.EDU (Jeff Bulf, aka Dr. Memory)
  163.  
  164.  
  165.  
  166. "Funny thing you know, one of the other summer students here are the ACGC is
  167. writing a matrix transformation library, and nicked some of his matrix
  168. formulas from the new GL reference manual.  Guess what?  They did some really
  169. bizarre things (especially the perspective :-) until I told him they in row
  170. major form, not column major like we were used to!"
  171.     - rcskb@minyos.xx.rmit.OZ.AU (Kendall Bennett)
  172.  
  173.  
  174.  
  175. "I was also shocked to learn that OpenGL decided to use post multiplication of
  176. matrices.  Not because I learned pre-multiplication, but because it is
  177. opposite of what IRIS GL uses.  [...]
  178.  
  179. I learned the 'NEW' way of using matrices.  The reason that Foley decided to
  180. use post-multiplication of matrices in the second edition, is because it
  181. allows the notation to be written in the same manner as that used by
  182. mathematicians.
  183.  
  184. If you look in linear algebra books, they all use post-multiplication
  185. notation, and if you show a math person the 'OLD' way they will look at you
  186. like a leper...  At least this is the opinion I formed from my teacher (who
  187. was Dr. Foley)."
  188.     - wlw@cc.gatech.edu (Wayne Wooten)
  189.  
  190.  
  191.  
  192. "I learned Row Major at Computervision and have been confused by column major
  193. ever since.  Now the only question I want answered is whether the X offset is
  194. the fourth element of the data structure.
  195.  
  196. I'm always amused at people saying the everyone does it this way or that.
  197. Matrices are a fairly recent invention (in the 40's?).  And different
  198. disciplines use row & column matrices arbitrarily, at least in my limited
  199. understanding.  It's so arbitrary."
  200.     -Jan "YON" Hardenbergh (jch@oki.com)
  201.     [Jan is one of the designers of PEX. - EAH]
  202.  
  203.  
  204.  
  205. "The translation-matrix for me looks like:
  206.  
  207.  1  0  0  0
  208. -2  1  0  0
  209.  5  0  1  0
  210.  3  0  0  1 "
  211.     - Christian Linhart <chris@cosy.sbg.ac.at>
  212.  
  213.  
  214.  
  215. "The question of row versus column vectors is historical.  Steve Coons used
  216. row vectors for his influential early papers on transformations (University of
  217. Michigan Summer Courses in the mid 60's).  I asked him why in 1967 and he said
  218. it was because it was easier for the stenographer to type row vectors!
  219.  
  220. Regarding left and right hand co-ordinate systems, Larry Roberts who
  221. introduced 4x4 matrix transforms to graphics used RH co-ordinates and anyone
  222. brought up with his papers or Coons papers uses RH systems.  Ivan Sutherland,
  223. on the other hand, believed that z should be positive into the screen and
  224. hence used LH co-ordinates as did his pupils.  Sutherland's treatment of
  225. perspective also diverged from that devised by Roberts, hence the two schools
  226. of thought there."
  227.     - Robin Forrest (forrest@uk.ac.uea.sys)
  228.  
  229.  
  230.  
  231. "Actually, I have no clear preference, and I'll explain why.  In all my
  232. education (math and physics, B.S Physics UCLA 1986), only row-major matrices
  233. were used, so I naturally think of that as the standard.  However, I have an
  234. idea as to why so many people are switching to column-major notation.  It's
  235. so that matrices can be post-multiplied instead of pre-multiplied!  Post-
  236. multiplication notation accords better with the compound operators of C and
  237. C++.  If you're writing a matrix class in C++, and you want to multiply one
  238. matrix by another, it seems natural to define the operator `*=' as the
  239. multiplication operator, such that:
  240.  
  241.     A *= B means A = A * B.
  242.  
  243. I was writing such a class for a simple 3-D graphics program, and I quickly
  244. realised that the most common multiplication I wanted to do was PRE-
  245. multiplication.  I therefore said `Screw convention', and defined `*=' so that
  246.  
  247.     A *= B means A = B * A.
  248.  
  249. So, my solution was to change the standard C relationship between `*=' and
  250. `*'.  Apparently, others decided it was better to change the very operation
  251. of matrix multiplication to imply a column-major order, so that `A *= B'
  252. would still mean `A = A*B'."
  253.     - jasper@einstein.com (John Scott Peter XXXIII)
  254.  
  255.  
  256.  
  257. "`I am the major model of a modern Row Major General'..... I just saw the
  258. Pirates of Penzance last week.  Even Gilbert and Sullivan were Row Major
  259. fanatics, if I got the quote correct 8-)"
  260.     - Brian Corrie <Brian.Corrie@cs.anu.edu.au>
  261.  
  262.  
  263.  
  264. "I go for what you call "row major". My "pappy's knee" was
  265.  
  266.   CS374 -- R.F. Sproull -- Introduction to Computer Graphics, Fall 1976
  267.  
  268. at Caltech, straight from the (then new) book. Together with Sutherland about
  269. the most lucid lectures I ever heard (only heard Feynman speak once, and to a
  270. semi-technical audience). I later had the privilege of brief involvement with
  271. N&S vol. II while at Xerox Pasadena (our laser printers produced all the
  272. camera-ready copy)."
  273.     - Alan Wm Paeth <awpaeth@watpix.uwaterloo.ca>
  274.  
  275.  
  276.  
  277. "I learned linear algebra before computer graphics.  The linear algebra
  278. books normally write vectors as column vectors."
  279.     - Kevin.Wu@Eng.Sun.COM (Kevin Wu)
  280.  
  281.  
  282.  
  283. "I personally prefer row major...  however, computer hardware that I've worked
  284. on recently preferred column major.  The reason for this was the presence of an
  285. auto-increment load/store assembly instruction which allowed the matrix
  286. routines to work faster if the data was presented to them in column major
  287. form."
  288.     - reid.judd@East.Sun.COM (Reid Judd - Sun NC Development Center)
  289.  
  290. [This answer (and a few others received) discusses the storage of row vs.
  291. column, not the presentation on paper of row vs. column, a different issue.
  292. "Row major" is definitely an overloaded term.  - EAH]
  293.  
  294.  
  295.  
  296. "Avast there, ye scurvy scum!  Dore' uses column vectors and LIKES IT!!"
  297.     - hollasch@kpc.com (Steve Hollasch)
  298.  
  299.  
  300.  
  301. "The reason/justification/excuse for the change in matrices is to switch to
  302. the way the mathematicians have been doing it all along.  They have a far
  303. longer history than we do in this affine transform jazz."
  304.     - Sam Uselton (uselton@nas.nasa.gov)
  305.  
  306.  
  307.  
  308. "I'm a row major type person.  From my engineering and graphics background
  309. that is all I usually see.  I learned my first matrix stuff in a
  310. Linear Algebra class in college. [...]
  311.  
  312. I don't really see any difference between the two methods other than I wish
  313. they would pick one and stick with it!!!  By the way, engineers, structural 
  314. mechanics, fluids guys, and aerodynamicists use row major from what I've 
  315. been taught and seen."
  316.     - Mike Goza (goza@cheers.jsc.nasa.gov)
  317.  
  318.  
  319.  
  320. "Characterizing the matrices is not the clearest way IMO.  I prefer stating
  321. that points are either row vectors or column vectors.
  322.  
  323. Jim Blinn taught me to use row vectors in 1982 and Al Barr taught me to use
  324. column vectors in 1984.  I prefer column vectors these days, although I'll go
  325. either way as needed.  In teaching an undergraduate graphics course last
  326. semester I used row vectors, since I was handing out copies of Blinn papers
  327. and didn't want to confuse them too much.
  328.  
  329. I'm less sanguine about left-handed coordinate systems, which are Tools of the
  330. Devil (as anyone doing physically based modelling should think)."
  331.     - Jon Leech <leech@cs.unc.edu>
  332.  
  333.  
  334.  
  335. "I got started using row-major matrices for transforms when we used
  336. mimeographed copies of the manuscript for the 1st edition of Newman and
  337. Sproull in Prof. Bert Herzog's class at the Univ. of Michigan!  That should
  338. date me pretty well.  :-)
  339.  
  340. The system I work on now uses column-major matrices, and to make things more
  341. interesting, homogeneous coordinates are expressed as (w,x,y,z) instead of
  342. (x,y,z,w).  That mixes things up pretty well in those transform matrices and
  343. keeps me on my toes...
  344.  
  345. I hate to try to classify people by their preferences to use row-major versus
  346. column-major form.  Maybe engineers tend to use row-major, while
  347. mathematicians tend to use the transpose; don't know about physicists.
  348. Conventions do seem to tend to cluster around various subfields.  Computer
  349. graphics, maybe because it is so cross-disciplinary, is definitely in a state
  350. of flux on transform order conventions.
  351.  
  352. The row-major convention is good for thinking about transforms as operators.
  353. Column-major form is best for thinking about transforms as functions, e.g.,
  354.  
  355.                     v' = T(v)
  356.  
  357. for a transform T that maps vector v to vector v'; then transform
  358. concatenation is just function composition:
  359.  
  360.                     v' = T2(T1(v))
  361.  
  362. May you always compose your matrices in the "right" order..."
  363.     - Ron Capelli <capelli@vnet.ibm.com>
  364.  
  365.  
  366.  
  367. "It's even more surprising since the current version of GL normally
  368. pre-concatenates. I can imagine some hard to find bugs when people port old GL
  369. code in which they generate their own matrices from scratch.  [not true - EAH]
  370.  
  371. I'm not too upset though, cause column vectors and post-concatenation is what
  372. I learned as an engineer, and so it seems more natural to me.  Note, that
  373. pre-concatenation with column vectors or post-concatenation with row vectors
  374. is useful.  In this one instance I prefer PHIGS, which has a direct mechanism
  375. for doing both pre- & post-concatenation of matrices ...  you can
  376. post-concatenate in GL but only indirectly and inefficiently.
  377.  
  378. According to the GL manual, GL uses row vectors and pre-multiplication for
  379. purely historical reasons related to the efficiency of early hardware."
  380.     - ledwards@leland.Stanford.EDU (Laurence James Edwards)
  381.  
  382.  
  383.  
  384. "In 10 years we will probably remember the good old days when the translations
  385. were at the bottom of the matrix..."
  386.     - gavin@krypton.asd.sgi.com (Gavin Bell)
  387.