68

Is it possible to access the individual elements of a glsl mat4 type matrix? How?

bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • 1
    -1: This is easily looked up by [reading the GLSL specification](http://www.opengl.org/registry/), which is a very readable document. – Nicol Bolas Nov 29 '12 at 20:58
  • 4
    Funnily enough the information is _not_ in the linked reference – bobobobo Nov 29 '12 at 21:02
  • 2
    There is a section entitled "Matrix Components" in all of the GLSL specifications. In GL 4.3, that's section 5.6. – Nicol Bolas Nov 29 '12 at 21:51
  • 3
    Oh great. Putting an answer in now, let's see how many hits and upvotes this question gets over the next few years. – bobobobo Nov 29 '12 at 22:09
  • 2
    Betting "Famous question" in 2 years. – bobobobo Nov 29 '12 at 22:15
  • @bobobobo famous question already? :P – Luke B. Mar 25 '14 at 20:05
  • @LukeB. dunno how it was in 2014, but currently it has 14562 views, which is more than enough for _Famous question_ badge. – Ruslan Jun 27 '16 at 07:23
  • 2
    "very readable document" is heavily opinionated, IMO. No spec is "very readable", as specs contains 99% information that you don't need and never will, unless you are designer of the spec. – metalim Dec 08 '16 at 15:58
  • +1 because searching for the problem on duckduckgo brought me straight here where I found an immediate answer. If you hadn't posted this here, I'd have had to waste time searching for the spec and wading through it to find the answer. So thanks. :) – daiscog Jan 15 '17 at 23:08

1 Answers1

84

The Section 5.6 of the GLSL reference manual says you can access mat4 array elements using operator[][] style syntax in the following way:

mat4 m;
m[1] = vec4(2.0); // sets the second column to all 2.0
m[0][0] = 1.0; // sets the upper left element to 1.0
m[2][3] = 2.0; // sets the 4th element of the third column to 2.0

Remember, OpenGL defaults to column major matrices, which means access is of the format mat[col][row]. In the example, m[2][3] sets the 4th ROW (index 3) of the 3rd COLUMN (index 2) to 2.0. In the example m[1]=vec4(2.0), it is setting an entire column at once (because m[1] refers to column #2, when only ONE index is used it means that COLUMN. m[1] refers to the SECOND COLUMN VECTOR).

bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • 36
    You can also use m[1].xyzw. – Luke B. Jan 18 '13 at 15:59
  • 11
    Column or row is pretty meaningless. More important: is the translation component of the matrix in (m[3][0],m[3][1],m[3][2]) or in (m[0][3],m[1][3],m[2][3]) ? – Bram Jun 24 '15 at 22:09
  • 3
    What do you mean by `defaults to`? Is there an option to switch to row major matrices? – Ruslan Aug 04 '15 at 10:57
  • 7
    mat[col][row] is more intuitive imho as those "parameters" always looked like x,y positions to me .. I think default mathematics writing mat[row][col] -> mat[y][x] is one of the biggest fails in mathematics .... (IMHO of course :) – Buksy Nov 30 '15 at 20:29
  • well it s just a matter of transpose .. i don"t see any fuss about it.. – VivienLeger May 28 '17 at 19:26
  • 2
    @Ruslan yes, the layout(row_major) specifier. – tly Jul 04 '17 at 16:48
  • @Buksy its the annoying fact that sometimes we use matrices to represent lists of points; where column major makes sense; and other times we use use matrices as lists of transformations, where row major makes sense. then you have the annoying property that to turn a point to a translation from origin to point, you need to set fourth column of an identity matrix to the points xyzw. there's no winning here; you will fconfuse the cpu no matter what you choose. – Dmytro Oct 07 '17 at 17:52
  • What are those float values?? How can we use them, or what do they change? – frank17 Nov 14 '17 at 23:00
  • @LukeB. For future users like myself, to use swizzling (m[1].xyzw), `#define GLM_FORCE_SWIZZLE` has to be set (see [link](https://github.com/g-truc/glm/blob/master/manual.md#-214-glm_force_swizzle-enable-swizzle-operators)). But it makes building the code slower. – Hakim Mar 23 '20 at 12:27