Opengl實(shí)驗(yàn)四資料_第1頁
Opengl實(shí)驗(yàn)四資料_第2頁
Opengl實(shí)驗(yàn)四資料_第3頁
Opengl實(shí)驗(yàn)四資料_第4頁
Opengl實(shí)驗(yàn)四資料_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、計(jì)算機(jī)學(xué)院 12計(jì)算機(jī)科學(xué)與技術(shù)專業(yè)3班學(xué)號3112005883姓名:曲紹霖 教師評定:實(shí)驗(yàn)四模型的建立以及光照、紋理的交互和運(yùn)動(dòng)一、實(shí)驗(yàn)?zāi)康恼莆誒penGL的使用方法二、實(shí)驗(yàn)要求在Windows平臺上用VC+結(jié)合GLUT做實(shí)驗(yàn),要求掌握結(jié)合VC+和OpenGL的紋理模型的理解及實(shí)現(xiàn),實(shí)驗(yàn)完成后要求根據(jù)自己的成果撰寫一 份實(shí)驗(yàn)報(bào)告。三、實(shí)驗(yàn)環(huán)境操作系統(tǒng):Windows xp開發(fā)環(huán)境:VC+6.0以及GLUT圖形交互設(shè)備:鼠標(biāo)和鍵盤四、實(shí)驗(yàn)內(nèi)容運(yùn)用所以學(xué)過的知識做一個(gè)ope ngl小程序。實(shí)現(xiàn)代碼:#include#include #include#include #include #inclu

2、de/全局變量#define BITMAP_ID 0x4D42#define pi 3.1415926/模式選擇開關(guān)int iModel=1;int iTest=1;/圖形旋轉(zhuǎn)與開關(guān)unsigned int ispin=1;unsigned int irots=1;/翡翠和紅寶石的材質(zhì)數(shù)據(jù)GLfloat no_mat=0.0,0.0,0.0,1.0;GLfloat mat_ambient=0.0215,0.1745,0.0215,0.55;GLfloat mat_ambient_color=0.1745,0.01175,0.01175,0.55;GLfloat mat_diffuse=0.07

3、568,0.61424,0.07568,0.55;GLfloat mat_diffuse_color=0.61424,0.04136,0.04136,0.55;GLfloat mat_specular=0.633,0.727811,0.633,0.55;GLfloat mat_specular_color=0.727811,0.626959,0.626959,0.55;GLfloat no_shininess=0.0;GLfloat low_shininess=76.8;GLfloat high_shininess=76.8;GLfloat mat_emission=0.3,0.2,0.2,0

4、.0;/ 所在光源位置數(shù)據(jù)GLfloat position=0.0,0.0,1.5,1.0;GLfloat anglex=0,angley=0; / 設(shè)置 X,Y 旋轉(zhuǎn)的角度 /紋理數(shù)據(jù)結(jié)構(gòu) typedef structint width;int height;unsigned int texID;unsigned char *data;texture;texture *groundTex;/ 草坪紋理指針texture *wallTex; / 柵欄紋理指針/調(diào)入位圖作為紋理數(shù)據(jù)unsigned char *LoadBmpFile(char *filename,BITMAPINFOHEADER

5、 *bmpInfoHeader) FILE *file;BITMAPFILEHEADER bmpFileHeader;unsigned char *image;unsigned intimageIdx = 0;unsigned chartempRGB;file = fopen(filename, rb);if (file = NULL)return NULL;/ 讀取 BMP 頭文件fread(&bmpFileHeader, sizeof(BITMAPFILEHEADER), 1, file);if ( bmpFileHeader.bfType!= BITMAP_ID)fclose(file)

