交互式圖形學(xué)-基于OPenGL自頂向下(第五版)課后實(shí)例程序全_第1頁(yè)
交互式圖形學(xué)-基于OPenGL自頂向下(第五版)課后實(shí)例程序全_第2頁(yè)
交互式圖形學(xué)-基于OPenGL自頂向下(第五版)課后實(shí)例程序全_第3頁(yè)
交互式圖形學(xué)-基于OPenGL自頂向下(第五版)課后實(shí)例程序全_第4頁(yè)
交互式圖形學(xué)-基于OPenGL自頂向下(第五版)課后實(shí)例程序全_第5頁(yè)
已閱讀5頁(yè),還剩55頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、怎么上傳不了??!A.1 Sierpinski鏤墊程序/* two-dimensional Sierpinski gasket */* generated using randomly selected vertices */* and bisection */#include /*you may have to change the include to orelsewhere depending on where it is stored on your system */* glut.h usually has included for gl.h and glu.h */void myin

2、it(void)/* attributes */ glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */* set up viewing */* 50.0 50.0 camera coordinate window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 50.0, 0.0, 50.0); glMatrixMode

3、(GL_MODELVIEW);void display(void)/* A triangle */ GLfloat vertices32=0.0,0.0,25.0,50.0,50.0,0.0; int i, j, k; int rand(); /* standard random number generator */ GLfloat p2 =7.5,5.0; /* an arbitrary initial point inside traingle */ glClear(GL_COLOR_BUFFER_BIT); /* clear the window */ glBegin(GL_POINT

4、S);/* compute and plots 5000 new points */ for( k=0; k5000; k+) j=rand()%3; /* pick a vertex at random */ /* Compute point halfway between selected vertex and old point */ p0 = (p0+verticesj0)/2.0; p1 = (p1+verticesj1)/2.0; /* plot new point */ glVertex2fv(p); glEnd(); glFlush(); /* clear buffers */

5、 void main(int argc, char* argv)/* Standard GLUT initialization */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); /* default, not needed */ glutInitWindowSize(500,500); /* 500 500 pixel window */ glutInitWindowPosition(0,0); /* place window top left on display */ glutCreateWindo

6、w(Sierpinski Gasket); /* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */ myinit(); /* set attributes */ glutMainLoop(); /* enter event loop */A.2 生成Sierpinski鏤墊的遞歸程序/* Recursive subdivision of triangle to form Sierpinski gasket */* number of recursive step

7、s given on command line */#include #include/* initial triangle */GLfloat v32=-1.0, -0.58, 1.0, -0.58, 0.0, 1.15;int n;void triangle( GLfloat *a, GLfloat *b, GLfloat *c)/* display one triangle */ glVertex2fv(a); glVertex2fv(b); glVertex2fv(c);void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c, i

8、nt m)/* triangle subdivision using vertex numbers */ GLfloat v02, v12, v22; int j; if(m0) for(j=0; j2; j+) v0j=(aj+bj)/2; for(j=0; j2; j+) v1j=(aj+cj)/2; for(j=0; j2; j+) v2j=(bj+cj)/2; divide_triangle(a, v0, v1, m-1); divide_triangle(c, v1, v2, m-1); divide_triangle(b, v2, v0, m-1); elsetriangle(a,

9、b,c); /* draw triangle at end of recursion */void display(void) glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_TRIANGLES);divide_triangle(v0, v1, v2, n);glEnd(); glFlush();void myinit() glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2.0, 2.0, -2.0, 2.0); glMatrixMode(GL_MODELVIEW); glClearColor

10、(1.0, 1.0, 1.0, 1.0); glColor3f(0.0,0.0,0.0);void main(int argc, char *argv) n=atoi(argv1);/* or set number of subdivision steps here */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow(Sierpinski Gasket); glutDisplayFunc(display);myi

11、nit(); glutMainLoop();A.3 三維Sierpinski鏤墊的遞歸程序/* Recursive subdivision of a tetrahedron to form 3D Sierpinski gasket */* number of recursive steps given on command line */#include #include /* initial tetrahedron */GLfloat v43=0.0, 0.0, 1.0,0.0, 0., -0.33333, -0., -0., -0.,0., -0., -0.;GLfloat colors4

12、3=1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0;int n;void triangle( GLfloat *va, GLfloat *vb, GLfloat *vc) glVertex3fv(va); glVertex3fv(vb); glVertex3fv(vc);void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d) glColor3fv(colors0); triangle(a,b,c); glColor3fv(colors1); triangle(a,c,d); glCol

13、or3fv(colors2); triangle(a,d,b); glColor3fv(colors3); triangle(b,d,c);void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m) GLfloat mid63; int j; if(m0) /* compute six midpoints */ for(j=0; j3; j+) mid0j=(aj+bj)/2; for(j=0; j3; j+) mid1j=(aj+cj)/2; for(j=0; j3; j+) mid2j=(aj+dj)/2

