model view projection multiplication order

| | August 4, 2015

I’m debugging a lighting problem where the camera position is effecting the diffused lighting component on my 3d model. In researching my problem I went back and am reading over all the how to documents. I found that one of my old sources (where I learned open gl 2.0) said this (summarized):

When creating the modelViewProjection matrix you can’t change the order or you’ll get unexpected results. First take the View Matrix and post-multiply it by the projection matrix to create a viewProjection matrix. Then post-multiply the model matrix to get the modelViewProjection matrix.

Looking at my code, all this time I haven’t been doing this:

matrixMultiply(mView, thisItem->objModel, mModelView);
matrixMultiply(mProjection, mModelView, mModelViewProjection);

As a test I tried changing it to this:

matrixMultiply(mView, thisItem->objModel, mModelView);
matrixMultiply(mProjection, mView, mViewProjection);
matrixMultiply(mViewProjection, thisItem->objModel, mModelViewProjection);

…but the result appears the same.

EDIT: To answer a question about what the matrices contain. A snapshot of the matrix values:

Model Matrix

View Matrix

Projection Matrix

EDIT2: As a test I did the math both ways to compare the results and even with moving the camera and model around and rotating the model I’m getting the same results for the final ModelViewProjection.

One Response to “model view projection multiplication order”

  1. It depends on the implementation of the multiply operation. The internals will suppose an ordering within your matrices, row major or column major. If the ordering of your matrices is arranged the way the function expects them, then it is up to the second dpendancy: namely the respect of the correct mathematical commutativity. (which they should)

    When you have these 2 conditions, then your multiplication order is the reverse of the pipeline. Therefore you do totalmat = proj * view * model.

    If ever your matrices were to be transposed, you need to reverse this application order. Whenever one component of what I mentioned before is opposed, it commutes the multiplication.

Leave a Reply