Results 1 to 11 of 11

Thread: qt opengl coloring cube example-vbo edited

  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 qt opengl coloring cube example-vbo edited

    To write a vbo sample I edited the qt opengl coloring cube example. What I did is to create a vao and two vbos for vertices and colors variables. Unfortunately, when program runs, it doesn't render anything. Attached file is the program, as I am only using Linux, you may not worry about any virus detections.
    I would appreciate if you run this example on your system and try if you can make it give the desired output.
    Attached Files Attached Files

  2. #2
    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: qt opengl coloring cube example-vbo edited

    You are not linking the buffers to the shader program you are using anywhere. Are you sure this is just a modified version of an example?
    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.


  3. #3
    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: qt opengl coloring cube example-vbo edited

    Quote Originally Posted by wysota View Post
    You are not linking the buffers to the shader program you are using anywhere.
    I changed the coloring example and added vbo to it. I do not understand your statement. I already linked vertices and colors variables to vertex and color respectively in the shader program. I do not understand where I am doing wrong.
    could get more into the details please.

  4. #4
    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: qt opengl coloring cube example-vbo edited

    Quote Originally Posted by saman_artorious View Post
    I already linked vertices and colors variables to vertex and color respectively in the shader program.
    Where exactly did you do that? Which file, which line?
    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.


  5. #5
    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: qt opengl coloring cube example-vbo edited

    Please check the file
    glwidget.cpp
    .
    The linking of shader programs is happening inside
    initializeGL()
    Rendering and initialization(if not initialized)is done in the
    paintGL()
    function.

    Qt Code:
    1. if(!initialized)
    2. {
    3. GLuint vertexId = shaderProgram.attributeLocation("vertex");
    4.  
    5. GLuint colorId = shaderProgram.attributeLocation("color");
    6.  
    7. glGenVertexArrays(1, &VAO_ID);
    8. glBindVertexArray(VAO_ID);
    9.  
    10. glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]);
    11. glVertexAttribPointer(vertexId, 3, GL_FLOAT, GL_FALSE, sizeof(vertices), 0);
    12. glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*vertices.size(), vertices.constData(), GL_STATIC_DRAW);
    13.  
    14. glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[1]);
    15. glVertexAttribPointer(colorId, 3, GL_FLOAT, GL_FALSE, sizeof(colors), 0);
    16. glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*colors.size(), colors.constData(), GL_STATIC_DRAW);
    17.  
    18. initialized = true;
    19. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: qt opengl coloring cube example-vbo edited

    Again, which line in your opinion binds the VBO to the shader? Because none of what you posted in this post does. This code only fills the buffer.
    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.


  7. #7
    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: qt opengl coloring cube example-vbo edited

    Quote Originally Posted by wysota View Post
    Again, which line in your opinion binds the VBO to the shader? Because none of what you posted in this post does. This code only fills the buffer.
    Here:
    Qt Code:
    1. int vertexLocation = shaderProgram->attributeLocation("vertex");
    2. shaderProgram->enableAttributeArray(vertexLocation);
    3. int colorLocation = shaderProgram->attributeLocation("color");
    4. shaderProgram->enableAttributeArray(colorLocation);
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: qt opengl coloring cube example-vbo edited

    I think you forgot to read the docs for "enableAttributeArray". This only tells the shader to expect an array instead of a single value. It does not tell it what data to assign to this array. I already told you in the other thread a couple of days ago which function you need to use. Now RTFM for enableAttributeArray and read the other thread again.
    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.


  9. #9
    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: qt opengl coloring cube example-vbo edited

    I added the two lines concerning setAttributeBuffer() to my code. However, it gives me segmentation fault when after glDrawArray() is called. Though it executes fine with
    Qt Code:
    1. glDrawElements(GL_TRIANGLES, vertices.size(), GL_FLOAT, vertices.constData());
    To copy to clipboard, switch view to plain text mode 
    but does not render anything on the screen.

    Qt Code:
    1. shaderProgram->setAttributeBuffer(vertexLocation, GL_FLOAT , 0, 3, 0);
    2. shaderProgram->setAttributeBuffer(colorLocation , GL_FLOAT , 0, 1, 0);
    To copy to clipboard, switch view to plain text mode 

    whole render part:
    Qt Code:
    1. if(!initialized)
    2. {
    3. GLuint vertexId = shaderProgram->attributeLocation("vertex");
    4.  
    5. GLuint colorId = shaderProgram->attributeLocation("color");
    6.  
    7. glGenBuffers(2, VBO_ID);
    8. glGenVertexArrays(1, &VAO_ID);
    9. glBindVertexArray(VAO_ID);
    10.  
    11. glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]);
    12. glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*vertices.size(), vertices.constData(), GL_STATIC_DRAW);
    13. glVertexAttribPointer(vertexId, 3, GL_FLOAT, GL_FALSE, 0, 0);// param - 1 ->0
    14. glEnableVertexAttribArray(vertexId);
    15.  
    16. glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[1]);
    17. glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), colors, GL_DYNAMIC_DRAW);
    18. glVertexAttribPointer(colorId, 1, GL_FLOAT, GL_FALSE, 0, 0);
    19. glEnableVertexAttribArray(colorId);
    20.  
    21. glBindBuffer(GL_ARRAY_BUFFER, 0);
    22. }
    23.  
    24. glBindVertexArray(VAO_ID);
    25.  
    26. int vertexLocation = shaderProgram->attributeLocation("vertex");
    27. int colorLocation = shaderProgram->attributeLocation("color");
    28.  
    29. shaderProgram->enableAttributeArray(vertexLocation);
    30. shaderProgram->enableAttributeArray(colorLocation);
    31.  
    32. shaderProgram->setAttributeBuffer(vertexLocation, GL_FLOAT , 0, 3, 0);
    33. shaderProgram->setAttributeBuffer(colorLocation , GL_FLOAT , 0, 1, 0);
    34.  
    35. glEnable (GL_BLEND);
    36. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    37. glDrawArrays(GL_TRIANGLES, 0, numVerts);
    To copy to clipboard, switch view to plain text mode 

    could you please run this code on your system, I don't know exactly what I am missing. Although, I added setArrtibuteBuffers.
    Last edited by saman_artorious; 1st July 2013 at 13:36.

  10. #10
    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: qt opengl coloring cube example-vbo edited

    Make sure you are setting up the buffers correctly. I would also advise to use Qt classes for VBO manipulation.
    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.


  11. #11
    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: qt opengl coloring cube example-vbo edited

    Quote Originally Posted by wysota View Post
    Make sure you are setting up the buffers correctly. I would also advise to use Qt classes for VBO manipulation.
    Thanks for your guidance. I solved the problem. The correct code for what I aimed for is:
    Qt Code:
    1. if(!initialized)
    2. {
    3. GLuint vertexId = shaderProgram->attributeLocation("vertex");
    4. GLuint colorId = shaderProgram->attributeLocation("color");
    5.  
    6. glGenBuffers(2, VBO_ID);
    7.  
    8. glGenVertexArrays(1, &VAO_ID);
    9. glBindVertexArray(VAO_ID);
    10.  
    11. glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]);
    12. glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*verticesSize, vertices.constData(), GL_STATIC_DRAW);
    13. glVertexAttribPointer(vertexId, 3, GL_FLOAT, GL_FALSE, 0, 0);// param - 1 ->0
    14. glEnableVertexAttribArray(vertexId);
    15.  
    16. glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[1]);
    17. glBufferData(GL_ARRAY_BUFFER, sizeof(float)*verticesSize, colors, GL_STATIC_DRAW);
    18. glVertexAttribPointer(colorId, 1, GL_FLOAT, GL_FALSE, 0, 0);
    19. glEnableVertexAttribArray(colorId);
    20.  
    21. glBindBuffer(GL_ARRAY_BUFFER, 0);
    22.  
    23. vertices.clear();
    24.  
    25. free(colors);
    26.  
    27. initialized = true;
    28. }
    29.  
    30. glBindVertexArray(VAO_ID);
    31.  
    32. int vertexLocation = shaderProgram->attributeLocation("vertex");
    33. int colorLocation = shaderProgram->attributeLocation("color");
    34.  
    35. shaderProgram->enableAttributeArray(vertexLocation);
    36. shaderProgram->enableAttributeArray(colorLocation);
    37.  
    38. glEnableClientState(GL_VERTEX_ARRAY);
    39.  
    40. glEnable (GL_BLEND);
    41. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    42. glDrawArrays(GL_TRIANGLES, 0, 6*numVerts);
    43.  
    44. glFlush();
    45.  
    46. glBindVertexArray(0);
    47.  
    48. glDisableVertexAttribArray(0);
    49. glDisableVertexAttribArray(1);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Drawing a cube
    By qt_developer in forum Newbie
    Replies: 4
    Last Post: 30th May 2012, 10:59
  2. Drawing a cube
    By qt_developer in forum Newbie
    Replies: 1
    Last Post: 29th May 2012, 00:04
  3. How to design a 3D cube by using QPqinter?
    By anupam in forum General Programming
    Replies: 6
    Last Post: 12th April 2011, 12:17
  4. cube with widgets
    By mkind in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2011, 06:51
  5. how to redraw cube to rectangle
    By wagmare in forum Qt Programming
    Replies: 6
    Last Post: 11th February 2009, 05:26

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.