0

I have a 2x4 matrix. which represent 4 points of a square. I need to rotate said square by x radians (which obviously can be converted to degrees) with the rotation point/ anchor being the center of the square.

Since OpenGL ES 2.0 has removed the transformation functions (glPush/glPop, glRotate, glTranslate, glScale, etc.) I need to do the rotation myself. Can someone help me write a function to preform the rotation?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
ManOx
  • 1,935
  • 5
  • 23
  • 37

1 Answers1

0

Rotations in 2D can be expressed as (where theta is in radians):

cs = cos(theta);
sn = sin(theta);

out.x = in.x * cs - in.y * sn;
out.y = in.x * sn + in.y * cs;

The above linear equations can be expressed as a 2x2 matrix (in american way, arranged in columns):

|  cs  sn |
| -sn  cs |

The matrix can be expanded to add also translations:

|  cs  sn  tx |
| -sn  cs  ty |
|   0   0  1  |

In OpengGLES 2.0 you will probably be packing those 4 points as an array of two components, you will need to transform them in a vertex shader. You compute the above matrix and send it down the pipeline using an uniform.

This can get quite long if you don't know OpenGL ES 2.0 pipeline. Do you require more info?

Trax
  • 1,890
  • 12
  • 15
  • Haha yes, I have a pretty basic understand of the actual things that go under the hood in OpenGL in general. The OpenGL ES pipeline as I understand it is even more crazy. I'm not even using a custom Shader BTW so if I could do this without using the GLSL I'd be a very happy camper. – ManOx May 19 '13 at 08:34
  • There is no fixed pipeline in OpenGL ES 2.0, you will need to use GLSL. I found this for you: http://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf It a simple start, you will need to change the vertex shader to add an affine transform but that is relatively easy. – Trax May 19 '13 at 08:42
  • So there's no way to do the rotation without custom code in the shader? – ManOx May 19 '13 at 08:54
  • Check this: http://stackoverflow.com/questions/4784137/choose-opengl-es-1-1-or-opengl-es-2-0 – Trax May 19 '13 at 09:30