Results 1 to 7 of 7

Thread: QGLBuffer VBO implementation

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QGLBuffer VBO implementation

    I want to transfer vertices & colors variables in my drawing GPU. Unfortunately, it cannot render when the program runs.

    definitions is as follow:
    Qt Code:
    1. QVector<QVector3D> vertices;
    2. float* colors;
    3.  
    4. QGLBuffer* m_bufferData;
    5. QGLBuffer* m_colorData;
    6. float* m_color;
    To copy to clipboard, switch view to plain text mode 
    Here is what I do iniside initializeGL(), As vertices is built once and used forever, I do not allocate and map it. However, colors change regularly in the code, so I mapped colors to m_color in the code.
    Qt Code:
    1. void PlanPositionIndicator::QtVBO()
    2. {
    3. m_bufferData = new QGLBuffer(QGLBuffer::VertexBuffer);
    4. m_bufferData->create();
    5. m_bufferData->bind();
    6. m_bufferData->setUsagePattern(QGLBuffer::DynamicDraw);
    7. m_bufferData->allocate(6*sizeof(float)* ANGLE_COUNT*RANGE_COUNT);
    8. m_bufferData->release();
    9. //m_data = (QVector<QVector3D>*)m_bufferData->map (QGLBuffer::ReadWrite);
    10.  
    11.  
    12. m_colorData = new QGLBuffer(QGLBuffer::VertexBuffer);
    13. m_colorData->create();
    14. m_colorData->bind();
    15. m_colorData->setUsagePattern(QGLBuffer::DynamicDraw);
    16. m_colorData->allocate(6*sizeof(float)* ANGLE_COUNT*RANGE_COUNT);
    17. m_colorData->release();
    18. m_color = (float*)m_colorData->map(QGLBuffer::ReadWrite);
    19. }
    To copy to clipboard, switch view to plain text mode 

    So far so good, next, in the paintGL(), after setting up matrices and binging the shader program, I call the render function to render my drawing as following:
    Qt Code:
    1. void PlanPositionIndicator::render(QGLShaderProgram* shaderProgram)
    2. {
    3. glEnableClientState(GL_VERTEX_ARRAY);
    4. if (m_bufferData->bind ()) {
    5.  
    6. m_bufferData->write(0,vertices.constData(),sizeof(vertices));
    7.  
    8. glEnableClientState(GL_VERTEX_ARRAY);
    9. glVertexPointer( 3, GL_FLOAT, 0, &vertices);
    10.  
    11. shaderProgram->setAttributeArray("vertex", vertices.constData());
    12. shaderProgram->enableAttributeArray("vertex");
    13. shaderProgram->setAttributeArray("color", GL_FLOAT,colors,1);
    14.  
    15. shaderProgram->enableAttributeArray("color");
    16. glEnable (GL_BLEND);
    17. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    18. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    19.  
    20. glDisableClientState(GL_VERTEX_ARRAY);
    21. m_bufferData->release();
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    Finally, I attempt to change values of colors array. Firstly I did it with map() & unmap() but it gave me segmentation fault. Next, I used write(), it runs well, but does not render anything.

    Qt Code:
    1. for(int i = 0; i < RANGE_COUNT; ++i)
    2. {
    3. float rnd = ((rand() + segmentIndex) % 10000) / 10000.0f;
    4.  
    5. QGLBuffer* glBuffer = ppi->Get_mColorData();
    6. float* m_color = ppi->Get_mColor();
    7.  
    8. // m_color = (float*)glBuffer->map (QGLBuffer::ReadWrite);
    9.  
    10. float a[5];
    11. glBuffer->write(0,m_color,100);
    12.  
    13. // m_color[k++] = rnd;
    14. // m_color[k++] = rnd;
    15. // m_color[k++] = rnd;
    16. // m_color[k++] = rnd;
    17. // m_color[k++] = rnd;
    18. // m_color[k++] = rnd;
    19.  
    20. // glBuffer->unmap ();
    21. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions would be appreciable.

  2. #2
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGLBuffer VBO implementation

    I still do not understand why the allocated mapped m_color buffer above gives me a segmentation. I am dubious if my approach is correct. Has anyone done this before?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGLBuffer VBO implementation

    Your drawing code doesn't use the buffers anywhere.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGLBuffer VBO implementation

    what you mean!?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGLBuffer VBO implementation

    I mean you create the buffers but don't use them anywhere. You are using the original QVector:

    Qt Code:
    1. shaderProgram->setAttributeArray("vertex", vertices.constData());
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGLBuffer VBO implementation

    Quote Originally Posted by wysota View Post
    I mean you create the buffers but don't use them anywhere.
    Here, I set up their relation:
    Qt Code:
    1. m_bufferData->write(0,vertices.constData(),sizeof(vertices));
    To copy to clipboard, switch view to plain text mode 

    And pass the original variable in the function you mentioned above.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGLBuffer VBO implementation

    No, here you copy data from the vector to the buffer. But then you use the vector again instead of the buffer. You should use setAttributeBuffer and not setAttributeArray.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Multiple VBO with QGLBuffer
    By brijesh in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2013, 17:25
  2. Using QGLBuffer and QGLWidget::renderText problem.
    By oleg_stepanenko@bk.ru in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2012, 10:59
  3. QGLBuffer instead of Vertex Buffer Object -> How To?
    By FlashMuller in forum Qt Programming
    Replies: 0
    Last Post: 18th November 2010, 11:41
  4. Regarding the implementation of QList
    By rachit.gupta in forum Qt Tools
    Replies: 1
    Last Post: 23rd September 2009, 12:41
  5. Alarm Implementation in Qt/X11
    By technofreek in forum Qt Programming
    Replies: 0
    Last Post: 7th January 2009, 06:30

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.