How to calculate the view orientation ?

Hello,

I would kike to calcule the actual view orientation.
For that, I execute this code, to get the vertical of the observer and the normal to the plane of projection :

Visual3d_ViewOrientation *myViewOrientation = new Visual3d_ViewOrientation();
*myViewOrientation = myView->ViewOrientation();

Graphic3d_Vector *myViewRefUp = new Graphic3d_Vector();
Graphic3d_Vector *myViewRefPlane = new Graphic3d_Vector();

*myViewRefUp = myViewOrientation->ViewReferenceUp();
*myViewRefPlane = myViewOrientation->ViewReferencePlane();

Now I would like to calculate the three angles of my view, alpha, beta and gamma :
alpha = asin(myViewRefPlane->Y()) * 180 / Pi;
beta = asin(myViewRefUp->X()) * 180 / Pi;
gamma = asin(myViewRefUp->Y()) * 180 / Pi;

It seems to be wrong but I don't know which vector coordinates I must use to get the good view orientation.
Anybody can help me ?

Thank you

Pawel's picture

Hi Mathieu,

I haven't checked your approach but can give an outline of my solution: After re-orienting the view query the OpenGL GL_MODELVIEW_MATRIX and use it to compute the orientation angles:

GLfloat matrix1[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix1);

Matrix4 mat;
mat.element[0][0] = matrix1[0];mat.element[0][1] = matrix1[4];mat.element[0][2] = matrix1[8];mat.element[0][3] = 0;
mat.element[1][0] = matrix1[1];mat.element[1][1] = matrix1[5];mat.element[1][2] = matrix1[9];mat.element[1][3] = 0;
mat.element[2][0] = matrix1[2];mat.element[2][1] = matrix1[6];mat.element[2][2] = matrix1[10];mat.element[2][3] = 0;
mat.element[3][0] = matrix1[3];mat.element[3][1] = matrix1[7];mat.element[3][2] = matrix1[11];mat.element[3][3] = 1;

double tran[16];
int result = unmatrix(&mat, tran);

xAngle = tran[6] * 180.0f / PI;
yAngle = tran[7] * 180.0f / PI;
zAngle = -tran[8] * 180.0f / PI;

The function unmatrix (decomposes the transformation matrix into a sequence of simple transformations) is described in Graphics Gems II and can be found at google books. The code is provided too at the book website.

So far it has worked for me.
Pawel

Mathieu's picture

Hi Pawel !

Thank you very much for your help.
It seems to be good but when I turn around the 'y' axis, xAngle is also modified.
I don't understand why, have you got the same behavior ?

Many thanks for your help

Regards,

Mathieu

Pawel's picture

Hi Mathieu,

what is your 'y' axis?

Remember that the three angles you have obtained have to be applied sequentially. Hence, every rotation is to be considered in a coordinate system that resulted from the previous rotation.

Actually, the behaviour you describe could be right. However, to fully answer your question you would have to provide an exact description of your coordinate systems.

Pawel

Mathieu's picture

Thank you again for your help.

Now I think that I've a problem to identify my rotation axis... They might be different to my Triedron.
I'm still looking for a solution, I'll find ! :)

Mathieu's picture

I found the problem : it's me !

All is ok, like you said, I've to consider every rotation in a coordinate system that resulted from the previous rotation.

Thank you very much Pawel for your help !

Regards

Mathieu