Commit af66eaf
authored
Fixed GLSL implementations of matrix operators (#2298)
Fixed an issue with GLSL matrix multiplication and division, described in #2089
The gist of the problem is this. Taking C++ code as the basis, MaterialX uses row major matrices, with translation in the last row, so it transforms points like so `p * M`. When composing transforms `M1 * M2`, the final result is `p * M1 * M2`.
However, GLSL has matrices in col-major storage, meaning that when you simply upload the CPU memory, the matrices on the GPU are transposed. That effectively means that translation is now in the last column, so transforming point works like `M * p`, and composing transforms works like `M2 * M1 * p`, to match the CPU `p * M1 * M2`.
That means that matrix multiplication has to have the inputs swapped. Alternatively, we could do `transpose(transpose({{in1}}) * transpose({{in2}}))` for the same effect.
The fix to division has two parts. In GLSL, only the `*` operator has special meaning (matrix multiplication), division operator `/` only does component wise division. So instead of `M1 / M2` you have to write `M1 * inverse(M2)`. And on top that, the matrix multiplication swap of arguments described above also applies, for the final form of `inverse(M2) * M1`. Again, alternatively we could transpose the arguments and then transpose the result.1 parent 75d795f commit af66eaf
1 file changed
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
241 | | - | |
| 240 | + | |
| 241 | + | |
242 | 242 | | |
243 | 243 | | |
244 | 244 | | |
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
255 | | - | |
256 | | - | |
| 255 | + | |
| 256 | + | |
257 | 257 | | |
258 | 258 | | |
259 | 259 | | |
| |||
0 commit comments