OpenGL运用辅佐库创立规矩几许目标
生活随笔
收集整理的這篇文章主要介紹了
OpenGL运用辅佐库创立规矩几许目标
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
輔佐類分類: 1)窗口初始化函數(shù) 2)窗口處置和工作處置函數(shù) 3)定義場(chǎng)景制造循環(huán)函數(shù) 4)三圍物體制造函數(shù) 5)顏色索引表裝入函數(shù) 6)空閑工作處置函數(shù) 下面描寫(xiě)了一個(gè)程序,該程序盡可以包含輔佐庫(kù)的全部函數(shù)。該程序完結(jié)的功用是:定義鍵盤(pán)的功用,0~9及a分別標(biāo)明選擇11種底子 形體的一種進(jìn)行制造;定義鼠標(biāo)的功用為:左鍵按下標(biāo)明選擇實(shí)心閃現(xiàn)方式,右鍵按下標(biāo)明選擇網(wǎng)狀閃現(xiàn)方式;空閑工作的功用定義 為 http://www.fpnanchang.com/linked/20130422.do :重復(fù)的對(duì)形體進(jìn)行擴(kuò)展或減小。程序頂用到了列出的除顏色索引裝入函數(shù)以外的其他全部函數(shù)。 代碼如下: #include
#include //為了讓下面的頭文件編譯的時(shí)分經(jīng)過(guò)
#include
#include
#include
using namespace std;void MyInit(void);
void DrawScene(int,int);
void CALLBACK Display(void);
void CALLBACK MyReshape(GLsizei w, GLsizei h);
void CALLBACK SetSolid(AUX_EVENTREC *);
void CALLBACK SetWire(AUX_EVENTREC *);
void CALLBACK SetScale();
void CALLBACK Key_0(void);
void CALLBACK Key_1(void);
void CALLBACK Key_2(void);
void CALLBACK Key_3(void);
void CALLBACK Key_4(void);
void CALLBACK Key_5(void);
void CALLBACK Key_6(void);
void CALLBACK Key_7(void);
void CALLBACK Key_8(void);
void CALLBACK Key_9(void);
void CALLBACK Key_a(void);/*全局變量*/
GLint Width = 0; //窗口寬度
GLint Height = 0; //窗口高度
GLint ShapeType, Solid = 1; //形體類型及能否實(shí)心
GLfloat Scale = 1.0f; //形體縮放比例/*初始化函數(shù):定義質(zhì)料與光照*/
void MyInit(void)
{GLfloat diffuse[] = {0.3f, 0.6f, 0.9f, 1.0f};GLfloat specular[] = {0.8f, 0.8f, 0.8f, 1.0f};GLfloat position_one[] = {1.0f, 1.0f, 1.0f, 0.0f};GLfloat position_two[] = {-1.0f, 1.0f, 1.0f, 0.0f};//定義質(zhì)料glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0f);//定義linght0,linght1glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);glLightfv(GL_LIGHT0, GL_SPECULAR, specular);glLightfv(GL_LIGHT0, GL_POSITION, position_one);glLightfv(GL_LIGHT1, GL_POSITION, position_two);//運(yùn)用雙面光照glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);//激活光照與深度勘探glEnable(GL_LIGHT0);glEnable(GL_LIGHT1);glEnable(GL_LIGHTING);glEnable(GL_DEPTH_TEST);
}/*閃現(xiàn)相應(yīng)函數(shù):調(diào)用集結(jié)形體制造,程序會(huì)循環(huán)的調(diào)用該函數(shù)進(jìn)行制造,因此當(dāng)其他程序中修改了以下制造程序中的一些參數(shù)時(shí),無(wú)需調(diào)用該函數(shù),程序會(huì)自動(dòng)地用該函數(shù)從頭進(jìn)行制造。以下程序中先指定視口、根除緩存,經(jīng)過(guò)一系列坐標(biāo)轉(zhuǎn)換后,在制造集結(jié)形體,然后溝通前后緩存,將制造的內(nèi)容閃現(xiàn)到屏幕上。
*/void CALLBACK Display(void)
{glViewport(0, 9, Width, Height);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();glRotatef(30.0f, 1.0f, 0.0f, 0.0f);glRotatef(30.0f, 0.0f, 1.0f, 0.0f);glScalef(Scale, Scale, Scale);DrawScene(ShapeType,Solid);glFlush();glPopMatrix();auxSwapBuffers();
}/*當(dāng)窗口大小發(fā)生變化時(shí),相應(yīng)地調(diào)整坐標(biāo)系*/
void CALLBACK MyReshape(GLsizei w, GLsizei h)
{Width = w;Height = h;if(w <= h)glOrtho(-2.0f, 2.0f, -2.0f * (GLfloat)h / (GLfloat)w, 2.0f * (GLfloat)h / (GLfloat)w, -2.0f, 2.0f);elseglOrtho(-2.0f * (GLfloat)w / (GLfloat)h, 2.0f * (GLfloat)w / (GLfloat)h, -2.0f, 2.0f, -2.0f, 2.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();
}/*場(chǎng)景制造函數(shù)該函數(shù)制造輔佐庫(kù)中的全部底子幾何體。其間ShapeType參數(shù)抉擇制造何種形體,Solid參數(shù)抉擇能否制造實(shí)心體。這里調(diào)用了當(dāng)時(shí)輔佐庫(kù)中的供應(yīng)全部形體制造函數(shù)。細(xì)心的用戶或許會(huì)發(fā)現(xiàn),程序工作的效果中可以碰到了輔佐庫(kù)中的一個(gè)小錯(cuò)誤,即當(dāng)制造圓柱和圓錐時(shí),從實(shí)心到網(wǎng)狀或從網(wǎng)狀的圓柱或圓錐,同一幀畫(huà)面中也無(wú)法一同閃現(xiàn)實(shí)心和網(wǎng)狀的圓柱或圓錐。輔佐庫(kù)并不常用,因此并不必?fù)?dān)憂,不過(guò)仍是希望OpenGL的下一個(gè)版別中能改掉這個(gè)小錯(cuò)誤。
*/
void DrawScene(int ShapeType,int Solid)
{switch(ShapeType){case 0:if(Solid)auxSolidSphere(1.0);elseauxWireSphere(1.0);break;case 1:if(Solid)auxSolidCube(1.0);elseauxWireCube(1.0);break;case 2:if(Solid)auxSolidBox(1.5, 1.0, 1.0);elseauxWireBox(1.5, 1.0, 1.0);break;case 3:if(Solid)auxSolidTorus(1.0, 1.0);elseauxWireTorus(1.0, 1.0);break;case 4:if(Solid)auxSolidCylinder(1.0, 2.0);elseauxWireCylinder(1.0, 2.0);break;case 5:if(Solid)auxSolidIcosahedron(1.0);elseauxWireIcosahedron(1.0);break;case 6:if(Solid)auxSolidOctahedron(1.0);elseauxWireOctahedron(1.0);break;case 7:if(Solid)auxSolidTetrahedron(1.0);elseauxWireTetrahedron(1.0);break;case 8:if(Solid)auxSolidDodecahedron(1.0);elseauxWireDodecahedron(1.0);break;case 9:if(Solid)auxSolidCone(1.0, 1.0);elseauxWireCone(1.0, 1.0);break;case 10:if(Solid)auxSolidTeapot(1.0);elseauxWireTeapot(1.0);break;}
}/*鼠標(biāo)工作處置函數(shù)鼠標(biāo)時(shí)辰處置函數(shù)定義得非常簡(jiǎn)略,單機(jī)
*/
void CALLBACK SetSolid(AUX_EVENTREC *)
{Solid= 1;
}void CALLBACK SetWire(AUX_EVENTREC *)
{Solid = 0;
}/*空閑時(shí)辰處置函數(shù):空閑時(shí)辰處置函數(shù)的功用為:先對(duì)所制造的形體進(jìn)行減小,當(dāng)減小到一定程度(此處為0.1倍原大小)后,再對(duì)形體進(jìn)行擴(kuò)展,當(dāng)擴(kuò)展到一定程度(此處為原大小)后,又初步進(jìn)行減小,如此重復(fù)。程序中運(yùn)用了一個(gè)靜態(tài)變量來(lái)標(biāo)明擴(kuò)展減小的情況。
*/
void CALLBACK SetScale()
{static int ZoomIn = 0;if(ZoomIn http://www.dalian2001.com/linked/20130422.do
轉(zhuǎn)載于:https://www.cnblogs.com/fpqqchao/archive/2013/04/23/3036949.html
總結(jié)
以上是生活随笔為你收集整理的OpenGL运用辅佐库创立规矩几许目标的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 英国萨里大学金耀初教授:进化计算在人工智
- 下一篇: 【Java学习笔记之十五】Java中的s