Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Loading VBOs at runtime

  1. #1
    Newbie Newbie
    Join Date
    Jul 2013
    Posts
    2

    Loading VBOs at runtime

    I am trying to load models in at run time, say when a user presses a button. However when it comes round to draw the loaded model, I get the following exception and nothing gets drawn:
    First-chance exception at 0x54E37020 (nvoglv32.dll) in OpenGLProject.exe: 0xC0000005: Access violation reading location 0x00000000

    If however I use the same code before I call glutMainLoop() its all fine

    Is there some kind of limitation as to how and when you can create VBOs and what not? This is the code I use to load my models:

    Code :
    for(MeshNonProcessed m_unProcessed : meshes)
    {
    	Mesh mesh;
    	glGenBuffers(1, &mesh.Vertex_VBO);
    	glBindBuffer(GL_ARRAY_BUFFER, mesh.Vertex_VBO);
    	glBufferData(GL_ARRAY_BUFFER, m_unProcessed.Vertices.size() * sizeof(Vertex), m_unProcessed.Vertices.data(), GL_STATIC_DRAW);
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
     
    	glGenBuffers(1, &mesh.Element_VBO);
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.Element_VBO);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_unProcessed.Elements.size() * sizeof(unsigned int),
    		m_unProcessed .Elements.data(), GL_STATIC_DRAW);
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    	mesh.Element_Count = m_unProcessed.Elements.size();
     
    	glGenVertexArrays(1, &mesh.VAO);
    	glBindVertexArray(mesh.VAO);
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.Element_VBO);
    	glBindBuffer(GL_ARRAY_BUFFER, mesh.Vertex_VBO);
            // bind vertex attributes
    	glEnableVertexAttribArray(0);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));
    	glEnableVertexAttribArray(1);
    	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 3));
    	glEnableVertexAttribArray(2);
    	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 6));
    	glEnableVertexAttribArray(3);
    	glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 8));
    	glEnableVertexAttribArray(4);
    	glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 11));
    	glEnableVertexAttribArray(5);
    	glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 14));
     
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
    	glBindVertexArray(0);
    }

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    491
    Quote Originally Posted by killermud View Post
    Code :
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
    	glBindVertexArray(0);
    This leaves GL_ELEMENT_ARRAY_BUFFER unbound within the VAO, so any subsequent glDrawElements() (or similar) call while the VAO is bound will interpret its pointer argument as a pointer to client memory, not as a buffer offset.

    The behaviour of GL_ELEMENT_ARRAY_BUFFER is different from that of GL_ARRAY_BUFFER, which isn't part of the VAO state. Instead, the buffer bound to GL_ARRAY_BUFFER is stored as part of the VAO state for that attribute whenever glVertexAttribPointer() is called.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •