r/GraphicsProgramming • u/[deleted] • 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?
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:
which space something's in and which space it needs to end up in;
the order of transformations; and
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:
To rotate an object around its own axis: start in model space, scale next, apply rotation, translate to world space.
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.
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.
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. :)
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/