GLBasic forum

Codesnippets => Math => Topic started by: Hemlos on 2009-Nov-30

Title: Enter the Matrix, Converting any matrix to GLBasic mat[16]
Post by: Hemlos on 2009-Nov-30
How do i convert a standard(is there one?) rotation matrix m[4][4] to the GLBasic mat[16]?

The help file says :

| a0  a4  a8 a12 |
| a1  a5  a9 a13 |
| a2  a6 a10 a14 |
| a3  a7 a11 a15 |

But i dont understand it.

Is mat[0][1] a1 or a4?
mat[columns][rows]?
mat[rows][columns]?
Title: Re: Enter the Matrix
Post by: Hemlos on 2009-Nov-30
Answering myself, lol...  :x

It depends on the input, it needs to be converted to the opengl standard matrix.
So i put together 2 functions to convert any matrix from any function to the opengl matrix.
This is needed because youll find matrices on the internet written in one fashion or the other, as a matrix can be different from one language to the next.

Title: Re: Enter the Matrix, Converting any matrix to GLBasic mat[16]
Post by: Kitty Hello on 2009-Nov-30
The GLBasic, and thus OpenGL matrix is a set of 3 vectors (4 floats long, last one is zero) that describe direction and scale of x,y, and z axis:
The final vector is the translation:
[Xx, Xy, Xz, 0,  Yx, Yy, Yz, 0,  Zx, Zy, Zz, 0,  Tx, Ty, Tz, 1]
Where the captical letters are the axis, and the lowercase ones are the component of that axis's direction. Sort of...
Title: Re: Enter the Matrix, Converting any matrix to GLBasic mat[16]
Post by: Hemlos on 2009-Nov-30
Hmmm im trying to convert 4 vectors into the matrix to rotate an object.

The object is oriented so that the object is facing +z, and it top is facing +y.

The 4 vectors are defined as follows and never vary from thier original lengths, they are always 1 length in distance from each other and can rotate around the origin in 6 degrees of freedom.

The 4 vectors are always locally oriented to each other as follows:

A. an origin position in 3d (x,y,z)

B. a vector oriented to the right of origin, is locally -x direction

C. a vector oriented in front of the origin, is locally +z direction

D. a vector oriented directly above the origin, is locally +y direction

Again,  B C and D vectors are 1 unit length away from the origin.

Here is the local orientation as a diagram, how these positions are related:

// orientation                    vector desciption
// (C=0,1,0)                     up - locally +y in respect to origin
//        |  /(D=0,0,1)          look - locally + z in respect to origin
//        | /
//        |/___(B=-1,0,0)      right -  locally -x in respect to origin
//   (A=0,0,0)                    origin - local x y z

I can rotate all 3(BCD) around the origin in 6 degrees of freedom with quaternions...no problem...

I have tried the sequence of vector inputs into a matrix array, but they always seem to be attached to 0,0,0 no matter what.

Here is the code i have, converted with the function set below this:

Code (glbasic) Select
DIM m[4][4]
m[0][0]=cam1.rx
m[1][0]=cam1.ry
m[2][0]=cam1.rz
m[3][0]=0

m[0][1]=cam1.ux
m[1][1]=cam1.uy
m[2][1]=cam1.uz
m[3][1]=0

m[0][2]= cam1.lx
m[1][2]= cam1.ly
m[2][2]= cam1.lz
m[3][2]=0

m[0][3]=0
m[1][3]=0
m[2][3]=0
m[3][3]=1

DIM mat[16]
mXY_Matrix()
X_MOVEMENT x,y,z
X_PUSHMATRIX
X_MULTMATRIX mat[]

X_DRAWOBJ Object,0
X_POPMATRIX


This object is always attached in the middle of the object to point xyz (0,0,0)...the pushmatrix isnt moving the object....and the object is stretched out meeting at the look vector....and the object seems flat. I also tried using mYX_Matrix(), and it is messed up looking also, scaling the object up to huge proportions.




Can you help make a function to convert these vectors into the X_MULT, so it moves and rotates an object?

Here are the converters:
Code (glbasic) Select

//Where m[4][4] = m[column][row] = m[x][y]
FUNCTION Opengl_XY_Matrix:
//opengl matrix

DIM mat[16]

mat[0]=  m[0][0]
mat[1]=  m[0][1]
mat[2]=  m[0][2]
mat[3]=  m[0][3]

mat[4]=  m[1][0]
mat[5]=  m[1][1]
mat[6]=  m[1][2]
mat[7]=  m[1][3]

mat[8]=  m[2][0]
mat[9]=  m[2][1]
mat[10]= m[2][2]
mat[11]= m[2][3]