14、; for(j=0; j3; j+) mid3j=(bj+cj)/2; for(j=0; j3; j+) mid4j=(cj+dj)/2; for(j=0; j3; j+) mid5j=(bj+dj)/2; /* create 4 tetrahedrons by subdivision */ divide_tetra(a,mid0,mid1,mid2, m-1); divide_tetra(mid0,b,mid3,mid5, m-1); divide_tetra(mid1,mid3,c,mid4, m-1); divide_tetra(mid2,mid4,d,mid5, m-1); else

15、tetra(a,b,c,d); /* draw tetrahedron at end of recursion */void display(void) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES);divide_tetra(v0,v1,v2,v3,n);glEnd();glFlush();void myReshape(int w, int h) glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if

16、(w = h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glutPostRedisplay();void main(int argc, char *argv) n=atoi(

17、argv1);/* or enter number of subdivision steps here */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow(3D Gasket); glutReshapeFunc(myReshape); glutDisplayFunc(display); glEnable(GL_DEPTH_TEST); glClearColor (1.0, 1.0, 1.

18、0, 1.0); glutMainLoop();A.4 Marching-Square程序/* generates contours using marching squares */* region size */#define X_MAX 1.0#define Y_MAX 1.0#define X_MIN -1.0#define Y_MIN -1.0/* number of cells */#define N_X 50#define N_Y 50/* contour value */#define THRESHOLD 0.0#includevoid display()double f(do

19、uble,double);int cell(double,double,double,double);void lines(int,int,int,double,double,double,double);double dataN_XN_Y;int i,j;int c;glClear(GL_COLOR_BUFFER_BIT);/* form data array from function */for(i=0;iN_X;i+)for(j=0;jN_Y;j+)dataij=f(X_MIN+i*(X_MAX-X_MIN) /(N_X-1.0),Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-

20、1.0);/* process each cell */for(i=0;iN_X;i+)for(j=0;jTHRESHOLD) n+=1;if(bTHRESHOLD) n+=8;if(cTHRESHOLD) n+=4;if(dTHRESHOLD) n+=2;return n ;/* draw line segments for each case */void lines(int num,int i,int j,double a ,double b,double c,double d)void draw_one(int,int,int,double,double,double,double);

21、void draw_adjacent(int,int,int, double,double,double,double);void draw_opposite(int,int,int,double,double,double,double);switch(num) case 1 : case 2: case 4 : case 7: case 8: case 11: case 13: case 14:draw_one(num,i,j,a,b,c,d);break;case 3: case 6: case 9: case 12:draw_adjacent(num,i,j,a,b,c,d);brea

22、k;case 5: case 10:draw_opposite(num,i,j,a,b,c,d);break;case 0: case 15:break;void draw_one(int num,int i,int j,double a,double b,double c,double d)double x1,y1,x2,y2;double ox,oy;double dx,dy;dx=(X_MAX-X_MIN)/(N_X-1.0);dy=(Y_MAX-Y_MIN)/(N_Y-1.0);ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0);oy=Y_MIN+j*(Y_MAX-Y

23、_MIN)/(N_Y-1.0);switch(num) case 1 : case 14:x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx*(THRESHOLD-a)/(b-a);y2=oy;break;case 2: case 13:x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx*(THRESHOLD-d)/(c-d);y2=oy+dy;break;case 4: case 11:x1=ox+dx*(THRESHOLD-d)/(c-d);y1=oy+dy;x2=ox+dx;y2=oy+dy*(THRESHOLD-b

24、)/(c-b);break;case 7: case 8:x1=ox+dx*(THRESHOLD-a)/(b-a);y1=oy;x2=ox+dx;y2=oy+dy*(THRESHOLD-b)/(c-b);break;glBegin(GL_LINES);glVertex2d(x1,y1);glVertex2d(x2,y2);glEnd();void draw_adjacent(int num,int i,int j,double a,double b,double c ,double d)double x1,y1,x2,y2;double ox,oy;double dx,dy;dx=(X_MAX

25、-X_MIN)/(N_X-1.0);dy=(Y_MAX-Y_MIN)/(N_Y-1.0);ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0);oy=Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0);switch(num) case 3 : case 12:x1=ox+dx*(THRESHOLD-a)/(b-a);y1=oy;x2=ox+dx*(THRESHOLD-d)/(c-d);y2=oy+dy;break;case 6: case 9:x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx;y2=oy+dy*(THRESHOL

26、D-b)/(c-b);break;glBegin(GL_LINES);glVertex2d(x1,y1);glVertex2d(x2,y2);glEnd();void draw_opposite(int num,int i,int j,double a,double b,double c ,double d)double x1,y1,x2,y2,x3,y3,x4,y4;double ox,oy;double dx,dy;dx=(X_MAX-X_MIN)/(N_X-1.0);dy=(Y_MAX-Y_MIN)/(N_Y-1.0);ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0)

