r/GraphicsProgramming • u/Cage_The_Nicolas • May 20 '22
Question Transform cube so that one face is positioned at x,y,z and the opposite face at x1,y1,z1
I'm trying to create a system that renders a "thick" line with OpenGL. What I was wanting to do is generate a transformation matrix for this cube so that given 2 points, one face will be placed at the first point and the opposite face at the other point. The scale part is easy, just compute the distance and scale the cube on the x, y or z axis. The translation is also not that difficult, just place the mesh at the middle point between the two given points.
My problem is with the rotation matrix...
Thanks.
5
May 20 '22
How can you expect anyone to help you if you don’t provide anything that you’ve done and the results?
3
u/wm_cra_dev May 20 '22
Get the direction the line is facing, and the initial "direction" the cube is pointing in.
Find the rotation to get from the initial direction to the line direction.
Make a rotation matrix for that rotation.
8
u/TimJoijers May 20 '22
Instead of thinking about rotation, think about orientation. You need orthonormal basis. You can get one by three steps: Given two points A and B, get direction vector B - A and normalize it. This is your first basis vector v1. Then, choose "up" vector, for example (0, 1, 0), and use normalize(cross(v1, up)) as second basis vector v2. Finally, for last basis vector v3 you can use normalize(cross(v1, v2)).
Your matrix is {v1, v2, v3}.
The "up" vector may need to be chosen by some logic if you want this to work also for case where v1 would match any single fixed vector such as (0, 1, 0). One way to do this is to examine which component of v1 is smallest, and set that component of "up" to one, and all other components zero.