<p>There is no 100% consensus on what order matrix multiplication should model things, worse the industry is split along this. Some sources use row major and some sources use column major matrices. Great care should be taken to verify which is being used in your source and what is being used by your API!</p>

<p>Some API's go as far as allowing one to multiply vectors on both sides of the matrix. The wisdom is thus in reading your API reference and verifying how it works. Old, fixed pipeline OpenGL and glm, does in fact use column major matrices. While (<strong>warning</strong>) DirectX had chosen to use row major matrices [<a href="http://www.mindcontrol.org/~hplus/graphics/matrix-layout.html" rel="noreferrer">source</a>]. Obviously with programmable shaders any application can have any order, even mixed one</p>

<p>Due to fundamental properties of matrices this duality is the same as transposing the matrix. If you transpose the matrix you reverse the order of calculation, as you are now thinking outside in instead of inside out or vice versa.</p>

<p>The moral of the story is that you should always check your assumptions. And just printing out values does not always help as the printing function can screw things up, and memory not aligned the same way. Also simple testing may make you blind to having mixed conventions inside your program. <strong>Read what convention is in use</strong>! Because, as confusing as having two sources with different  conventions is, it pales in comparasion to a piece of software that mixes the two in the codebase.</p>

CC BY-SA 3.0
2017-07-18T07:58:37.583Z
2017-07-18T08:34:18.817Z
2017-07-18T08:34:18.817Z
joojaa
<p>OpenGL uses column-major matrices. For example, the translation values will be in the last row rather than the last column of the matrix. For example when loading matrices into uniforms in glsl, the <a href="https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glUniform.xml" rel="noreferrer"><code>glUniformMatrix4fv()</code> function</a> takes its matrix parameters in column-major order:</p>

<blockquote>
  <p>Each matrix is assumed to be supplied in column major order.</p>
</blockquote>

<p>In glm, the matrices are also in column-major order. From the <code>matrix_transform.hpp</code> header, the comment for the <code>translate()</code> method says this:</p>

<pre><code>/// #include &lt;glm/glm.hpp&gt;
/// #include &lt;glm/gtc/matrix_transform.hpp&gt;
/// ...
/// glm::mat4 m = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));
/// // m[0][0] == 1.0f, m[0][1] == 0.0f, m[0][2] == 0.0f, m[0][3] == 0.0f
/// // m[1][0] == 0.0f, m[1][1] == 1.0f, m[1][2] == 0.0f, m[1][3] == 0.0f
/// // m[2][0] == 0.0f, m[2][1] == 0.0f, m[2][2] == 1.0f, m[2][3] == 0.0f
/// // m[3][0] == 1.0f, m[3][1] == 1.0f, m[3][2] == 1.0f, m[3][3] == 1.0f
</code></pre>

<p>Note that the translation amount (1,1,1) is in the last row rather than the last column.</p>

CC BY-SA 3.0
2017-07-18T03:17:51.180Z
2017-07-18T03:17:51.180Z
user1118321
<p>I have been learning OpenGL 3.3 and I saw this expression:</p>

<pre><code>gl_Position = projection * view * model * vertex
</code></pre>

<p>I am confused because I read in some book that if the vector (vertex) is placed on the right side of the matrix then it would be a column vector and to transform this vector you should have a column major matrix.</p>

<p>But glm library follows GLSL right. So I printed the <code>glm::mat4</code> and it was a row-major matrix.</p>

<opengl><glsl><matrix><legacy-opengl>
Why do we multiply vertex from left side in vertex shader with matrices?
videogamechef
# Why do we multiply vertex from left side in vertex shader with matrices?

(Asked by: username_0 on 2017-07-18)

I have been learning OpenGL 3.3 and I saw this expression:

```
gl_Position = projection * view * model * vertex

```

I am confused because I read in some book that if the vector (vertex) is placed on the right side of the matrix then it would be a column vector and to transform this vector you should have a column major matrix.

But glm library follows GLSL right. So I printed the `glm::mat4` and it was a row-major matrix.

**Tags:** opengl, glsl, matrix, legacy-opengl


---

## 2 answers

### Answer by username_1 on 2017-07-18. Score: 7 | Accepted answer

There is no 100% consensus on what order matrix multiplication should model things, worse the industry is split along this. Some sources use row major and some sources use column major matrices. Great care should be taken to verify which is being used in your source and what is being used by your API!

Some API's go as far as allowing one to multiply vectors on both sides of the matrix. The wisdom is thus in reading your API reference and verifying how it works. Old, fixed pipeline OpenGL and glm, does in fact use column major matrices. While (**warning**) DirectX had chosen to use row major matrices [[source](http://www.mindcontrol.org/~hplus/graphics/matrix-layout.html)]. Obviously with programmable shaders any application can have any order, even mixed one

Due to fundamental properties of matrices this duality is the same as transposing the matrix. If you transpose the matrix you reverse the order of calculation, as you are now thinking outside in instead of inside out or vice versa.

The moral of the story is that you should always check your assumptions. And just printing out values does not always help as the printing function can screw things up, and memory not aligned the same way. Also simple testing may make you blind to having mixed conventions inside your program. **Read what convention is in use**! Because, as confusing as having two sources with different conventions is, it pales in comparasion to a piece of software that mixes the two in the codebase.


---

### Answer by username_2 on 2017-07-18. Score: 5

OpenGL uses column-major matrices. For example, the translation values will be in the last row rather than the last column of the matrix. For example when loading matrices into uniforms in glsl, the [`glUniformMatrix4fv()` function](https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glUniform.xml) takes its matrix parameters in column-major order:

> Each matrix is assumed to be supplied in column major order.

In glm, the matrices are also in column-major order. From the `matrix_transform.hpp` header, the comment for the `translate()` method says this:

```
/// #include <glm/glm.hpp>
/// #include <glm/gtc/matrix_transform.hpp>
/// ...
/// glm::mat4 m = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));
/// // m[0][0] == 1.0f, m[0][1] == 0.0f, m[0][2] == 0.0f, m[0][3] == 0.0f
/// // m[1][0] == 0.0f, m[1][1] == 1.0f, m[1][2] == 0.0f, m[1][3] == 0.0f
/// // m[2][0] == 0.0f, m[2][1] == 0.0f, m[2][2] == 1.0f, m[2][3] == 0.0f
/// // m[3][0] == 1.0f, m[3][1] == 1.0f, m[3][2] == 1.0f, m[3][3] == 1.0f

```

Note that the translation amount (1,1,1) is in the last row rather than the last column.


---

