#include "glutttt.h"
#include <math.h>
#include <QImage>
#include <GLAUX>
static GLfloat boxcol[5][3] = {
{1.0,0.0,0.0},
{1.0,0.5,0.0},
{1.0,1.0,0.0},
{0.0,1.0,0.0},
{0.0,1.0,1.0}
};
static GLfloat topcol[5][3] = {
{0.5,0.0,0.0},
{0.5,0.25,0.0},
{0.5,0.5,0.0},
{0.5,0.0,0.0},
{0.0,0.5,0.5}
};//两个颜色数组
//设置光源
GLfloat lightAmbient[4] = {0.5,0.5,0.5,1.0};
GLfloat lightDiffuse[4] = {1.0,1.0,1.0,1.0};
GLfloat lightPosition[4] = {0.0,0.0,2.0,1.0};
//雾的设定
GLuint fogMode[3] = {GL_EXP,GL_EXP2,GL_LINEAR};//三种雾的效果,依次递进
GLfloat fogColor[4] = {0.5,0.5,1.0,0.3};
glutttt::glutttt(QWidget *parent)
: QGLWidget(parent)
{
xrot = yrot = zrot = 0;
zoom = -10.0;
hold = 0.0;
box = top = tri = 0;
xLoop = yLoop = 0;
fogFilter = 3;
wiggle_count = 0;
filter = 0;
light = false;
blend = false;
fullScreen = false;
sphere_x = 2.0;
sphere_y = 0;
obj =0;
p1 = 0;
p2 = 1;
part1 = 0;
part2 = 0;
setGeometry(50,50,800,600);
if (fullScreen)
{
showFullScreen();
}
startTimer(5);
}
void glutttt::initializeGL()
{
loadGLTextures();//先载入纹理
buildLists();//建立列表
glEnable(GL_TEXTURE_2D);//启用纹理
glShadeModel(GL_SMOOTH);//阴影平滑
glClearColor(0.0,0.0,0.0,0.5);//设置清除屏幕时所使用的颜色
glClearDepth(1.0);//设置深度缓存
glEnable(GL_DEPTH_TEST);//启用深度测试
glDepthFunc(GL_LEQUAL);//所做深度测试的类型
glEnable(GL_COLOR_MATERIAL);//可以用颜色来帖纹理
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//投影修正
glPolygonMode(GL_BACK,GL_FILL);//背景
glPolygonMode(GL_FRONT,GL_FILL);//前景
//灯
glLightfv(GL_LIGHT1,GL_AMBIENT,lightAmbient);
glLightfv(GL_LIGHT1,GL_DIFFUSE,lightDiffuse);
glLightfv(GL_LIGHT1,GL_POSITION,lightPosition);
glEnable(GL_LIGHT1);
//雾
glFogi(GL_FOG_MODE,fogMode[fogFilter]);
glFogfv(GL_FOG_COLOR,fogColor);
glFogf(GL_FOG_DENSITY,0.1);//雾的浓度
glHint(GL_FOG_HINT,GL_DONT_CARE);//确定雾的渲染方式,不关心建议值,GL_NICEST极棒的,每一像素渲染,GL_FASTEST对每一顶点渲染,速度快
glFogf(GL_FOG_START,1.0);//雾离屏幕的距离
glFogf(GL_FOG_END,5.0);
//glEnable(GL_FOG);
//融合
glColor4f(1.0,1.0,1.0,0.5);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
//初始化网格的点,整数循环可以消除浮点数运算取整造成的脉冲锯齿出现
for (int x=0;x<45;x++)
{
for (int y=0;y<45;y++)
{
points[x][y][0] = float((x/5.0)-5.0);//使波浪居中【-4.5~4.5】
points[x][y][1] = float((y/5.0)-5.0);
points[x][y][2] = float(sin((((x/5.0)*40.0)/360.0)*3.141592654*2.0));
}
}
}
void glutttt::buildLists()
{
box = glGenLists(3);//建立两个显示列表,box指向第一个列表
glNewList(box,GL_COMPILE);//把列表存储到box所指空间,第二个参数预先在内存中构造这个列表,COMPILE相当于编程,调用的是已经变异好的盒子
glBegin(GL_QUADS);
//第一个
glNormal3f(0.0,-1.0,0.0);
glTexCoord2f(1.0,1.0);glVertex3f(-1.0,-1.0,-1.0);
glTexCoord2f(0.0,1.0);glVertex3f(1.0,-1.0,-1.0);
glTexCoord2f(0.0,0.0);glVertex3f(1.0,-1.0,1.0);
glTexCoord2f(1.0,0.0);glVertex3f(-1.0,-1.0,1.0);
//第二个
glNormal3f(0.0,0.0,1.0);
glTexCoord2f(0.0,0.0);glVertex3f(-1.0,-1.0,1.0);
glTexCoord2f(1.0,0.0);glVertex3f(1.0,-1.0,1.0);
glTexCoord2f(1.0,1.0);glVertex3f(1.0,1.0,1.0);
glTexCoord2f(0.0,1.0);glVertex3f(-1.0,1.0,1.0);
//第三个
glNormal3f(0.0,0.0,-1.0);
glTexCoord2f(1.0,0.0);glVertex3f(-1.0,-1.0,-1.0);
glTexCoord2f(1.0,1.0);glVertex3f(-1.0,1.0,-1.0);
glTexCoord2f(0.0,1.0);glVertex3f(1.0,1.0,-1.0);
glTexCoord2f(0.0,0.0);glVertex3f(1.0,-1.0,-1.0);
//第四个
glNormal3f(1.0,0.0,0.0);
glTexCoord2f(1.0,0.0);glVertex3f(1.0,-1.0,-1.0);
glTexCoord2f(1.0,1.0);glVertex3f(1.0,1.0,-1.0);
glTexCoord2f(0.0,1.0);glVertex3f(1.0,1.0,1.0);
glTexCoord2f(0.0,0.0);glVertex3f(1.0,-1.0,1.0);
//第五个
glNormal3f(-1.0,0.0,0.0);
glTexCoord2f(0.0,0.0);glVertex3f(-1.0,-1.0,-1.0);
glTexCoord2f(1.0,0.0);glVertex3f(-1.0,-1.0,1.0);
glTexCoord2f(1.0,1.0);glVertex3f(-1.0,1.0,1.0);
glTexCoord2f(0.0,1.0);glVertex3f(-1.0,1.0,-1.0);
glEnd();
glEndList();//完成了一个显示列表
top = box + 1;//上一个显示列表的指针加1,得到第二个显示列表,列表指针为top
glNewList(top,GL_COMPILE);
glBegin(GL_QUADS);
glNormal3f(0.0,1.0,0.0);
glTexCoord2f(0.0,1.0);glVertex3f(-1.0,1.0,-1.0);
glTexCoord2f(0.0,0.0);glVertex3f(-1.0,1.0,1.0);
glTexCoord2f(1.0,0.0);glVertex3f(1.0,1.0,1.0);
glTexCoord2f(1.0,1.0);glVertex3f(1.0,1.0,-1.0);
glEnd();
glEndList();//第二个显示列表完毕
tri = top +1;
glNewList(tri,GL_COMPILE);
glBegin(GL_TRIANGLES);
//第一面
glNormal3f(0.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);glVertex3f(0.0,1.0,0.0);
glColor3f(0.0,1.0,0.0);glVertex3f(-1.0,-1.0,1.0);
glColor3f(1.0,0.0,1.0);glVertex3f(1.0,-1.0,1.0);
//第二面
glNormal3f(0.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);glVertex3f(0.0,1.0,0.0);
glColor3f(0.0,0.0,1.0);glVertex3f(1.0,-1.0,1.0);
glColor3f(0.0,1.0,0.0);glVertex3f(1.0,-1.0,-1.0);
//第三面
glNormal3f(0.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);glVertex3f(0.0,1.0,0.0);
glColor3f(0.0,1.0,0.0);glVertex3f(1.0,-1.0,-1.0);
glColor3f(0.0,0.0,1.0);glVertex3f(-1.0,-1.0,-1.0);
//第四面
glNormal3f(0.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);glVertex3f(0.0,1.0,0.0);
glColor3f(0.0,0.0,1.0);glVertex3f(-1.0,-1.0,-1.0);
glColor3f(0.0,1.0,0.0);glVertex3f(-1.0,-1.0,1.0);
glEnd();
glEndList();
}
void glutttt::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除屏幕和深度缓存
glBindTexture(GL_TEXTURE_2D,texture[filter]);//绑定纹理
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_LINE);
gluQuadricNormals(qobj,GLU_SMOOTH);
switch(obj)
{
case 0:
drawFlag();//旗
break;
case 1:
drawList();//盒子列表
break;
case 2:
glLoadIdentity();
glTranslatef(0,0,zoom);//
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
auxWireDodecahedron(2.0);
break;
case 3:
glLoadIdentity();
glTranslatef(0,0,zoom);//球
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
glutSolidSphere(2,64,64);//实心
break;
case 4:
glLoadIdentity();
glTranslatef(0,0,zoom);//球
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
glutWireSphere(2,64,64);//网状
break;
case 5:
glLoadIdentity();
glTranslatef(0,0,zoom);
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
gluCylinder(qobj,2.0,2.0,4.0,100,5);//圆柱
break;
case 6:
glLoadIdentity();
glTranslatef(0,0,zoom);
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
gluCylinder(qobj,2.0,0.0,4.0,100,5);//圆锥
break;
case 7:
glLoadIdentity();
glTranslatef(0,0,zoom);//圆盘
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
gluDisk(qobj,1.0,2.0,1000,1);
break;
case 8:
glLoadIdentity();
glTranslatef(0,0,zoom);//绘制部分圆盘
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
part1 += p1;
part2 += p2;
if (part1>359)
{
p1 = 0;
part1 = 0;
p2 = 1;
part2 = 0;
}
if (part2>359)
{
p1 = 1;
p2 =0;
}
gluPartialDisk(qobj,1.0,3.0,1000,1,part1,part2-part1);
break;
case 9: //网状茶壶
glLoadIdentity();
glTranslatef(0,0,zoom);
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
glutWireTeapot(3.0);
break;
case 10: //实心茶壶
glLoadIdentity();
glTranslatef(1.5,-2.5,zoom);
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
glutSolidTeapot(3.0);
break;
case 11:
glLoadIdentity();
glTranslatef(0,0,zoom);
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
glutSolidCone(2,4,64,64);//实心圆锥
break;
case 12:
glLoadIdentity();
glTranslatef(0,0,zoom);
gl