6、;return NULL;fread(bmpInfoHeader, sizeof(BITMAPINFOHEADER), 1, file);fseek(file, bmpFileHeader.bfOffBits, SEEK_SET);/ 分配內(nèi)存給位圖數(shù)據(jù)image = (unsigned char*)malloc(bmpInfoHeader-biSizeImage);if (!image) free(image); fclose(file); return NULL;/ 讀取位圖數(shù)據(jù)fread(image, 1, bmpInfoHeader-biSizeImage, file);if (ima

7、ge = NULL) fclose(file); return NULL;/ 反轉(zhuǎn) R 和 B 值已得到 RGB ,因?yàn)槲粓D顏色格式是 BGRfor (imageIdx = 0; imageIdx biSizeImage; imageIdx+=3) tempRGB = imageimageIdx; imageimageIdx = imageimageIdx + 2; imageimageIdx + 2 = tempRGB;fclose(file);return image;/調(diào)入紋理文件 texture *LoadTexFile(char *filename)BITMAPINFOHEADER

8、texInfo;texture *thisTexture;thisTexture = (texture*)malloc(sizeof(texture);if (thisTexture = NULL)return NULL;/ 調(diào)入紋理數(shù)據(jù)并檢查有效性thisTexture-data = LoadBmpFile(filename, &texInfo);if (thisTexture-data = NULL)free(thisTexture);return NULL;/ 設(shè)置紋理的寬和高thisTexture-width = texInfo.biWidth; thisTexture-height

9、= texInfo.biHeight;/ 生成紋理對象名glGenTextures(1, &thisTexture-texID);return thisTexture;/初始化所有紋理數(shù)據(jù)和屬性bool LoadAllTextures()/ 調(diào)入草坪紋理數(shù)據(jù)groundTex = LoadTexFile(ground.bmp);if (groundTex = NULL)return false;/調(diào)入柵欄紋理數(shù)據(jù)wallTex = LoadTexFile(wall.bmp);if (wallTex = NULL)return false;/設(shè)置草坪紋理控制 glBindTexture(GL_T

10、EXTURE_2D, groundTex-texID);/使用雙線性紋理濾波 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);/設(shè)置重復(fù)模式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

11、;/定義草坪紋理gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, groundTex-width, groundTex-height, GL_RGB, GL_UNSIGNED_BYTE, groundTex-data);/設(shè)置柵欄紋理控制 glBindTexture(GL_TEXTURE_2D, wallTex-texID);/使用雙線性紋理濾波 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_M

12、IN_FILTER, GL_LINEAR);/設(shè)置重復(fù)模式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);/定義柵欄紋理gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, wallTex-width, wallTex-height, GL_RGB, GL_UNSIGNED_BYTE, wallTex-data);return true;/初始化光照void LoadA

13、llLights()/ 定義光源,它是一種白色的光源GLfloat ambient=0.0,0.0,0.0,1.0;GLfloat diffuse=1.0,1.0,1.0,1.0;GLfloat specular=1.0,1.0,1.0,1.0;GLfloat lmodel_ambient=0.5,0.5,0.5,1.0;GLfloat local_view=0.0;glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);/ 過很多次反射后最終遺留在環(huán)境中的光線強(qiáng)度 (顏色) glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);/ 漫反射后最

14、終遺留在環(huán)境中的光線強(qiáng)度(顏色) glLightfv(GL_LIGHT0,GL_SPECULAR, specular);/ 鏡面反射后最終遺留在環(huán)境中的光線強(qiáng)度(顏色)glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient); glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER,local_view);glEnable(GL_LIGHTING);/ 在后面的渲染中使用光照 glEnable(GL_LIGHT0);/ 使用第 0 號光照/ 初始化 OpenGL 繪制環(huán)境void init(void)glClear

