// 3d-move.c #include #include #include #include double WIDTH = 800; double HEIGHT = 800; GLboolean motion = GL_TRUE; GLfloat xangle = 0.0, yangle = 0.0, xspin = 0.0, yspin = 0.0; GLfloat size = 1.0, factor = 10.0; GLboolean leftb = GL_FALSE, middleb = GL_FALSE; void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: /* ESC */ case 'q': case 'Q': exit(0); case 'r': case 'R': xangle = 0.0; yangle = 0.0; size = 1.0; xspin = 0.0; yspin = 0.0; } } void update(void) { if (!leftb){ xangle -= xspin; yangle -= yspin; } glutPostRedisplay(); } void init(void) { } void Draw(void) { GLfloat objectColor[] = {1.0, 0.0, 0.0, 1.0}; glColor3fv(objectColor); glPushMatrix(); { glTranslatef(WIDTH/2, HEIGHT/2, 0.0); // glutSolidCube(300); // glutWireCube(300); // glutSolidSphere(150, 15, 15); // glutWireSphere(150, 15, 15); // glutSolidCone(200, 150, 10, 10); // glutWireCone(200, 150, 10, 10); // glutSolidTorus(30, 150, 20, 20); // glutWireTorus(30, 150, 20, 20); // glScalef(100.0, 100.0, 100.0); // glutSolidDodecahedron(); // glScalef(100.0, 100.0, 100.0); // glutWireDodecahedron(); // glutSolidTeapot(100); glutWireTeapot(100); } glPopMatrix(); } void draw(void) { int t, f; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); { glTranslatef(WIDTH / 2, HEIGHT / 2, 0); glScalef(size, size, size); glRotatef(xangle, 0.0, 1.0, 0.0); glRotatef(yangle, 1.0, 0.0, 0.0); glTranslatef(-WIDTH / 2, -HEIGHT / 2, 0); Draw(); } glPopMatrix(); glutSwapBuffers(); } int oldx, oldy; void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON) { oldx = x; oldy = y; if (state == GLUT_DOWN) leftb = GL_TRUE; else leftb = GL_FALSE; } if (button == GLUT_MIDDLE_BUTTON) { oldx = x; oldy = y; if (state == GLUT_DOWN) middleb = GL_TRUE; else middleb = GL_FALSE; } } void visibility(int state) { if (state == GLUT_VISIBLE && motion) { glutIdleFunc(update); } else { glutIdleFunc(NULL); } } void mouse_motion(int x, int y) { if (leftb) { xspin = x/factor - oldx/factor; xangle -= xspin; yspin = y/factor - oldy/factor; yangle -= yspin; } if (middleb) { size -= y/factor - oldy/factor; if (size < 0.0) size = 0.0; } oldx = x; oldy = y; glutPostRedisplay(); } int main(int argc, char *argv[]) { int i; glutInit(&argc, argv); glutInitWindowSize(WIDTH, HEIGHT); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("3D Move Program"); glutDisplayFunc(draw); glutKeyboardFunc(keyboard); glViewport(0, 0, WIDTH, HEIGHT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, WIDTH, 0, HEIGHT, -10000, 10000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0, 0, 0, 0); glClearDepth(1.0); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glutMouseFunc(mouse); glutMotionFunc(mouse_motion); glutVisibilityFunc(visibility); init(); glutMainLoop(); return 0; }