Thread: OpenGL and C++ - Interleaved VBO Memory Exception

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Milk and Beans
    Posts
    5

    OpenGL and C++ - Interleaved VBO Memory Exception

    Hi all,

    I'm currently working on a bit of homework ("Create any 2D scene you want".. How great is uni?), and to bump my marks up I need/would very much like to use a vertex array (have done heirarchical drawing, use of textures, alpha blending)

    In the project we were given, there is an interleaved array which holds the vx, vy, vr, vg, vb for a five point star, and this is drawn using (GL_LINE_LOOP). So, far, so good.

    I'm trying to create a vertex array, which interleaves vx, vy, and the texture coordinates for a rectangle. Sounds easy right? Textured rectangle from vertex array?

    The Error is occuring everytime on the line i've signed ~#:
    Unhandled exception at 0x10103a6c in GfxProject.exe: 0xC0000005: Access violation reading location 0x00000000.

    Okay before I chat more, here's the code:

    the global interleaved array:
    Code:
    /*    OpenGL Interleaved vertex array    */      //x         y      texcoords
    static GLfloat StageModel_interleaved [] ={ -4.0f, -4.0f,     0,0,
                                                               -4.0f, -0.04f,    0,1,
                                                                4.0f, -0.04f,    1,1,
                                                                  4.0f, -4.0f,    1,0}; 
    
    static GLubyte StageVertexIndices [] = {0, 1, 2, 3};
    In renderscene():

    Code:
    glPushMatrix();
    
            //drawing the staging.. the ground etc
            glVertexPointer(2, GL_FLOAT, (    (2*sizeof(GLfloat)) + (2*sizeof(GLuint))    ), StageModel_interleaved); 
            glTexCoordPointer(2, GL_UNSIGNED_INT, (    (2*sizeof(GLfloat)) + (2*sizeof(GLuint))    ), &(StageModel_interleaved[2]));    
            //size of tex index?!
                DrawStage_glDrawElements(1);
    glPopMatrix();
    and finally the actual draw function:

    Code:
    void DrawStage_glDrawElements(int texArrayIndex)
    {
        glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, textureLib[texArrayIndex]);
      ~#          glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, StageVertexIndices);
        glDisable(GL_TEXTURE_2D);
    }
    Please somebody help me: I know I've done something stupid I just can't see what!

    Many thanks C Board!
    Last edited by MyNameIsNathan; 01-01-2012 at 10:37 AM. Reason: I like tidy.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    StageVertexIndices isn't a pointer and glDrawElements expects one. What you actually pass is the VALUE. You should use "&StageVertexIndices" in your call.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Milk and Beans
    Posts
    5

    Thank you!

    I knew I'd done something silly! You sir are a G! Thank you ever so much!

    However I am still getting the same error..

    Unhandled exception at 0x10106ebf in GfxProject.exe: 0xC0000005: Access violation reading location 0x00000000.

    I guess its moved forward in memory since the other error. Perhaps now fiddling with the stride will help.

    If you've any more ideas that'd be really helpful: I've been using OpenGL only since late October.

    I'll be linking to this thread in my write up, for sure!

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by MyNameIsNathan View Post
    I knew I'd done something silly! You sir are a G! Thank you ever so much!

    However I am still getting the same error..
    No, he is wrong, and you are doing it right (you are passing a valid pointer). This code fragment seems to be correct, the error may be somewhere else.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    Milk and Beans
    Posts
    5

    indeed

    Quote Originally Posted by kmdv View Post
    No, he is wrong, and you are doing it right (you are passing a valid pointer). This code fragment seems to be correct, the error may be somewhere else.
    I looked back at the demo project we'd been given and the way the pointer was passed was the same, it must be the stride or the vertex array itself, in respect to the format of the texcoords:

    I've tried passing them as floats and using 4*sizeof(GLfloat), I've tried as ints and using 2*sizeof(GLfloat) + 2*sizeof(GLuint), and no difference. same error, slightly different memory location.

    I assume because its saying 'Access violation reading location 0x0000000' it doesn't know where to look, or the location doesn't exist.

    Any which way I am uber confused.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    That access violation means that you are passing a NULL pointer (or maybe something has not been initialized correctly inside opengl). Where and how is StageVertexIndeces declared? As kmdv said, the code you have posted looks good so the problem is most likely in some other parts.

  7. #7
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Along those same lines: make sure you're using glEnableClientState to turn on whichever arrays you need to use (ie, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY), and that you aren't enabling any arrays that don't get used in your scene.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exception handling ( memory allocation)
    By manzoor in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2008, 06:43 AM
  2. exception handling, function call and memory
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 01-30-2008, 08:00 AM
  3. OpenGL memory increases
    By IdunnO in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2006, 10:48 AM
  4. Loading an image into memory using OpenGL
    By Kaelin in forum C++ Programming
    Replies: 3
    Last Post: 02-08-2005, 12:03 PM
  5. memory allocation exception
    By Micko in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 01:53 PM

Tags for this Thread