Results 1 to 2 of 2

Thread: Qt OpenGL (QOpenGLWidget) - Simple Triangle

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt OpenGL (QOpenGLWidget) - Simple Triangle

    As a newbee to QT+OpenGL using QOpenGLWidget, I am unable to color my triangle. Please find my code here, using QMainWindow for GUI ...

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

    Here the GUI - window

    Qt Code:
    1. // MainWindow.h
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. };
    22.  
    23. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Here implementation - file ...

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 


    Here is the widget rendering Opengl-Context.

    Qt Code:
    1. #ifndef OPENGLWIDGET_H
    2. #define OPENGLWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QOpenGLWidget>
    6. #include <QOpenGLFunctions>
    7. #include <QOpenGLContext>
    8. #include <QOpenGLShaderProgram>
    9. #include <QOpenGLBuffer>
    10. #include <QOpenGLVertexArrayObject>
    11. #include <QMatrix4x4>
    12.  
    13. class OpenglWidget : public QOpenGLWidget, public QOpenGLFunctions
    14. {
    15. public:
    16. OpenglWidget(QWidget *parent = 0);
    17. ~OpenglWidget();
    18.  
    19. protected:
    20. void initializeGL();
    21. void resizeGL(int width, int height);
    22. void paintGL();
    23. GLuint m_posAttr;
    24. GLuint m_colAttr;
    25. GLuint m_matrixUniform;
    26. QOpenGLShaderProgram *m_program;
    27. };
    28. #endif // OPENGLWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Here is the implementation file ...

    Qt Code:
    1. #include "openglwidget.h"
    2. OpenglWidget::OpenglWidget(QWidget *parent) :
    3. QOpenGLWidget(parent)
    4. {
    5. setFormat(QSurfaceFormat::defaultFormat());
    6. }
    7.  
    8. OpenglWidget::~OpenglWidget()
    9. {
    10. }
    11.  
    12. static const char *vertexShaderSource =
    13. "attribute highp vec4 posAttr;\n"
    14. "attribute lowp vec4 colAttr;\n"
    15. "varying lowp vec4 col;\n"
    16. "uniform highp mat4 matrix;\n"
    17. "void main() {\n"
    18. " col = vec4(1, 0, 0, 1);\n"
    19. " gl_Position = matrix * posAttr;\n"
    20. "}\n";
    21.  
    22. static const char *fragmentShaderSource =
    23. "varying lowp vec4 col;\n"
    24. "void main() {\n"
    25. " gl_FragColor = col;\n"
    26. "}\n";
    27.  
    28. void OpenglWidget::initializeGL()
    29. {
    30. makeCurrent();
    31. initializeOpenGLFunctions();
    32. glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    33.  
    34. // Create Shader (Do not release until VAO is created)
    35. m_program = new QOpenGLShaderProgram(this);
    36. m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
    37. m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
    38. m_program->link();
    39. m_posAttr = m_program->attributeLocation("posAttr");
    40. m_colAttr = m_program->attributeLocation("colAttr");
    41. m_matrixUniform = m_program->attributeLocation("matrix");
    42.  
    43. m_program->release();
    44. }
    45.  
    46. void OpenglWidget::paintGL()
    47. {
    48. glClear(GL_COLOR_BUFFER_BIT);
    49. makeCurrent();
    50.  
    51. //m_program->bind();
    52.  
    53. QMatrix4x4 matrix;
    54. matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
    55. matrix.translate(0, 0, -2);
    56.  
    57. m_program->setUniformValue(m_matrixUniform, matrix);
    58.  
    59. GLfloat vertices[] = {
    60. 0.0f, 0.707f, 0.0f,
    61. -0.5f, -0.5f, 0.0f,
    62. 0.5f, -0.5f, 0.0f
    63. };
    64.  
    65. GLfloat colors[] = {
    66. 1.0f, 0.0f, 0.0f,
    67. 0.0f, 1.0f, 0.0f,
    68. 0.0f, 0.0f, 1.0f
    69. };
    70.  
    71. glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    72. glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
    73.  
    74. glEnableVertexAttribArray(0);
    75. glEnableVertexAttribArray(1);
    76.  
    77. glDrawArrays(GL_TRIANGLES, 0, 3);
    78.  
    79. glDisableVertexAttribArray(1);
    80. glDisableVertexAttribArray(0);
    81.  
    82. m_program->release();
    83. }
    84.  
    85. void OpenglWidget::resizeGL(int width, int height)
    86. {
    87. glViewport(0, 0, width, height);
    88. }
    To copy to clipboard, switch view to plain text mode 

    Here the rendered triangle is just white. I am unable to understand whether the shader is compiled and attributes are linked but still, I am unable to color the triangle.

    If I can get any guidance... ?

    test_triangle.jpg
    Last edited by saket; 31st July 2016 at 17:53. Reason: updated contents

Similar Threads

  1. Qt opengl use shader to draw triangle
    By zhangzhisheng in forum Qt Programming
    Replies: 1
    Last Post: 7th May 2015, 15:57
  2. Fail to generate a triangle by openGL(Qt5.0.2)
    By stereoMatching in forum Newbie
    Replies: 5
    Last Post: 23rd June 2013, 12:46
  3. Replies: 2
    Last Post: 30th December 2010, 17:03
  4. Simple example for OpenGL ES that uses a perpective matrix for Maemo and Symbian
    By PierreChicoine in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 24th June 2010, 02:09

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.