I'm beginning with GLSL and I learning the basic, I am using glm to do the matrix calculations at this point everthing ok,the problem is how I can move the origin to the center of my object, not the other way round, for example if I have a quad whose coordinates are bottom left 0,0. bottom right 2,0. top left 0,2. top right 2,2. how can I move the origin to the center of the quat without re-writing my coordinates?
Easy way of building the rotation matrix:
- Start with an identity matrix
- Translate the matrix by -centre of the object
- Rotate the matrix by the desired amount
- Translate the matrix by centre of the object
- Use the resulting matrix to transform the object that you desire to rotate
In order to do that, without moving your quad, you have to use the matrices in OpenGL. In order to do this, you must,
a. Push the matrix (tell OpenGL that you will start to do some transformations, which will switch to the local coordinate system).
b. Translate to the center (Since this is the local coordinate system, you cant directly jump to a point, you must add/subtract your x, y, or z values to get there.
c. Do transformations, like rotation, and scaling. It has to be done before drawing, but it is local coordinates, so only this shape will rotate.
d. Draw the points, using the local coordinate system. Yes, you will have to reinput your vertices, but transformations are alot easier.
e. Pop the matrix (Tell OpengGL that you are done, and it will reset the transformation back to the starting point, and to the world coordinate system).
glPushMatrix(); //at this point, you are at 0, 0, 0, local space glTranslatef(1.0, 1.0, 0.0); //translate 1 to the right, and up glRotatef(45.0, 0.0, 0.0, 1.0); //rotate it 45 degrees glVertex3f(-1.0, -1.0, 0.0); //bottom left glVertex3f(-1.0, 1.0, 0.0); //top left glVertex3f(1.0, 1.0, 0.0); //top right glVertex3f(1.0, -1.0, 0.0); //bottom right glPopMatrix(); //end that transformation sequence