1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include "windowopengl.h"
WindowOpengl::WindowOpengl(QWidget *parent)
: QGLWidget(parent)
{
setMinimumWidth(500);
//program = new QGLShaderProgram;
}
WindowOpengl::~WindowOpengl()
{
}
void WindowOpengl::initializeGL()
{
program = new QGLShaderProgram();
glClearColor(0.f,0.f,0.f,1.f);
if (!program->hasOpenGLShaderPrograms())
{
QMessageBox::information(this, "Beuglage", "Ya une erreur quequepar, tu es vraiment un Zéros !");
}
if (!program->addShaderFromSourceCode(QGLShader::Vertex,
"in vec4 vertex;\n"
"uniform mat4 matrix;\n"
"void main(void)\n"
"{\n"
" gl_Position = matrix * vertex;\n"
"}\n"))
{
QMessageBox::information(this, "Beuglage", "Ya une erreur quequepar, tu es vraiment un Zéros !");
}
if (!program->addShaderFromSourceCode(QGLShader::Fragment,
"uniform vec4 color;\n"
"void main(void)\n"
"{\n"
"gl_FragColor = color;\n"
"}\n"))
{
QMessageBox::information(this, "Beuglage", "Ya une erreur quequepar, tu es vraiment un Zéros !");
}
if (!program->link())
{
QMessageBox::information(this, "Beuglage", "Ya une erreur quequepar, tu es vraiment un Zéros !");
}
}
void WindowOpengl::resizeGL(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, width, height);
}
void WindowOpengl::paintGL()// draw the scene:
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
if (!program->bind())
{
QMessageBox::information(this, "Beuglage", "Ya une erreur quequepar, tu es vraiment un Zéros !");
}
static GLfloat const triangleVertices[] = {
1.0f, 1.0f, -5.0f,
0.0f, 1.0f, -5.0f,
1.0f, 0.0f, -5.0f
};
int vertexLocation = program->attributeLocation("vertex");
int colorLocation = program->uniformLocation("color");
int matrixLocation = program->uniformLocation("matrix");
QMatrix4x4 pmvMatrix;
pmvMatrix.perspective(45.0, 1.5, 0.1, 60.0 );
program->enableAttributeArray(vertexLocation);
program->setAttributeArray(vertexLocation, triangleVertices,3);
program->setUniformValue(matrixLocation, pmvMatrix);
program->setUniformValue(colorLocation, QColor(250, 50, 0, 255));
glDrawArrays(GL_TRIANGLES, 0, 3);
program->disableAttributeArray(vertexLocation);
program->release();
} |