版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
計(jì)算機(jī)圖形學(xué)實(shí)驗(yàn)內(nèi)容PAGEPAGE21計(jì)算機(jī)圖形學(xué)實(shí)驗(yàn)肖加清實(shí)驗(yàn)一圖形學(xué)實(shí)驗(yàn)基礎(chǔ)實(shí)驗(yàn)?zāi)康恼莆誚C++繪圖的一般步驟;掌握OpenGL軟件包的安裝方法;掌握OpenGL繪圖的一般步驟;掌握OpenGL的主要功能與基本語法。二、實(shí)驗(yàn)內(nèi)容1、VC++繪圖實(shí)驗(yàn)(1)實(shí)驗(yàn)內(nèi)容:以下是繪制金剛石圖案。已給出VC++參考程序,但里面有部分錯(cuò)誤,請改正,實(shí)現(xiàn)以下圖案。N=3//視圖類的一個(gè)成員函數(shù),這個(gè)成員函數(shù)可以放在OnDraw函數(shù)里調(diào)用。//在視圖類的頭文件(.h)里定義此函數(shù)voidDiamond();//在視圖類的實(shí)現(xiàn)文件(.cpp)里實(shí)現(xiàn)此函數(shù)voidCTestView::Diamond(){ CP2*P; intN; doubleR;R=300;N=10;P=newCP2[N]; CClientDCdc(this); CRectRect; GetClientRect(&Rect); doubletheta; theta=2*PI/N; for(inti=0;i<N;i++) { P[i].x=R*cos(i*theta); P[i].y=R*sin(i*theta); } for(i=0;i<=N-2;i++) { for(intj=i+1;j<=N-1;j++) {//其中ROUND函數(shù)需要自己實(shí)現(xiàn),實(shí)現(xiàn)四舍五入的功能。 dc.MoveTo(ROUND(P[i].x+Rect.right/2),ROUND(P[i].y+Rect.bottom/2)); dc.LineTo(ROUND(P[j].x+Rect.right/2),ROUND(P[j].y+Rect.bottom/2)); } } delete[]P; }2、OpenGL繪圖(1)以下是用OpenGL繪制茶壺的代碼,請?jiān)贠penGL環(huán)境下運(yùn)行,分析OpenGL程序的結(jié)構(gòu)#include<windows.h>#include<GL/gl.h>#include<GL/glu.h>#include<GL/glaux.h>//定義輸出窗口的大小#defineWINDOW_HEIGHT300#defineWINDOW_WIDTH500//用戶初始化函數(shù)voidmyninit(void);//窗口大小變化時(shí)的回調(diào)函數(shù)voidCALLBACKmyReshape(GLsizeiw,GLsizeih);//每幀OpenGL都會(huì)調(diào)用這個(gè)函數(shù),應(yīng)該把顯示代碼放在這個(gè)函數(shù)中voidCALLBACKdisplay(void);intwindow_width=WINDOW_WIDTH;intwindow_height=WINDOW_HEIGHT;//視點(diǎn)離物體的距離floatdistance=3.6f;//初始化,此時(shí)為空,可以在這里進(jìn)行初始化操作voidmyinit(void){}voidCALLBACKdisplay(void){ //設(shè)置清屏的顏色,并清屏和深度緩沖 glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //設(shè)置成模型矩陣模式 glMatrixMode(GL_MODELVIEW); //載入單位化矩陣 glLoadIdentity(); //坐標(biāo)中心向Z軸平移-distance,這樣使坐標(biāo)中心位于視點(diǎn)前方 glTranslatef(0.0,0.0,-distance); //在坐標(biāo)中心顯示一個(gè)茶壺 auxWireTeapot(1.0); //等待現(xiàn)有的OpenGL命令執(zhí)行完成 glFlush(); //交換前后緩沖區(qū) auxSwapBuffers();}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){ if(!h)return; //這定視區(qū) glViewport(0,0,w,h); //設(shè)定透視方式 glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0,1.0*(GLfloat)w/(GLfloat)h,1.0,30.0); window_width=w; window_height=h;}//移近移遠(yuǎn)的回調(diào)函數(shù)voidCALLBACKMoveNear(void){ distance-=0.3f;}voidCALLBACKMoveFar(void){ distance+=0.3f;}//主函數(shù)intmain(intargc,char**argv){ //初始化OpenGL的顯示方式 auxInitDisplayMode(AUX_DOUBLE|AUX_RGB|AUX_DEPTH16); //設(shè)定OpenGL窗口位置和大小 auxInitPosition(0,0,WINDOW_WIDTH,WINDOW_HEIGHT); //打開窗口 auxInitWindow("OpenGL的一個(gè)簡單的例子!"); //調(diào)用初始化函數(shù) myinit(); //設(shè)定窗口大小變化的回調(diào)函數(shù) auxReshapeFunc(myReshape); //設(shè)定移動(dòng)視點(diǎn)的回調(diào)函數(shù) auxKeyFunc('A',MoveNear);//按A鍵變大 auxKeyFunc('a',MoveFar);//按a鍵變小 //使display函數(shù)一直被調(diào)用 auxIdleFunc(display); //開始OpenGL的循環(huán) auxMainLoop(display); //結(jié)束程序 return(0);}其運(yùn)行結(jié)果如圖所示:(2)OPENGL繪制矩形的簡單例子參考程序:#include<gl/glut.h>voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f);//設(shè)置窗口背景顏色為白色 glMatrixMode(GL_PROJECTION);//設(shè)置投影參數(shù) gluOrtho2D(0.0,200.0,0.0,150.0);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT);//用當(dāng)前背景色填充窗口 glColor3f(1.0f,0.0f,0.0f);//設(shè)置當(dāng)前的繪圖顏色為紅色 glRectf(50.0f,100.0f,150.0f,50.0f);//繪制一個(gè)矩形glFlush();//處理所有的OpenGL程序}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//初始化窗口的顯示模式 glutInitWindowSize(400,300);//設(shè)置窗口的尺寸 glutInitWindowPosition(100,120);//設(shè)置窗口的位置 glutCreateWindow("矩形");//創(chuàng)建一個(gè)名為矩形的窗口 glutDisplayFunc(Display);//設(shè)置當(dāng)前窗口的顯示回調(diào)函數(shù) Initial();//完成窗口初始化 glutMainLoop();//啟動(dòng)主GLUT事件處理循環(huán) return0;}三、實(shí)驗(yàn)結(jié)果分析實(shí)驗(yàn)二直線生成算法與用戶接口與交互式技術(shù)一、實(shí)驗(yàn)?zāi)康?、掌握直線生成算法(1)DDA算法(2)Bresenham算法掌握交互式技術(shù)(1)鼠標(biāo)(2)鍵盤數(shù)字鍵、字母鍵、功能鍵和特殊鍵3、實(shí)現(xiàn)拾取操作二、實(shí)驗(yàn)內(nèi)容1、以下給出了DDA算法的C++代碼,請參考用VisualC++實(shí)現(xiàn)Bresenham算法。實(shí)現(xiàn)DDA畫線程序?qū)嶒?yàn)步驟:1.建立一個(gè)DDALine的工程文件;2.添加ddaline()成員函數(shù)方法:在工作區(qū)中選擇CLASSVIEW類窗口,右擊CDDAlineView類,選擇“addmemberfunction…”,定義如下的成員函數(shù):voidddaline(CDC*pDC,intx0,inty0,intx1,inty1,COLORREFcolor);3.編寫自定義的成員函數(shù)ddaline()程序voidCDDALineView::ddaline(CDC*pDC,intx0,inty0,intx1,inty1,COLORREFcolor){intlength,i;floatx,y,dx,dy;length=abs(x1-x0);if(abs(y1-y0)>length)length=abs(y1-y0);dx=(x1-x0)/length;dy=(y1-y0)/length;x=x0+0.5;y=y0+0.5;for(i=1;i<=length;i++){pDC->SetPixel((int)x,(int)y,color);x=x+dx;y=y+dy;}}4.編寫OnDraw()函數(shù)voidCDDALineView::OnDraw(CDC*pDC){CDDALineDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereddaline(pDC,100,100,400,100,RGB(255,0,0));ddaline(pDC,400,100,400,400,RGB(0,255,0));ddaline(pDC,400,400,100,400,RGB(0,0,255));ddaline(pDC,100,400,100,100,RGB(255,255,0));ddaline(pDC,100,100,400,400,RGB(255,0,255));ddaline(pDC,100,400,400,100,RGB(0,255,255));}}5.編譯、調(diào)試和運(yùn)行程序,查看程序結(jié)果。2、以下是利于鼠標(biāo)和字母鍵實(shí)現(xiàn)用戶接口與交互式技術(shù),請參照利用特殊鍵實(shí)現(xiàn),寫出其代碼。(1)OPENGL中利用鼠標(biāo)實(shí)現(xiàn)橡皮筋技術(shù)的例子#include<gl/glut.h>intiPointNum=0;//已確定點(diǎn)的數(shù)目intx1=0,x2=0,y1=0,y2=0;//確定的點(diǎn)坐標(biāo)intwinWidth=400,winHeight=300;//窗口的寬度和高度voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f);}voidChangeSize(intw,inth){ winWidth=w; winHeight=h; glViewport(0,0,w,h);//指定窗口顯示區(qū)域 glMatrixMode(GL_PROJECTION);//設(shè)置投影參數(shù) glLoadIdentity(); gluOrtho2D(0.0,winWidth,0.0,winHeight);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); if(iPointNum>=1) { glBegin(GL_LINES);//繪制直線段 glVertex2i(x1,y1); glVertex2i(x2,y2); glEnd(); } glutSwapBuffers();//交換緩沖區(qū)}voidMousePlot(GLintbutton,GLintaction,GLintxMouse,GLintyMouse){ if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN) { if(iPointNum==0||iPointNum==2){ iPointNum=1; x1=xMouse; y1=winHeight-yMouse; } else{ iPointNum=2; x2=xMouse; y2=winHeight-yMouse; glutPostRedisplay();//指定窗口重新繪制 } } if(button==GLUT_RIGHT_BUTTON&&action==GLUT_DOWN){ iPointNum=0; glutPostRedisplay(); }}voidPassiveMouseMove(GLintxMouse,GLintyMouse){ if(iPointNum==1) { x2=xMouse; y2=winHeight-yMouse; glutPostRedisplay(); } }intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//使用雙緩存及RGB模型 glutInitWindowSize(400,300); glutInitWindowPosition(100,100); glutCreateWindow("橡皮筋技術(shù)"); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize);//指定窗口在整形回調(diào)函數(shù) glutMouseFunc(MousePlot);//指定鼠標(biāo)響應(yīng)函數(shù) glutPassiveMotionFunc(PassiveMouseMove);//指定鼠標(biāo)移動(dòng)響應(yīng)函數(shù) Initial(); glutMainLoop(); return0;}(2)OPENGL中利用鍵盤實(shí)現(xiàn)橡皮筋技術(shù)的例子#include<gl/glut.h>intiPointNum=0;//已確定點(diǎn)的數(shù)目intx1=0,x2=0,y1=0,y2=0;//確定的點(diǎn)坐標(biāo)intwinWidth=400,winHeight=300;//窗口的寬度和高度voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f);}voidChangeSize(intw,inth){ winWidth=w; winHeight=h; glViewport(0,0,w,h);//指定窗口顯示區(qū)域 glMatrixMode(GL_PROJECTION);//設(shè)置投影參數(shù) glLoadIdentity(); gluOrtho2D(0.0,winWidth,0.0,winHeight);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); if(iPointNum>=1) { glBegin(GL_LINES);//繪制直線段 glVertex2i(x1,y1); glVertex2i(x2,y2); glEnd(); } glutSwapBuffers();//交換緩沖區(qū)}voidPassiveMouseMove(GLintxMouse,GLintyMouse){ if(iPointNum==1) { x2=xMouse; y2=winHeight-yMouse; glutPostRedisplay(); } }voidKey(unsignedcharkey,intx,inty){ switch(key){ case'p': if(iPointNum==0||iPointNum==2) { iPointNum=1; x1=x; y1=winHeight-y; } else { iPointNum=2; x2=x; y2=winHeight-y; glutPostRedisplay(); } break; default:break; } }intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//使用雙緩存及RGB模型 glutInitWindowSize(400,300); glutInitWindowPosition(100,100); glutCreateWindow("橡皮筋技術(shù)"); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize);//指定窗口在整形回調(diào)函數(shù) glutKeyboardFunc(Key);//指定鍵盤響應(yīng)函數(shù) glutPassiveMotionFunc(PassiveMouseMove);//指定鼠標(biāo)移動(dòng)響應(yīng)函數(shù) Initial(); glutMainLoop(); return0;}3、以下OPENGL實(shí)現(xiàn)拾取操作的例子,請掌握實(shí)現(xiàn)拾取操作的方法。#include<gl/glut.h>#include"stdio.h"constGLintpickSize=32;intwinWidth=400,winHeight=300;voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f);}voidDrawRect(GLenummode){ if(mode==GL_SELECT)glPushName(1);//壓入堆棧 glColor3f(1.0f,0.0f,0.0f); glRectf(60.0f,50.0f,150.0f,150.0f); if(mode==GL_SELECT)glPushName(2);//壓入堆棧glColor3f(0.0f,1.0f,0.0f);glRectf(230.0f,50.0f,330.0f,150.0f);if(mode==GL_SELECT)glPushName(3);//壓入堆棧glColor3f(0.0f,0.0f,1.0f);glRectf(140.0f,140.0f,240.0f,240.0f);}voidProcessPicks(GLintnPicks,GLuintpickBuffer[]){ GLinti; GLuintname,*ptr;printf("選中的數(shù)目為%d個(gè)\n",nPicks);ptr=pickBuffer;for(i=0;i<nPicks;i++){ name=*ptr;//選中圖元在堆棧中的位置 ptr+=3;//跳過名字和深度信息 ptr+=name-1;//根據(jù)位置信息獲得選中的圖元名字 if(*ptr==1)printf("你選擇了紅色圖元\n"); if(*ptr==2)printf("你選擇了綠色圖元\n"); if(*ptr==3)printf("你選擇了藍(lán)色圖元\n"); ptr++;} printf("\n\n");}voidChangeSize(intw,inth){winWidth=w;winHeight=h;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,winWidth,0.0,winHeight);}voidDisplay(void){glClear(GL_COLOR_BUFFER_BIT);DrawRect(GL_RENDER);glFlush();}voidMousePlot(GLintbutton,GLintaction,GLintxMouse,GLintyMouse){GLuintpickBuffer[pickSize];GLintnPicks,vp[4];if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN){ glSelectBuffer(pickSize,pickBuffer);//設(shè)置選擇緩沖區(qū)glRenderMode(GL_SELECT);//激活選擇模式glInitNames();//初始化名字堆棧glMatrixMode(GL_PROJECTION);glPushMatrix();glLoadIdentity();glGetIntegerv(GL_VIEWPORT,vp);//定義一個(gè)10×10的選擇區(qū)域gluPickMatrix(GLdouble(xMouse),GLdouble(vp[3]-yMouse),10.0,10.0,vp);gluOrtho2D(0.0,winWidth,0.0,winHeight);DrawRect(GL_SELECT);//恢復(fù)投影變換glMatrixMode(GL_PROJECTION);glPopMatrix();glFlush();//獲得選擇集并輸出nPicks=glRenderMode(GL_RENDER);ProcessPicks(nPicks,pickBuffer);glutPostRedisplay();}}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,100);glutCreateWindow("拾取操作");glutDisplayFunc(Display);glutReshapeFunc(ChangeSize);glutMouseFunc(MousePlot);Initial();glutMainLoop();return0;}三、實(shí)驗(yàn)結(jié)果分析實(shí)驗(yàn)三圓和橢圓及其它基本圖形的生成算法一、實(shí)驗(yàn)?zāi)康模?)掌握和利用VC++實(shí)現(xiàn)Bresenham算法;(2)掌握利用OPENGL繪制簡單多面體、二次及三次曲線的方法;(3)掌握利用OPENGL層次建模的方法。二、實(shí)驗(yàn)內(nèi)容(1)用VisualC++實(shí)現(xiàn)圓和橢圓的Bresenham算法;(2)利用OPENGL繪制簡單多面體、二次及三次曲線;(3)利用OPENGL層次建模1、VisualC++實(shí)現(xiàn)圓的Bresenham生成算法的參考程序如下,請?jiān)贛FC環(huán)境下實(shí)現(xiàn)程序?qū)崿F(xiàn)步驟:(1)建立MidPointCircle工程文件;(2)右擊CMidPointCircleView類,建立成員函數(shù)voidMidpointCircle(CDC*pDC,intx0,inty0,intr,COLORREFcolor)intCirPot(CDC*pDC,intx0,inty0,intx,inty,COLORREFcolor)(3)編寫成員函數(shù)代碼,程序如下:voidCMidPointCircleView::MidpointCircle(CDC*pDC,intx0,inty0,intr,COLORREFcolor){intx,y;floatd;x=0;y=r;d=1.25-r;CirPot(pDC,x0,y0,x,y,color);while(x<=y){if(d<0){d+=2*x+3;x++;}else{d+=2*(x-y)+5;x++;y--;}CirPot(pDC,x0,y0,x,y,color);}/*while*/}intCMidPointCircleView::CirPot(CDC*pDC,intx0,inty0,intx,inty,COLORREFcolor){pDC->SetPixel((x0+x),(y0+y),color);pDC->SetPixel((x0+y),(y0+x),color);pDC->SetPixel((x0+y),(y0-x),color);pDC->SetPixel((x0+x),(y0-y),color);pDC->SetPixel((x0-x),(y0-y),color);pDC->SetPixel((x0-y),(y0-x),color);pDC->SetPixel((x0-y),(y0+x),color);pDC->SetPixel((x0-x),(y0+y),color);return0;}(4)編寫OnDraw(CDC*pDC)函數(shù),程序如下:voidCMidPointCircleView::OnDraw(CDC*pDC){CMidPointCircleDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereMidpointCircle(pDC,100,100,10,RGB(255,0,0));MidpointCircle(pDC,500,300,60,RGB(255,255,0));}(6)編譯、運(yùn)行程序,查看結(jié)果。2、編寫畫橢圓法的掃描轉(zhuǎn)換程序,請?jiān)贛FC環(huán)境下實(shí)現(xiàn)程序?qū)崿F(xiàn)步驟:(1)建立MidPointEllise工程文件;(2)右擊CMidPointElliseView類,建立成員函數(shù)voidMidpointEllise(CDC*pDC,intx0,inty0,inta,intb,COLORREFcolor)(3)編寫成員函數(shù)代碼,程序如下:voidCMidPointEllipseView::MidpointEllise(CDC*pDC,intx0,inty0,inta,intb,COLORREFcolor){intx,y;floatd1,d2;x=0;y=b;d1=b*b+a*a*(-b+0.25);pDC->SetPixel(x+x0,y+y0,color);while(b*b*(x+1)<a*a*(y-0.5)){if(d1<0){d1+=b*b*(2*x+3);x++;}else{d1+=(b*b*(2*x+3)+a*a*(-2*y+2));x++;y--;}pDC->SetPixel(x0+x,y0+y,color);pDC->SetPixel(x0+x,y0-y,color);pDC->SetPixel(x0-x,y0+y,color);pDC->SetPixel(x0-x,y0-y,color);}//上半部分d2=(b*(x+0.5))*(b*(x+0.5))+(a*(y-1))*(a*(y-1))-(a*b)*(a*b);while(y>0){if(d2<0){d2+=b*b*(2*x+2)+a*a*(-2*y+3);x++;y--;}else{d2+=a*a*(-2*y+3);y--;}pDC->SetPixel(x0+x,y0+y,color);pDC->SetPixel(x0+x,y0-y,color);pDC->SetPixel(x0-x,y0+y,color);pDC->SetPixel(x0-x,y0-y,color);}//下半部分}(4)編寫OnDraw()函數(shù)voidCMidPointEllipseView::OnDraw(CDC*pDC){CMidPointEllipseDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereMidpointEllise(pDC,300,200,50,20,RGB(255,0,0));}編譯、運(yùn)行程序。3、用OPENGL繪制簡單多面體、二次及三次曲線的例子#include<gl/glut.h>staticGLsizeiiMode=1;staticGLfloatxRot=0.0f;//x方向旋轉(zhuǎn)參數(shù)staticGLfloatyRot=0.0f;//y方向旋轉(zhuǎn)參數(shù)GLUquadricObj*obj;//二次曲面對象voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); glColor3f(0.0f,0.0f,0.0f); obj=gluNewQuadric(); gluQuadricDrawStyle(obj,GLU_LINE);//以線框方式繪制二次曲面對象}voidChangeSize(intw,inth){glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1.5f,1.5f,-1.5f,1.5f);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(xRot,1.0f,0.0f,0.0f);//旋轉(zhuǎn)圖形 glRotatef(yRot,0.0f,1.0f,0.0f);//旋轉(zhuǎn)圖形 //指定需要繪制的圖元 switch(iMode) { case1: glutWireTetrahedron(); break; case2: glutSolidTetrahedron(); break; case3: glutWireOctahedron(); break; case4: glutSolidOctahedron(); break; case5: glutWireSphere(1.0f,15,15); break; case6: glutSolidSphere(1.0f,15,15);break; case7: glutWireTeapot(1.0f);break; case8: glutSolidTeapot(1.0f);break; case9: gluSphere(obj,1.0f,15,15); break; case10: gluCylinder(obj,1.0f,0.0f,1.0f,15,15); break; case11: gluPartialDisk(obj,0.3f,0.8f,15,15,30.0f,260.0f); break; default:break; } glFlush();}voidProcessMenu(intvalue){ iMode=value; glutPostRedisplay();}voidSpecialKeys(intkey,intx,inty){ if(key==GLUT_KEY_UP) xRot-=5.0f; if(key==GLUT_KEY_DOWN) xRot+=5.0f; if(key==GLUT_KEY_LEFT) yRot-=5.0f; if(key==GLUT_KEY_RIGHT) yRot+=5.0f; if(xRot>356.0f) xRot=0.0f; if(xRot<-1.0f) xRot=355.0f; if(yRot>356.0f) yRot=0.0f; if(yRot<-1.0f) yRot=355.0f; glutPostRedisplay();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("OpenGL模型繪制函數(shù)示例"); //創(chuàng)建菜單并定義菜單回調(diào)函數(shù) intnGlutPolyMenu=glutCreateMenu(ProcessMenu); glutAddMenuEntry("線框正四面體",1);//創(chuàng)建GLUT多面體繪制菜單 glutAddMenuEntry("實(shí)體正四面體",2); glutAddMenuEntry("線框正八面體",3); glutAddMenuEntry("實(shí)體正八面體",4); intnGlutCurveMenu=glutCreateMenu(ProcessMenu);//創(chuàng)建GLUT曲面繪制菜單 glutAddMenuEntry("線框球面",5); glutAddMenuEntry("實(shí)體球面",6); glutAddMenuEntry("線框茶壺",7); glutAddMenuEntry("實(shí)體茶壺",8); intnGluCurveMenu=glutCreateMenu(ProcessMenu);//創(chuàng)建GLU曲面繪制菜單 glutAddMenuEntry("線框球面",9); glutAddMenuEntry("線框圓錐面",10); glutAddMenuEntry("線框圓環(huán)面",11); intnMainMenu=glutCreateMenu(ProcessMenu);//創(chuàng)建主菜單 glutAddSubMenu("GLUT多面體",nGlutPolyMenu); glutAddSubMenu("GLUT曲面",nGlutCurveMenu); glutAddSubMenu("GLU曲面",nGluCurveMenu); glutAttachMenu(GLUT_RIGHT_BUTTON); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); Initial(); glutMainLoop(); return0;}4、用OPENGL實(shí)現(xiàn)繪制奧運(yùn)五環(huán)標(biāo)志#include<gl/glut.h>GLuintOlympicRings;voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); OlympicRings=glGenLists(1); glNewList(OlympicRings,GL_COMPILE); glColor3f(1.0,1.0,0.0); glTranslatef(-22.0,0.0,0.0); glutSolidTorus(0.5,20.0,15,50);//繪制黃色環(huán) glColor3f(0.0,1.0,0.0); glTranslatef(44.0,0.0,0.0); glutSolidTorus(0.5,20.0,15,50);//繪制綠色環(huán) glColor3f(0.0,0.0,0.0); glTranslatef(-22.0,30.0,0.0); glutSolidTorus(0.5,20.0,15,50);//繪制黑色環(huán) glColor3f(0.0,0.0,1.0); glTranslatef(-42.0,0.0,0.0); glutSolidTorus(0.5,20.0,15,50);//繪制藍(lán)色環(huán) glColor3f(1.0,0.0,0.0); glTranslatef(84.0,0.0,0.0); glutSolidTorus(0.5,20.0,15,50);//繪制紅色環(huán) glEndList();}voidChangeSize(intw,inth){ glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-70.0f,70.0f,-70.0f,70.0f);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glCallList(OlympicRings);//調(diào)用顯示列表 glFlush();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("OpenGL模型繪制函數(shù)示例"); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize); Initial(); glutMainLoop(); return0;}實(shí)驗(yàn)結(jié)果分析實(shí)驗(yàn)四裁剪算法一、實(shí)驗(yàn)?zāi)康模?)掌握Cohen-Sutherland線段裁剪算法,中點(diǎn)分割線段裁剪算法,Liang-Barsky裁剪算法。(2)掌握反走樣技術(shù)二、實(shí)驗(yàn)內(nèi)容1)實(shí)現(xiàn)Cohen-Sutherland線段裁剪算法;2)實(shí)現(xiàn)中點(diǎn)分割線段裁剪算法;3)實(shí)現(xiàn)Liang-Barsky裁剪算法;4)實(shí)現(xiàn)反走樣技術(shù)。1、Cohen-Sutherland編碼裁剪算法的程序設(shè)計(jì),請?jiān)贛FC環(huán)境下實(shí)現(xiàn),并理解代碼。實(shí)驗(yàn)環(huán)境:VisualC++步驟1:創(chuàng)建Code_Clip工程文件;步驟2:在主程序的程序頭部定義符號(hào)常量#defineLEFT1#defineRIGHT2#defineBOTTOM4#defineTOP8步驟3:定義成員變量和成員函數(shù)intWT;intWB;intWR;intWL;intC_S_Line(CDC*pDC,intx1,inty1,intx2,inty2);voidencode(intx,inty,int*code);步驟4:在構(gòu)造函數(shù)中為窗口邊界變量賦初值CCode_ClipView::CCode_ClipView(){//TODO:addconstructioncodehereWL=100;WR=400;WB=100;WT=300;}步驟5:編寫成員函數(shù)程序voidCCode_ClipView::encode(intx,inty,int*code){intc=0;if(x<WL)c=c|LEFT;elseif(x>WR)c=c|RIGHT;if(y<WB)c=c|BOTTOM;elseif(y>WT)c=c|TOP;*code=c;}intCCode_ClipView::C_S_Line(CDC*pDC,intx1,inty1,intx2,inty2){//CDC*pDC=GetDC();intcode1,code2,code,x,y;encode(x1,y1,&code1);//(x1,y1)處的編碼encode(x2,y2,&code2);//(x1,y1)處的編碼while(code1!=0||code2!=0)//當(dāng)code1不等于0或code2不等于0{if((code1&code2)!=0)return0;//當(dāng)code1與code2不等于0,在同側(cè);code=code1;if(code1==0)code=code2;if((LEFT&code)!=0)//求交點(diǎn){x=WL;y=y1+(y2-y1)*(WL-x1)/(x2-x1);}elseif((RIGHT&code)!=0){x=WR;y=y1+(y2-y1)*(WR-x1)/(x2-x1);}elseif((BOTTOM&code)!=0){y=WB;x=x1+(x2-x1)*(WB-y1)/(y2-y1);}elseif((TOP&code)!=0){y=WT;x=x1+(x2-x1)*(WT-y1)/(y2-y1);}if(code==code1){x1=x;y1=y;encode(x,y,&code1);}else{x2=x;y2=y;encode(x,y,&code2);}}//endwhile,表示code1,code2都為0,其中的線段為可視部分pDC->MoveTo(x1,y1);pDC->LineTo(x2,y2);}步驟6:編寫OnDraw()程序voidCCode_ClipView::OnDraw(CDC*pDC){CCode_ClipDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahere//定義三條直線的坐標(biāo)intx11,y11,x21,y21,x12,y12,x22,y22,x13,y13,x23,y23;x11=50;y11=150;x21=450;y21=250;x12=150;y12=150;x22=350;y22=240;x13=50;y13=400;x23=500;y23=350;//定義畫筆CPenPenRed(PS_SOLID,1,RGB(255,0,0));//定義紅色筆CPenPenBlue(PS_SOLID,1,RGB(0,0,255));//定義藍(lán)色筆//先畫出窗口,用藍(lán)色pDC->SelectObject(&PenBlue);pDC->Rectangle(WL,WB,WR,WT);//先畫出三條直線,用紅線pDC->SelectObject(&PenRed);pDC->MoveTo(x11,y11);pDC->LineTo(x21,y21);pDC->MoveTo(x12,y12);pDC->LineTo(x22,y22);pDC->MoveTo(x13,y13);pDC->LineTo(x23,y23);//用藍(lán)線,畫出裁剪三條線pDC->SelectObject(&PenBlue);C_S_Line(pDC,x11,y11,x21,y21);C_S_Line(pDC,x12,y12,x22,y22);C_S_Line(pDC,x13,y13,x23,y23);}步驟7:編譯、調(diào)試,查看運(yùn)行結(jié)果。2、多邊形裁剪實(shí)驗(yàn)環(huán)境:VisualC++和OpenGL/*代碼*/#include<windows.h>#include<GL/gl.h>#include<GL/glu.h>#include<GL/glaux.h>voidmyinit(void);voidCALLBACKmyReshape(GLsizeiw,GLsizeih);voidCALLBACKdisplay(void);voidCALLBACKdisplay(void){GLdoubleeqn[4]={1.0,0.0,0.0,0.0};glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,0.0,1.0);glPushMatrix();glTranslatef(0.0,0.0,-5.0);/*cliptheleftpartofwire_sphere:x<0*/glClipPlane(GL_CLIP_PLANE0,eqn);glEnable(GL_CLIP_PLANE0);glRotatef(-90.0,1.0,0.0,0.0);auxWireSphere(1.0);glPopMatrix();glFlush();}voidmyinit(void){glShadeModel(GL_FLAT);}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){ glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,20.0);glMatrixMode(GL_MODELVIEW);}voidmain(void){auxInitDisplayMode(AUX_SINGLE|AUX_RGB);auxInitPosition(0,0,500,500);auxInitWindow("裁剪例子");myinit();auxReshapeFunc(myReshape);auxMainLoop(display);}運(yùn)行結(jié)果:3、OPENGL實(shí)現(xiàn)直線段的反走樣#include<gl/glut.h>GLuintlineList;//指定顯示列表IDvoidInitial(){ glClearColor(1.0f,1.0f,1.0f,0.0f); glLineWidth(12.0f); glColor4f(0.0,0.6,1.0,1.0); lineList=glGenLists(1); glNewList(lineList,GL_COMPILE);//定義顯示列表 glBegin(GL_LINE_LOOP); glVertex2f(1.0f,1.0f); glVertex2f(4.0f,2.0f); glVertex2f(2.0f,5.0f); glEnd(); glEndList();}voidChangeSize(GLsizeiw,GLsizeih){ if(h==0) h=1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) gluOrtho2D(0.0,5.0,0.0,6.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D(0.0,5.0*(GLfloat)w/(GLfloat)h,0.0,6.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}voidDisplayt(void){ glClear(GL_COLOR_BUFFER_BIT); glCallList(lineList);glFlush();}voidDisplayw(void){ glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_LINE_SMOOTH);//使用反走樣 glEnable(GL_BLEND);//啟用混合函數(shù) glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);//指定混合函數(shù) glCallList(lineList);glFlush();}voidmain(void){ glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,300); glutCreateWindow("原始圖形"); glutDisplayFunc(Displayt);glutReshapeFunc(ChangeSize); Initial(); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(300,300); glutInitWindowSize(300,300); glutCreateWindow("反走樣圖形"); glutDisplayFunc(Displayw);glutReshapeFunc(ChangeSize); Initial(); glutMainLoop();}三、實(shí)驗(yàn)結(jié)果分析實(shí)驗(yàn)五二維變換一、實(shí)驗(yàn)?zāi)康模?)二維平移變換原理(2)二維縮放變換(3)二維對稱變換(4)二維旋轉(zhuǎn)變換(5)二維錯(cuò)切變換二、實(shí)驗(yàn)內(nèi)容1、實(shí)現(xiàn)一個(gè)三角形的基本幾何變換。 實(shí)驗(yàn)環(huán)境:OpenGL和VisualC++。/*代碼*/#include<windows.h>#include<GL/gl.h>#include<GL/glu.h>#include<GL/glaux.h>voidmyinit(void);voiddraw_triangle(void);voidCALLBACKdisplay(void);voidCALLBACKmyReshape(GLsizeiw,GLsizeih);voiddraw_triangle(void){glBegin(GL_LINE_LOOP);glVertex2f(0.0,25.0);glVertex2f(25.0,-25.0);glVertex2f(-25.0,-25.0);glEnd();}voidCALLBACKdisplay(void){glClearColor(0.0,0.0,0.0,1.0);glClear(GL_COLOR_BUFFER_BIT);/*drawanoriginaltriangle*/glLoadIdentity();glColor3f(1.0,1.0,1.0);/*white*/draw_triangle(); /*translatingatrianglealongX_axis*/ glLoadIdentity(); glTranslatef(-20.0,0.0,0.0);glColor3f(1.0,0.0,0.0);/*red*/draw_triangle(); /*scalingatrianglealongX_axisby1.5andalongY_axisby0.5*/glLoadIdentity();glScalef(1.5,0.5,1.0);glColor3f(0.0,1.0,0.0);/*green*/draw_triangle(); /*rotatingatriangleinacounterclockwisedirectionaboutZ_axis*/glLoadIdentity();glRotatef(90.0,0.0,0.0,1.0);glColor3f(0.0,0.0,1.0);/*blue*/draw_triangle(); /*scalingatrianglealongY_axisandreflectingitaboutY_axis*/glLoadIdentity();glScalef(1.0,-0.5,1.0);glColor3f(1.0,1.0,0.0);/*yellow*/draw_triangle();glFlush();}voidmyinit(void){ glShadeModel(GL_FLAT);}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-50.0,50.0,-50.0*(GLfloat)h/(GLfloat)w,50.0*(GLfloat)h/(GLfloat)w,-1.0,1.0);elseglOrtho(-50.0*(GLfloat)w/(GLfloat)h,50.0*(GLfloat)w/(GLfloat)h,-50.0,50.0,-1.0,1.0);glMatrixMode(GL_MODELVIEW);}voidmain(void){auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);auxInitPosition(0,0,500,500);auxInitWindow("三角形的基本幾何變換"); myinit();auxReshapeFunc(myReshape);auxMainLoop(display);}運(yùn)行結(jié)果:2、參考例子2 #include<gl/glut.h>voidinitial(void){ glClearColor(1.0,1.0,1.0,1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-10.0,10.0,-10.0,10.0);//指定二維裁剪窗口}voidtriangle(GLsizeimode){ if(mode==1) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//多邊形模式為線框 else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);//多邊形模式為填充多邊形 glBegin(GL_TRIANGLES); glVertex2f(0.0,5.0); glVertex2f(5.0,-5.0); glVertex2f(-5.0,-5.0); glEnd();}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glViewport(0,0,200,200);//指定從0,0開始長寬均為200的 triangle(1); glColor3f(0.0,0.0,1.0); glViewport(200,0,200,200); triangle(2); glFlush();}voidmain(void){ glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(100,100); glutInitWindowSize(400,200); glutCreateWindow("多視區(qū)"); initial(); glutDisplayFunc(Display); glutMainLoop();}三、實(shí)驗(yàn)結(jié)果分析實(shí)驗(yàn)六三維變換一、實(shí)驗(yàn)?zāi)康恼莆杖S平移變換,三維縮放變換,三維旋轉(zhuǎn)變換,三維平行投影。二、實(shí)驗(yàn)內(nèi)容1)利用三維變換原理實(shí)現(xiàn)一個(gè)三維長方體物體的平移,旋轉(zhuǎn)和縮放變換。2)利用三維透視原理實(shí)現(xiàn)平行投影和透視投影。1、繪制一個(gè)三維的正面透視立方體。采用OpenGL進(jìn)行編程。實(shí)驗(yàn)環(huán)境:OpenGL和VisualC++。#include<windows.h>#include<GL/gl.h>#include<GL/glu.h>#include<GL/glaux.h>voidmyinit(void);voidCALLBACKmyReshape(GLsizeiw,GLsizeih);voidCALLBACKdisplay(void);voidCALLBACKdisplay(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,1.0,1.0);glLoadIdentity();/*clearthematrix*/glTranslatef(0.0,0.0,-5.0);/*viewingtransformation*/glScalef(1.0,2.0,1.0);/*modelingtransformation*/auxWireCube(1.0);/*drawthecube*/glFlush();}voidmyinit(void){glShadeModel(GL_FLAT);}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){ glMatrixMode(GL_PROJECTION);/*prepareforandthen*/glLoadIdentity();/*definetheprojection*/glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);/*transformation*/glMatrixMode(GL_MODELVIEW);/*backtomodelviewmatrix*/glViewport(0,0,w,h);/*definetheviewport*/}voidmain(void){auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);auxInitPosition(0,0,500,500);auxInitWindow("三維的正面透視立方體");myinit();auxReshapeFunc(myReshape);auxMainLoop(display);}2、利用OPENGL建立分子模型#include<gl/glut.h>voidInitial(){ glEnable(GL_DEPTH_TEST);//啟用深度測試 glClearColor(1.0f,1.0f,1.0f,1.0f); //背景為白色}voidChangeSize(intw,inth){ if(h==0) h=1;glViewport(0,0,w,h);//設(shè)置視區(qū)尺寸 glMatrixMode(GL_PROJECTION);//指定當(dāng)前操作投影矩陣堆棧 glLoadIdentity();//重置投影矩陣 GLfloatfAspect; fAspect=(float)w/(float)h;//計(jì)算視區(qū)的寬高比 gluPerspective(45.0,fAspect,1.0,500.0);//指定透視投影的觀察空間 glMatrixMode(GL_MODELVIEW); glLoadIdentity();}voidDisplay(void){ staticflo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年奔馳車輛租賃市場拓展合同模板3篇
- 科研機(jī)構(gòu)疫情防控及應(yīng)急管理制度
- 2024年環(huán)保設(shè)備采購合同標(biāo)的與環(huán)保標(biāo)準(zhǔn)
- 2024年房屋交易代理及居間合同3篇
- 2024年第十一章貨品保險(xiǎn)合同范本
- 2024年度環(huán)保凈化設(shè)備監(jiān)造與空氣凈化技術(shù)支持合同范本3篇
- 介入室規(guī)章制度及流程
- 2024年汽車抵押貸款合同范本6篇
- 2024版建筑拆除工程安全與環(huán)保責(zé)任合同書2篇
- 2024年度學(xué)校洗衣房用燃料供應(yīng)合同3篇
- 一年級(jí)語文上冊百詞競賽(含答案)
- 國家開放大學(xué)《中文學(xué)科論文寫作》形考任務(wù)(1-4)試題及答案解析
- 魯迅《藥》課本劇劇本
- 集成電路板維修方法
- 小升初小學(xué)數(shù)學(xué)幾何圖形應(yīng)用題專題練習(xí)《巧算周長》
- 外科洗手法-課件
- 人員定位系統(tǒng)管理制度管理辦法規(guī)定匯編(修訂完稿)
- 衛(wèi)生部關(guān)于發(fā)布《綜合醫(yī)院組織編制原則試行草案》的通知((78)衛(wèi)醫(yī)字第1689號(hào))
- 斷路器試驗(yàn)精品課件
- TSG-R0005-2022《移動(dòng)式壓力容器安全技術(shù)監(jiān)察規(guī)程》(2022版)
- Q∕SY 03026-2019 石腦油-行業(yè)標(biāo)準(zhǔn)
評(píng)論
0/150
提交評(píng)論