我目前正在使用 Qt 尝试建立一个小型粒子系统。其中,我将 GLWidget 子类化并破解了它。一切都很顺利,直到我做了一些未知的更改,现在小部件只在我移动鼠标时重新绘制(由于我已经触发了 QTimer,它应该一直在这样做)。相关代码:
OpenGLWidget.h
class OpenGLWidget : public QGLWidget {
Q_OBJECT
public:
OpenGLWidget(QWidget * parent = 0);
~OpenGLWidget();
public slots:
void toggleEmitter();
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
QSize minimumSizeHint() const;
QSize sizeHint() const;
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
protected slots:
void timeOut();
private:
void testFunc(Particle & p, unsigned int t);
QTime time;
QTimer timer;
Emitter emitter;
.cpp 中的相关代码
// in the constructor
time.start();
connect(&timer, SIGNAL(timeout()), this, SLOT(timeOut()));
timer.start(0);
void OpenGLWidget::initializeGL() {
GLuint tex = 0;
qglClearColor(bgColor);
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE); // _MINUS_SRC_ALPHA );
glPointSize(3.0f);
tex = bindTexture(QPixmap(QString("testparticle2.png")), GL_TEXTURE_2D);
emitter = Emitter(Vector(0, 0, 0.0f), tex, 50, fastdelegate::FastDelegate2<Particle &, unsigned int>(this, &OpenGLWidget::testFunc));
}
void OpenGLWidget::paintGL() {
makeCurrent();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mainCam.SetView();
glRotatef(xRot, 1, 0, 0);
glRotatef(yRot, 0, 1, 0);
emitter.Process(time.elapsed());
totalTime += time.elapsed();
time.restart();
}
void OpenGLWidget::resizeGL(int width, int height) {
contextWidth = width;
contextHeight = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0f, width / (float) height, 0.01f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void OpenGLWidget::timeOut() {
updateGL();
}
void OpenGLWidget::mousePressEvent(QMouseEvent *event) {
lastX = event->pos().x();
lastY = event->pos().y();
}
void OpenGLWidget::mouseMoveEvent(QMouseEvent *event) {
int dx = event->x() - lastX;
int dy = event->y() - lastY;
if (event->buttons() & Qt::LeftButton) {
xRot += 3 * dy;
yRot += 3 * dx;
} else if (event->buttons() & Qt::RightButton) {
xRot += 3 * dy;
yRot += 3 * dx;
}
lastX = event->pos().x();
lastY = event->pos().y();
}