Computer Graphics Seminar Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

OpenGL Interactive Input-Device Functions

Interactive device input in an OpenGL program is handled with routines in the OpenGL Utility Toolkit
(GLUT), because these routines need to interface with a window system. In GLUT, there are functions to
accept input from standard devices, such as a mouse or a keyboard, as well as from tablets, space balls,
button boxes, and dials. For each device, a procedure (the call back function) is specified and it is invoked
when an input event from that device occurs. These GLUT commands are placed in the main procedure
along with the other GLUT statements.

I did my seminar on two input device functions:

1. GLUT Tablet Functions:


Usually, tablet activation occurs only when the mouse cursor is in the display window. A button
event for tablet input is recorded with glutTabletButtonFunc (tabletFcn); and the arguments for the
invoked function are similar to those for a mouse: void tabletFcn (GLint tabletButton, GLint
action,GLint xTablet, GLint yTablet) We designate a tablet button with an integer identifier such as
1, 2, 3, and so on, and the button action is specified with either GLUT_UP or GLUT_DOWN. The
returned values xTablet and yTablet are the tablet coordinates. The number of available tablet buttons
can be determined with the following command glutDeviceGet
(GLUT_NUM_TABLET_BUTTONS); Motion of a tablet stylus or cursor is processed with the
following function: glutTabletMotionFunc (tabletMotionFcn); where the invoked function has the
form void tabletMotionFcn (GLint xTablet, GLint yTablet) The returned values xTablet and yTablet
give the coordinates on the tablet surface.

Example:
#include <GL/glut.h>
#include <iostream>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Draw a simple point in the middle
glBegin(GL_POINTS);
glVertex2f(0.0f, 0.0f);
glEnd();
glutSwapBuffers();
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
std::cout << "Mouse click at (" << x << ", " << y << ")" << std::endl;
}
}
void initOpenGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glPointSize(5.0f);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("OpenGL Mouse Input Example");
initOpenGL();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

2. GLUT Spaceball Functions:


Following function is used to specify an operation when a spaceball button is activated for a selected display
window: glutSpaceballButtonFunc (spaceballFcn); The callback function has two parameters: void
spaceballFcn (GLint spaceballButton, GLint action) Spaceball buttons are identified with the same integer
values as a tablet, and parameter action is assigned either the value GLUT_UP or the value GLUT_DOWN. The
number of available spaceball buttons can be determined with a call to glutDeviceGet using the argument
GLUT_NUM_SPACEBALL_BUTTONS Translational motion of a spaceball, when the mouse is in the display
window, is recorded with the function call glutSpaceballMotionFunc (spaceballTranlFcn); The three-
dimensional translation distances are passed to the invoked function as, for example: void
spaceballTranslFcn (GLint tx, GLint ty, GLint tz) A spaceball rotation is recorded with glutSpaceballRotateFunc
(spaceballRotFcn); The three-dimensional rotation angles are then available to the callback function, as
follows: void spaceballRotFcn (GLint thetaX, GLint thetaY, GLint thetaZ)

Example:
#include <GL/glut.h>
#include <vrpn_Tracker.h>
#include <iostream>
vrpn_Tracker_Remote *tracker;
void VRPN_CALLBACK handle_tracker(void* userData, const vrpn_TRACKERCB t) {
std::cout << "Position: (" << t.pos[0] << ", " << t.pos[1] << ", " << t.pos[2] << ")\n";
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Placeholder for drawing
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.5, -0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.0, 0.5);
glEnd();
glutSwapBuffers();
}
void idle() {
tracker->mainloop();
glutPostRedisplay();
}
int main(int argc, char** argv) {
// Initialize VRPN tracker
tracker = new vrpn_Tracker_Remote("Tracker0@localhost");
tracker->register_change_handler(NULL, handle_tracker);
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("GLUT Spaceball Example");
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}

You might also like