Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it legal to reverse the vector-matrix mult order in a shader?

While the graphical output is completely messed up when I do this, I tried it out of curiousity. I was expecting it to crash due to illegal math operation:

 //    gl_Position=Projection*Modelview*Position; <- normal, works fine
 //    gl_Position=Position*Modelview*Projection; <- bad output, but still runs fine

Position is the vector, the others are matrices.

OpenGL is column-major, which should mean that you can only multiply a vector on the right of a matrix. There is no mathematical way to multiply a vector to the left of a matrix if the vector is a column since the inner dimensions would not match: 4x1 * 4x4

So why do I actually get output when I try it? What is OpenGL actually doing with the vector-matrix multiplication?

like image 403
johnbakers Avatar asked Mar 24 '23 17:03

johnbakers


1 Answers

Vector-matrix multiplication with a vector on the left side is interpreted in GLSL as if your column vector became a row vector.

Which, in effect, is like multiplying your column vector by the transpose of your matrix.

http://en.wikibooks.org/wiki/GLSL_Programming/Vector_and_Matrix_Operations#Operators

like image 170
DuckMaestro Avatar answered Mar 31 '23 11:03

DuckMaestro