Giter Club home page Giter Club logo

Comments (3)

greggman avatar greggman commented on July 25, 2024

The code in the article is the same as glmatrix

https://github.com/toji/gl-matrix/blob/21b745f3a8b095c4a5304978fa8ab131b76a5f9e/src/mat3.js#L294

which is the same as glm

https://github.com/g-truc/glm

which is the same as opengl

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glMultMatrix.xml

It's not a bug. It's what pretty much every graphics library calls "multiply"

In glm (C++) your example of M = TRS is written as

matrix = translation * rotation * scale

The math applied is the same as in this article. The order of storage is the same as well, translation is in offsets 12,13,14 in the matrix (well, offsets 7,8 for a 3x3 matrix)

Also, the confusing part is WebGL consider rows to be columns as mentioned in the this article. No transposing is going on. Only how the data is interpreted

from webgl2-fundamentals.

bnham avatar bnham commented on July 25, 2024

Yeah, I realize now that in multiply(a, b), both a and b are treated as column-major rather than row-major. That makes the result a*b (a postmultiplied by b) as expected.

For the next person that gets tripped up here, I was thrown off because the naming of the variables is different than what you'd expect when using standard math notation, e.g. this series of variables:

    var a00 = a[0 * 3 + 0];
    var a01 = a[0 * 3 + 1];
    var a02 = a[0 * 3 + 2];
    ...

Which I assumed referenced a matrix layout like this (row-major), because usually the first subscript denotes the row and the second subscript denotes the column in math. For instance, naively you might expect a02 to select the first row and the third column:

a00 a01 a02
a10 a11 a12
a20 a21 a22

But actually the variables in the code reference a matrix layout like this (column-major):

a00 a10 a20
a01 a11 a21
a02 a12 a22

I was also thrown off because the diagrams in the articles have the rows where columns would normally be and vice versa, as you wrote about here: https://webgl2fundamentals.org/webgl/lessons/webgl-matrix-vs-math.html

Anyway closing this and thanks for the response. Hopefully the comment helps the next person who gets confused.

from webgl2-fundamentals.

greggman avatar greggman commented on July 25, 2024

Thanks. If/when I write WebGPU articles I'll try to use more "math"y diagrams and explanations and point out the "rows are columns"

In another github issue I pointed out if you want to see the rows as columns you can write it like this

const xaxis = [xx, xy, xz, 0];
const yaxis = [yx, yy, yz, 0];
const zaxis = [zx, zy, zz, 0];
const trans = [tx, ty, tz, 1];

const matrix = [...xaxis, ...yaxis, ...zaxis, ...trans];

or maybe clearer

const column1 = [
  xx,
  xy,
  xz,
  0,
];
const column2 = [
  yx,
  yy,
  yz,
  0,
];
const column3 = [
  zx,
  zy,
  zz,
  0,
];
const column4 = [
  tx,
  ty,
  tz,
  1,
];

const matrix = [...column1, ...column2, ...column3, ...column4];

which if you squint can look like columns 😅

from webgl2-fundamentals.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.