辅助类分类:
1)窗口初始化函数
2)窗口处理和事件处理函数
3)定义场景绘制循环函数
4)三围物体绘制函数
5)颜色索引表装入函数
6)空闲事件处理函数
下面设计了一个程序,该程序尽可能涵盖辅助库的所有函数。该程序实现的功能是:定义键盘的功能,0~9及a分别表示选择11种基本
形体的一种进行绘制;定义鼠标的功能为:左键按下表示选择实心显示模式,右键按下表示选择网状显示模式;空闲事件的功能定义
为:反复的对形体进行放大或缩小。程序中用到了列出的除颜色索引装入函数以外的其他所有函数。
代码如下:
#include<iostream>
#include<Windows.h> //为了让下面的头文件编译的时候通过
#include<gl\GLU.h>
#include<gl\GL.h>
#include<gl\GLAUX.H>
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glu32.lib")
#pragma comment (lib, "glaux.lib")
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(-5.0f, 5.0f, -5.0f * (GLfloat)h / (GLfloat)w, 5.0f * (GLfloat)h / (GLfloat)w, -5.0f, 5.0f);
}
else
{
glOrtho(-5.0f * (GLfloat)w / (GLfloat)h, 5.0f * (GLfloat)w / (GLfloat)h, -5.0f, 5.0f, -5.0f, 5.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 && Scale < 1.0f)
{
Scale = Scale * 1.1f;
if(Scale >= 1.0f)
{
ZoomIn = 0;
}
}
else
{
Scale = Scale / 1.1f;
if(Scale <= 0.1f)
{
ZoomIn = 1;
}
}
Display();
}
/*
键盘事件处理函数
每一个案件选择一种形体类型,由于程序中定义了控制场景绘制的主循环函数,因此在选择一种形体
类型之后,无需调用场景绘制函数,程序认可根据选择的形体进行绘制。键盘事件处理函数也必须是
回调函数,参数为void
*/
void CALLBACK Key_0(void)
{
ShapeType = 0;
}
void CALLBACK Key_1(void)
{
ShapeType = 1;
}
void CALLBACK Key_2(void)
{
ShapeType = 2;
}
void CALLBACK Key_3(void)
{
ShapeType = 3;
}
void CALLBACK Key_4(void)
{
ShapeType = 4;
}
void CALLBACK Key_5(void)
{
ShapeType = 5;
}
void CALLBACK Key_6(void)
{
ShapeType = 6;
}
void CALLBACK Key_7(void)
{
ShapeType = 7;
}
void CALLBACK Key_8(void)
{
ShapeType = 8;
}
void CALLBACK Key_9(void)
{
ShapeType = 9;
}
void CALLBACK Key_a(void)
{
ShapeType = 10;
}
int main(int argc, char *argv[])
{
std::cout << "compile successful" << std::endl;
auxInitDisplayMode(AUX_DOUBLE | AUX_RGB | AUX_DEPTH16 | AUX_STENCIL);
auxInitPosition(0, 0, 400, 400);
auxInitWindow("AuxLib Demo");
MyInit();
auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, SetSolid);
auxMouseFunc(AUX_RIGHTBUTTON, AUX_MOUSEDOWN, SetWire);
//auxIdleFunc(SetScale); //设置为空闲的事件,所以会在无操作一直执行
auxKeyFunc(AUX_SPACE, SetScale);
auxKeyFunc(AUX_0, Key_0);
auxKeyFunc(AUX_1, Key_1);
auxKeyFunc(AUX_2, Key_2);
auxKeyFunc(AUX_3, Key_3);
auxKeyFunc(AUX_4, Key_4);
auxKeyFunc(AUX_5, Key_5);
auxKeyFunc(AUX_6, Key_6);
auxKeyFunc(AUX_7, Key_7);
auxKeyFunc(AUX_8, Key_8);
auxKeyFunc(AUX_9, Key_9);
auxKeyFunc(AUX_a, Key_a);
auxReshapeFunc(MyReshape);
auxMainLoop(Display);
return 0;
}
http://www.cnblogs.com/javawebsoa/archive/2013/04/22/3036558.html