r/GraphicsProgramming • • Mar 26 '21

Rotating a quad

Really noob question, but I'm really bad at math so can somebody explain how can I rotate a quad around its x, y or z axis?

0 Upvotes

10 comments sorted by

View all comments

3

u/[deleted] Mar 26 '21

The transformation is quite simple.

The basic idea of it is to apply the transformation to each vertex. You rotate a vertex around the center. The formula is similar to the 2D rotation except that the 3rd coordinate would remain unchanged which depends on the axis of rotation.

You seem to be new to graphics so I would suggest familiarising with concepts such as model transformation matrices which essentially combines translation, scaling and rotation to one matrix which can be multiplied to each vertex to get the desired transformation.

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

1

u/[deleted] Mar 26 '21

I'm aware of matrices and their transformation, I just can't quite get the quad to rotate around itself. I get it to rotate around the screen space axis, but not its own.

Since I need this rotation for a particle system, to rotate each particle, I thought about doing it in the vertex shader. Bur I just can't seem to get the math right.

1

u/[deleted] Mar 26 '21

For rotation of theta around x axis

x = x';

y = y' * cos(theta) - z' * sin(theta);

z = y' * sin(theta) + z' * cos(theta);

Keep in mind that this is respect to the origin, so if your coordinate spaces are not centered to your quad, you would need to apply the translation before rotating.

1

u/[deleted] Mar 26 '21

The traslation is actually the part I don't get. If I have a position vector, I translate it to the origin, whats the point? I don't quite get what should I translate? The model matrix?

1

u/[deleted] Mar 26 '21

The model matrix transfers from model space to the world space. Your meshes have vertices centered on the model, so, just adding the rotation would do.

1

u/[deleted] Mar 26 '21

I got it finally, thanks a lot!

1

u/Passname357 Mar 26 '21

To rotate about an arbitrary point you can translate to the origin then rotate (because rotations are about the origin) then translate the object you rotated back to its original location. Changing bases is just multiplying by the basis matrix.

1

u/zertech Mar 26 '21

You want to make the actual origin match your quad's center. Than by rotating around the origin, your rotating around the quads center as well. Than you do the opposite translation, and now the vertex locations are the same as if it were rotated in place.