Jump to content
  • Advertisement
Sign in to follow this  
DavidJr

OpenGL glDrawElements Error in Debug Mode

This topic is 1228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to code using OpenGL using Vertex Array method for study purpose. I loaded the vertices, normals, triangles correctly but when I try to run my code in debug mode, it crashed and pointed to glDrawElements, it is said that reading access violation. I try to run it without debugger or change the build configuration to release the program just works fine. Is it okay?

Share this post


Link to post
Share on other sites
Advertisement

Is it okay?

Of course not.
Verify your parameters. You are likely drawing more vertices than are valid.
Why don’t you provide more information, such as the way in which you call the function, how many vertices are actually valid, etc.


L. Spiro

Share this post


Link to post
Share on other sites

Hi, Spiro, thanks for your reply. What do you mean by drawing vertices than are valid?

 

This is the way I call glDrawElements

    glPushMatrix();

    glTranslatef(0.0f, 0.0f, 0.0f);

    for (int i = 0; i < Group; ++i)
    {
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glVertexPointer(3, GL_FLOAT, sizeof (SVertex), &(pMesh[i].pVertex[0].Pos));
        glNormalPointer(GL_FLOAT, sizeof (SNormal), &(pMesh[i].pNormal[0].Pos));
        glTexCoordPointer(2, GL_FLOAT, sizeof (SVertex), &(pMesh[i].pVertex[0].UV));

        glBindTexture(GL_TEXTURE_2D, pMesh[i].TexID);
        glDrawElements(GL_TRIANGLES, pMesh[i].TriSize, GL_UNSIGNED_INT, &(pMesh[i].pTri[0]));

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }

    glPopMatrix();

I tried with 2 models, let's say Model1 and Model2.

When I try to parse and load Model1, the program works fine and the model is displayed. When I try with Model2 the program crashed. I got "Access violation reading location" error in the output.

Here is the "Autos" says:

Untitled.jpg

 

Could it be my loader function was wrong? I'm pretty sure read the file correctly.

Share this post


Link to post
Share on other sites

It is the number of Triangles * 3. If I don't times it with 3, the model will not display completely

Share this post


Link to post
Share on other sites

i am not sure if glDrawElements can work, because you do not set any index buffer anywhere, so basically opengl reads some random place in memory which causes access violation

Share this post


Link to post
Share on other sites
::glDrawElements() uses an IBO (index-buffer object) if the pointer passed to it is an offset from 0 (not a valid pointer).
You are passing a valid pointer (we assume) and so there are no IBO’s in this equation.

How many indices are in your index buffer?


L. Spiro

Share this post


Link to post
Share on other sites

i am not sure if glDrawElements can work, because you do not set any index buffer anywhere, so basically opengl reads some random place in memory which causes access violation

This is completely incorrect and useless; the requirement to have an index buffer is only valid in core GL 3+ contexts. Otherwise it is perfectly valid to use glDrawElements from system memory, which is what the OP is doing.

Share this post


Link to post
Share on other sites

L Spiro, Model1 has 3 meshes and each mesh has 211, 1043, and 8 indices. Model 2 has 4 meshes and each mesh has 190, 685, 902, and 990 indices. They all stored in TriTotal.

 

This is the way I load the Triangles and the program crashed when I try to render it.

            fscanf(pFile, "%d", &pMesh[iMesh].TriTotal);
            pMesh[iMesh].TriSize = pMesh[iMesh].TriTotal * 3;
            pMesh[iMesh].pTri = (STriangle*) malloc(sizeof (STriangle) * pMesh[iMesh].TriTotal);
            
            for (int i = 0; i < pMesh[iMesh].TriTotal; ++i)
            {
                fscanf(pFile, "%d %d %d", &pMesh[iMesh].pTri[i].X, &pMesh[iMesh].pTri[i].Y, &pMesh[iMesh].pTri[i].Z);
            }

Recently I tried to malloc the pTri by times it with TriSize instead of TriTotal and the program didn't crashed.

           fscanf(pFile, "%d", &pMesh[iMesh].TriTotal);
            pMesh[iMesh].TriSize = pMesh[iMesh].TriTotal * 3;
            pMesh[iMesh].pTri = (STriangle*) malloc(sizeof (STriangle) * pMesh[iMesh].TriSize);
            
            for (int i = 0; i < pMesh[iMesh].TriTotal; ++i)
            {
                fscanf(pFile, "%d %d %d", &pMesh[iMesh].pTri[i].X, &pMesh[iMesh].pTri[i].Y, &pMesh[iMesh].pTri[i].Z);
            }

Currently I use Windows SDK which provides OpenGL 1.1 (study purpose). Soon if I can get this things work I'm going to move on with VBO.

Edited by DavidJr

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!