glwidget.cpp Example File
opengl/textures/glwidget.cpp
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
#include "glwidget.h"
GLuint GLWidget::sharedObject = 0;
int GLWidget::refCount = 0;
GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget)
: QGLWidget(parent, shareWidget)
{
clearColor = Qt::black;
xRot = 0;
yRot = 0;
zRot = 0;
}
GLWidget::~GLWidget()
{
if (--refCount == 0) {
makeCurrent();
glDeleteLists(sharedObject, 1);
}
}
QSize GLWidget::minimumSizeHint() const
{
return QSize(50, 50);
}
QSize GLWidget::sizeHint() const
{
return QSize(200, 200);
}
void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle)
{
xRot += xAngle;
yRot += yAngle;
zRot += zAngle;
updateGL();
}
void GLWidget::setClearColor(const QColor &color)
{
clearColor = color;
updateGL();
}
void GLWidget::initializeGL()
{
if (!sharedObject)
sharedObject = makeObject();
++refCount;
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
}
void GLWidget::paintGL()
{
qglClearColor(clearColor);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(0.0, 0.0, -10.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);
glCallList(sharedObject);
}
void GLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
glMatrixMode(GL_MODELVIEW);
}
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) {
rotateBy(8 * dy, 8 * dx, 0);
} else if (event->buttons() & Qt::RightButton) {
rotateBy(8 * dy, 0, 8 * dx);
}
lastPos = event->pos();
}
void GLWidget::mouseReleaseEvent(QMouseEvent * )
{
emit clicked();
}
GLuint GLWidget::makeObject()
{
static const int coords[6][4][3] = {
{ { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
{ { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
{ { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
{ { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
{ { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
{ { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
};
GLuint textures[6];
for (int j=0; j < 6; ++j)
textures[j] = bindTexture(QPixmap(QString(":/images/side%1.png").arg(j + 1)),
GL_TEXTURE_2D);
GLuint list = glGenLists(1);
glNewList(list, GL_COMPILE);
for (int i = 0; i < 6; ++i) {
glBindTexture(GL_TEXTURE_2D, textures[i]);
glBegin(GL_QUADS);
for (int j = 0; j < 4; ++j) {
glTexCoord2d(j == 0 || j == 3, j == 0 || j == 1);
glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1],
0.2 * coords[i][j][2]);
}
glEnd();
}
glEndList();
return list;
}