Results 1 to 2 of 2

Thread: Qt opengl use shader to draw triangle

  1. #1
    Join Date
    May 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Qt opengl use shader to draw triangle

    I just want to draw a triangle with Qt. When I run the program, there is no triangle. What is wrong with my code?
    Qt Code:
    1. #include "glwidget.h"
    2.  
    3. GLWidget::GLWidget(QWidget *parent) :QOpenGLWidget(parent)
    4. {
    5. this->setFocusPolicy(Qt::StrongFocus);
    6. QSurfaceFormat format;
    7. format.setDepthBufferSize(24);
    8. format.setMajorVersion(4);
    9. format.setMinorVersion(2);
    10. format.setSamples(16);
    11. format.setProfile(QSurfaceFormat::CoreProfile);
    12. setFormat(format);
    13.  
    14. }
    15.  
    16. void GLWidget::initializeGL()
    17. {
    18. initializeOpenGLFunctions();
    19. glClearColor(0.13F, 0.13F, 0.14F, 0.0F);
    20. m_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,"../PCRP/vertexshadercode.vert");
    21. m_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,"../PCRP/fragmentshadercode.frag");
    22. m_shaderProgram.link();
    23. m_MVPMatrixLoc=m_shaderProgram.uniformLocation("u_MVPMatrix");
    24. GLfloat vertex[]={1.0,0.0,0.0,1.0,
    25. 0.0,1.0,0.0,1.0,
    26. 1.0,1.0,0.0,1.0};
    27. m_Vbo.create();
    28. m_Vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
    29. m_Vbo.bind();
    30. m_Vbo.allocate(vertex,sizeof(vertex));
    31. m_ViewMatrix.lookAt(QVector3D(0.0f, 0.0f, 1.5f), QVector3D(0.0f, 0.0f, -5.0f), QVector3D(0.0f, 1.0f, 0.0f));
    32.  
    33. }
    34.  
    35. void GLWidget::paintGL()
    36. {
    37. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    38. glEnable(GL_DEPTH_TEST);
    39. glEnable(GL_MULTISAMPLE);
    40. m_shaderProgram.bind();
    41. m_shaderProgram.enableAttributeArray("a_Position");
    42. m_shaderProgram.setAttributeBuffer("a_Position",GL_FLOAT,0,4);
    43.  
    44. m_ModelMatrix.setToIdentity();
    45. m_MVPMatrix=m_ProjectionMatrix*m_ViewMatrix*m_ModelMatrix;
    46.  
    47. m_shaderProgram.setUniformValue(m_MVPMatrixLoc,m_MVPMatrix);
    48. glDrawArrays(GL_TRIANGLES,0,3);
    49.  
    50. }
    51.  
    52. void GLWidget::resizeGL(int w, int h)
    53. {
    54. glViewport(0, 0, (GLint)width(), (GLint)height());
    55. m_ProjectionMatrix.setToIdentity();
    56. m_ProjectionMatrix.perspective(45,(float) (width()/height()),0.1f,1000.0f);
    57. }
    58.  
    59. this is vertexshadercode.vert
    60.  
    61. attribute vec4 a_Position;
    62.  
    63. uniform mat4 u_MVPMatrix;
    64.  
    65. void main()
    66. {
    67.  
    68. gl_Position = u_MVPMatrix*a_Position;
    69. } ;
    70.  
    71. this is fragmentshadercode.frag
    72.  
    73. void main(void)
    74. {
    75. gl_FragColor=vec4(1.0,0.0,0.0,1.0);
    76. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by zhangzhisheng; 7th May 2015 at 10:36.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt opengl use shader to draw triangle

    You don't check the return values from the addShaderFromSource() calls. Do you even know that your shader programs are being loaded?

    Try using the addShaderFromSourceCode() version (defining your shader programs as static char * strings - as in the Qt Creator OpenGLWindow example program) and see if you get something different.

    At worst, start with the example program and modify it to your needs.

Similar Threads

  1. Fail to generate a triangle by openGL(Qt5.0.2)
    By stereoMatching in forum Newbie
    Replies: 5
    Last Post: 23rd June 2013, 13:46
  2. Replies: 2
    Last Post: 7th February 2011, 17:19
  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.