How can I transform a glm::vec3 by a glm::mat4

This topic is 2167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi!
I want to transform a glm::vec3 ( camera.target) by a glm::mat4(camera.rotationMatrix). I try multiply this give me an error:error: no match for 'operator*' in 'originalTarget * ((Camera*)this)->Camera::rotationMatrix'. I suppose that I can´t multiply a vec3 * mat4.
Does GLM some function to transform this? Other way to do the transform?

The code:

void Camera::Update(void)
{
// Aplicamos la rotacion sobre el target
glm::vec3 originalTarget = target;
glm::vec3 rotatedTarget = originalTarget * rotationMatrix;

// Aplicamos la rotacion sobre el Up
glm::vec3 originalUp = up;
glm::vec3 rotatedUp = originalUp * rotationMatrix;

// Establecemos las matrices de vista y proyeccion
view = lookAt(
position, //eye
rotatedTarget, //direction
rotatedUp //up
);
projection = perspective(
FOV,
(float) getParent()->getEngine()->GetCurrentWidth() / getParent()->getEngine()->GetCurrentWidth() ,
near_plane,
far_plane);
} Edited by ShotoReaper

Share on other sites
If the vec3 represents a normal, you would take the upper left 3x3 and apply it, this removes any translation that might be stored in the 4x4 because you probably dont want to translate a normal. If the 4x4 matrix contains a translation and the vec3 represents a point, you could put the vec3 in a vec4 with w = 1, and apply the 4x4. If the vec3 is a normal you dont want to extract the upper left 3x3, store the vec3 in a vec4 with w = 0. Edited by NumberXaero

Share on other sites
if you have a simple modelview matrix, the 3x3 part is the rotation, or look-vectors if you will
a simple modelview matrix has only rotation and translation
if you scale your matrix, its look-vectors will no longer be unit-length, and you'll have to normalize all operations in the shader
the 4 columns: right, up, forward, translation
the 4th row is useless, except the perspective divide w in the translation vector, which is used in transformation

in GLSL:
vec3 rotatedVector = mat3(matview) * someVec3;
or
vec3 rotatedVector = vec3(matview * someVec4);
or
vec3 rotatedVector = (matview * someVec4).xyz // swizzles are powerful

if your matrix has a scale other than 1.0:
vec3 something = normalize(mat3(matview) * in_something.xyz);

using swizzles (.xyz, .xz, .x, .xw, .xxx, .xyxy) doesn't cost anything, it just as fast using "vector.xyz" as using "vector"
so use them liberally to explain to your future self what you are doing

the w coordinate is the homogenous coordinate, usually only being 1.0 and 0.0
where 1.0 is euclidian space, as in, not infinite
lights usually are placed at (x, y, z, 0) because they are "infinitely far away"
so, if you want to transform a vec3 with a vec4 matrix, for example by providing your vertex position in xyz only:
gl_Position = matproj * matview * vec4(in_vertex.xyz, 1.0);

and finally, I notice it's another GLM question, to which i have no answer..
in any case, if you know what the contents of the matrix is, which i have explained, you should be able to grab the 3x3 part
Edited by Kaptein

Share on other sites
Ok, thanks by the answers. I change the vec3 to vec4 and its works.

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• Forum Statistics

• Total Topics
633417
• Total Posts
3011781
• Who's Online (See full list)

There are no registered users currently online

×