Results 1 to 4 of 4

Thread: QGLShaderProgram, failing to recreate tutorial example

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •  
Qt is a trademark of The Qt Company.