4
\$\begingroup\$

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?

\$\endgroup\$
6
\$\begingroup\$

Easy way of building the rotation matrix:

  1. Start with an identity matrix
  2. Translate the matrix by -centre of the object
  3. Rotate the matrix by the desired amount
  4. Translate the matrix by centre of the object
  5. Use the resulting matrix to transform the object that you desire to rotate
\$\endgroup\$
  • 1
    \$\begingroup\$ It might be worth mentioning that the way you combine these transforms are by multiplying the matrices together in order to form a matrix that applies the transformations in sequence, addressing the OP's concern about rewriting coordinates. \$\endgroup\$ – Lars Viklund Jul 28 '13 at 13:01
  • \$\begingroup\$ @LarsViklund Reading it again it does seem that my answer could be fleshed out a bit more. Brb. \$\endgroup\$ – aaaaaaaaaaaa Jul 28 '13 at 13:53
  • \$\begingroup\$ I was thinking something like that Lars Viklund, but I don't know how to do it, eBusiness I can't do that way beacuse if i translate to the -centre my quad will just be translate with it. \$\endgroup\$ – user33283 Jul 28 '13 at 13:59
  • \$\begingroup\$ @user33283 That is why you translate twice, first to the origin, then you rotate, and then you translate back to the original position. It all gets combined into a single matrix that does all the steps combined without additional overhead. \$\endgroup\$ – aaaaaaaaaaaa Jul 28 '13 at 14:04
  • 1
    \$\begingroup\$ Also important to mention that if you are doing the matrix multiplication in OpenGL you should remember you have to do it in reverse order because of its column majors order. So, in your C code, you would first translate +center, then rotate and then translate center. \$\endgroup\$ – Dan Mar 13 '15 at 21:42
0
\$\begingroup\$

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).

For example,

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

Done!

\$\endgroup\$
  • 2
    \$\begingroup\$ Your example uses old, deprecated functions, and the OP explicitly stated he's using GLSL. thus programmable pipeline. You might want to change your example code to something modern. \$\endgroup\$ – Bartek Banachewicz Jul 28 '13 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.