Matrix / vector multiplication order

后端 未结 1 1843

I\'ve read a dozen articles online about the correct order of rotation, translation and scale matrix multiplication in OpenGL. However, now that I started implementing it myself

相关标签:
1条回答
  • 2021-01-26 09:49

    Your C++ matrices are probably stored as row major under the hood. Which means that multiplying them from left to right is an "origin" transformation, while right to left would be a "local" incremental transformation.

    OpenGL however uses a column major ordering memory layout (The 13th, 14th and 15th elements in the 16 element array are treated as the translation component).

    To use your row major matrices in OpenGL, there are 2 things you can do:

    1. glUniformMatrix* functions, have a parameter to pass in a GL_TRUE to the transpose:

    void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);

    This will re-arrange them to be column major.

    1. The other is to revert the ordering of operations in the shader:

    gl_Position = vec4(a_Position, 0.0, 1.0) * u_Matrix;

    But most GLSL literature you will find will use local left to right column major ordering, so it may be better to stick with that.

    Another alternative is to change the layout on the C++ side to make them column major (but I personally think row major is easier to deal with there).

    0 讨论(0)
提交回复
热议问题