JerichoJosh
18th December 2013, 15:40
Hi everyone,
I'm drawing a large amount of triangles on the screen using the intialize-resize-paint method.
The program works, but without animation.
I've been trying for a long time to figure out how to use a Timer to animate this, but I haven't been able to find a way.
(Background info: the animation I want is to make the triangles rotate and scale individually.)
Can somebody help me?
Since it is quite a large program, I have brought the code down to structure only.
Thank you ahead of time!
In the main:
int main(int argc, char **argv)
{
QApplication a(argc,argv);
GlWidget w;
w.show();
return a.exec();
}
In the glwidget header file:
class GlWidget : public QGLWidget
{
Q_OBJECT //this macro is included because we might want to use signals and slots
public:
GlWidget(QWidget *parent = 0);
~GlWidget();
QSize sizeHint() const; //to set reasonable default sizes
protected:
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
private:
makeTriangles();
QVector<QVector3D> vertices, colors;
};
In the glwidget source file:
#include "glwidget.h"
//! [0]
GlWidget::GlWidget(QWidget *parent) : QGLWidget(QGLFormat(), parent)
{
}
GlWidget::~GlWidget()
{
}
QSize GlWidget::sizeHint() const
{
return QSize(520, 640);
}
//! [0]
//! [1]
void GlWidget::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
QGLWidget::qglClearColor(QColor(20,20,20,255));
shaderProgram.addShaderFromSourceFile(QGLShader::V ertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::F ragment, ":/fragmentShader.fsh");
shaderProgram.link();
makeTriangles();
}
//! [1]
//! [2]
void GlWidget::resizeGL(int width, int height)
{
if (height == 0) {
height = 1;
}
pMatrix.setToIdentity();
pMatrix.perspective(60.0, (float) width / (float) height, 0.001, 1000);
glViewport(0, 0, width, height);
}
//! [2]
//! [3]
void GlWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
shaderProgram.bind();
shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
shaderProgram.setAttributeArray("color", colors.constData());
shaderProgram.enableAttributeArray("color");
shaderProgram.setAttributeArray("vertex", vertices.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
shaderProgram.disableAttributeArray("vertex");
shaderProgram.disableAttributeArray("color");
shaderProgram.release();
}
GlWidget::makeTriangles()
{
/*specify vertices and colors of triangles, here, the variables vertices and color are defined*/
}
//! [3]
I'm drawing a large amount of triangles on the screen using the intialize-resize-paint method.
The program works, but without animation.
I've been trying for a long time to figure out how to use a Timer to animate this, but I haven't been able to find a way.
(Background info: the animation I want is to make the triangles rotate and scale individually.)
Can somebody help me?
Since it is quite a large program, I have brought the code down to structure only.
Thank you ahead of time!
In the main:
int main(int argc, char **argv)
{
QApplication a(argc,argv);
GlWidget w;
w.show();
return a.exec();
}
In the glwidget header file:
class GlWidget : public QGLWidget
{
Q_OBJECT //this macro is included because we might want to use signals and slots
public:
GlWidget(QWidget *parent = 0);
~GlWidget();
QSize sizeHint() const; //to set reasonable default sizes
protected:
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
private:
makeTriangles();
QVector<QVector3D> vertices, colors;
};
In the glwidget source file:
#include "glwidget.h"
//! [0]
GlWidget::GlWidget(QWidget *parent) : QGLWidget(QGLFormat(), parent)
{
}
GlWidget::~GlWidget()
{
}
QSize GlWidget::sizeHint() const
{
return QSize(520, 640);
}
//! [0]
//! [1]
void GlWidget::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
QGLWidget::qglClearColor(QColor(20,20,20,255));
shaderProgram.addShaderFromSourceFile(QGLShader::V ertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::F ragment, ":/fragmentShader.fsh");
shaderProgram.link();
makeTriangles();
}
//! [1]
//! [2]
void GlWidget::resizeGL(int width, int height)
{
if (height == 0) {
height = 1;
}
pMatrix.setToIdentity();
pMatrix.perspective(60.0, (float) width / (float) height, 0.001, 1000);
glViewport(0, 0, width, height);
}
//! [2]
//! [3]
void GlWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
shaderProgram.bind();
shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
shaderProgram.setAttributeArray("color", colors.constData());
shaderProgram.enableAttributeArray("color");
shaderProgram.setAttributeArray("vertex", vertices.constData());
shaderProgram.enableAttributeArray("vertex");
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
shaderProgram.disableAttributeArray("vertex");
shaderProgram.disableAttributeArray("color");
shaderProgram.release();
}
GlWidget::makeTriangles()
{
/*specify vertices and colors of triangles, here, the variables vertices and color are defined*/
}
//! [3]