/* PICKING LAB */ #include #include #include // function prototypes void display(void); void reshape(int width, int height); void keyboard(unsigned char key, int x, int y); void mouse(int button, int state, int x, int y); void motion(int x, int y); void init(void); // global vars // viewpoint double theta=.5, phi=.5, d=30; // window int width = 400; int height = 400; using namespace std; int main(int argc, char **argv) { // set up window glutInitWindowSize(400, 400); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Picking Big Objects Lab"); // register callback functions glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMotionFunc(motion); glutMouseFunc(mouse); // initalize opengl parameters init(); // loop until something happens glutMainLoop(); return 0; } void init() { // initialize viewing system glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, 1.0, 0.5, 100.0); glMatrixMode(GL_MODELVIEW); // shading model glEnable(GL_SMOOTH); // initialize background color to black glClearColor(0.0,0.0,0.0,0.0); // enable depth buffering glEnable(GL_DEPTH_TEST); } void reshape(int newWidth, int newHeight) { width = newWidth; height = newHeight; // preserve aspect ratio if (width < height) glViewport(0,0,width,width); else glViewport(0,0,height,height); } void display() { // we'll draw axis lines centered at (0,0,0) double center[3]={0,0,0}; // compute eye position // the eye is a distance d from the center at // at an angle phi from z in plane y=1 // and then rotate the theta out of the y=1 plane // e.g. when the center is (0,0,0) and phi=theta=0 we are at (0,0,d) // looking at the origin glLoadIdentity(); double eye[3]={0,0,0}; eye[0] = center[0] + (float) d * cos(theta) * sin(phi); eye[1] = center[1] + (float) d * sin(theta); eye[2] = center[2] + (float) d * cos(theta) * cos(phi); // compute up vector // we use (0,1,0) as the default up position // this doesn't work if the eye is on the y-axis // but we just won't let the user do that double up[3]={0,1,0}; gluLookAt(eye[0],eye[1],eye[2], center[0],center[1],center[2],up[0], up[1], up[2]); // clear buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1,0,0); glBegin(GL_TRIANGLES); glVertex3f(0,0,0); glVertex3f(5,0,0); glVertex3f(2.5,5,0); glColor3f(0,0,1); glBegin(GL_TRIANGLES); glVertex3f(0,0,-5); glVertex3f(10,0,-5); glVertex3f(5,10,-5); glEnd(); // draw to screen glutSwapBuffers(); } void keyboard(unsigned char key, int mouseX, int mouseY) { switch (key) { case 'h': cout << "Use i to zoom in." << endl; cout << "Use o to zoom in." << endl; break; case 'i': d-=5; break; case 'o': d+=5; break; } glutPostRedisplay(); } /* * This routine reads mouse movement and adjusts * camera position/orientation. */ void motion(int x, int y) { static int currX=-1; static int currY=-1; // wait until a mouse position is recorded and // avoid really big jumps if (currX>0 && abs(x-currX) < width/6 && abs(y-currY) < height/6) { //update phi and theta based on change in x and y int xChange = currX - x; int yChange = y - currY; phi += (xChange / 180.0); theta += (yChange / 180.0); // limit theta to -4pi/9 and 4pi/9 // it is disorienting to lose "up" if (theta < -4*3.14159/9.0) theta = -4*3.14159/9.0; if (theta > 4*3.14159/9.0) theta = 4*3.14159/9.0; } currX = x; currY = y; glutPostRedisplay(); } void mouse(int button, int state, int x, int y) { }