1

I've created a wrapped up QGLWidget class and am trying to create a basic sprite factory style class that accesses it. In my SpriteMachine (think sprite factory) class, I'm trying to do all of the sprite drawing using VBOs, and a glDrawElements call (in my code its just drawElements, accessing a wrapper function in the parent that calls glDrawElements). I get an access violation on drawElements and I can't figure out why.

I checked and I'm almost certain my vertex data is well formatted. Also, I'm calling initializeGlFunctions in the initializeGL method of the parent widget before any drawing happens so I don't think I improperly initialized everything. First, here is the code in the sprite class that does the drawing:

void Machine::sync(){
#define ADD_ATTRIBUTE_LOCATION(ATTRNAME, ASIZE, OFFSET)                                                                         \
    (                                                                                                                           \
        (attrLoc = flat_shader.attributeLocation(ATTRNAME)),                                                                    \
        (flat_shader.enableAttributeArray(attrLoc)),                                                                            \
        (parent->vertexAttribPointer(attrLoc, ASIZE, GL_FLOAT, GL_FALSE, sizeof(VertexAttribArray), (const void*) offset)),     \
        (offset += OFFSET),                                                                                                     \
        (void)0                                                                                                                 \
    )

    int i;
    int q;
    int oldSize;
    int size;
    int width;
    int height;
    qreal texInvW;
    qreal texInvH;
    qreal scaledCoords[4];
    GLuint vbo[2];
    quintptr offset;
    int attrLoc;
    QPair<SpriteProperties*, BoundingBox> spriteData;
    VertexAttribArray* verts;
    GLushort*          indices;

    parent->makeCurrent();

    glEnable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    offset = 0;

    flat_shader.bind();
    flat_shader.setUniformValue("mvp_matrix", parent->getOrtho());
    flat_shader.setUniformValue("texture", 0);
    ADD_ATTRIBUTE_LOCATION("a_position", 3, sizeof(float) * 3);
    ADD_ATTRIBUTE_LOCATION("a_texcoord", 2, sizeof(float) * 2);
    ADD_ATTRIBUTE_LOCATION("a_scale", 2, sizeof(float) * 2);
    ADD_ATTRIBUTE_LOCATION("a_scale_lo", 2, sizeof(float) * 2);
    ADD_ATTRIBUTE_LOCATION("a_scale_hi", 2, sizeof(float) * 2);

    parent->genBuffers(2, vbo);
    oldSize = 0;
    for(i = 0; i < spriteToSync.size(); i++){
        size = spriteToSync[i].size();
        if(size > 0){
            parent->bindTexture(atlasList[i]);

            texInvW = 1.0 / atlasList[i].width();
            texInvH = 1.0 / atlasList[i].height();
            if(size > oldSize){
                if(oldSize != 0){
                    delete[] verts;
                    delete[] indices;
                }
                verts   = new VertexAttribArray[4 * size];
                indices = new GLushort[4 * size];
            }
            for(q = 0; q < size; q++){
                spriteData = spriteToSync[i][q];
                width  = spriteData.first->location.x1 - spriteData.first->location.x0;
                height = spriteData.first->location.y1 - spriteData.first->location.y0;
                scaledCoords[0] = spriteData.first->location.x0 * texInvW;
                scaledCoords[1] = spriteData.first->location.y0 * texInvH;
                scaledCoords[2] = spriteData.first->location.x1 * texInvW;
                scaledCoords[3] = spriteData.first->location.y1 * texInvH;

                if(spriteData.first->flags & DRAW_TILED){
                    verts[q * 4 + 0].p = QVector3D(spriteData.second.x1, spriteData.second.y0,UNIFORM_DEPTH);
                    verts[q * 4 + 0].t = QVector2D(scaledCoords[2], scaledCoords[1]);
                    verts[q * 4 + 0].s = QVector2D(((qreal) (spriteData.second.x1 - spriteData.second.x0)) / width,
                                                   ((qreal) (spriteData.second.y1 - spriteData.second.y0)) / height);
                    verts[q * 4 + 0].s_lo = QVector2D(scaledCoords[0], scaledCoords[1]);
                    verts[q * 4 + 0].s_hi = QVector2D(scaledCoords[2] - scaledCoords[0], scaledCoords[3] - scaledCoords[1]);

                    verts[q * 4 + 1].p = QVector3D(spriteData.second.x0, spriteData.second.y0, UNIFORM_DEPTH);
                    verts[q * 4 + 1].t = QVector2D(scaledCoords[0], scaledCoords[1]);
                    verts[q * 4 + 1].s = verts[q * 4 + 0].s;
                    verts[q * 4 + 1].s_lo = verts[q * 4 + 0].s_lo;
                    verts[q * 4 + 1].s_hi = verts[q * 4 + 0].s_hi;

                    verts[q * 4 + 2].p = QVector3D(spriteData.second.x0, spriteData.second.y1, UNIFORM_DEPTH);
                    verts[q * 4 + 2].t = QVector2D(scaledCoords[0], scaledCoords[3]);
                    verts[q * 4 + 2].s = verts[q * 4 + 0].s;
                    verts[q * 4 + 2].s_lo = verts[q * 4 + 0].s_lo;
                    verts[q * 4 + 2].s_hi = verts[q * 4 + 0].s_hi;

                    verts[q * 4 + 3].p = QVector3D(spriteData.second.x1, spriteData.second.y1, UNIFORM_DEPTH);
                    verts[q * 4 + 3].t = QVector2D(scaledCoords[2], scaledCoords[3]);
                    verts[q * 4 + 3].s = verts[q * 4 + 0].s;
                    verts[q * 4 + 3].s_lo = verts[q * 4 + 0].s_lo;
                    verts[q * 4 + 3].s_hi = verts[q * 4 + 0].s_hi;
                }
                else{
                    verts[q * 4 + 0].p = QVector3D(spriteData.second.x0 + width, spriteData.second.y0,UNIFORM_DEPTH);
                    verts[q * 4 + 0].t = QVector2D(scaledCoords[2], scaledCoords[1]);
                    verts[q * 4 + 0].s = QVector2D(1, 1);
                    verts[q * 4 + 0].s_lo = QVector2D(scaledCoords[0], scaledCoords[1]);
                    verts[q * 4 + 0].s_hi = QVector2D(scaledCoords[2] - scaledCoords[0], scaledCoords[3] - scaledCoords[1]);

                    verts[q * 4 + 1].p = QVector3D(spriteData.second.x0, spriteData.second.y0, UNIFORM_DEPTH);
                    verts[q * 4 + 1].t = QVector2D(scaledCoords[0], scaledCoords[1]);
                    verts[q * 4 + 1].s = verts[q * 4 + 0].s;
                    verts[q * 4 + 1].s_lo = verts[q * 4 + 0].s_lo;
                    verts[q * 4 + 1].s_hi = verts[q * 4 + 0].s_hi;

                    verts[q * 4 + 2].p = QVector3D(spriteData.second.x0, spriteData.second.y0 + height, UNIFORM_DEPTH);
                    verts[q * 4 + 2].t = QVector2D(scaledCoords[0], scaledCoords[3]);
                    verts[q * 4 + 2].s = verts[q * 4 + 0].s;
                    verts[q * 4 + 2].s_lo = verts[q * 4 + 0].s_lo;
                    verts[q * 4 + 2].s_hi = verts[q * 4 + 0].s_hi;

                    verts[q * 4 + 3].p = QVector3D(spriteData.second.x0 + width, spriteData.second.y0 + height, UNIFORM_DEPTH);
                    verts[q * 4 + 3].t = QVector2D(scaledCoords[2], scaledCoords[3]);
                    verts[q * 4 + 3].s = verts[q * 4 + 0].s;
                    verts[q * 4 + 3].s_lo = verts[q * 4 + 0].s_lo;
                    verts[q * 4 + 3].s_hi = verts[q * 4 + 0].s_hi;
                }
                indices[q * 4 + 0] = q * 4 + 0;
                indices[q * 4 + 1] = q * 4 + 1;
                indices[q * 4 + 2] = q * 4 + 2;
                indices[q * 4 + 3] = q * 4 + 3;
            }

            parent->bindBuffer(GL_ARRAY_BUFFER, vbo[0]);
            parent->bufferData(GL_ARRAY_BUFFER, size * 4 * sizeof(VertexAttribArray), verts, GL_STATIC_DRAW);
            parent->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[1]);
            parent->bufferData(GL_ELEMENT_ARRAY_BUFFER, size * 4 * sizeof(GLushort), indices, GL_STATIC_DRAW);

            parent->drawElements(GL_QUADS, 4 * size, GL_UNSIGNED_SHORT, 0);

            oldSize = size;
            spriteToSync[i].clear();
        }
    }
    parent->deleteBuffers(2, vbo);
    if(oldSize > 0){
        delete[] verts;
        delete[] indices;
    }
}