15、Color(1.0,1.0,1.0,0.0);/ 背景顏色為白色glEnable(GL_DEPTH_TEST);/ 這句是啟用深度測試,這樣,在后面的物體會(huì)被擋著 glShadeModel(GL_SMOOTH);void myDisplay(void)/禁用光照和紋理glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); / 清除屏幕和深度緩存 glLoadIdentity();/ 重置當(dāng)前的模型觀察矩陣if(iTest=1)gluLookAt(2

16、.0,2.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);/ 設(shè)置圖元描述的視角/設(shè)置正方體旋轉(zhuǎn)并繪制glRotatef(angley, 0.0f, 1.0f, 0.0f);glRotatef(anglex, 1.0f, 0.0f, 0.0f);glBegin(GL_QUADS);glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f, 1.0f,-1.0f);/ 顏色改為綠色/ 四邊形的右上頂點(diǎn) (頂面 )glColor3f(1.0f,0.0f,0.0f);/顏色改為紅色glVertex3f(-1.0f, 1.0f,-1.0f); / 四邊形的左

17、上頂點(diǎn) (頂面 )glColor3f(0.0f,0.0f,1.0f);/ 顏色改成藍(lán)色glVertex3f(-1.0f, 1.0f, 1.0f);/ 四邊形的左下頂點(diǎn) (頂面 )glColor3f(1.0f,0.5f,0.0f);/ 顏色改成橙色glVertex3f( 1.0f, 1.0f, 1.0f);/ 四邊形的右下頂點(diǎn) (頂面 )glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, 1.0f);/ 顏色改為綠色/ 四邊形的右上頂點(diǎn) (底面 )glColor3f(1.0f,0.0f,0.0f);glVertex3f(-1.0f,-1.0f,

18、1.0f);/顏色改為紅色/ 四邊形的左上頂點(diǎn) (底面 )glColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);/ 顏色改成藍(lán)色/ 四邊形的左下頂點(diǎn) (底面 )glColor3f(1.0f,0.5f,0.0f);glVertex3f( 1.0f,-1.0f,-1.0f);/ 顏色改成橙色/ 四邊形的右下頂點(diǎn) (底面 )glColor3f(1.0f,0.5f,0.0f);/ 顏色改成橙色glVertex3f( 1.0f, 1.0f, 1.0f);/ 四邊形的右上頂點(diǎn) (前面 )glColor3f(0.0f,0.0f,1.0f); glVe

19、rtex3f(-1.0f, 1.0f, 1.0f);/ 顏色改成藍(lán)色/ 四邊形的左上頂點(diǎn) (前面 )glColor3f(1.0f,0.0f,0.0f);glVertex3f(-1.0f,-1.0f, 1.0f);/顏色改為紅色/ 四邊形的左下頂點(diǎn) (前面 )glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, 1.0f);/ 顏色改為綠色/ 四邊形的右下頂點(diǎn) (前面 )glColor3f(1.0f,0.5f,0.0f);/ 顏色改成橙色glVertex3f( 1.0f,-1.0f,-1.0f);/ 四邊形的右上頂點(diǎn) (后面 )/ 顏色改成藍(lán)色gl

20、Color3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);/ 四邊形的左上頂點(diǎn) (后面 )glColor3f(1.0f,0.0f,0.0f);glVertex3f(-1.0f, 1.0f,-1.0f);/顏色改為紅色/ 四邊形的左下頂點(diǎn) (后面 )glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f, 1.0f,-1.0f);glColor3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);glColor3f(1.0f,0.0f,0.0f);glVertex3f(

21、-1.0f, 1.0f,-1.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);glColor3f(1.0f,0.0f,0.0f);glVertex3f(-1.0f,-1.0f, 1.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f( 1.0f, 1.0f,-1.0f);/ 顏色改為綠色/ 四邊形的右下頂點(diǎn) (后面 )/ 顏色改成藍(lán)色/ 四邊形的右上頂點(diǎn) (左面 )/顏色改為紅色/ 四邊形的左上頂點(diǎn) (左面 )/ 顏色改成藍(lán)色/ 四邊形的左下頂點(diǎn) (左面 )/顏色改為紅色/ 四邊形的右下頂點(diǎn)

22、(左面 )/ 顏色改為綠色/ 四邊形的右上頂點(diǎn) (右面 )/ 顏色改成橙色/ 四邊形的左上頂點(diǎn) (右面 )/ 顏色改為綠色/ 四邊形的左下頂點(diǎn) (右面 )/ 顏色改成橙色/ 四邊形的右下頂點(diǎn) (右面 )glColor3f(1.0f,0.5f,0.0f);glVertex3f( 1.0f, 1.0f, 1.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f( 1.0f,-1.0f, 1.0f);glColor3f(1.0f,0.5f,0.0f);glVertex3f( 1.0f,-1.0f,-1.0f);glEnd();/ 結(jié)束繪制if(iTest=2)LoadAl

23、lLights();/ 載入光照glPushMatrix();gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);/設(shè)置可動(dòng)光源位置glPushMatrix();glRotated(anglex,1.0f,0.0f,0.0f);glRotated(angley,0.0f,1.0f,0.0f);glLightfv(GL_LIGHT0,GL_POSITION,position);glTranslated(0.0,0.0,1.5);glDisable(GL_LIGHTING);/繪制一個(gè)紅色球體表示光源位置glColor3f(1.0,0.3,0.3);glu

24、tWireSphere(0.07,8,8);glEnable(GL_LIGHTING);glPopMatrix();if(iModel=1)/材質(zhì)一,翡翠,球體glPushMatrix();glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient); glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,low_shininess); glMaterialf

