Computer Graphics Seminar Report
Computer Graphics Seminar Report
Computer Graphics Seminar Report
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.
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;
}
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;
}