The SpaceMouse controller may implement this interface:
SpaceMouseListener : to listen the left and right button of the SpaceMouse.
SpaceMouseMotionListener: to listen the movement on the six degrees of freedom.
Sample code
//this is what should look your SpaceMouse controller at the beginning.
public class SpaceMouse3DViewController implements SpaceMouseListener, SpaceMouseMotionListener {
//Base interface for JOGL 3D view components, We will use it to apply our action on the view.
private cgJOGL3DView _view;
public SpaceMouse3DViewController(cgJOGL3DView view) {
_view = view;
}
@Override
public void spaceMouseMoved(SpaceMouseMotionEvent event) {
//event listen on the movement on the six degrees of freedoms
}
@Override
public void spaceMousePressed(SpaceMouseButtonEvent event) {
//event listen pressing buttons
}
@Override
public void spaceMouseReleased(SpaceMouseButtonEvent event) {
//event listen releasing buttons
}
}
In the example below, we show how to apply the translation of our SpaceMouse on the view, You can also apply the translation on every other Matrix4d.
Sample Code
private Matrix4d translationSpaceMouse(
SpaceMouseMotionEvent event){
// initialise our translation matrix
Matrix4d translation = new Matrix4d();
translation.setIdentity();
// set the translation of our spaceMouse on our translation matrix
translation.m03 = -event.tx;
translation.m13 = -event.ty;
translation.m23 = event.tz;
return translation;
}
public void spaceMouseMoved(SpaceMouseMotionEvent event) {
if (_view == null){
return;
}
Matrix4d translation=translationSpaceMouse(event);
//get the matrix of our model
Matrix4d model = _view.getModelMatrix();
//get the matrix of our view
Matrix4d viewMatrix = _view.getViewMatrix();
//apply our translation on our view.
translation.mul(viewMatrix);
//set our model
_view.setModelMatrix(model);
//set our view alter by our translation
_view.setViewMatrix(translation);
//repaint the view, to display the changement on our view and model.
_view.getViewComponent().repaint();
}
In the example below, we show how to apply the rotation of our SpaceMouse on the view.
So now we know how to rotate and translate our view. To combine these transformations, we need to multiply matrix in this order:
TransformMatrix = TranslateMatrix*RotateMatrix*ViewMatrix;
Writing the operations in another order wouldn’t produce the result expected.
Sample Code
public void spaceMouseMoved(SpaceMouseMotionEvent event) {
if (_view == null){
return;
}
Matrix4d translation = translationSpaceMouse(event);
Matrix4d rotation =rotationSpaceMouse(event);
//get the matrix of our view
Matrix4d viewMatrix = _view.getViewMatrix();
//apply our rotation on our view.
rotation.mul(viewMatrix);
//apply our translation on our new vie.
translation.mul(rotation);
//set our view alter by our rotation and translation
_view.setViewMatrix(translation);
//repaint the view, to display the changement on our view and model.
_view.getViewComponent().repaint();
}
Sample Code
@Override//
public void spaceMousePressed(SpaceMouseButtonEvent event) {
if(event.buttonId==0){
//left button pressed;
}
if(event.buttonId==1){
//right button pressed;
}
};
@Override
public void spaceMouseReleased(SpaceMouseButtonEvent event){
if(event.buttonId==0){
//left button released;
}
if(event.buttonId==1){
//right button released;
}
};