辅佐类分类: 1)窗口初始化函数 2)窗口处置和工作处置函数 3)定义场景制造循环函数 4)三围物体制造函数 5)颜色索引表装入函数 6)空闲工作处置函数 下面描写了一个程序,该程序尽可以包含辅佐库的全部函数。该程序完结的功用是:定义键盘的功用,0~9及a分别标明选择11种底子 形体的一种进行制造;定义鼠标的功用为:左键按下标明选择实心闪现方式,右键按下标明选择网状闪现方式;空闲工作的功用定义 为 http://www.fpnanchang.com/linked/20130422.do :重复的对形体进行扩展或减小。程序顶用到了列出的除颜色索引装入函数以外的其他全部函数。 代码如下: #include
#include //为了让下面的头文件编译的时分经过
#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; //形体类型及能否实心
GLfloat Scale = 1.0f; //形体缩放比例
/*初始化函数:定义质料与光照*/
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};
//定义质料
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,linght1
glLightfv(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);
//运用双面光照
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
//激活光照与深度勘探
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}
/*
闪现相应函数:调用集结形体制造,程序会循环的调用该函数进行制造,
因此当其他程序中修改了以下制造程序中的一些参数时,无需调用该函数,
程序会自动地用该函数从头进行制造。
以下程序中先指定视口、根除缓存,经过一系列坐标转换后,在制造集结形体,
然后沟通前后缓存,将制造的内容闪现到屏幕上。
*/
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();
}
/*当窗口大小发生变化时,相应地调整坐标系*/
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);
else
glOrtho(-2.0f * (GLfloat)w / (GLfloat)h, 2.0f * (GLfloat)w / (GLfloat)h, -2.0f, 2.0f, -2.0f, 2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/*
场景制造函数
该函数制造辅佐库中的全部底子几何体。其间ShapeType参数抉择制造何种形体,
Solid参数抉择能否制造实心体。这里调用了当时辅佐库中的供应全部形体制造函数。细心的
用户或许会发现,程序工作的效果中可以碰到了辅佐库中的一个小错误,即当制造圆柱和圆锥时,
从实心到网状或从网状的圆柱或圆锥,同一帧画面中也无法一同闪现实心和网状的圆柱或圆锥。
辅佐库并不常用,因此并不必担忧,不过仍是希望OpenGL的下一个版别中能改掉这个小错误。
*/
void DrawScene(int ShapeType,int Solid)
{
switch(ShapeType)
{
case 0:
if(Solid)
auxSolidSphere(1.0);
else
auxWireSphere(1.0);
break;
case 1:
if(Solid)
auxSolidCube(1.0);
else
auxWireCube(1.0);
break;
case 2:
if(Solid)
auxSolidBox(1.5, 1.0, 1.0);
else
auxWireBox(1.5, 1.0, 1.0);
break;
case 3:
if(Solid)
auxSolidTorus(1.0, 1.0);
else
auxWireTorus(1.0, 1.0);
break;
case 4:
if(Solid)
auxSolidCylinder(1.0, 2.0);
else
auxWireCylinder(1.0, 2.0);
break;
case 5:
if(Solid)
auxSolidIcosahedron(1.0);
else
auxWireIcosahedron(1.0);
break;
case 6:
if(Solid)
auxSolidOctahedron(1.0);
else
auxWireOctahedron(1.0);
break;
case 7:
if(Solid)
auxSolidTetrahedron(1.0);
else
auxWireTetrahedron(1.0);
break;
case 8:
if(Solid)
auxSolidDodecahedron(1.0);
else
auxWireDodecahedron(1.0);
break;
case 9:
if(Solid)
auxSolidCone(1.0, 1.0);
else
auxWireCone(1.0, 1.0);
break;
case 10:
if(Solid)
auxSolidTeapot(1.0);
else
auxWireTeapot(1.0);
break;
}
}
/*
鼠标工作处置函数
鼠标时辰处置函数定义得非常简略,单机
*/
void CALLBACK SetSolid(AUX_EVENTREC *)
{
Solid= 1;
}
void CALLBACK SetWire(AUX_EVENTREC *)
{
Solid = 0;
}
/*
空闲时辰处置函数:
空闲时辰处置函数的功用为:先对所制造的形体进行减小,当减小到一定程度(此处为0.1倍原大小)后,
再对形体进行扩展,当扩展到一定程度(此处为原大小)后,又初步进行减小,如此重复。程序中运用了
一个静态变量来标明扩展减小的情况。
*/
void CALLBACK SetScale()
{
static int ZoomIn = 0;
if(ZoomIn http://www.dalian2001.com/linked/20130422.do