資源簡介
利用OPENGL開發的茶壺示例,包括燈光投影、馬賽克渲染、茶壺旋轉以及鍵盤、鼠標的響應,點擊鍵盤1/2/3或者鼠標的左、中、右均可以改變茶壺的旋轉方向。
程序含有源代碼和編譯好的可執行程序,且源代碼中關鍵代碼段均有詳細的中文注釋,方便大家掌握基本開發方法。

代碼片段和文件信息
#define?GLUT_DISABLE_ATEXIT_HACK
#include?
#include?
GLfloat?planes[]=?{-1.0?0.0?1.0?0.0};
GLfloat?planet[]=?{0.0?-1.0??0.0?1.0};
GLfloat?vertices[][3]?=?{{-1.0-1.0-1.0}{1.0-1.0-1.0}
{1.01.0-1.0}?{-1.01.0-1.0}?{-1.0-1.01.0}?
{1.0-1.01.0}?{1.01.01.0}?{-1.01.01.0}};
GLfloat?colors[][4]?=?{{0.00.00.00.5}{1.00.00.00.5}
{1.01.00.00.5}?{0.01.00.00.5}?{0.00.01.00.5}?
{1.00.01.00.5}?{1.01.01.00.5}?{0.01.01.00.5}};
typedef?struct?lightStruct?
{
GLfloat?position[4];
GLfloat?ambient[4];
GLfloat?diffuse[4];
GLfloat?specular[4];
}?lightStruct;//定義光源數據結構
lightStruct?light=
{
{10.01.00.01.0}?//燈光位置?
{0.00.00.01.0}?//環境光
{3.02.00.01.0}?//漫反射光
{0.00.00.01.0}?//鏡面光
};
void?setLight()
{
glLightfv(GL_LIGHT0?GL_POSITION?light.position);
glLightfv(GL_LIGHT0?GL_AMBIENT??light.ambient);
glLightfv(GL_LIGHT0?GL_DIFFUSE??light.diffuse);
glLightfv(GL_LIGHT0?GL_SPECULAR?light.specular);
????glEnable(GL_LIGHT0);
????glEnable(GL_LIGHTING);
????glEnable(GL_DEPTH_TEST);
}
void?polygon(int?a?int?b?int?c??int?d)
{
? glBegin(GL_POLYGON);
/* glColor4fv(colors[a]);*/
/* glTexCoord2f(0.00.0);?*/
glVertex3fv(vertices[a]);
/* glColor4fv(colors[b]);?*/
/* glTexCoord2f(0.01.0);?*/
glVertex3fv(vertices[b]);
/* glColor4fv(colors[c]);?*/
/* glTexCoord2f(1.01.0);?*/
glVertex3fv(vertices[c]);
/* glColor4fv(colors[d]);?*/
/* glTexCoord2f(1.00.0);?*/
glVertex3fv(vertices[d]);
glEnd();
}
void?colorcube(void)
{
/*?map?vertices?to?faces?*/
polygon(0321);
polygon(2376);
polygon(0473);
polygon(1265);
polygon(4567);
polygon(0154);
}
static?GLfloat?theta[]?=?{0.00.00.0};
static?GLint?axis?=?2;
void?display(void)
{
//清除顯存
????glClear(GL_COLOR_BUFFER_BIT?|?GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
setLight();
//以下代碼用來控制旋轉的方向
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);
?/*?colorcube();*/
//以下代碼用來控制茶壺大小
glutSolidTeapot(1.0);
//交換緩沖區
glutSwapBuffers();
}
void?spinCube()
{
//控制旋轉的角度和速度
theta[axis]?+=?0.1;
if(?theta[axis]?>?360.0?)?theta[axis]?-=?360.0;
/*?display();?*/
glutPostRedisplay();
}
//用來捕獲鼠標點擊的情況并給相應的角度控制變量賦值
void?mouse(int?btn?int?state?int?x?int?y)
{
if(btn==GLUT_LEFT_BUTTON?&&?state?==?GLUT_DOWN)?axis?=?0;
if(btn==GLUT_MIDDLE_BUTTON?&&?state?==?GLUT_DOWN)?axis?=?1;
if(btn==GLUT_RIGHT_BUTTON?&&?state?==?GLUT_DOWN)?axis?=?2;
}
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_MO
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2011-01-03?21:09??TeaPot\
?????目錄???????????0??2011-01-03?21:09??TeaPot\Debug\
?????文件??????192634??2011-01-03?21:09??TeaPot\Debug\TeaPot.exe
?????文件??????211456??2011-01-03?21:09??TeaPot\Debug\TeaPot.ilk
?????文件???????12891??2011-01-03?21:09??TeaPot\Debug\TeaPot.obj
?????文件??????294584??2011-01-03?21:09??TeaPot\Debug\TeaPot.pch
?????文件??????361472??2011-01-03?21:09??TeaPot\Debug\TeaPot.pdb
?????文件???????33792??2011-01-03?21:09??TeaPot\Debug\vc60.idb
?????文件???????45056??2011-01-03?21:09??TeaPot\Debug\vc60.pdb
?????文件????????5829??2011-01-02?21:13??TeaPot\TeaPot.c
?????文件????????3399??2011-01-03?21:09??TeaPot\TeaPot.dsp
?????文件?????????520??2011-01-03?21:09??TeaPot\TeaPot.dsw
?????文件???????33792??2011-01-03?21:09??TeaPot\TeaPot.ncb
?????文件???????48640??2011-01-03?21:09??TeaPot\TeaPot.opt
?????文件????????2107??2011-01-03?21:09??TeaPot\TeaPot.plg
- 上一篇:基于uml的公交查詢用例圖
- 下一篇:平面坐標轉換-四參數仿射變換源碼
評論
共有 條評論