Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROGRAM 6 Earth E. Angel.

Similar presentations


Presentation on theme: "PROGRAM 6 Earth E. Angel."— Presentation transcript:

1 PROGRAM 6 Earth E. Angel

2 程式敘述 利用OpengGL 中 quad strips 和 triangle fans畫出一具有經緯度的圓球體
在視窗上按下滑鼠左鍵,從視角觀看地球繞著圓心由上往下旋轉 按下滑鼠右鍵,地球繞著圓心水平方向順時鐘旋轉

3 void earth() { double x, y, z, thet, phi; int nlat, nlong; double c; x=y=0; z=1; glBegin(GL_TRIANGLE_FAN);//指定的座標用來畫出三角扇形串列,第一個座標點當作原點,而每個後續的點會與前面一個點及原點構成三角形 glVertex3d(x,y,z); c= /180.0; z=sin(c*80.0); for(thet=-180.0; thet<=180.0;thet+=20.0) x=sin(c*thet)*cos(c*80.0); y=cos(c*thet)*cos(c*80.0); } glEnd(); z=-1;

4 glBegin(GL_TRIANGLE_FAN);
glVertex3d(x,y,z); z=-sin(c*80.0); for(thet=-180.0; thet<=180.0;thet+=20.0) { x=sin(c*thet)*cos(c*80.0); y=cos(c*thet)*cos(c*80.0); } glEnd(); for(phi=-80.0; phi<=80.0; phi+=20.0) glBegin(GL_QUAD_STRIP);//指定的座標會建立四邊形串列 x=sin(c*thet)*cos(c*phi); y=cos(c*thet)*cos(c*phi); z=sin(c*phi); x=sin(c*thet)*cos(c*(phi+20.0)); y=cos(c*thet)*cos(c*(phi+20.0)); z=sin(c*(phi+20.0));

5 static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(theta[0], 1.0, 0.0, 0.0);//將現有的矩陣乘上一個旋轉矩陣,繞著向後面三參數所成的座標,和原點所形成的法向量,旋轉第一個參數角度 glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); earth(); glFlush(); glutSwapBuffers();//在雙重緩衝區模式下進行緩衝區的切換 }

6 void spinEarth() { theta[axis] += 2.0; if( theta[axis] > ) theta[axis] -= 360.0; glutPostRedisplay();//通知GLUT 更新現在視窗 } void mouse(int btn, int state, int x, int y) if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) theta[0]+=2; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) theta[1]+=2; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) theta[2]+=2; glutPostRedisplay();

7 void myReshape(int w, int h)
{ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (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); }

8 void main(int argc, char **argv)
{ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("sphere"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(NULL); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); glClearColor(1.0, 1.0, 1.0, 1.0); glColor3f(0.0, 0.0, 0.0); glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//此函數讓程式設計者可以變更畫出多邊形的方式,在此指出多邊形的前面與後面會因為模式變更而改變. GL_LINE會產生多邊形的輪廓 glutMainLoop(); }

9 執行結果


Download ppt "PROGRAM 6 Earth E. Angel."

Similar presentations


Ads by Google