Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @kylemcdonald kylemcdonald created this gist Mar 30, 2018.
    37 changes: 37 additions & 0 deletions ofMatrix4x4-vs-glm.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #include "ofMain.h"

    int main() {
    // both initialize to identity matrix
    cout << glm::mat4() << endl;
    cout << ofMatrix4x4() << endl;

    // both row-major (translation stored in mat[3][0,1,2])
    glm::mat4 glmMat;
    glmMat = glm::translate(glmMat, glm::vec3(1,2,3));
    ofMatrix4x4 ofMat;
    ofMat.translate(1,2,3);
    for(int i = 0; i < 4; i++) {
    cout << glmMat[i] << endl;
    cout << ofMat._mat[i] << endl;
    }

    // if working with multiple transformations, note that:
    // glmMat *= glm::translate(glmMat, translation);
    // is actually equivalent to:
    // ofMat.preMultTranslate(translation);

    // also,
    glm::mat4 glmMat1, glmMat2;
    glmMat1 *= glmMat2;
    // is equivalent to:
    ofMatrix4x4 ofMat1, ofMat2;
    ofMat1.preMult(ofMat2);

    // glm::eulerAngleX accepts radians
    // ofMatrix4x4 uses float by default
    // glm::mat4 can use float or double based on initialization
    // so we explicitly initialize with a float
    cout << glm::eulerAngleX(float(HALF_PI)) << endl;
    // ofMatrix4x4::newRotationMatrix accepts degrees
    cout << ofMatrix4x4::newRotationMatrix(90, 1, 0, 0) << endl;
    }