QGLShaderProgram, failing to recreate tutorial example
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
Code:
#include <QtGui>
#include <QtOpenGL>
#include "MyQGLWidget.h"
{
}
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
Re: QGLShaderProgram, failing to recreate tutorial example
Maybe i've missed something in the code , but at the first glance :
Quote:
" gl_Position = matrix * vertex;"
and the "matrix" doesn't seem to recieve any values (and it's not a uniform variable either) . THere are two basic ways to transform your vertice coordinates into the screen space:
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex ;
OR a shorter one:
gl_Position = ftransform() ;
Same with the fragment shader : can't see color value initiated.Just to see if things work try smth. like that:
gl_FragColor = vec4( 0.5 , 0.0 , 0.0 , 1.0 );
Again,i may be wrong : i'm not quite familiar with Qt "wrappers" for openGL shaders.
P.S : there is a great program called ShaderDesigner , might be useful to write and debug a shader before using it with Qt.
Re: QGLShaderProgram, failing to recreate tutorial example
Thank you for posting,
gl_ModelViewProjectionMatrix does not seem to work , altering the transformation matrix is ineffective.
And setting the color within the shader code works perfectly.
matrix problem is resolved by defining it as uniform in the environment.
Remains the color problem
@previous suggestion
I am afraid, that this would be to much for me atm.
I am struggeling to grasp the basics.
But thank you none the less.
Added after 16 minutes:
program->setUniformValue(colorLocation, QVector4D(0.5, 0, 0, 1));
Does the trick QColor seems not to be compatible.
Beware of halfbaked tutorials....
Re: QGLShaderProgram, failing to recreate tutorial example
Good choice:) Shader's writing does require some openGL processing pipeline knowledge.Fortunattely there are a lot of books and net resources available .
Great book : Randi J. Rost. OpenGL Shading Language
Good luck!