GLM confusion

Started by
1 comment, last by TheStudent111 8 years, 3 months ago

While messing around glm, I stumbled upon something I need clarification on. I have always knew that GLM and OpenGL followed column major notation. But when I print the matrix out, it looks like its in row major form.

#include <iostream>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtx\string_cast.hpp>

using std::cout;
using std::endl;

int main()
{
    glm::vec3 translate(3.0f, 4.0f, 4.0f);
    glm::mat4 identity(1.0f);
    glm::mat4 model = glm::translate(identity, translate);

    cout << "[0][0]: "  << model[0][0] << "  [0][1]: " << model[0][1] << "  [0][2]: " << model[0][2] <<  "  [0][3]: " << model[0][3] << endl;
    cout << "[1][0]: "  << model[1][0] << "  [1][1]: " << model[1][1] << "  [1][2]: " << model[1][2] <<  "  [1][3]: " << model[1][3] << endl;
    cout << "[2][0]: "  << model[2][0] << "  [2][1]: " << model[2][1] << "  [2][2]: " << model[2][2] <<  "  [2][3]: " << model[2][3] << endl;
    cout << "[3][0]: "  << model[3][0] << "  [3][1]: " << model[3][1] << "  [3][2]: " << model[3][2] <<  "  [3][3]: " << model[3][3] << endl;

    cout << " " << endl;
    cout << glm::to_string(model) << endl;


    system("Pause");
    return 0;
}

If you run the above code, the translation column is actually a translation row. This does not make sense, doesn't GLM follow column major. Shouldn't the translation be on [0][3], [1][3], [2][3], instead of [3][0], [3][1], [3][2].

Advertisement

I guess that depends how GLM implements operator[]. Often, column-major libraries will implement it as matrix[column][row] instead of the standard convention of matrix[row][column]. If this is true for GLM, then the translation is in the 0/1/2 elements of the column 3, which sounds right.

Ok, so


 cout << "[3][0]: "  << model[3][0] << "  [3][1]: " << model[3][1] << "  [3][2]: " << model[3][2] <<  "  [3][3]: " << model[3][3] << endl;

Is actually column 3, and not row 3. If that's the case, then it makes sense. Thanks Hodgman.

Side note, do you have links to were I can find how the translation matrix was derived. I can find the derivation of rotation matrix but not translation.

This topic is closed to new replies.

Advertisement