mat[12]= m[3][0]
mat[13]= m[3][1]
mat[14]= m[3][2]
mat[15]= m[3][3]

RETURN 1

ENDFUNCTION



Code (glbasic) Select

//Where m[4][4] = m[row][column] = m[y][x]
FUNCTION Opengl_YX_Matrix:
//opengl matrix



DIM mat[16]

mat[0]=  m[0][0]
mat[2]=  m[1][0]
mat[1]=  m[2][0]
mat[3]=  m[3][0]

mat[4]=  m[0][1]
mat[5]=  m[1][1]
mat[6]=  m[2][1]
mat[7]=  m[3][1]

mat[8]=  m[0][2]
mat[9]=  m[1][2]
mat[10]= m[2][2]
mat[11]= m[3][2]

mat[12]= m[0][3]
mat[13]= m[1][3]
mat[14]= m[2][3]
mat[15]= m[3][3]

RETURN 1

ENDFUNCTION
Title: Re: Enter the Matrix, Converting any matrix to GLBasic mat[16]
Post by: Kitty Hello on 2009-Dec-01
Code (glbasic) Select

//! Rotations: Roll, Yaw, Pitch and Offset by: x,y,z
//? Verdrehung: Roll-, Gier-, Nickwinkel und Verschiebung um: x,y,z
// \param rx - roll angle                       |Rollwinkel
// \param ry - yaw angle                        |Gierwinkel
// \param rz - pitch angle                      |Nickwinkel
// \param x  - offset x                         |Verschiebung in x
// \param y  - offset y                         |Verschiebung in y
// \param z  - offset z                         |Verschiebung in z
// \param M[] - [out] the transformation matrix |[Ausgabe]Die Transformationsmatrix
FUNCTION NewtonBuildMatrix: rx, ry, rz, x, y, z, M[]
LOCAL sa,sb,sc, ca,cb,cc
LOCAL sacb, cacb
DIM M[16]
    ca       = COS(-rx);
    sa       = SIN(-rx);
    sb       = COS(ry);
    cb       = SIN(ry);
    cc       = COS(-rz);
    sc       = SIN(-rz);

sacb=sa*cb
cacb=ca*cb

    M[0]  =   sb * cc;
    M[1]  =  -sb * sc;
    M[2]  =  -cb;

    M[4]  = -sacb * cc + ca * sc;
    M[5]  =  sacb * sc + ca * cc;
    M[6]  =  -sa * sb;

    M[8]  =  cacb * cc + sa * sc;
    M[9]  = -cacb * sc + sa * cc;
    M[10] =   ca * sb;

M[12]= x
M[13]= y
M[14]= z
    M[15]= 1;
ENDFUNCTION
Title: Re: Enter the Matrix, Converting any matrix to GLBasic mat[16]
Post by: Hemlos on 2009-Dec-01
This still wont represent a quaternion rotation, it is euler rotations.

Questions:

1. The camera uses glRotatef() to rotate the camera??
2. Objects use glRotatef(), to rotate an object?  X_ROTATION is glRotatef() ??

Is there a way to rotate an object, the same way we do it with a camera,(origin, pointto, up) ???

Please say yes  :nw:
Title: Re: Enter the Matrix, Converting any matrix to GLBasic mat[16]
Post by: Kitty Hello on 2009-Dec-15
the camera just transforms the projection matrix. I'm using something like gluLookAt() for that.
The X_ROTATION is glRotate (basically). Internally I provide all that myself and call glMultMatrix, later, because that's faster.
Title: Re: Enter the Matrix, Converting any matrix to GLBasic mat[16]
Post by: Hemlos on 2009-Dec-15
Hmm, X_ROTATION gimbal lock problem is really bad when 1 of the 3 rotations are near 90 degrees.

I got this info from opengl...i dont understand it too well..
and gluLookAt is equivalent to glMultMatrixf(M); glTranslated (-eyex, -eyey, -eyez);

Is there a way to rotate objects like this too?

http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html

QuoteLet

F=
centerX − eyeX
centerY − eyeY
centerZ − eyeZ

Let UP be the vector (upXupYupZ).

Then normalize as follows:

f=F
||F||



UP'=UP
||UP||


Finally, let s=fÃâ€"UP', and u=sÃâ€"f.

M is then constructed as follows:

M=
(s[0] s[1] s[2] 0
u[0] u[1] u[2] 0
-f[0] -f[1] -f[2] 0
0 0 0 1
)

and gluLookAt is equivalent to glMultMatrixf(M); glTranslated (-eyex, -eyey, -eyez);