25、v(GL_FRONT,GL_EMISSION,no_mat); glutSolidSphere(1.0,64,64);glPopMatrix();if(iModel=2)/材質(zhì)二,紅寶石,球體 glPushMatrix(); glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient_color); glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse_color); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular_color); glMaterialfv(GL_FRONT,GL_S

26、HININESS,high_shininess); glMaterialfv(GL_FRONT,GL_EMISSION,no_mat); glutSolidSphere(1.0,64,64);glPopMatrix();glPopMatrix();if(iTest=3)glEnable(GL_TEXTURE_2D);/ 啟動(dòng)二維紋理LoadAllTextures();/ 調(diào)入紋理if(iModel=1)/設(shè)置正方體旋轉(zhuǎn)并繪制 gluLookAt(5.0,9.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0);glColor3f(1.0f, 1.0f, 1.0f);/ 對顏色清除,否

27、則圖元描述的顏色會(huì)和紋理沖突glRotatef(angley, 0.0f, 1.0f, 0.0f);/ 設(shè)置花園旋轉(zhuǎn)軸為 y/ 使用“草坪”紋理繪制土地 glBindTexture(GL_TEXTURE_2D,groundTex-texID);/ 設(shè)置當(dāng)前紋理為地面 glBegin(GL_QUADS);/繪制地面glTexCoord2f(5.0f, 5.0f);glVertex3f(6.0f, -3.0f, 6.0f);glTexCoord2f(5.0f, 0.0f);glVertex3f(6.0f, -3.0f, -6.0f);glTexCoord2f(0.0f, 0.0f);glVerte

28、x3f(-6.0f, -3.0f, -6.0f);glTexCoord2f(0.0f, 5.0f);glVertex3f(-6.0f, -3.0f, 6.0f);glEnd();/ 地面繪制結(jié)束/ 使用“柵欄”紋理繪制柵欄 glBindTexture(GL_TEXTURE_2D,wallTex-texID);/ 設(shè)置當(dāng)前紋理為柵欄glBegin(GL_QUADS);/繪制正前方柵欄glNormal3f(0.0f, 0.0f, 1.0f);glTexCoord2f(0.0f, 0.0f); glVertex3f(-6.0f,-3.0f,6.0f);glTexCoord2f(0.0f, 1.0f)

29、; glVertex3f(-6.0f, -1.5f,6.0f);glTexCoord2f(5.0f, 1.0f); glVertex3f(6.0f, -1.5f,6.0f);glTexCoord2f(5.0f, 0.0f); glVertex3f(6.0f,-3.0f,6.0f);/繪制右邊柵欄glNormal3f(1.0f, 0.0f, 0.0f);glTexCoord2f(0.0f, 0.0f); glVertex3f(6.0f,-3.0f,-6.0f);glTexCoord2f(0.0f, 1.0f); glVertex3f(6.0f, -1.5f,-6.0f);glTexCoord2f

