Results 1 to 4 of 4

Thread: QGLShaderProgram, failing to recreate tutorial example

  1. #1
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

    Qt Code:
    1. #include <QtGui>
    2. #include <QtOpenGL>
    3. #include "MyQGLWidget.h"
    4.  
    5. MyQGLWidget::MyQGLWidget(const QGLFormat & format, QWidget * parent) : QGLWidget(format, parent)
    6. {
    7. }
    8. MyQGLWidget::~MyQGLWidget()
    9. {
    10. }
    11.  
    12. void MyQGLWidget::initializeGL()
    13. {
    14. program = new QGLShaderProgram();
    15.  
    16. glClearColor(0.1,0.1,0.0,1);
    17.  
    18. program->addShaderFromSourceCode(QGLShader::Vertex,
    19. "attribute highp vec4 vertex;"
    20. "attribute mediump mat4 matrix;"
    21. "void main(void)"
    22. "{"
    23. " gl_Position = matrix * vertex;"
    24. "}");
    25. program->addShaderFromSourceCode(QGLShader::Fragment,
    26. "uniform mediump vec4 color;"
    27. "void main(void)"
    28. "{"
    29. " gl_FragColor = color;"
    30. "}");
    31. program->link();
    32.  
    33. }
    34. void MyQGLWidget::paintGL()
    35. {
    36. glClear(GL_COLOR_BUFFER_BIT );
    37. program->bind();
    38. QVector3D triangleVertices[] = {
    39. QVector3D(0, 0.0f, 1.0f),
    40. QVector3D(0, 1.0f, 1.0f),
    41. QVector3D(1.0f, 0, 1.0f)
    42. };
    43.  
    44. int vertexLocation = program->attributeLocation("vertex");
    45. int colorLocation = program->attributeLocation("color");
    46. int matrixLocation = program->attributeLocation("matrix");
    47.  
    48. QMatrix4x4 pmvMatrix;
    49.  
    50. program->enableAttributeArray(vertexLocation);
    51. program->setAttributeArray(vertexLocation, triangleVertices);
    52. program->setUniformValue(matrixLocation, pmvMatrix);
    53. program->setUniformValue(colorLocation, QColor(1, 1, 1));
    54.  
    55. glDrawArrays(GL_TRIANGLES, 0, 3);
    56.  
    57. program->disableAttributeArray(vertexLocation);
    58. program->release();
    59. }
    60.  
    61.  
    62.  
    63. void MyQGLWidget::resizeGL(int width, int height)
    64. {
    65.  
    66. int side = qMin(width, height);
    67. glViewport((width - side) / 2, (height - side) / 2, side, side);
    68.  
    69. glMatrixMode(GL_PROJECTION);
    70. glLoadIdentity();
    71.  
    72. gluPerspective( 45.0, 1, 0.1, 60.0 );
    73.  
    74. glMatrixMode(GL_MODELVIEW);
    75.  
    76. }
    To copy to clipboard, switch view to plain text mode 

    Regards Christoph
    Last edited by Mixpicles; 28th April 2011 at 13:43. Reason: corrected code

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGLShaderProgram, failing to recreate tutorial example

    Maybe i've missed something in the code , but at the first glance :

    " 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.

  3. #3
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default 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....
    Last edited by Mixpicles; 28th April 2011 at 14:24.

  4. #4
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!

Similar Threads

  1. Shutdown on Mac failing
    By ntp in forum Qt Programming
    Replies: 4
    Last Post: 11th December 2010, 01:41
  2. Qt Installation failing
    By kmorgado in forum Newbie
    Replies: 4
    Last Post: 4th July 2010, 23:34
  3. [SOLVED] Recreate .ini file
    By el in forum Newbie
    Replies: 2
    Last Post: 3rd December 2009, 12:19
  4. Failing Builds in msvc2005 -> QT4.4.3
    By plitanium in forum Installation and Deployment
    Replies: 4
    Last Post: 2nd March 2009, 12:39
  5. I'm failing with Threading
    By thomaspu in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2008, 19:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.