Thursday, April 4, 2013

3DConnexion SpaceNavigator on GLFW

After a bit of research I concluded that glfw is the best way to use the space navigator. I've tested it in ubuntu linux 12.10 and 13.04 under glfw 2.7. On Windows 7 one of the axis wasn't working but it's been a while since I tested it. Perhaps they have fixed the drivers.

Here's the relevant code segment. I use glm for matrices. All I do is get the value from the array filled by glfwGetJoystickPos and apply that to my model's matrix. TRSCALE and RTSCALE are scaling factors for translation and rotation.

void Engine::checkSpaceNavigator() {

#define TRSCALE 0.2f
#define RTSCALE 5.0f

    float position[6];
    std::fill_n(position,6,0.0f);

    if (glfwGetJoystickPos( GLFW_JOYSTICK_1, position,6) == 6 ) {

    cursor.modelMatrix = glm::translate(vec3(position[0]*TRSCALE,0,0))*cursor.modelMatrix;
    cursor.modelMatrix = glm::translate(vec3(0,-1*position[2]*TRSCALE,0))*cursor.modelMatrix;
    cursor.modelMatrix = glm::translate(vec3(0,0,-1*position[1]*TRSCALE))*cursor.modelMatrix;
    cursor.rotate(glm::rotate(RTSCALE*-1*position[3],glm::vec3(1,0,0)));
    cursor.rotate(glm::rotate(RTSCALE*position[4],glm::vec3(0,0,1)));
    cursor.rotate(glm::rotate(RTSCALE*position[5],glm::vec3(0,1,0)));
    }
}

The above code controls some object directly. If you want to control your viewMatrix to fly around the scene then use the following:

 viewMatrix = glm::translate(vec3(-1*position[0]*TRSCALE,0,0))*viewMatrix;
 viewMatrix = glm::translate(vec3(0,position[2]*TRSCALE,0))*viewMatrix;
 viewMatrix = glm::translate(vec3(0,0,position[1]*TRSCALE))*viewMatrix;
 viewMatrix = glm::rotate(RTSCALE*-1*position[5],glm::vec3(0,1,0))*viewMatrix;
 viewMatrix = glm::rotate(RTSCALE*-1*position[4],glm::vec3(0,0,1))*viewMatrix;
 viewMatrix = glm::rotate(RTSCALE*position[3],glm::vec3(1,0,0))*viewMatrix;

Enjoy, and as always if you have any questions write a comment. 

The windows driver for spacenavigator did not play well with GLFW, I found one axis to NOT be working. If anybody has an update on that shoot me a comment.

No comments:

Post a Comment