# include # include # include struct ClickedPoints { float x, y; }; # define MAX_CLICKS 1000 struct ClickedPoints points[MAX_CLICKS]; int clickCount = 0; int WindowWidth = 256; int WindowHeight = 256; void Draw(void) { int loop; glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); glPointSize(5.0f); glBegin(GL_POINTS); for(loop = 0; loop < clickCount; loop++) glVertex2f(points[loop].x, points[loop].y); glEnd(); glLineWidth(2.0f); glBegin(GL_LINE_STRIP); for(loop = 0; loop < clickCount; loop++) glVertex2f(points[loop].x, points[loop].y); glEnd(); glutSwapBuffers(); } void OnMousePress(int button, int state, int x, int y) { float x1, y1; if(clickCount >= MAX_CLICKS) return; //The following two lines converts the //pixel values into [-1, 1] space x1 = (x*2.0f)/WindowWidth - 1.0f; y1 = 1.0f - (y*2.0f)/WindowHeight; points[clickCount].x = x1; points[clickCount].y = y1; clickCount++; glutPostRedisplay(); } void OnWindowResize(int width, int height) { //See how the output changes if you change left and top int left = 0; int bottom = 0; glViewport(left, bottom, width, height); WindowWidth = width; WindowHeight = height; } void OnKeyPress(unsigned char key, int x, int y) { # define ESC_Key 27 switch (key) { case ESC_Key: exit(0); break; } //printf("\nThe mouse is @..(%d, %d)\n", x, y); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(WindowWidth, WindowHeight); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutCreateWindow("tooch"); glutDisplayFunc(Draw); glutKeyboardFunc(OnKeyPress); glutMouseFunc(OnMousePress); glutReshapeFunc(OnWindowResize); glClearColor(0.1, 0.3, 0.6, 0.0); glutMainLoop(); return 0; }