Jump to content
  • Advertisement
Sign in to follow this  
Hector San Roman Lanza

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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(
(float) getParent()->getEngine()->GetCurrentWidth() / getParent()->getEngine()->GetCurrentWidth() ,
} Edited by ShotoReaper

Share this post

Link to post
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 this post

Link to post
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;
vec3 rotatedVector = vec3(matview * someVec4);
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 this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!