bài tập đồ họa mẫu

109 1.6K 3
bài tập đồ họa mẫu

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

30 bài tập code mẫu cho môn đồ họa , vẽ hình chữ nhật,hình vuông ,...vẽ điểm,hình tam giác, hình tròn , vẽ quy luật của trái đất và mặt trời,vẽ đường tròn và đường thẳng theo các thuật toán,vẽ hình tam giác, tạo hình quay quanh một trục quanh một điểm, vẽ theo thuật toán breseham thuật toán foodfill, thuật toán scanfoodfill, vẽ đường cong bezier, yên ngựa theo đường cong bezier.....

Sau đây mình xin chia sẻ tài liệu về môn đồ họa máy tính cho các bạn đang theo học môn lập trình này có thêm tài liệu để tham khảo khi lập trình.Chúc các bạn học tạp thật tốt với môn đồ họa này nhé 30 bài tập code mẫu Bài tập vẽ điểm Bài 1 #include <windows.h> #include <gl/gl.h> #include <gl/glut.h> void Draw() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glBegin(GL_POINTS); glVertex3f(0.2, 0.2, 0.0); glVertex3f(0.8, 0.2, 0.0); glVertex3f(0.2, 0.5, 0.0); glVertex3f(0.8, 0.5, 0.0); glVertex3f(0.2, 0.8, 0.0); glVertex3f(0.8, 0.8, 0.0); glEnd(); glFlush(); } void Initialize() { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); } int main(int iArgc, char** cppArgv) { glutInit(&iArgc, cppArgv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(250, 250); glutInitWindowPosition(200, 200); glutCreateWindow("Xin chao ban"); Initialize(); glutDisplayFunc(Draw); glutMainLoop(); return 0; } Bài 2 aapoly #include<windows.h> #include <GL/glut.h> #include <stdlib.h> #include <stdio.h> #include <string.h> GLboolean polySmooth = GL_TRUE; static void init(void) { glCullFace (GL_BACK); glEnable (GL_CULL_FACE); glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE); glClearColor (0.0, 0.0, 0.0, 0.0); } #define NFACE 6 #define NVERT 8 void drawCube(GLdouble x0, GLdouble x1, GLdouble y0, GLdouble y1, GLdouble z0, GLdouble z1) { static GLfloat v[8][3]; static GLfloat c[8][4] = { {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0}, {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0}, {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0}, {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0} }; /* indices of front, top, left, bottom, right, back faces */ static GLubyte indices[NFACE][4] = { {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3}, {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1} }; v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0; v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1; v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0; v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1; v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0; v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1; #ifdef GL_VERSION_1_1 glEnableClientState (GL_VERTEX_ARRAY); glEnableClientState (GL_COLOR_ARRAY); glVertexPointer (3, GL_FLOAT, 0, v); glColorPointer (4, GL_FLOAT, 0, c); glDrawElements (GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices); glDisableClientState (GL_VERTEX_ARRAY); glDisableClientState (GL_COLOR_ARRAY); #else printf ("If this is GL Version 1.0, "); printf ("vertex arrays are not supported.\n"); exit(1); #endif } void display(void) { if (polySmooth) { glClear (GL_COLOR_BUFFER_BIT); glEnable (GL_BLEND); glEnable (GL_POLYGON_SMOOTH); glDisable (GL_DEPTH_TEST); } else { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable (GL_BLEND); glDisable (GL_POLYGON_SMOOTH); glEnable (GL_DEPTH_TEST); } glPushMatrix (); glTranslatef (0.0, 0.0, -8.0); glRotatef (30.0, 1.0, 0.0, 0.0); glRotatef (60.0, 0.0, 1.0, 0.0); drawCube(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5); glPopMatrix (); glFlush (); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 't': case 'T': polySmooth = !polySmooth; glutPostRedisplay(); break; case 27: exit(0); /* Escape key */ break; default: break; } } /* Main Loop */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_ALPHA | GLUT_DEPTH); glutInitWindowSize(200, 200); glutCreateWindow(argv[0]); init (); glutReshapeFunc (reshape); glutKeyboardFunc (keyboard); glutDisplayFunc (display); glutMainLoop(); return 0; } Bài 3 alpha 3D #include<windows.h> #include <GL/glut.h> #include <stdlib.h> #include <stdio.h> #define MAXZ 8.0 #define MINZ -8.0 #define ZINC 0.4 static float solidZ = MAXZ; static float transparentZ = MINZ; static GLuint sphereList, cubeList; static void init(void) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 0.15 }; GLfloat mat_shininess[] = { 100.0 }; GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); sphereList = glGenLists(1); glNewList(sphereList, GL_COMPILE); glutSolidSphere (0.4, 16, 16); glEndList(); cubeList = glGenLists(1); glNewList(cubeList, GL_COMPILE); glutSolidCube (0.6); glEndList(); } void display(void) { GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 }; GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 }; GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 }; glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix (); glTranslatef (-0.15, -0.15, solidZ); glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid); glCallList (sphereList); glPopMatrix (); glPushMatrix (); glTranslatef (0.15, 0.15, transparentZ); glRotatef (15.0, 1.0, 1.0, 0.0); glRotatef (30.0, 0.0, 1.0, 0.0); glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent); glEnable (GL_BLEND); glDepthMask (GL_FALSE); glBlendFunc (GL_SRC_ALPHA, GL_ONE); glCallList (cubeList); glDepthMask (GL_TRUE); glDisable (GL_BLEND); glPopMatrix (); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, (GLint) w, (GLint) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0); else [...]... (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; } Bài 4 mặt trăng,mặt trời #include #ifdef APPLE #include #else #include #endif #include #define NON -1 #define SUN 1 #define PLANET 2 static int year =... glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMouseFunc(pick); // thiết lập hàm pick xử lý thông điệp mouse glutMainLoop(); return 0; } Bài 5 đường cong Bezier #include #include #include GLfloat ctrlpoints[4][3] = { { -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0}, {2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}}; void init(void)... glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc (keyboard); glutMainLoop(); return 0; } Bài 6 vẽ cái yên ngựa nhé #include #include #include GLfloat ctrlpoints[4][4][3] = { { {-1.5, -1.5, 4.0}, {-0.5, -1.5, 2.0}, {0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}}, { {-1.5,... glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; } Bài 7 vẽ hình chữ nhật nè #include #ifdef APPLE #include #else #include #endif #include void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,1.0,0.0);... glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(300,300); glutInitWindowPosition(200,200); glutCreateWindow("Hello Thuy Linh"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; } Bài 8 vẽ đường tròn theo thuật toán breseham #include #include void init() { glClearColor(0.0,0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-70,70,-70,70,-1,1);... int main() { glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(400,100); glutInitWindowSize(300,300); glutCreateWindow("bresenham"); init(); glutDisplayFunc(display); glutMainLoop(); } Bài 9 Vẽ đường thẳng theo thuật toán breseham #include #include void init() { glClearColor(0.0,0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-70,70,-70,70,-1,1);... int main() { glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(400,100); glutInitWindowSize(300,300); glutCreateWindow("bresenham"); init(); glutDisplayFunc(display); glutMainLoop(); } Bài 10 tạo hình chữ nhật quay #include #ifdef APPLE #include #else #include #endif #include static GLfloat spin = 0.0; /* góc quay hiện tại của hình chữ... glutDisplayFunc(display); glutReshapeFunc(reshape); /* đăng ký hàm reshape cho sự kiện cửa sổ bị thay đổi kích thước */ glutMouseFunc(mouse); /* đăng ký hàm mouse cho sự kiện về chuột */ glutMainLoop(); return 0; } Bài 11 Vẽ tam giác #include #ifdef APPLE #include #else #include #endif #include void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); . về môn đồ họa máy tính cho các bạn đang theo học môn lập trình này có thêm tài liệu để tham khảo khi lập trình.Chúc các bạn học tạp thật tốt với môn đồ họa này nhé 30 bài tập code mẫu Bài tập. chế độ selection thì đặt tên cho //mặt trời glLoadName(PLANET); if (ichosen == PLANET) // nếu trái đất đang được chọn thì sẽ vẽ khác đi glutSolidSphere(0.2, 30, 30) ; else glutWireSphere(0.2,. glColor3f(1.0, 1.0, 1.0); glBegin(GL_LINE_STRIP); for (i = 0; i <= 30; i++) glEvalCoord1f((GLfloat) i /30. 0); glEnd(); /* The following code displays the control points as dots. */ glPointSize(5.0);

Ngày đăng: 14/06/2014, 14:43

Từ khóa liên quan

Mục lục

  • Bài tập vẽ điểm

  • Bài 2 aapoly

  • Bài 3 alpha 3D

  • Bài 4 mặt trăng,mặt trời

  • Bài 5 đường cong Bezier

  • Bài 6 vẽ cái yên ngựa nhé

  • Bài 7 vẽ hình chữ nhật nè

  • Bài 10 tạo hình chữ nhật quay

  • Bài 11

  • Vẽ tam giác

  • Bài 12 callback

  • Bài 13 checker

  • Bài 14 vẽ clip

  • Bài 16 colormat

  • Bài 17 thuật toán foodfill

  • bài 18 thuật toán scanfill

  • Bài 19 picksquare

  • Bài 20 midpoint

  • Bài 21 vẽ đường thẳng cơ bản

  • Bài 22 lineloop

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

Tài liệu liên quan