[Solved] How to get only the world scale in a Shader

Here, I’ll walk you right to it in 2 seconds of google searching:
Data Type (GLSL) - OpenGL Wiki

Good to know we should search “how extract scale from mat4 g_WorldMatrix” it in KHRONOS general Data Type wiki document…

still, its knowledge about what document it is in… if it really tell how to do it, not just how to read matrix values.

In general, if you have code in language X that does a thing… and want to translate that to language Y… it pays to look for how to do that in language Y documentation.

In this case, you have code for extracting scale from a world rotation matrix… which is done in some other language but hopefully it is clear what it’s doing (accessing elements of a matrix and turning them into vectors). So step one would be to figure out how to “access matrix elements” in GLSL… best place to look at that is GLSL docs.

I simply searched for “GLSL matrix3” and found that doc… scrolled down and saw that it explains how to access matrix elements.

But you know, going one step further, I just googled “extract world scale from matrix3 in glsl”… which is exactly what you want.

First link is: android - Get position, rotation and scale from matrix in OpenGL - Stack Overflow

…which I guess also assumes you know the syntax for accessing matrix elements but will at least give you some background on why it works.

well, if i would hear matrix3, not matrix4, i would think same

“it is clear what it’s doing (accessing elements of a matrix and turning them into vectors)”

but i heard its matrix4, so i thought its not that easy. here quote " mat4 g_WorldMatrix contains the location, rotation, and scale of the model"

When it’s a world transform a matrix4 is a matrix3 with translation also… as mentioned in the linked stackoverflow question.

I did look at that stack overflow post, as well as another that had a good visual representation of the math behind it

But the part I was lacking was the GLSL syntax with matrices. I did overlook the Khronos GLSL documentation however, but the Matrix section of the Khronos documentation you linked had a few good examples, and I was able to figure it out. Thanks :smiley:

2 Likes

so, you could not say to us that its just picking values from mat3 matrix(taken from mat4)? instead of linking us document how to read matrix values that was not real answer?

but nvm, i dont want continue this anyway.

For some reason I mistakenly thought a matrix4f was like working with a 4 dimensional array before I saw the example code in the Khronos doc. But now I see it is just a 4x4 size 2 dimensional array.

Her’e’s the GLSL code in JME context, hopefully this can help others learn how to use matrices if they find themself in a similar situation as myself :slightly_smiling_face:

float xTot, zTot, yTot;
xTot = g_WorldMatrix[0][0] + g_WorldMatrix[0][1] + g_WorldMatrix[0][2];
yTot = g_WorldMatrix[1][0] + g_WorldMatrix[1][1] + g_WorldMatrix[1][2];
zTot = g_WorldMatrix[2][0] + g_WorldMatrix[2][1] + g_WorldMatrix[2][2];

vec3 scaleVec = vec3(xTot,yTot,zTot);

I was also able to check the result against the actual WorldScale to ensure that I figured out the math correctly, as I already had the code to pass the WorldScale in as a uniform prior to figuring out the math behind matrices.

2 Likes

Some things about your code is that you left out the “length” which is kind of important. Right now your scale will be off.

Also, some cool tricks in GLSL is that matrix[0][0], matrix[0][1], matrix[0][2] is also a vec3 matrix[0].

So you should be able to change your code above to:

float xTot, zTot, yTot;
xTot = length(g_WorldMatrix[0]);
yTot = length(g_WorldMatrix[1]);
zTot = length(g_WorldMatrix[2]);

vec3 scaleVec = vec3(xTot,yTot,zTot);
1 Like

And by the way, I didn’t mean this to sound as insulting as it might read. I was trying to highlight the fact that you guys hadn’t looked in the right place yet (the GLSL docs about matrix3).

It was clear you hadn’t because the translation was relatively straight-forward at that point (as already determined by now).

i also was too much harsh.

just wanted to say, that same as “Getting Answers” teach about that others dont read in mind.

same way “giving answers” here should also have in mind that “listeners dont read in mind”

main issue was we dont even knew that no need complex algebra matrix operations to solve it. If we would know, the document to find matrix simple operations would be next step for sure. (i had in mind complex algebra matrix operations going there)

But now its solved, so we are all happy.