-2

Unless I am mistaken, "Access violation reading location 0x00000000" means you are trying to deference a pointer that has yet to be initialized, which is why I'm stumped at this error. Pasted below is my code, with a comment indicating where the visual studio debugger tells me the error is occurring. It confuses me because none of the arguments I pass into the function are pointers. Any ideas?

void Mesh::Render(Shader* shader)
{
    glBindVertexArray(m_vao);
    glEnableVertexAttribArray(shader->GetAttributeLocation("position"));
    glVertexAttribPointer(0, 3, GL_FALSE, GL_FALSE, sizeof(Vertex), 0);

    glDrawElements(GL_TRIANGLES, m_size, GL_UNSIGNED_INT, 0); // error here

    glDisableVertexAttribArray(shader->GetAttributeLocation("position"));
    glBindVertexArray(0);
}

m_size is declared as a non pointer integer

And if it helps at all, the debugger takes me to some source that is not available, and so the debugger instead points to this line in the disassembly:

001DFEC7  mov         edi,dword ptr [esi]  

I don't know assembly so I'm not sure if that is even of any help.

EDIT

In case anyone was wondering, I am binding the element array buffer required for using VAOs. The rest of the Mesh class is below

Mesh::Mesh()
{
    glGenVertexArrays(1, &m_vao);
    glGenBuffers(1, &m_vbo);
    glGenBuffers(1, &m_ibo);
    m_size = 0;
}

Mesh::~Mesh()
{
    glDeleteVertexArrays(1, &m_vao);
    glDeleteBuffers(1, &m_vbo);
    glDeleteBuffers(1, &m_ibo);
}

void Mesh::AddVertices(Vertex* vertices, int vertSize, int* indices, int indexSize)
{
    m_size = indexSize;

    glBindVertexArray(m_vao);

    glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    glBufferData(GL_ARRAY_BUFFER, vertSize * sizeof(Vertex), vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize * sizeof(int), indices, GL_STATIC_DRAW);

}
BossLetsPlays
  • 121
  • 1
  • 14
  • 2
    sure there are pointers. `this` is a pointer, `shader` is a pointer, the last argument to `glVertexAttribPointer` is a pointer (which you have set to zero....) – Richard Hodges Apr 02 '16 at 22:43
  • Are **m_vao** and other variables used here all global ? – Arif Burhan Apr 02 '16 at 22:43
  • [As mentioned in this question](http://stackoverflow.com/q/23026612/1287251) (and others), you've bound an invalid buffer which is being accessed only once `glDrawElements` is called. You should test all your calls to OpenGL functions to see if any of them are failing. It's possible one of them has failed, so you don't have a valid buffer bound but yet you still try to proceed with rendering. – Cornstalks Apr 02 '16 at 22:45
  • Since you're using a vertex array object (`m_vao`), it must have a GL_ELEMENT_ARRAY_BUFFER bound to it if you want to render using `glDrawElements` (and other index-based rendering calls). While you haven't shown the code for setting up your VAO, that's the probable problem. Try binding and loading an element array with your indices. – radical7 Apr 02 '16 at 22:54
  • @ArifBurhan `m_vao` and other variables are members of the Mesh class. @Cornstalks Well my breakpoints lead me to be looking at `glDrawElements` and upon others saying I can't use 0 as the last parameter (which should work just fine) I changed it and got the same error but different location, so I'm not sure whats going on. – BossLetsPlays Apr 02 '16 at 23:04
  • @radical7 I'll update the post so people who have that same thought can see it too, but I do have an element array buffer bound – BossLetsPlays Apr 02 '16 at 23:05
  • @BossLetsPlay Was the element array bound into the VAO? – radical7 Apr 02 '16 at 23:07
  • @radical7 Yes, unless I missed something really stupid, both the element and the vertex buffer objects are bound to the VAO – BossLetsPlays Apr 02 '16 at 23:09

2 Answers2

2

The other two answers here are only partially correct. glDrawElements expects the last parameter to be a pointer to some indices. However, contrary to what other answers suggest, it's okay if this parameter is 0 (NULL) because:

the indices parameter of glDrawElements [...] is interpreted as an offset within the buffer object measured in basic machine units.

if you call glBindBuffer with GL_ELEMENT_ARRAY_BUFFER.

If this is what you're attempting, the segfault means that a previous call to an OpenGL function has failed and you haven't bound a valid buffer. When you call glDrawElements, it doesn't have a valid buffer to work with, so it segfaults. Check every OpenGL function call for success to find which one has failed.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • It's more likely that the the OP just didn't bind an element-array buffer than having a `glBindBuffer` fail. Vertex array objects (VAOs) are meant as a container for all that vertex-related data: vertex buffers objects (VBOs) and element arrays, and so that you could just do a single bind call, and update all the buffer bindings. – radical7 Apr 02 '16 at 23:08
  • I previously did not know about glGetError so I'm going to mark this as the right answer, only difference was the function you believed would be causing my issues. The actual cause it turns out was I had mistakenly put `GL_FALSE` in the 3rd parameter of `glVertexAttribPointer` instead of `GL_FLOAT` as it was meant to be. – BossLetsPlays Apr 02 '16 at 23:23
-1

The last argument, according to this, is a "a pointer to the location where the indices are stored."

You have a 0. So it tries to dereference location 0.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • The statement about a pointer that you reference is only valid when OpenGL is using client-side vertex arrays. The OP's code uses a server-side vertex array (specified by the call to `glBindVertexArray`), which changes the meaning of the last parameter to being an offset into the bound element array buffer, and not a pointer. – radical7 Apr 02 '16 at 22:56