Surface and Volumetric Data Rendering and Visualization LAB: Moving the camera Massimo Mauro Department of Information Engineering Faculty of Engineering University of Brescia Via Branze, 38 – 25231 Brescia - ITALY Sergio © 2012Benini S. Benini, M.Mauro Università degli Studi di Brescia - S. Benini - 2011/12 1 Contents Moving the camera with the keyboard Moving the camera with the mouse Sergio © 2012Benini S. Benini, M.Mauro Università degli Studi di Brescia - S. Benini - 2011/12 2 Moving camera with the keyboard Moving our system with the keyboard is quite simple. We want to rotate left/right and up/down with arrows, and we want to zoom in/zoom out with '+/-'. For the zoom, glutKeyboardFunc is good. For the rotation, it isn't, because the arrows are special keys (they don't have an ASCII code). We need to use glutSpecialFunction, and use constants (predefined in glut.h) GLUT_KEY_LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, GLUT_KEY_DOWN. Sergio © 2012Benini S. Benini, M.Mauro Università degli Studi di Brescia - S. Benini - 2011/12 3 Moving camera with the keyboard /Keyboard Zoom and Exit void handleKeypress(unsigned char key, int x, int y) { switch (key) { //Keyboard Rotation void handleSpecialKeypress(int key, int x, int y) { switch (key) { case 27: //Escape key exit(0); break; case GLUT_KEY_LEFT: _angleY+=2.0; glutPostRedisplay(); break; case '+': _zTranslate+=0.25; glutPostRedisplay(); break; case GLUT_KEY_RIGHT: _angleY-=2.0; glutPostRedisplay(); break; case '-': _zTranslate-=0.25; glutPostRedisplay(); break; case GLUT_KEY_UP: _angleX+=2.0; glutPostRedisplay(); break; } } case GLUT_KEY_DOWN: _angleX-=2.0; glutPostRedisplay(); break; } } Sergio © 2012Benini S. Benini, M.Mauro Università degli Studi di Brescia - S. Benini - 2011/12 4 Moving camera with the mouse Moving our system with the mouse is a bit more tricky. Two functions are needed: – glutMouseFunc detects which mouse button has been clicked, his state (pressed or released), and the click position. – glutMotionFunc detects active mouse motion, i.e. motion that is done while pressing a mouse button. We might want to implement zoom with mouse wheel. Unfortunately, GLUT doesn't have a simple and platform-independent way to handle mouse wheel. We will implement the zoom with 'CTRL+Moving Down' for the zoom in and 'CTRL+Moving Up' for the zoom out. – glutGetModifiers will detect the 'CTRL' button. Sergio © 2012Benini S. Benini, M.Mauro Università degli Studi di Brescia - S. Benini - 2011/12 5 Moving camera with the mouse //Mouse click detection void handleMouseClick(int button, int state, int x, int y) { //Mouse moving for rotation and zoom void handleMouseMove(int x, int y) { int mod = glutGetModifiers(); if(button == GLUT_LEFT_BUTTON) { if(dragging&&mod!=GLUT_ACTIVE_CTRL) { _angleX += (y - drag_y_origin)*0.2; _angleY += (x - drag_x_origin)*0.2; drag_x_origin = x; drag_y_origin = y; } else if(dragging&&mod==GLUT_ACTIVE_CTRL){ _zTranslate+=(y - drag_y_origin)*0.02; drag_y_origin = y; } if(state == GLUT_DOWN) { dragging = 1; drag_x_origin = x; drag_y_origin = y; } } } glutPostRedisplay(); } Sergio © 2012Benini S. Benini, M.Mauro Università degli Studi di Brescia - S. Benini - 2011/12 6