cancel
Showing results for 
Search instead for 
Did you mean: 

Quick question about getting orientation for OpenGL

tmason101
Honored Guest
Hello,

So I have a weird issue with orientation. Essentially I can get the correct position without an issue but my orientation is screwed up such that either looking up means I am looking down in the OpenGL environment OR looking left means I am looking right in the environment.

Obviously I would like it such that I am looking in the right direction at all times.

So, here is my code example:


glm::mat4 BulletOrientationMatrix = glm::mat4(1.0f);

ovrMatrix4f OculusRiftOrientation = OVR::Matrix4f(Current_HeadPose[EyeToCalculate].Orientation).Inverted();

for (int o = 0; o < 4; o++) {
for (int i = 0; i < 4; i++) {
BulletOrientationMatrix[o][i] = OculusRiftOrientation.M[o][i];
}
}

AdjustedPitch += HorizontalAngle;

HorizontalAngle = 0;

BulletOrientationMatrix = glm::transpose(BulletOrientationMatrix);

BulletOrientationMatrix = glm::rotate(BulletOrientationMatrix, AdjustedPitch, glm::vec3(0.0f, 1.0f, 0.0f));


With this code I currently look up in the OpenGL environment when I look down physically in real life.

My position, adjusted for both DK2 position tracking and eye-adjustment, is fine so it is just this orientation stuff.

Any assistance greatly appreciated!
9 REPLIES 9

nuclear
Explorer
Why are you inverting the matrix? If as you say your orientation changes in exactly the opposite way from what it should, maybe you should try not inverting it.

Btw, don't invert rotation matrices, transposing a rotation matrix inverts it, and it's a much cheaper operation.

tmason101
Honored Guest
Hello,

Thanks much for your reply.

When I remove the call for inversion of the Orientation Matrix looking up and down works fine but left and right are opposites. I tried removing the transpose from glm as well but then the original problem is there again.

It's like I get either or.

What else do I need to do to get directional viewing working?

Thank you for all of your help.

vrdaveb
Oculus Staff
You may need to change the handedness of your matrix. Try multiplying by:
{ 1, 0,  0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1 }

tmason101
Honored Guest
"vrdaveb" wrote:
You may need to change the handedness of your matrix. Try multiplying by:
{ 1, 0,  0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1 }


This indeed works! Thank you.

One last nitpick; head tilting is the opposite now 😞

Do I just change the handedness in another part of the matrix above?

Thank you again!

vrdaveb
Oculus Staff
Not sure what conventions your project is using. The first matrix just scaled the Z axis by -1. You might try scaling X and Y by -1 instead:
{ -1, 0,  0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 }

tmason101
Honored Guest
"vrdaveb" wrote:
Not sure what conventions your project is using. The first matrix just scaled the Z axis by -1. You might try scaling X and Y by -1 instead:
{ -1, 0,  0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 }


Hello,

Tried that, but then I got back to where we started.

I am working with GLM and OpenGL, by the way.

Thank you for your time.

tmason101
Honored Guest
Just to update the thread, thanks to the help from vrdaveb up/down and left/right head movement is fine with the following code:

OculusToGLMMatrix = glm::mat4(1.0f);

ovrMatrix4f OculusRiftOrientation = OVR::Matrix4f(Current_HeadPose[EyeToCalculate].Orientation);

for (int o = 0; o < 4; o++) {
for (int i = 0; i < 4; i++) {
OculusToGLMMatrix[o][i] = OculusRiftOrientation.M[o][i];
}
}

/*

The AdjustedPitch stays a certain value such that when the person moves their mouse the resultant rotation inside the
3D environment is added/subtracted from the Adjusted pitch.

In this way the person can rotate using the mouse and the Oculus Rift device freely.

*/

AdjustedPitch += HorizontalAngle;

HorizontalAngle *= CurrentMouseMovementSpeed * (*deltaTime);

OculusToGLMMatrix = glm::transpose(OculusToGLMMatrix);

/*

Change the "handedness" of the rotation using the following Rotation Fix matrix.

*/

GLfloat* RotationFixArray = new GLfloat[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,
0.0f, 0.0f, 0.0f, 1.0f };

glm::mat4 RotationFixOrientation = glm::make_mat4(RotationFixArray);

OculusToGLMMatrix *= RotationFixOrientation;

OculusToGLMMatrix = glm::rotate(OculusToGLMMatrix, AdjustedPitch, glm::vec3(0.0f, 1.0f, 0.0f));

CurrentRightDirection = glm::vec3(OculusToGLMMatrix[0][0], OculusToGLMMatrix[1][0], OculusToGLMMatrix[2][0]);

CurrentUpDirection = glm::vec3(OculusToGLMMatrix[0][1], OculusToGLMMatrix[1][1], OculusToGLMMatrix[2][1]);

CurrentViewingDirection = glm::vec3(OculusToGLMMatrix[0][2], OculusToGLMMatrix[1][2], OculusToGLMMatrix[2][2]);

/*

View adjusted position is from prior calculations based on the position of the eyes and the position of the device relative to the computer.

*/

CurrentCameraMatrix = glm::lookAt(ViewAdjustedPosition, ViewAdjustedPosition + CurrentViewingDirection, CurrentUpDirection);

tmason101
Honored Guest
The problem with the above code that remains is head tilting not working.

Currently with the above code when you TILT your head left you tilt right and vice versa.

Is there anything I can do to fix that based on what you see above?

Thank you.

EpicZa
Honored Guest
"tmason101" wrote:
The problem with the above code that remains is head tilting not working.

Currently with the above code when you TILT your head left you tilt right and vice versa.

Is there anything I can do to fix that based on what you see above?

Thank you.



Did you get a solution to this?

I'm running into the same issues of the left and right or if I don't invert the matrix up and down.