#include "glwidget.h"
#include <QTimer>
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};
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{
ui.setupUi(this);
//fullScreen = false;
xRot = 0.0;
yRot = 0.0;
zRot = 0.0;
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(advanceGears()));
timer->start(10);
}
void GLWidget::initializeGL()
{
setGeometry(300,150,640,480);//设置窗口初始化位置和大小
glShadeModel(GL_SMOOTH); //设置阴影平滑模式
glClearColor(0.0,0.0,0.0,0);//窗口背景色
glClearDepth(1.0); //设置深度缓存
glEnable(GL_DEPTH_TEST); //允许深度测试
glDepthFunc(GL_LEQUAL); //设置深度测试类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); //进行透视矫正
//光源
glLightfv(GL_LIGHT1,GL_AMBIENT,lightAmbient);
glLightfv(GL_LIGHT1,GL_DIFFUSE,lightDiffuse);
glLightfv(GL_LIGHT1,GL_POSITION,lightPosition);
glEnable(GL_LIGHT1);
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5, 0.0, -6.0);
/*glRotatef(rTri,0.0,1.0,0.0);*/
glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
//前侧面
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);
//右侧面
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);
//后侧面
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);
//左侧面
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();
glBegin(GL_QUADS);
//底面
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);
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();
glLoadIdentity();
glTranslatef(1.5, 0.0, -7.0);
/*glRotatef(rQuad,1.0,1.0,1.0);*/
glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
glColor3f(0.0,1.0,0.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(1.0,1.0,1.0);
glColor3f(1.0,0.5,0.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(1.0,-1.0,-1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glColor3f(1.0,1.0,0.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(1.0,1.0,-1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,1.0);
glColor3f(1.0,0.0,1.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,-1.0);
glEnd();
xRot += 10;
yRot += 10;
zRot += 10;
}
void GLWidget::resizeGL(int width, int height)
{
glViewport(0,0,(GLint)width,(GLint)height); //重置当前视口
glMatrixMode(GL_PROJECTION); //选择投影矩阵
glLoadIdentity(); //重置选择 好的投影矩阵
gluPerspective(45.0,(GLfloat)width/(GLfloat)height,0.1,100); //建立透视投影矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// glTranslated(0.0, 0.0, -40.0);
}
void GLWidget::setXRotation(int angle)
{
normalizeAngle(&angle);
if (angle != xRot) {
xRot = angle;
emit xRotationChanged(angle);
updateGL();
}
}
void GLWidget::setYRotation(int angle)
{
normalizeAngle(&angle);
if (angle != yRot) {
yRot = angle;
emit yRotationChanged(angle);
updateGL();
}
}
void GLWidget::setZRotation(int angle)
{
normalizeAngle(&angle);
if (angle != zRot) {
zRot = angle;
emit zRotationChanged(angle);
updateGL();
}
}
void GLWidget::advanceGears()
{
//glEnable(GL_LIGHTING);
updateGL();
}
void GLWidget::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->x() - lastPos.x();
int dy = event->y() - lastPos.y();
if (event->buttons() & Qt::LeftButton) {
setXRotation(xRot + 8 * dy);
setYRotation(yRot + 8 * dx);
} else if (event->buttons() & Qt::RightButton) {
setXRotation(xRot + 8 * dy);
setZRotation(zRot + 8 * dx);
}
lastPos = event->pos();
}
GLWidget::~GLWidget()
{
}
void GLWidget::normalizeAngle(int *angle)
{
while (*angle < 0)
*angle += 360 * 16;
while (*angle > 360 * 16)
*angle -= 360 * 16;
}