Now here is the code that calls the sprite class:

void MyGLWidget::paintGL(){
    SpriteMachine::Machine spriteDraw(this);
    SpriteMachine::key     atlas;
    SpriteMachine::key     sprites[2];

    ortho.setToIdentity();
    ortho.ortho(0, width, height, 0, -1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    atlas = spriteDraw.loadAtlas(":/gfx/gfx/hnight.png");

    sprites[0] = spriteDraw.create(atlas, SpriteMachine::BoundingBox(208, 64, 239, 95));
    sprites[1] = spriteDraw.create(atlas, SpriteMachine::BoundingBox(192, 96, 207, 111));

    spriteDraw.draw(sprites[0], SpriteMachine::BoundingBox(10, 10, 0, 0));

    spriteDraw.sync();
}

I have verified that my vertex array is formatted correctly with floats, and that the shader is properly linked and loaded. I know a lot of the class structure here isn't visible, but is there anything glaringly wrong with my GL?

UPDATE

I do not in fact have any sort of glEnableClientState calls, I have similar code that worked where I never used them, is this pertinent?

3
  • You don't need glEnableClientState, glEnableVertexAttribArray will be sufficient. However, you should make sure that only the attribute arrays which you actually need are enabled.
    – derhass
    Mar 19, 2014 at 21:12
  • Oh ok. Thats a start at least! Mar 19, 2014 at 22:40
  • I should also mention that I recognize loading a file within paintGL is stupid, but its there right now just to see if the code would make it (which it did not). This file has been verified to load. Mar 19, 2014 at 23:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.