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

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.

1

u/zertech Mar 26 '21

Your missing a translation matrix.

You combine 3 matrices to do that.

Matrix A - translation - translates by distance from quad center to origin.

Matrix B - rotation matrix. Same one your already using.

Matrix C - does the opposite translation that matrix A does. Moves cube by distance from center to old location of quad center.

Than in your VS, you multiply the vertex by the matrices like this: (A * b * C) * Vertex.

1

u/Marvin-Wynston-Smyth Mar 26 '21 edited Mar 27 '21

I can see you're back on track so that's good, but I'll drop a line anyway. :)

Three things are very important:

  1. which space something's in and which space it needs to end up in;

  2. the order of transformations; and

  3. how to use transformations to get what you want.

You can save yourself a lot of pain now by spending a weekend on it, because later you might need quaternions for nice rotations and matrix stacks to do individual moving parts on a model etc, and the ordeals of camera space and TBN for normal mapping await. Not to mention projective texture lighting. All that stuff is prohibitively hard if you're not solid on transforms.

So for number 1, in general, we send everything to the vertex shader in model space. So your quad could be a "unit quad" with each vertex direction just being 1.0f (they can be anything, but 1 is easy). But the important thing is, the origin is right in the middle of model space.

For the order of transforms, generally scale needs to come first. Weird stuff can happen if you scale something that isn't in model space (especially with lighting normals!!)

For using transforms to get what you want, some general ones:

  1. To rotate an object around its own axis: start in model space, scale next, apply rotation, translate to world space.

  2. We never really need to rotate something in world space, but if somehow we did it would be: model space, scale, translate to world space, apply rotation.

  3. To rotate an object around something else (ie an orbit, or the camera around a central point), we can think of it in terms of using "orbit space", which places the origin in the centre of the "orbitee" point. So that would be: model space, scale, translate by the vector between orbiting object and orbitee point, apply the rotation, then apply the world transform.

  4. Door hinges: To make stuff hinge, we invent "hinge space", which is effectively model space with the object offset by half its length on the axis perpendicular to the axis of hinge. So to make a door swing, it would be: model space, scale, transform to hinge space, apply the rotation, apply the world transform.

Pen and paper are your friends as you go.

So if you can do that with your quads, you'll be the matrix master in no time. :)