Results 1 to 6 of 6

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

Threaded View

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


Similar Threads

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