Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply vec3 with mat4 using glm

Tags:

c++

glm-math

At the moment, I convert the vec3 to vec4, multiply it with the matrix, and then cast it back to vec3

Code:

glm::vec3 transformed = glm::vec3(matrix * glm::vec4(point, 0.0))

It works, but I think it is not the good way to calculate it. Is it possible to apply a mat4 on a vec3, without casting it to vec4 and back?

like image 817
Iter Ator Avatar asked Dec 05 '25 14:12

Iter Ator


2 Answers

When working with OpenGL it is very wise to stick with homogeneous coordinates. For 3D space these are 4D vectors where normally the fourth element equals 1. When you do this all your calculations are in 4 dimensional space, so no conversions needed anywhere.

Also note that in your example, you will actually lose any translation that may have been recorded in the transformation matrix. If you want to keep this you'll need to use 1 for the 4th element, not 0.

Appendix F of the Red Book describes how and why homogeneous coordinates are used within OpenGL.

like image 101
wich Avatar answered Dec 08 '25 02:12

wich


When working with OpenGL it is very wise to stick with homogeneous coordinates. For 3D space these are 4D vectors where normally the fourth element equals 1. When you do this all your calculations are in 4 dimensional space, so no conversions needed anywhere.

Also note that in your example, you will actually lose any translation that may have been recorded in the transformation matrix. If you want to keep this you'll need to use 1 for the 4th element, not 0.

Appendix F of the Red Book describes how and why homogeneous coordinates are used within OpenGL.

like image 25
wich Avatar answered Dec 08 '25 04:12

wich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!