Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

OpenGL in Qt 5.1 – Part 2

void Scene::initialize()
{
    // Assumes we have a current QOpenGLContext and that
    // m_shaderProgram is a QOpenGLShaderProgram

    // Create VAO for first object to render
    m_vao1 = new QOpenGLVertexArrayObject( this );
    m_vao1->create();
    m_vao1->bind();

    // Setup VBOs and IBO (use QOpenGLBuffer to buffer data,
    // specify format, usage hint etc). These will be
    // remembered by the currently bound VAO
    m_positionBuffer.create();
    m_positionBuffer.setUsagePattern( QOpenGLBuffer::StreamDraw );
    m_positionBuffer.bind();
    m_positionBuffer.allocate( positionData,
                               vertexCount * 3 * sizeof( float ) );
    m_shaderProgram.enableAttributeArray( "vertexPosition" );
    m_shaderProgram.setAttributeBuffer( "vertexPosition", GL_FLOAT, 0, 3 );

    m_colorBuffer.create();
    m_colorBuffer.setUsagePattern( QOpenGLBuffer::StaticDraw );
    m_colorBuffer.bind();
    m_colorBuffer.allocate( colorData,
                            vertexCount * 3 * sizeof( float ) );
    m_shaderProgram.enableAttributeArray( "vertexColor" );
    m_shaderProgram.setAttributeBuffer( "vertexColor", GL_FLOAT, 0, 3 );

    // Repeat for buffers of normals, texture coordinates,
    // tangents, ...
    ...

    // Create VAO for second object to render
    m_vao2 = new QOpenGLVertexArrayObject( this );
    m_vao2->create();
    m_vao2->bind();

    // Setup VBOs and IBO for next object
    ...

    // Rinse and repeat for other objects
    m_skyBoxVAO = new QOpenGLVertexArrayObject( this );
    ...
}

void Scene::render()
{
    // Clear buffers
    m_funcs->glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    // Bind shader program, textures for first set of objects
    m_phongShaderProgram->bind();
    ...

    // Switch to the vertex data for first object and draw it
    m_vao1->bind();
    m_funcs->glDrawElements(...);

    // Switch to the vertex data for second object and draw it
    m_vao2->bind();
    m_funcs->glDrawElements(...);

    // Maybe change shader program, textures etc
    // and draw other objects
    m_skyboxShaderProgram->bind();
    ...
    m_skyboxVAO->bind();
    m_funcs->glDrawElements(...);
    ...
}

About KDAB


20 Comments

15 - Mar - 2013

Joakim D

15 - Mar - 2013

Sean Harmer

15 - Mar - 2013

Carsten Neumann

15 - Mar - 2013

Sean Harmer

16 - Mar - 2013

Grumpy OpenGL

17 - Mar - 2013

Sean Harmer

16 - Mar - 2013

Grumpy OpenGL

 create() --> glGenVertexArrays(1, &m_id)
 bind() --> glBindVertexArray(m_id)
 destroy() --> glDeleteVertexArrays(1, &m_id)
 release() --> glBindVertexArray(0)
 isCreated() --> return m_id!=0

17 - Mar - 2013

Sean Harmer

17 - Mar - 2013

Grumpy OpenGL

17 - Mar - 2013

Sean Harmer

18 - Mar - 2013

noizex

17 - Mar - 2013

Christopher

27 - Jun - 2013

JL

24 - Nov - 2013

Michael

31 - Mar - 2014

lunnersword

26 - May - 2015

sidali

11 - Nov - 2015

RM

1 - Dec - 2015

Sean Harmer

1 - Dec - 2015

RM

2 - Dec - 2015

Sean Harmer

SeanHarmer

Sean Harmer

Managing Director KDAB UK