2

I need a function that applies a translation like glTranslate() to a point. Does someone know of a function like glTranslate() and glRotate() to modify and retrieve a matrix to multiply?

Derek
  • 1,104
  • 13
  • 35
ghiboz
  • 7,863
  • 21
  • 85
  • 131

2 Answers2

3

There are thousands of free matrix classes out there. Have a hunt round google and then you can set up a translation and rotation without using the gl* functions at all ...

edit: If you really just want to create the matrix without using a more than handy matrix class then you can define glRotatef( angle, x, y, z ) as follows:

const float cosA = cosf( angle );
const float sinA = sinf( angle );
float m[16] = { cosA + ((x * x) * (1 - cosA)), ((x * y) * (1 - cosA)) - (z * sinA), ((x * z) * (1 - cosA)) + (y * sinA), 0.0f,
                ((y * x) * (1 - cosA)) + (z * sinA), cosA + ((y * y) * (1 - cosA)), ((y * z) * (1 - cosA)) - (x * sinA), 0.0f,
                ((z * x) * (1 - cosA)) - (y * sinA), ((z * y) * (1 - cosA)) + (x * sinA), cosA + ((z * z) * (1 - cosA)), 0.0f,
                0.0f, 0.0f, 0.0f, 1.0f
               };

As taken from wikipedia.

A translation matrix is really easy to create: glTranslatef( x, y, z) is define as follows:

float m[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 1.0f, 0.0f,
                   x,    y,    z, 1.0f
               };

You can look up matrix multiplication and matrix-vector multiplication pretty easily.

To make your life simple, though, you could just download a matrix class which does all this for you ...

Goz
  • 61,365
  • 24
  • 124
  • 204
  • thanks, but I don't need a class to manage the matrix, only a code like the 2 opengl functions wrote above – ghiboz Dec 14 '10 at 12:49
  • @ghiboz: Updated my answer for you. But I'd strongly recommend just using a matrix class. – Goz Dec 14 '10 at 13:12
  • @Goz, your matrix is missing the leading diagonal 1s. See my answer. – Jackson Pope Dec 14 '10 at 14:03
  • 1
    BTW- does your M[16] use the right convention (i.e. for multiplying a matrix by a column vector)? I believe it's a DX-ish matrix which you've shown and it should be a transpose of it if we follow the OpenGL conventions. – Kos Dec 15 '10 at 11:35
  • 1
    @Kos: Actually GL and DX use the same float order for their matrices (See 9.005 @ http://www.opengl.org/resources/faq/technical/transformations.htm). That said the rotation may still be transposed. – Goz Dec 15 '10 at 11:44
  • This matrix is column-major (ie. "fortran" convention) if you want to multiply by a column vector (ie. "DX" convention), and row-major (ie "C" convention) if you want to multiply it by a row vector (ie. "GL" convention). Beware when multiplying. – Alexandre C. Dec 15 '10 at 11:57
  • @Gez - thanks for the link. Now I'm confused- I need to wrap my head around that :-). True that 'row-major' and 'column-major' are just internal representations and have nothing to do with, say, the x,y,z translation coords being in the last row or last column. – Kos Dec 15 '10 at 14:14
1

Here you can find general info about transformations using the matrices.

Here you can find examples and complete functions that does matrix multiplications (either scaling, rotating or whatever). It is written in objective C, but it shouldn't be to hard to rewrite it in c++..

sinek
  • 2,458
  • 3
  • 33
  • 55