Commit d45008a8 authored by Maciej Suminski's avatar Maciej Suminski
Browse files

Different way of rendering groups (with a single DrawElements call) in OpenGL GAL.

parent 7a8e1fc6
Loading
Loading
Loading
Loading
+45 −38
Original line number Original line Diff line number Diff line
@@ -411,6 +411,10 @@ void OPENGL_GAL::BeginDrawing()
    // If any of VBO items is dirty - recache everything
    // If any of VBO items is dirty - recache everything
    if( vboNeedsUpdate )
    if( vboNeedsUpdate )
        rebuildVbo();
        rebuildVbo();

    // Clear indices buffer
    itemsToDraw.clear();
    itemsToDrawSize = 0;
}
}




@@ -463,6 +467,41 @@ void OPENGL_GAL::blitMainTexture( bool aIsClearFrameBuffer )


void OPENGL_GAL::EndDrawing()
void OPENGL_GAL::EndDrawing()
{
{
    // TODO Checking if we are using right VBOs, in other case do the binding.
    // Right now there is only one VBO, so there is no problem.

    // Prepare buffers
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );

    // Bind vertices data buffer and point to the data
    glBindBuffer( GL_ARRAY_BUFFER, curVboVertId );
    glVertexPointer( 3, GL_FLOAT, VBO_ITEM::VertSize, 0 );
    glColorPointer( 4, GL_FLOAT, VBO_ITEM::VertSize, (GLvoid*) VBO_ITEM::ColorByteOffset );

    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, curVboIndId );
    GLuint* indicesPtr = static_cast<GLuint*>( glMapBuffer( GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY ) );

    wxASSERT_MSG( indicesPtr != NULL, "OPENGL_GAL::EndDrawing: Could not map GPU memory" );

    std::list<VBO_ITEM*>::const_iterator it, end;
    for( it = itemsToDraw.begin(), end = itemsToDraw.end(); it != end; ++it )
    {
        memcpy( indicesPtr, (*it)->GetIndices(), (*it)->GetSize() * VBO_ITEM::IndSize );
        indicesPtr += (*it)->GetSize() * VBO_ITEM::IndStride;
    }

    bool result = glUnmapBuffer( GL_ELEMENT_ARRAY_BUFFER );
    wxASSERT_MSG( result == TRUE, "OPENGL_GAL::EndDrawing: Unmapping indices buffer failed" );

    glDrawElements( GL_TRIANGLES, itemsToDrawSize, GL_UNSIGNED_INT, (GLvoid*) 0 );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );

    // Deactivate vertex array
    glDisableClientState( GL_COLOR_ARRAY );
    glDisableClientState( GL_VERTEX_ARRAY );

    // Draw the remaining contents, blit the main texture to the screen, swap the buffers
    // Draw the remaining contents, blit the main texture to the screen, swap the buffers
    glFlush();
    glFlush();
    blitMainTexture( true );
    blitMainTexture( true );
@@ -487,13 +526,11 @@ void OPENGL_GAL::rebuildVbo()
    prof_start( &totalTime, false );
    prof_start( &totalTime, false );
#endif /* __WXDEBUG__ */
#endif /* __WXDEBUG__ */


    // Buffers for storing cached items data
    // Buffer for storing cached items data
    GLfloat* verticesBuffer = new GLfloat[VBO_ITEM::VertStride * vboSize];
    GLfloat* verticesBuffer = new GLfloat[VBO_ITEM::VertStride * vboSize];
    GLuint*  indicesBuffer  = new GLuint[vboSize];


    // Pointers for easier usage with memcpy
    // Pointer for easier usage with memcpy
    GLfloat* verticesBufferPtr = verticesBuffer;
    GLfloat* verticesBufferPtr = verticesBuffer;
    GLuint*  indicesBufferPtr  = indicesBuffer;


    // Fill out buffers with data
    // Fill out buffers with data
    for( std::deque<VBO_ITEM*>::iterator vboItem = vboItems.begin();
    for( std::deque<VBO_ITEM*>::iterator vboItem = vboItems.begin();
@@ -503,9 +540,6 @@ void OPENGL_GAL::rebuildVbo()


        memcpy( verticesBufferPtr, (*vboItem)->GetVertices(), size * VBO_ITEM::VertSize );
        memcpy( verticesBufferPtr, (*vboItem)->GetVertices(), size * VBO_ITEM::VertSize );
        verticesBufferPtr += size * VBO_ITEM::VertStride;
        verticesBufferPtr += size * VBO_ITEM::VertStride;

        memcpy( indicesBufferPtr, (*vboItem)->GetIndices(), size * VBO_ITEM::IndSize );
        indicesBufferPtr += size * VBO_ITEM::IndStride;
    }
    }


    // Upload vertices coordinates and indices to GPU memory
    // Upload vertices coordinates and indices to GPU memory
@@ -513,15 +547,12 @@ void OPENGL_GAL::rebuildVbo()
    glBufferData( GL_ARRAY_BUFFER, vboSize * VBO_ITEM::VertSize, verticesBuffer, GL_DYNAMIC_DRAW );
    glBufferData( GL_ARRAY_BUFFER, vboSize * VBO_ITEM::VertSize, verticesBuffer, GL_DYNAMIC_DRAW );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );


    // Allocate the biggest possible buffer for indices
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, curVboIndId );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, curVboIndId );
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, vboSize * VBO_ITEM::IndSize,
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, vboSize * VBO_ITEM::IndSize, NULL, GL_STREAM_DRAW );
                  indicesBuffer, GL_DYNAMIC_DRAW );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );


    // Remove temporary buffers
    delete[] verticesBuffer;
    delete[] verticesBuffer;
    delete[] indicesBuffer;

    vboNeedsUpdate = false;
    vboNeedsUpdate = false;


#ifdef __WXDEBUG__
#ifdef __WXDEBUG__
@@ -1610,32 +1641,8 @@ void OPENGL_GAL::DeleteGroup( int aGroupNumber )


void OPENGL_GAL::DrawGroup( int aGroupNumber )
void OPENGL_GAL::DrawGroup( int aGroupNumber )
{
{
    std::deque<VBO_ITEM*>::iterator it = vboItems.begin();
    itemsToDraw.push_back( vboItems[aGroupNumber] );
    std::advance( it, aGroupNumber );
    itemsToDrawSize += vboItems[aGroupNumber]->GetSize();

    // TODO Checking if we are using right VBOs, in other case do the binding.
    // Right now there is only one VBO, so there is no problem.

    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );

    // Bind vertices data buffer and point to the data
    glBindBuffer( GL_ARRAY_BUFFER, curVboVertId );
    glVertexPointer( 3, GL_FLOAT, VBO_ITEM::VertSize, 0 );
    glColorPointer( 4, GL_FLOAT, VBO_ITEM::VertSize, (GLvoid*) VBO_ITEM::ColorByteOffset );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );

    // Bind indices data buffer
    int size = (*it)->GetSize();
    int offset = (*it)->GetOffset();
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, curVboIndId );
    glDrawRangeElements( GL_TRIANGLES, 0, vboSize - 1, size,
                         GL_UNSIGNED_INT, (GLvoid*) ( offset * VBO_ITEM::IndSize ) );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );

    // Deactivate vertex array
    glDisableClientState( GL_COLOR_ARRAY );
    glDisableClientState( GL_VERTEX_ARRAY );
}
}




+3 −0
Original line number Original line Diff line number Diff line
@@ -353,6 +353,9 @@ private:
    bool                  vboNeedsUpdate;         ///< Flag indicating if VBO should be rebuilt
    bool                  vboNeedsUpdate;         ///< Flag indicating if VBO should be rebuilt
    glm::mat4             transform;              ///< Current transformation matrix
    glm::mat4             transform;              ///< Current transformation matrix
    std::stack<glm::mat4> transformStack;         ///< Stack of transformation matrices
    std::stack<glm::mat4> transformStack;         ///< Stack of transformation matrices
    std::list<VBO_ITEM*>  itemsToDraw;            ///< Stores items that are going to be
                                                  ///< drawn in the current frame
    int                   itemsToDrawSize;        ///< Number of indices to be drawn


    double                curvePoints[12];        ///< Coefficients for curves
    double                curvePoints[12];        ///< Coefficients for curves
    // FIXME to be removed:
    // FIXME to be removed:
+2 −2
Original line number Original line Diff line number Diff line
@@ -126,8 +126,8 @@ public:
    //int  GetVbo() const;
    //int  GetVbo() const;


    ///< Data organization information for vertices {X,Y,Z,R,G,B,A}.
    ///< Data organization information for vertices {X,Y,Z,R,G,B,A}.
    // Each vertex consists of 7 floats
    // Each vertex consists of 7 floats, but it is padded to 8
    static const int VertStride         = 7;
    static const int VertStride         = 8;
    static const int VertSize           = VertStride * sizeof(GLfloat);
    static const int VertSize           = VertStride * sizeof(GLfloat);


    static const int CoordStride        = 3;
    static const int CoordStride        = 3;