資源描述:
《用OpenGL畫圓柱》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、用OpenGL畫一個(gè)圓柱一.環(huán)境設(shè)置新建Win32Application工程OpenGL在工程->設(shè)置目錄下設(shè)置OpenGL運(yùn)行環(huán)境在Link->對(duì)象/庫模塊下添加opengl32.libglu32.libglut32.lib到這里,OpenGL的環(huán)境配置完成。二.實(shí)驗(yàn)內(nèi)容新建一個(gè)Cylinder.c文件到OpenGL工程代碼:#include#include#include#includevoiddisplay(void){GLUquadr
2、icObj*cylinder_obj1;GLUquadricObj*cylinder_obj2;//GLUquadricObj*cylinder_obj3;//然后再初始化cylinder_obj1=gluNewQuadric();cylinder_obj2=gluNewQuadric();//cylinder_obj3=gluNewQuadric();glClearColor(0.0,0.0,0.0,0.0);//設(shè)置當(dāng)前清除顏色glClear(GL_COLOR_BUFFER_BIT);//GL_COLOR_BUFFE
3、R_BIT標(biāo)志用glClearColor設(shè)置的顏色清除顏色緩沖區(qū)glColor3f(0.1,1.0,0.1);//glColor{34}{bsifdubusui}{v}(....)//畫圓柱體glPushMatrix();glRotatef(60,1.0,0.0,0.0);//繞y軸旋轉(zhuǎn)的角度glRotatef(30,0.0,1.0,0.0);//繞z軸旋轉(zhuǎn)的角度glRotatef(0,0.0,0.0,1.0);//延x軸旋轉(zhuǎn)的角度gluCylinder(cylinder_obj1,2,2,4,100,50);gluDi
4、sk(cylinder_obj2,0,2,100,50);glPopMatrix();glFlush();}voidReshape(intw,inth){glViewport(0,0,(GLsizei)w,(GLsizei)h);//設(shè)置視口glMatrixMode(GL_PROJECTION);//指定當(dāng)前矩陣glLoadIdentity();//將當(dāng)前矩陣換成單位陣gluPerspective//設(shè)置透視圖(45.0,//透視角設(shè)置為45度,在Y方向上以角度為單位的視野(GLfloat)w/(GLfloat)h,//
5、窗口的寬與高比1.0,//視野透視深度:近點(diǎn)1.0f50.0//視野透視深度:始點(diǎn)0.1f遠(yuǎn)點(diǎn)1000.0f);glMatrixMode(GL_MODELVIEW);//指名當(dāng)前矩陣glLoadIdentity();glTranslatef(0.0,0.0,-25.0);//建立透視投影矩陣}intmain(intargc,char*argv[]){glutInit(&argc,argv);//glut初始化glutInitDisplayMode(GLUT_RGB
6、GLUT_SINGLE);//設(shè)置rgb顏色及單緩存模式
7、glutInitWindowPosition(100,100);//設(shè)置窗口初始位置glutInitWindowSize(400,400);//設(shè)置窗口大小glutCreateWindow("OpenGL圓柱體");//創(chuàng)建顯示窗口glutReshapeFunc(&Reshape);//設(shè)置旋轉(zhuǎn)回調(diào)函數(shù)glutDisplayFunc(&display);//設(shè)置顯示回調(diào)函數(shù)glutMainLoop();//進(jìn)入消息循環(huán)return0;}添加代碼后,出現(xiàn)這個(gè)錯(cuò)誤,說不能打開GL/glut庫,所以還要配置繼續(xù)配置OpenGL
8、環(huán)境需要將分別加入到不同的位置打開文件夾D:軟件包及安裝VC98IncludeGL把glut.h復(fù)制到這個(gè)文件夾中打開文件夾D:軟件包及安裝VC98Lib把glut.lib和glut32.lib都放到這個(gè)靜態(tài)函數(shù)庫所在的文件夾中打開文件夾C:WindowsSystem32將glut.dll和glut32.dll都放到這個(gè)文件夾內(nèi)運(yùn)行編譯后的結(jié)果: