Results 1 to 2 of 2

Thread: Cube OpenGL ES 2.0 example with QGraphicsScene

  1. #1
    Join Date
    Oct 2015
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Cube OpenGL ES 2.0 example with QGraphicsScene

    Hi,
    I saw https://doc.qt.io/archives/qq/qq26-openglcanvas.html and tried to alter the code of the example "Cube OpenGL ES 2.0" so it uses a QGraphicsView. I only get black screen. I'm on Ubuntu 14.04 / Qt5.3.2.

    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QGLWidget>
    3.  
    4. #include "scene.h"
    5. #include "view.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10. QGLWidget *widget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
    11. widget->setAutoFillBackground(false);
    12. widget->makeCurrent();
    13. Scene *scene= new Scene();
    14. View *mainView = new View();
    15. mainView->setViewport(widget);
    16. mainView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    17. mainView->setScene(scene);
    18. mainView->show();
    19. mainView->resize(800,800);
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    scene.cpp:
    Qt Code:
    1. #include "scene.h"
    2.  
    3. #include <QGraphicsScene>
    4. #include <QGraphicsSceneMouseEvent>
    5. #include <QTimer>
    6.  
    7. Scene::Scene() : angularSpeed(0)
    8. {
    9. initializeGLFunctions();
    10. glClearColor(0.0f,0.0f,0.0f,1.0f);
    11. initShaders();
    12. initTextures();
    13. glEnable(GL_DEPTH_TEST);
    14. glEnable(GL_CULL_FACE);
    15. geometries.init();
    16. timer.start(12, this);
    17. }
    18. void Scene::drawBackground(QPainter *painter, const QRectF &rect)
    19. {
    20. painter->beginNativePainting();
    21. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    22. QMatrix4x4 matrix;
    23. matrix.translate(0.0, 0.0, -5.0);
    24. matrix.rotate(rotation);
    25. program.setUniformValue("mvp_matrix", projection * matrix);
    26. program.setUniformValue("texture", 0);
    27. geometries.drawCubeGeometry(&program);
    28. }
    29. void Scene::initShaders()
    30. {
    31. program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl");
    32. program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl");
    33. program.link();
    34. program.bind();
    35. }
    36. void Scene::initTextures()
    37. {
    38. glEnable(GL_TEXTURE_2D);
    39. textureData = new QOpenGLTexture(QImage(":/cube.png"));
    40. textureData->bind();
    41. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    42. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    43. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    44. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    45. }
    46. void Scene::timerEvent(QTimerEvent *)
    47. {
    48. angularSpeed *= 0.99;
    49. if (angularSpeed < 0.01) {
    50. angularSpeed = 0.0;
    51. } else {
    52. rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
    53. update();
    54. }
    55. }
    56. void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    57. {
    58. mousePressPosition = QVector2D(event->scenePos());
    59. }
    60. void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    61. {
    62. QVector2D diff = QVector2D(e->scenePos()) - mousePressPosition;
    63. QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
    64. qreal acc = diff.length() / 100.0;
    65. rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
    66. angularSpeed += acc;
    67. }
    To copy to clipboard, switch view to plain text mode 

    view.h
    Qt Code:
    1. #include <QGraphicsView>
    2. #include <QResizeEvent>
    3. #include "geometryengine.h"
    4.  
    5.  
    6. class View : public QGraphicsView
    7. {
    8. Q_OBJECT
    9. protected:
    10. void resizeEvent(QResizeEvent *event){
    11. if (scene())
    12. scene()->setSceneRect(
    13. QRect(QPoint(0, 0), event->size()));
    14. QGraphicsView::resizeEvent(event);
    15. }
    16. public:
    17. View(){}
    18. };
    19.  
    20. #endif // VIEW_H
    To copy to clipboard, switch view to plain text mode 
    Any ideas where it went wrong?

  2. #2
    Join Date
    Oct 2015
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Cube OpenGL ES 2.0 example with QGraphicsScene

    - Projection matrices were missing.
    - Shader bindings must come after beginNativePainting()
    - Buffers must be unbounded in geometryengine.cpp ( glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);)

    Now program works as intended on ubuntu. When I try it on imx6 Sabre board (3.10.17 with eglfs) I got flicker (like two of the same image try to draw on another (one in smaller resolution, other in fullscreen)). Besides that, dice texture isn't shown (Black cube).

    shader.cpp
    Qt Code:
    1. #include "scene.h"
    2.  
    3. #include <QGraphicsScene>
    4. #include <QGraphicsSceneMouseEvent>
    5. #include <QTimer>
    6. #include <QVBoxLayout>
    7. #include <QLabel>
    8. #include <QGraphicsItem>
    9. #include <QRectF>
    10.  
    11. Scene::Scene() : angularSpeed(0)
    12. {
    13. QWidget *instructions = createDialog(tr("Instructions"));
    14. instructions->layout()->addWidget(new QLabel(tr("Use mouse wheel to zoom model, and click and drag to rotate model")));
    15. instructions->layout()->addWidget(new QLabel(tr("Move the sun around to change the light position")));
    16. addWidget(instructions);
    17.  
    18. QPointF pos(10, 10);
    19. foreach (QGraphicsItem *item, items()) {
    20. item->setFlag(QGraphicsItem::ItemIsMovable);
    21. item->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
    22.  
    23. const QRectF rect = item->boundingRect();
    24. item->setPos(pos.x() - rect.x(), pos.y() - rect.y());
    25. pos += QPointF(0, 10 + rect.height());
    26. }
    27.  
    28. uint32_t w = 800;
    29. uint32_t h = 800;
    30. //glViewport(0, 0, w, h);
    31. qreal aspect = qreal(w) / qreal(h ? h : 1);
    32. const qreal zNear = 1.0, zFar = 7.0, fov = 45.0;
    33. projection.setToIdentity();
    34. projection.perspective(fov, aspect, zNear, zFar);
    35. initShaders();
    36. //initTextures();
    37. textureData = new QOpenGLTexture(QImage(":/cube.png"));
    38. geometries.init();
    39. timer.start(12, this);
    40. }
    41. void Scene::drawBackground(QPainter *painter, const QRectF &rect)
    42. {
    43. painter->beginNativePainting();
    44. glEnable(GL_DEPTH_TEST);
    45. glEnable(GL_CULL_FACE);
    46. glClearColor(0.8f,0.8f,0.1f,1.0f);
    47. program.bind();
    48. textureData->bind();
    49. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    50. QMatrix4x4 matrix;
    51. matrix.translate(0.0, 0.0, -5.0);
    52. matrix.rotate(rotation);
    53. program.setUniformValue("mvp_matrix", projection * matrix);
    54. program.setUniformValue("texture", 0);
    55. geometries.drawCubeGeometry(&program);
    56. program.release();
    57. textureData->release();
    58. glDisable(GL_DEPTH_TEST);
    59. glDisable(GL_CULL_FACE);
    60. painter->endNativePainting();
    61. QTimer::singleShot(20, this, SLOT(update()));
    62. }
    63. void Scene::initShaders()
    64. {
    65. program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl");
    66. program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl");
    67. program.link();
    68. // program.bind();
    69. }
    70. void Scene::initTextures()
    71. {
    72. glEnable(GL_TEXTURE_2D);
    73.  
    74. // textureData->bind();
    75. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    76. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    77. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    78. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    79. }
    80. void Scene::timerEvent(QTimerEvent *)
    81. {
    82. angularSpeed *= 0.99;
    83. if (angularSpeed < 0.01) {
    84. angularSpeed = 0.0;
    85. } else {
    86. rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
    87. update();
    88. }
    89. }
    90. void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    91. {
    92. mousePressPosition = QVector2D(event->scenePos());
    93. }
    94. void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    95. {
    96. QVector2D diff = QVector2D(e->scenePos()) - mousePressPosition;
    97. QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
    98. qreal acc = diff.length() / 100.0;
    99. rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
    100. angularSpeed += acc;
    101. }
    102.  
    103. QDialog *Scene::createDialog(const QString &windowTitle) const
    104. {
    105. QDialog *dialog = new QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    106.  
    107. dialog->setWindowOpacity(0.8);
    108. dialog->setWindowTitle(windowTitle);
    109. dialog->setLayout(new QVBoxLayout);
    110.  
    111. return dialog;
    112. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jannn; 9th October 2015 at 12:32. Reason: updated contents

Similar Threads

  1. Replies: 0
    Last Post: 25th January 2015, 16:40
  2. qt opengl coloring cube example-vbo edited
    By saman_artorious in forum Qt Programming
    Replies: 10
    Last Post: 4th July 2013, 11:49
  3. Replies: 1
    Last Post: 9th November 2011, 19:34
  4. QGraphicsScene OpenGL rendering
    By nelisdnurste in forum Qt Programming
    Replies: 4
    Last Post: 9th October 2011, 11:02
  5. Using QGraphicsScene instead of OpenGL
    By ntp in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2008, 22:16

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.