Results 1 to 1 of 1

Thread: QGraphicsView, OpenGL, Buffer Objects

  1. #1
    Join Date
    Jan 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView, OpenGL, Buffer Objects

    QGraphicsItem has a code to render itself using OpenGL. When I use Buffer Objects and add simple QGraphicsItem program simetimes crashes, sometimes not.
    main.h:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "myQGraphicsView.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. myQGraphicsView w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    myQGraphicsView:
    Qt Code:
    1. #ifndef MYQGRAPHICSVIEW_H_
    2. #define MYQGRAPHICSVIEW_H_
    3.  
    4. #include <QGLWidget>
    5. #include <QGraphicsView>
    6. #include <QResizeEvent>
    7.  
    8. class itemWrap;
    9. class myQGraphicsView: public QGraphicsView
    10. {
    11. friend itemWrap;
    12. public:
    13. myQGraphicsView(QWidget *parent = 0);
    14. ~myQGraphicsView();
    15.  
    16. private:
    17. void updateMap();
    18.  
    19. GLuint vbo;
    20. };
    21. #endif
    To copy to clipboard, switch view to plain text mode 
    myQGraphicsView.cpp:
    Qt Code:
    1. #include "myQGraphicsView.h"
    2. #include <QGraphicsRectItem>
    3. #include <QPointer>
    4.  
    5. #include <gl/GL.h>
    6. #include <gl/glext.h>
    7.  
    8. PFNGLGENBUFFERSPROC glGenBuffers = 0;
    9. PFNGLBINDBUFFERPROC glBindBuffer = 0;
    10. PFNGLBUFFERDATAPROC glBufferData = 0;
    11. PFNGLDELETEBUFFERSPROC glDeleteBuffers = 0;
    12.  
    13. GLfloat vboVerticies2[4 * 3] =
    14. {
    15. -1.0, -1.0, 0.0,
    16. 0.0, -1.0, 0.0,
    17. -1.0, 0.0, 0.0,
    18. 0.0, 0.0, 0.0
    19. };
    20.  
    21. class itemWrap: public QGraphicsRectItem
    22. {
    23. QPointer<myQGraphicsView> view_;
    24. public:
    25. itemWrap(myQGraphicsView * obj){view_ = obj;}
    26. ~itemWrap(){};
    27. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /* = 0 */)
    28. {
    29. GLint proj[16];
    30. GLfloat model[16];
    31. glMatrixMode(GL_PROJECTION);
    32. glLoadIdentity();
    33. glMatrixMode(GL_MODELVIEW);
    34. glLoadIdentity();
    35. glGetIntegerv(GL_PROJECTION_MATRIX,proj);
    36. glGetFloatv(GL_MODELVIEW_MATRIX,model);
    37.  
    38. if(!view_.isNull())
    39. {
    40. painter->beginNativePainting();
    41. view_->updateMap();
    42. painter->endNativePainting();
    43. }
    44. }
    45. };
    46.  
    47.  
    48. myQGraphicsView::myQGraphicsView( QWidget *parent /*= 0*/ )
    49. :QGraphicsView(parent)
    50. {
    51. setViewport(new QGLWidget);
    52. setScene(new QGraphicsScene(QRect(0,0,100,100), this));
    53. QGraphicsRectItem * ri = new itemWrap(this);
    54. ri->setRect(0,0, 100, 100);
    55. ri->setBrush(QBrush(QColor(0,0,0,0)));
    56. scene()->addItem(ri);
    57.  
    58. ri = new QGraphicsRectItem();
    59. ri->setRect(50, 50, 100, 100);
    60. ri->setBrush(QBrush(QColor(Qt::red)));
    61. ri->setZValue(10);
    62. scene()->addItem(ri);
    63.  
    64. }
    65.  
    66. myQGraphicsView::~myQGraphicsView()
    67. {
    68. glDeleteBuffers(1,&vbo);
    69. }
    70.  
    71. void myQGraphicsView::updateMap()
    72. {
    73. if (!glGenBuffers)
    74. {
    75. glGenBuffers = (PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers");
    76. glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
    77. glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
    78. glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)wglGetProcAddress("glDeleteBuffers");
    79.  
    80. glEnableClientState(GL_VERTEX_ARRAY);
    81. glVertexPointer(3,GL_FLOAT,0,0);
    82. glGenBuffers(1,&vbo);
    83. glBindBuffer(GL_ARRAY_BUFFER,vbo);
    84. //glBufferData(GL_ARRAY_BUFFER,12*sizeof(GLfloat),vboVerticies2,GL_STATIC_DRAW);
    85. glVertexPointer(3,GL_FLOAT,0,0);
    86. }
    87.  
    88. GLuint glErr = glGetError();
    89. char* gluErrStr = (char*)gluErrorString(glErr);
    90.  
    91. glMatrixMode(GL_PROJECTION);
    92. glLoadIdentity();
    93. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    94. glMatrixMode(GL_MODELVIEW);
    95. glLoadIdentity();
    96.  
    97. /*glColor3f(1.0,0.5,0.0);
    98. glBindBuffer(GL_ARRAY_BUFFER,vbo);
    99. glVertexPointer(3,GL_FLOAT,0,0);
    100. glDrawArrays(GL_TRIANGLE_STRIP,0,4);*/
    101.  
    102. glBegin(GL_TRIANGLES);
    103. glVertex3f( 0.1,-0.1,0.0);
    104. glVertex3f( 0.5, 0.2,0.0);
    105. glVertex3f( 0.4, 1.0,0.0);
    106. glEnd();
    107. }
    To copy to clipboard, switch view to plain text mode 
    If I uncomment lines in myQGraphicsView.cpp:
    Qt Code:
    1. glBufferData(GL_ARRAY_BUFFER,12*sizeof(GLfloat),vboVerticies2,GL_STATIC_DRAW);
    2. ....
    3. glColor3f(1.0,0.5,0.0);
    4. glBindBuffer(GL_ARRAY_BUFFER,vbo);
    5. glVertexPointer(3,GL_FLOAT,0,0);
    6. glDrawArrays(GL_TRIANGLE_STRIP,0,4);
    To copy to clipboard, switch view to plain text mode 
    almost everytime program crashes.
    Is it bug or I made something wrong?

    p.s. sources in attachment too


    Added after 4 minutes:


    Under Debugger it crashes in qpaintengineex_opengl2.cpp:
    Qt Code:
    1. void QGL2PaintEngineExPrivate::composite(const QGLRect& boundingRect)
    2. {
    3. setCoords(staticVertexCoordinateArray, boundingRect);
    4. setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray);
    5. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    6. }
    To copy to clipboard, switch view to plain text mode 
    on line
    Qt Code:
    1. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by klou; 20th January 2011 at 06:03.

Similar Threads

  1. Replies: 2
    Last Post: 30th December 2010, 17:03
  2. OpenGL Vertex Buffer Array/Object
    By Denarius in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2010, 08:59
  3. Replies: 1
    Last Post: 9th November 2009, 14:16
  4. Qt Opengl, Changing objects color
    By bod in forum Qt Programming
    Replies: 4
    Last Post: 2nd July 2008, 13:13
  5. OpenGL show image from buffer memory leak[SOLVED]
    By ^NyAw^ in forum Qt Programming
    Replies: 0
    Last Post: 30th January 2008, 16:21

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.