int main(int argc, char **argv)
{
GlWidget w;
w.show();
return a.exec();
}
int main(int argc, char **argv)
{
QApplication a(argc,argv);
GlWidget w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
{
Q_OBJECT //this macro is included because we might want to use signals and slots
public:
~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;
};
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;
};
To copy to clipboard, switch view to plain text mode
#include "glwidget.h"
//! [0]
{
}
GlWidget::~GlWidget()
{
}
QSize GlWidget
::sizeHint() const {
}
//! [0]
//! [1]
void GlWidget::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/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]
#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::Vertex, ":/vertexShader.vsh");
shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/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]
To copy to clipboard, switch view to plain text mode
Bookmarks