Comments (6)
I really depends on your point of view.
There's 2 things that often get conflated
-
Interpretation order
-
Storage order
Matrices in WebGL, OpenGL and on these pages are stored in row major order.
Create a C/C++ OpenGL test, do glTranslate(11,22,33)
, then glGet
the matrix and print it out and you`ll get
1, 0, 0, 0
0, 1, 0, 0,
0, 0, 1, 0,
10, 20, 30, 1,
That's clearly row-major storage.
This is the same in both OpenGL and DirectX. I've never worked in any code in my entire life that stored matrices in column major order. We can open the code for Unity or Unreal running in DirectX and see they all use row-major storage.
In other words
- elements 0,1,2 = xaxis
- elements 4,5,6 = yaxis
- elements 8,9,10 = zaxis
- elements 12,13,14 = translation
That is row major storage order by every computer programming book ever. Issue 2 above
The second issue is how the data is interpreted. Even though the matrices are clearly stored in row major order that's irrelevant to how multiplication works (which AFAIK is the only place row vs column interpretation matters). It's up to the the implementation of matrix * matrix and matrix * vector on how to use the data.
If we look at typical math book it will show a diagram like this for matrix * vector
matrix vector
+---------+ +---+
| a e i m | | x | = x * a + y * e + z * i + w * m
| b f j n | | y | = x * b + f * f + j * g + w * n
| c g k o | | z | = x * c + y * g + z * k + w * o
| d h l p | | w | = x * d + y * h + z * l + w * p
+---------+ +---+
But you can clearly see that based on that diagram and the fact that the matrices are stored in row-major storage, the math implemented in GLSL and in OpenGL's math library is not using them as they are stored if you want that result.
So by the math definition it's using the data in a column major way but in the computer science definition the data is stored row-major
or something like that π
from webgl-fundamentals.
@greggman , thanks , that's clear enough.
But what I was confused about is the transformation order.
In the article, you apply matrix in this order
// Compute the matrix
var matrix = m3.projection(gl.canvas.clientWidth, gl.canvas.clientHeight);
matrix = m3.translate(matrix, translation[0], translation[1]);
matrix = m3.rotate(matrix, angleInRadians);
matrix = m3.scale(matrix, scale[0], scale[1]);
You can see it's something like var matrix = projMatrix * transMatrix * rotateMatrix * scaleMatrix
right?
If put these matrices in shader, will be something like
...
var matrix = projMatrix * transMatrix * rotateMatrix * scaleMatrix;
gl_Position = vec4((matrix * vec3(a_position, 1)).xy, 0, 1);
...
So it should be scale first, then rotate, transport, projection. Am I right? And your explanation is the opposite order. Can you help me out? Thank you.
from webgl-fundamentals.
Maybe I missing what your saying.
The code above is in the same order in JS and GLSL and you'll get the same results in both
JS
var matrix = m3.projection(gl.canvas.clientWidth, gl.canvas.clientHeight);
matrix = m3.translate(matrix, translation[0], translation[1]);
matrix = m3.rotate(matrix, angleInRadians);
matrix = m3.scale(matrix, scale[0], scale[1]);
GLSL
mat4 matrix = projMatrix * transMatrix * rotateMatrix * scaleMatrix;
Is there somewhere I'm staying it's the opposite?
It goes both ways. You can look at it as
-
first you scale the position
scaledPosition = scaleMatrix * position
-
then you rotate the scaledPosition
rotatedPosition = rotateMatrix * scaledPosition
-
then you translate the rotatedPosition
translatedPosition = translateMatrix * rotatedPosition
-
then you convert the translatedPosition to clipspace
clipspacePosition = projectionMatrix * translationPosition
But most people read left to right. In that paradigm it as described in the article
-
projMatrix
converts clipspace to frustum space
-
transMatrix
moves the space
-
rotateMatrix
rotates the space
-
scaleMatrix
scales the space
It's that final space your positions exist in.
Maybe I should add the first explanation as well?
from webgl-fundamentals.
@greggman , that's exactly what I want, maybe I am not good at English expression π.
I always use the first sequence to think about it, and now I understand it at all. And translation is base on deep understanding I think.
Thanks again. π»
from webgl-fundamentals.
I added the first explanation to that article. No diagrams yet.
from webgl-fundamentals.
@greggman οΌI saw it, and I will update the article after you updated.
from webgl-fundamentals.
Related Issues (20)
- Duplicate gl.bindBuffer in webgl-fundamentals.html? HOT 1
- Suggestion: How about to use unit square instead of unit quad? HOT 4
- Is the example lost? HOT 3
- Is it modulo or module? HOT 1
- WebGL Using 2 or More Textures (wrong order of calls) HOT 6
- Explanation error HOT 1
- Skinning inverse world matrix HOT 1
- possible bug in m3.js HOT 1
- another bug in m3.js HOT 3
- error in textures example code HOT 2
- [SUGGESTION] Point Sprites HOT 4
- data-textures: Mention (data in texture)=(data supplied)/255?
- webgl-state-diagram use-2-programs does not work HOT 6
- Broken links HOT 1
- webgl state diagram : draw on cube
- webgl-state-diagram: extraneous column heading in vertex array HOT 2
- Broken link in Load Obj HOT 1
- webgl-how-it-works.html: some poor rounding, one wrong number
- webgl-image-processing: images don't actually have to be same-origin
- Camera tutorial requirements
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webgl-fundamentals.