#include #include #include #include #include /* you may need to locate glut.h on your computer and change the path accordingly. * It is most likely in ~/include or ~include/GL. */ /* the viewport is 500x500 with the origin at (0,0) */ #define WINDOW_SIZE 500 static int startX, startY, endX, endY; void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); } void display(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); printf("line = <%d, %d> -> <%d, %d>\n", startX, startY, endX, endY); glBegin(GL_LINES); glVertex2i(startX, startY); glVertex2i(endX, endY); glEnd(); glFlush (); } int motionX=0; int motionY=0; int Ctrl=0; void motion(int x, int y) { printf("motion = <%d, %d>\n", x, WINDOW_SIZE-y); } void keyboard(unsigned char key, int x, int y) { printf("key down = %c\n", key); switch(key){ case 'q': exit(0); break; default: break; } } void keyup(unsigned char key, int x, int y) { printf("key up = %c\n", key); } void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { printf("mouse down = <%d, %d>\n", x, WINDOW_SIZE - y); startX = x; startY = WINDOW_SIZE - y; } else { printf("mouse up = <%d, %d>\n", x, WINDOW_SIZE - y); endX = x; endY = WINDOW_SIZE - y; glutPostRedisplay(); } break; default: break; } } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 500.0); // 3d glMatrixMode (GL_MODELVIEW); glLoadIdentity(); /* assume a square window of size WINDOW_SIZE */ gluLookAt (WINDOW_SIZE/2.0, WINDOW_SIZE/2.0, 3.0*(WINDOW_SIZE)/4.0, WINDOW_SIZE/2.0, WINDOW_SIZE/2.0, 0.0, 0.0, 1.0, 0.0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (WINDOW_SIZE, WINDOW_SIZE); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); glutKeyboardUpFunc(keyup); glutMainLoop(); return 0; }