#include #include #include // Rotating cube - ideas borrowed from Hisashi Shirokawa GLuint WIN_WIDTH=glutGet(GLUT_SCREEN_WIDTH)/2.0; GLuint WIN_HEIGHT=glutGet(GLUT_SCREEN_HEIGHT)/2.0; GLfloat d2r = 0.0174532; GLfloat hAngle = 45.0, vAngle = 45.0; GLfloat eyex=0, eyey=0, eyez=0; GLfloat vertices[][3] = {{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; void polygon(int a, int b, int c, int d, int color) { glBegin(GL_POLYGON); glColor3fv(colors[color]); glVertex3fv(vertices[a]); glColor3fv(colors[color]); glVertex3fv(vertices[b]); glColor3fv(colors[color]); glVertex3fv(vertices[c]); glColor3fv(colors[color]); glVertex3fv(vertices[d]); glEnd(); } void cube() { polygon(0,1,5,4,7); // bottom blue green polygon(0,3,2,1,0); // back black polygon(0,4,7,3,3); // left green polygon(2,3,7,6,2); // top yellow polygon(1,2,6,5,4); // right blue polygon(4,5,6,7,1); // front red } // Point on sphere at origin (0,0,0) of radius r given hAngle=horizontial and vAngle=vertical angle void eyeAt(GLfloat r) { eyez = r * sin(vAngle*d2r) * cos(hAngle*d2r); eyex = r * sin(vAngle*d2r) * sin(hAngle*d2r); eyey = r * cos(vAngle*d2r); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); eyeAt(7.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); printf("(%f,%f,%f) %f %f\n",eyex,eyey,eyez,hAngle,vAngle); gluLookAt(eyex,eyey,eyez,0,0,0,0,1,0); cube(); glutSwapBuffers(); glFlush(); } void reshape(int w, int h) { glViewport(0,0,w,h); WIN_WIDTH=w; WIN_HEIGHT=h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,(GLfloat)(WIN_WIDTH)/(GLfloat)(WIN_HEIGHT),0.1,70.0); glMatrixMode(GL_MODELVIEW); } void move(int x, int y) { hAngle = (360.0/WIN_WIDTH)*((double)x+1); // Allow 360 degree vAngle = (180.0/WIN_HEIGHT)*((double)y+1); // Allow 180 degree glutPostRedisplay(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); glutInitWindowSize(WIN_WIDTH,WIN_HEIGHT); glutCreateWindow("View cube"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutPassiveMotionFunc(move); glMatrixMode(GL_MODELVIEW); glEnable(GL_DEPTH_TEST); glClearColor (1.0, 1.0, 1.0, 0.0); glutMainLoop(); }