title: Transformation in vertex shader only works with post-multiplying
body: <p>I am currently in the process of learning OpenGL and GLSL to write a simple software that loads models, display them on the screen, transform them etc.</p>
  
  <p>As a first stage, I wrote a pure-C++ program without using OpenGL.
  it works great, and it uses a Row-major matrix representation:</p>
  
  <p>So for instance mat[i][j] means row i and column j.</p>
  
  <pre><code>class mat4
  {
      vec4 _m[4]; // vec4 is a struct with 4 fields
      ...
  }
  </code></pre>
  
  <p>This is the relevant matrix multiplication method:</p>
  
  <pre><code>mat4 operator*(const mat4&amp; m) const
  {
      mat4 a(0.0);
  
      for (int i = 0; i &lt; 4; ++i)
      {
          for (int j = 0; j &lt; 4; ++j)
          {
              for (int k = 0; k &lt; 4; ++k)
              {
                  a[i][j] += _m[i][k] * m[k][j];
              }
          }
      }
  
      return a;
  }
  </code></pre>
  
  <p>In order to get from model space to clip space I do as follows in C++:</p>
  
  <pre><code>vec4 vertexInClipSpace =  projectionMat4 * viewMat4 * modelMat4 * vertexInModelSpace;
  </code></pre>
  
  <p>Now, trying to implement that in a GLSL Shader (Version 1.5) yields weird results. It works, but only if I <strong>post</strong> multiply the vertex instead of pre-multiplying it and in addition transpose each of the matrices.</p>
  
  <pre><code>uniform mat4 m;
  uniform mat4 v;
  uniform mat4 p;
  
  void main()
  {
      // works ok, but using post multiplication and transposed matrices :
      gl_Position =  vec4(vertex, 1.0f) * m * v * p;
  }
  </code></pre>
  
  <p>Although mathematically OK as <code>v2 = P * V * M * v1</code> is the same as <code>transpose(v2) = transpose(v1) * transpose(M) * transpose(V) * transpose(P)</code>  ,
  I obviously don't get something because I have not seen even 1 reference where they post multiply a vertex in the vertex shader.</p>
  
  <p>To sum up, here are specific questions:</p>
  
  <ul>
  <li>Why does this works? is it even legal to post multiply in glsl?</li>
  <li>How can I pass my C++ matrices so that they work properly inside the shader?</li>
  </ul>
  
  <p>Links to related Questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/8787547/glm-matrix-multiplication-and-opengl-glsl">link 1</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/6975089/why-isnt-this-orthographic-vertex-shader-producing-the-correct-answer">link 2</a></p>
  
  <h2><strong><em>EDIT:</em></strong></h2>
  
  <p>Problem was sort of "solved" by altering the "transpose" flag in the call to: </p>
  
  <pre><code>glUniformMatrix4fv(
          m_modelTransformID,
          1,
          GL_TRUE,
          &amp;m[0][0]
          ); 
  </code></pre>
  
  <p>Now the multiplication in the shader is a pre-multiplication:</p>
  
  <p>gl_Position = MVP * vec4(vertex, 1.0f);</p>
  
  <p>Which kind of left me puzzled as the mathematics doesn't make sense for a column-major matrices that are a transpose of row major matrices.</p>
  
  <p>could someone please explain?</p>