Mixpicles
28th April 2011, 10:59
Hello,
I fail to get a tutorial example running, eg. the screen stays black.
I assume I am overlooking something awfully simple.
Please let me know what I am missing.
Edit: the setup works partially, I cannot pass the color to the fragment shader, the matrix seems to be useless as well. Probably was cause my initial question.
So the question is, how to pass the data correctly
#include <QtGui>
#include <QtOpenGL>
#include "MyQGLWidget.h"
MyQGLWidget::MyQGLWidget(const QGLFormat & format, QWidget * parent) : QGLWidget(format, parent)
{
}
MyQGLWidget::~MyQGLWidget()
{
}
void MyQGLWidget::initializeGL()
{
program = new QGLShaderProgram();
glClearColor(0.1,0.1,0.0,1);
program->addShaderFromSourceCode(QGLShader::Vertex,
"attribute highp vec4 vertex;"
"attribute mediump mat4 matrix;"
"void main(void)"
"{"
" gl_Position = matrix * vertex;"
"}");
program->addShaderFromSourceCode(QGLShader::Fragment,
"uniform mediump vec4 color;"
"void main(void)"
"{"
" gl_FragColor = color;"
"}");
program->link();
}
void MyQGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT );
program->bind();
QVector3D triangleVertices[] = {
QVector3D(0, 0.0f, 1.0f),
QVector3D(0, 1.0f, 1.0f),
QVector3D(1.0f, 0, 1.0f)
};
int vertexLocation = program->attributeLocation("vertex");
int colorLocation = program->attributeLocation("color");
int matrixLocation = program->attributeLocation("matrix");
QMatrix4x4 pmvMatrix;
program->enableAttributeArray(vertexLocation);
program->setAttributeArray(vertexLocation, triangleVertices);
program->setUniformValue(matrixLocation, pmvMatrix);
program->setUniformValue(colorLocation, QColor(1, 1, 1));
glDrawArrays(GL_TRIANGLES, 0, 3);
program->disableAttributeArray(vertexLocation);
program->release();
}
void MyQGLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0, 1, 0.1, 60.0 );
glMatrixMode(GL_MODELVIEW);
}
Regards Christoph
I fail to get a tutorial example running, eg. the screen stays black.
I assume I am overlooking something awfully simple.
Please let me know what I am missing.
Edit: the setup works partially, I cannot pass the color to the fragment shader, the matrix seems to be useless as well. Probably was cause my initial question.
So the question is, how to pass the data correctly
#include <QtGui>
#include <QtOpenGL>
#include "MyQGLWidget.h"
MyQGLWidget::MyQGLWidget(const QGLFormat & format, QWidget * parent) : QGLWidget(format, parent)
{
}
MyQGLWidget::~MyQGLWidget()
{
}
void MyQGLWidget::initializeGL()
{
program = new QGLShaderProgram();
glClearColor(0.1,0.1,0.0,1);
program->addShaderFromSourceCode(QGLShader::Vertex,
"attribute highp vec4 vertex;"
"attribute mediump mat4 matrix;"
"void main(void)"
"{"
" gl_Position = matrix * vertex;"
"}");
program->addShaderFromSourceCode(QGLShader::Fragment,
"uniform mediump vec4 color;"
"void main(void)"
"{"
" gl_FragColor = color;"
"}");
program->link();
}
void MyQGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT );
program->bind();
QVector3D triangleVertices[] = {
QVector3D(0, 0.0f, 1.0f),
QVector3D(0, 1.0f, 1.0f),
QVector3D(1.0f, 0, 1.0f)
};
int vertexLocation = program->attributeLocation("vertex");
int colorLocation = program->attributeLocation("color");
int matrixLocation = program->attributeLocation("matrix");
QMatrix4x4 pmvMatrix;
program->enableAttributeArray(vertexLocation);
program->setAttributeArray(vertexLocation, triangleVertices);
program->setUniformValue(matrixLocation, pmvMatrix);
program->setUniformValue(colorLocation, QColor(1, 1, 1));
glDrawArrays(GL_TRIANGLES, 0, 3);
program->disableAttributeArray(vertexLocation);
program->release();
}
void MyQGLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0, 1, 0.1, 60.0 );
glMatrixMode(GL_MODELVIEW);
}
Regards Christoph