Results 1 to 6 of 6

Thread: Fail to generate a triangle by openGL(Qt5.0.2)

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Fail to generate a triangle by openGL(Qt5.0.2)

    OS : mac osx 10.8.3
    compiler : clang3.2

    I am a beginner of opengl, trying to play opengl with Qt5

    There are two problems about this simple program(plot a triangle)
    1 : I can't see the triangle
    2 : The program could not exit even I close the window

    hpp
    Qt Code:
    1. #ifndef CH1HELLOTRIANGLE_HPP
    2. #define CH1HELLOTRIANGLE_HPP
    3.  
    4. #include <QGLWidget>
    5.  
    6. #include <QtGui/QOpenGLFunctions>
    7. #include <QtGui/QOpenGLShaderProgram>
    8.  
    9. class QWidget;
    10.  
    11. class ch1HelloTriangle : public QGLWidget, protected QOpenGLFunctions
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit ch1HelloTriangle(QWidget *parent = 0);
    16.  
    17. protected:
    18. virtual void initializeGL();
    19. void initShaders();
    20. void InitializeVertexBuffer();
    21.  
    22. virtual void resizeGL(int w, int h);
    23. virtual void paintGL();
    24.  
    25. private:
    26. QOpenGLShaderProgram program;
    27.  
    28. GLuint positionBufferObject;
    29. };
    30.  
    31. #endif // CH1HELLOTRIANGLE_HPP
    To copy to clipboard, switch view to plain text mode 


    .cpp
    Qt Code:
    1. #include <locale.h>
    2.  
    3. #include <QWidget>
    4.  
    5. #include "ch1HelloTriangle.hpp"
    6.  
    7. namespace
    8. {
    9.  
    10. float const vertexPositions[] = {
    11. 0.75f, 0.75f, 0.0f, 1.0f,
    12. 0.75f, -0.75f, 0.0f, 1.0f,
    13. -0.75f, -0.75f, 0.0f, 1.0f,
    14. };
    15.  
    16. }
    17.  
    18. ch1HelloTriangle::ch1HelloTriangle(QWidget *parent) :
    19. QGLWidget(parent)
    20. {
    21. }
    22.  
    23. void ch1HelloTriangle::initializeGL()
    24. {
    25. initializeOpenGLFunctions();
    26. InitializeVertexBuffer();
    27. initShaders();
    28. }
    29.  
    30. void ch1HelloTriangle::initShaders()
    31. {
    32. // Override system locale until shaders are compiled
    33. setlocale(LC_NUMERIC, "C");
    34.  
    35. // Compile vertex shader
    36. if (!program.addShaderFromSourceCode(QOpenGLShader::Vertex,
    37. "attribute vec4 position;\n"
    38. "void main()\n"
    39. "{\n"
    40. " gl_Position = position;\n"
    41. "}\n"))
    42. {
    43. close();
    44. }
    45.  
    46. // Compile fragment shader
    47. if (!program.addShaderFromSourceCode(QOpenGLShader::Fragment,
    48. "out vec4 outputColor;\n"
    49. "void main()\n"
    50. "{\n"
    51. " outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
    52. "}\n"))
    53. {
    54. close();
    55.  
    56. }
    57.  
    58. // Link shader pipeline
    59. if (!program.link())
    60. close();
    61.  
    62. // Bind shader pipeline for use
    63. if (!program.bind())
    64. close();
    65.  
    66. // Restore system locale
    67. setlocale(LC_ALL, "");
    68. }
    69.  
    70. void ch1HelloTriangle::InitializeVertexBuffer()
    71. {
    72. glGenBuffers(1, &positionBufferObject);
    73.  
    74. glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
    75. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
    76. glBindBuffer(GL_ARRAY_BUFFER, 0);
    77. }
    78.  
    79. void ch1HelloTriangle::resizeGL(int w, int h)
    80. {
    81. // Set OpenGL viewport to cover whole widget
    82. glViewport(0, 0, w, h);
    83. }
    84.  
    85. void ch1HelloTriangle::paintGL()
    86. {
    87. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    88. glClear(GL_COLOR_BUFFER_BIT);
    89.  
    90. glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
    91.  
    92. int vertexLocation = program.attributeLocation("position");
    93. program.enableAttributeArray(vertexLocation);
    94.  
    95. glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
    96.  
    97. glDrawArrays(GL_TRIANGLES, 0, 3);
    98. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "ch1HelloTriangle.hpp"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. ch1HelloTriangle ch1;
    10. ch1.show();
    11.  
    12. return a.exec(&#41;;
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to stereoMatching for this useful post:


  3. #2
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fail to generate a triangle by openGL(Qt5.0.2)

    After a lot of trial and error, I solved the problem.

    Change two things :
    1 : read the shader by files

    Qt Code:
    1. // Compile vertex shader
    2. if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, "modernOpenGLShader/ch1/vertex")){
    3. QMessageBox::warning(this, "QOpenGLShader::Vertex", "QOpenGLShader::Vertex" + program.log());
    4. close();
    5. }
    6.  
    7. // Compile fragment shader
    8. if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, "modernOpenGLShader/ch1/frag")){
    9. QMessageBox::warning(this, "QOpenGLShader::Fragment", "QOpenGLShader::Fragment" + program.log());
    10. close();
    11. }
    To copy to clipboard, switch view to plain text mode 

    2 : change the fragment shader, remove the output variable

    Qt Code:
    1. void main() {
    2. //set every drawn pixel to white
    3. gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
    4. }
    To copy to clipboard, switch view to plain text mode 

    From the websites, "modern opengl":http://www.arcsynthesis.org/gltut/
    and "another site":tomdalling.com/blog/modern-opengl/01-getting-started-in-xcode-and-visual-cpp/

    The output qualify should exist(atleast the codes of the second website will work on my pc)
    But Qt will throw error message

    QOpenGLShader::FragmentERROR: 0:4: Invalid qualifiers 'out' in global variable context

    Do anyone know why?Thanks

  4. The following user says thank you to stereoMatching for this useful post:


  5. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Fail to generate a triangle by openGL(Qt5.0.2)

    Whether you get this error or not depends on the OpenGL version used by the shader engine. Probably the version Qt requests by default (or the one you use if you requested a different version explicitly) doesn't allow renaming the fragment shader output variable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following 2 users say thank you to wysota for this useful post:

    stereoMatching (8th May 2013)

  7. #4
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fail to generate a triangle by openGL(Qt5.0.2)

    Whether you get this error or not depends on the OpenGL version used by the shader engine. Probably the version Qt requests by default (or the one you use if you requested a different version explicitly) doesn't allow renaming the fragment shader output variable.
    Maybe you are right, since Qt need to portable, it may not support some features of the GLSL.

  8. The following user says thank you to stereoMatching for this useful post:


  9. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Fail to generate a triangle by openGL(Qt5.0.2)

    It's not about Qt as it is not Qt that spits out the error but rather the GLSL compiler.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:


  11. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Fail to generate a triangle by openGL(Qt5.0.2)

    Quote Originally Posted by stereoMatching View Post

    QOpenGLShader::FragmentERROR: 0:4: Invalid qualifiers 'out' in global variable context

    Do anyone know why?Thanks
    Yes, I know. You need to change the in & out identifiers inside your shader program to varying & attribute.

Similar Threads

  1. QGradient on triangle
    By oogolov in forum Qt Programming
    Replies: 3
    Last Post: 20th June 2012, 20:34
  2. drawing triangle with gradient based on vertices
    By marc2050 in forum Qt Programming
    Replies: 5
    Last Post: 4th August 2011, 13:45
  3. Replies: 2
    Last Post: 30th December 2010, 18:03
  4. Replies: 1
    Last Post: 24th December 2010, 01:15
  5. How to draw fast rectangle/triangle/circle
    By nileshsince1980 in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2008, 12: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.