30、(5.0f, 1.0f); glVertex3f(6.0f, -1.5f,6.0f);glTexCoord2f(5.0f, 0.0f); glVertex3f(6.0f,-3.0f,6.0f);/繪制左邊柵欄 glNormal3f(-1.0f, 0.0f, 0.0f);glTexCoord2f(0.0f, 0.0f); glVertex3f(-6.0f,-3.0f,-6.0f);glTexCoord2f(0.0f, 1.0f); glVertex3f(-6.0f,-1.5f,-6.0f);glTexCoord2f(5.0f, 1.0f); glVertex3f(-6.0f,-1.5f,6.0f

31、);glTexCoord2f(5.0f, 0.0f); glVertex3f(-6.0f,-3.0f,6.0f);/繪制后邊柵欄glNormal3f(0.0f, 0.0f, -1.0f);glTexCoord2f(0.0f, 0.0f); glVertex3f(-6.0f,-3.0f,-6.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-6.0f,-1.5f,-6.0f); glTexCoord2f(5.0f, 1.0f); glVertex3f(6.0f,-1.5f,-6.0f); glTexCoord2f(5.0f, 0.0f); glVertex3f

32、(6.0f,-3.0f,-6.0f); glEnd();/ 四面柵欄繪制結(jié)束glFlush();/ 強(qiáng)制刷新glutSwapBuffers(); / 雙緩沖/設(shè)窗口的坐標(biāo)void reshape(int w,int h) glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity();gluPerspective(40.0,(GLfloat)w/(GLfloat)h,1.0,100.0); glMatrixMode(GL_MODELVIEW);glLoadIdentity();/鍵盤控制v

33、oid SpecialKeys(int key,int x,int y) if(iTest=2&key=GLUT_KEY_F1) irots=!irots;anglex=0,angley=0;ispin=1; if(iTest=1|iTest=2|iTest=3) if(irots=0)if(iTest!=3) / 紋理模式下不支持 x 旋轉(zhuǎn)if(key = GLUT_KEY_UP) anglex -= 5.0f;if(key = GLUT_KEY_DOWN) anglex += 5.0f;if(key = GLUT_KEY_LEFT)angley -= 5.0f;if(key = GLUT_

34、KEY_RIGHT) angley += 5.0f; if(anglex 356.0f)anglex = 0.0f;if(anglex 356.0f) angley = 0.0f; if(angley =360)angley-=360;anglex+=10;if(anglex=360)anglex-=360;if(iTest=1) if(ispin=1)angley+=10;if(angley=360)angley-=360;elseanglex+=10;if(anglex=360)anglex-=360;if(iTest=3)if(ispin=1)angley+=10;if(angley=3

35、60)angley-=360;elseangley-=10;if(angley=-360)angley+=360;printf(%f,%f,irots=%d,ispin=%un,anglex,angley,irots,ispin);glutTimerFunc(100,TimerFunction,1);/菜單選項(xiàng)設(shè)置void nMenu(int value)switch(value)/1、2 為圖元模式case 1:iTest=1;iModel=1;irots=1;anglex=0,angley=0;ispin=1;break;case 2:iTest=1;iModel=1;irots=0;anglex=0,angley=0;ispin=1;break;/3、4 為光照模式case 3: iTest=2;iModel=1;irots=1;anglex=0;angley=0;break;case 4: iTest=2;iModel=2;irots=1;anglex=0;angley=0;break;/5、6 為紋理模式case 5:iTest=3;iModel=1;irots=1;anglex=0,angley=0;ispin=1;break;case 6:iTest=3;iModel=1;irots=0;anglex=0,angley=0;ispin=1;break

溫馨提示

  • 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)僅提供信息存儲空間,僅對用戶上傳內(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論