27、;oy=Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0);switch(num) case 5 :x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx*(THRESHOLD-a)/(b-a);y2=oy;x3=ox+dx*(THRESHOLD-d)/(c-d);y3=oy+dy;x4=ox+dx;y4=oy+dy*(THRESHOLD-b)/(c-b);break;case 10:x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx*(THRESHOLD-d)/(c-d);y2=oy+dy;x3=ox+dx*(T

28、HRESHOLD-d)/(c-d);y3=oy;x4=ox+dx;y4=oy+dy*(THRESHOLD-b)/(c-b);break;glBegin(GL_LINES);glVertex2d(x1,y1);glVertex2d(x2,y2);glVertex2d(x3,y3);glVertex2d(x4,y4);glEnd();void myReshape(int w, int h) glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w = h) gluOrtho2D(X_MIN, X_MAX

29、, Y_MIN * (GLfloat) h / (GLfloat) w, Y_MAX * (GLfloat) h / (GLfloat) w); else gluOrtho2D(X_MIN * (GLfloat) w / (GLfloat) h, X_MAX * (GLfloat) w / (GLfloat) h, Y_MIN, Y_MAX); glMatrixMode(GL_MODELVIEW);void main(int argc, char *argv) glutInit(&argc, argv);glutInitWindowSize(500, 500); glutCreateWindo

30、w(contour plot); glutReshapeFunc(myReshape); glutDisplayFunc(display); glClearColor(0.0, 0.0, 0.0, 1.0);glColor3f(1.0,1.0,1.0); glutMainLoop();A.5 生成正方形的程序/* This program illustrates the use of the GLUT library forinterfacing with a Window System */* The program opens a window, clears it to black,th

31、en draws a box at the location of the mouse each time theleft button is clicked. The right button exits the program.The program also reacts correctly when the window ismoved or resized by clearing the new window to black. */#include #include /* globals */GLsizei wh = 500, ww = 500; /* initial window

32、 size */GLfloat size = 3.0; /* half side length of square */void drawSquare(int x, int y) y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON);glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size);

33、glEnd(); glFlush();/* reshaping routine called whenever window is resizedor moved */void myReshape(GLsizei w, GLsizei h)/* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* ad

34、just viewport and clear */glViewport(0,0,w,h); glClear(GL_COLOR_BUFFER_BIT); glFlush();/* set global size for use by drawing routine */ww = w; wh = h; void myinit(void) glViewport(0,0,ww,wh);/* Pick 2D clipping window to match size of screen window. This choice avoids having to scale object coordina

35、teseach time window is resized. */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww, 0.0, (GLdouble) wh, -1.0, 1.0);/* set clear color to black and clear window */ glClearColor (0.0, 0.0, 0.0, 1.0);glClear(GL_COLOR_BUFFER_BIT);glFlush();void mouse(int btn,int state,int x,int

36、 y)if(btn=GLUT_RIGHT_BUTTON &state=GLUT_DOWN) exit(0)/* display callback required by GLUT*/viod display()int main(int argc,char* argv) glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutCreateWindow(square); myinit(); glutReshapeFunc (my Reshape); glutMouseFunc (mouse) glutMotio

37、nFunc (drawSqure); glutDisp layFunc (display); glutMainLoop();A.6 畫(huà)圖程序/* simple painting program with text, lines, triangles,rectangles,and points */#define NULL 0#define LINE 1#define RECTANGLE 2#define TRIANGLE 3#define POINTS 4#define TEXT 5#include #include void mouse(int, int, int, int);void ke

38、y(unsigned char, int, int);void display(void);void drawSquare(int, int);void myReshape(GLsizei, GLsizei);void myinit(void);void screen_box(int, int, int);void right_menu(int);void middle_menu(int);void color_menu(int);void pixel_menu(int);void fill_menu(int);int pick(int, int);/* globals */GLsizei w

39、h = 500, ww = 500; /* initial window size */GLfloat size = 3.0; /* half side length of square */int draw_mode = 0; /* drawing mode */int rx, ry; /* raster position */GLfloat r = 1.0, g = 1.0, b = 1.0; /* drawing color */int fill = 0; /* fill flag */void drawSquare(int x, int y) y=wh-y; glColor3ub( (

40、char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd();/* rehaping routine called whenever window is resizedor moved */void myReshape(GLsizei w, GLsizei h)/*

41、 adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */ glViewport(0,0,w,h); glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); display()

42、; glFlush();/* set global size for use by drawing routine */ ww = w; wh = h; void myinit(void)glViewport(0,0,ww,wh);/* Pick 2D clipping window to match size of X window. This choice avoids having to scale object coordinateseach time window is resized.*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);/* set clear color to black and clear window */ glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush();void mouse(int btn, int state, int x, int y) static int count; int where; static int xp2,yp2; if(btn=GLUT_LEFT_B

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論