code ví dụ về ngôn ngữ openGL

4 709 6
code ví dụ về ngôn ngữ openGL

Đang tải... (xem toàn văn)

Thông tin tài liệu

//Vẽ chong chóng quay theo thời gian #include GLfloat anpha=0; void init() { glClearColor (1.0, 0.0, 0.0, 1.0); //glColor3f(1.0, 1.0, 1.0); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); } void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); //Quay chong chóng glRotatef(anpha, 0, 0, 1); //Vẽ chong chóng glPushMatrix(); for(int i=1;i360) anpha=0; glutTimerFunc(50, timer,v); glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); init(); glutDisplayFunc(mydisplay); timer(0); glutMainLoop(); } // Vẽ bàn quay theo trục điều khiển phím #include GLfloat gocx=0, gocy=0, gocz=0; void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void matban(GLfloat x, GLfloat y, GLfloat z) { glPushMatrix(); glScalef (x, y, z); /* modeling transformation */ glutWireCube (1.0); glPopMatrix(); } void chanban(GLfloat x, GLfloat y, GLfloat z) { glPushMatrix(); glScalef (x, y, z); glutWireCube (1.0); glPopMatrix(); } void veban(GLfloat l, GLfloat w, GLfloat h, GLfloat d) { matban(l, d, w); glPushMatrix (); glTranslatef(l/2-d/2, -h/2, w/2-d/2); chanban(d, h, d); glPopMatrix (); glPushMatrix (); glTranslatef(l/2-d/2, -h/2, -(w/2-d/2)); chanban(d, h, d); glPopMatrix (); glPushMatrix (); glTranslatef(-(l/2-d/2), -h/2, w/2-d/2); chanban(d, h, d); glPopMatrix (); glPushMatrix (); glTranslatef(-(l/2-d/2), -h/2, -(w/2-d/2)); chanban(d, h, d); glPopMatrix (); } void display(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glPushMatrix (); glRotatef (gocx,1,0,0); glRotatef (gocy,0,1,0); glRotatef (gocz,0,0,1); veban(2, 1, 0.8, 0.1); glPopMatrix (); glFlush (); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'x': gocx+=5; if (gocx>=360) gocx =0; glutPostRedisplay(); break; case 'y': gocy+=5; if (gocy>=360) gocy =0; glutPostRedisplay(); break; case 'z': gocz+=5; if (gocz>=360) gocz =0; glutPostRedisplay(); break; default: gocx=0; gocy =0; gocz=0; glutPostRedisplay(); 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, 20.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (2.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc (keyboard); glutMainLoop(); return 0; }

Ngày đăng: 09/05/2016, 22:39

Tài liệu cùng người dùng

Tài liệu liên quan