• 10125阅读
  • 0回复

[转载]OpenGL in Qt 5.1 – Part 2 [复制链接]

上一主题 下一主题
离线XChinux
 

只看楼主 倒序阅读 楼主  发表于: 2013-03-21
关键词: Qt5OpenGL
原文见:http://www.kdab.com/opengl-in-qt-5-1-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=opengl-in-qt-5-1-part-2


OpenGL in Qt 5.1 – Part 2


Published: March 15, 2013 by Sean Harmer




Vertex Array Objects


Qt has QOpenGLBuffer (and before that QGLBuffer)to help manage various types of OpenGL buffer objects such asper-vertex attribute data and element index buffers. OpenGL also has asimple related container type call Vertex Array Objects (VAOs) to help with managing sets of vertex buffer objects.
KDAB has added code to Qt 5.1 that encapsulates VAOs with the QOpenGLVertexArrayObjectclass. Binding an instance of this class causes OpenGL to "remember"any vertex specification state that you then set up. We can laterrestore this vertex specification state very quickly by simplyre-binding the VAO itself. This allows us to very rapidly switch betweenvertex states for "objects" that we wish to draw in our renderingfunction:


  1. void Scene::initialize()
  2. {
  3.     // Assumes we have a current QOpenGLContext and that
  4.     // m_shaderProgram is a QOpenGLShaderProgram
  5.     // Create VAO for first object to render
  6.     m_vao1 = new QOpenGLVertexArrayObject( this );
  7.     m_vao1->create();
  8.     m_vao1->bind();
  9.     // Setup VBOs and IBO (use QOpenGLBuffer to buffer data,
  10.     // specify format, usage hint etc). These will be
  11.     // remembered by the currently bound VAO
  12.     m_positionBuffer.create();
  13.     m_positionBuffer.setUsagePattern( QOpenGLBuffer::StreamDraw );
  14.     m_positionBuffer.bind();
  15.     m_positionBuffer.allocate( positionData,
  16.                                vertexCount * 3 * sizeof( float ) );
  17.     m_shaderProgram.enableAttributeArray( "vertexPosition" );
  18.     m_shaderProgram.setAttributeBuffer( "vertexPosition", GL_FLOAT, 0, 3 );
  19.     m_colorBuffer.create();
  20.     m_colorBuffer.setUsagePattern( QOpenGLBuffer::StaticDraw );
  21.     m_colorBuffer.bind();
  22.     m_colorBuffer.allocate( colorData,
  23.                             vertexCount * 3 * sizeof( float ) );
  24.     m_shaderProgram.enableAttributeArray( "vertexColor" );
  25.     m_shaderProgram.setAttributeBuffer( "vertexColor", GL_FLOAT, 0, 3 );
  26.     // Repeat for buffers of normals, texture coordinates,
  27.     // tangents, ...
  28.     ...
  29.     // Create VAO for second object to render
  30.     m_vao2 = new QOpenGLVertexArrayObject( this );
  31.     m_vao2->create();
  32.     m_vao2->bind();
  33.     // Setup VBOs and IBO for next object
  34.     ...
  35.     // Rinse and repeat for other objects
  36.     m_skyBoxVAO = new QOpenGLVertexArrayObject( this );
  37.     ...
  38. }
  39. void Scene::render()
  40. {
  41.     // Clear buffers
  42.     m_funcs->glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  43.     // Bind shader program, textures for first set of objects
  44.     m_phongShaderProgram->bind();
  45.     ...
  46.     // Switch to the vertex data for first object and draw it
  47.     m_vao1->bind();
  48.     m_funcs->glDrawElements(...);
  49.     // Switch to the vertex data for second object and draw it
  50.     m_vao2->bind();
  51.     m_funcs->glDrawElements(...);
  52.     // Maybe change shader program, textures etc
  53.     // and draw other objects
  54.     m_skyboxShaderProgram->bind();
  55.     ...
  56.     m_skyboxVAO->bind();
  57.     m_funcs->glDrawElements(...);
  58.     ...
  59. }



VAOs were introduced with OpenGL 3 but are required for OpenGL 3.1and for OpenGL >=3.2 with the Core profile. In addition, VAOs areavailable via the GL_ARB_vertex_array_object orGL_OES_vertex_array_object extensions on OpenGL 2 and OpenGL ES 2respectively. The QOpenGLVertexArrayObject will use the core feature if available or fall-back to the appropriate extension if available.
The use of VAOs can greatly simplify your rendering code and make itfaster due to the OpenGL driver having to potentially do less sanitychecking than if doing more buffer operations.
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个