OpenGL update parts of VBO (point cloud) - c++

I have a udp client for reading points from a server that generates points in 4 msec and each packet have near 3000 points. i read this points and map them to a matrix with 1 million elements of coordinates. (i create a hash table from streamed points with fixed keys. i mean you can think of a 1000 keys and each key has maximum 1000 points but its varying. after completing this table any new value i get from server, map to my keys in a sorted manner, i mean, i updated points in smaller keys first and ... :D
i really shamed i cant do this, my LCD with 1990x1080 pixels do this so faster than me!! :-((
i want to insert this to a VBO and then upgrading each key parts in 30 ms.
so i want to create 1000 vbos for drawing and 1000 vbos for buffering and after each 33 ms iteration i change 50 vbos of drawing ones with buffering ones. each vbo has maximum 0f 1000 points and colors, and number of points in one VBO change from 10 to 1000;
my problem start here i don't understand how VBO's work and best solution for implementation of this problem? :D
1- my first question: why i create a VAO and how its associated to new gen buffers?
its look like its associated to exactly next glGenBuffer() right? but in any sample i read i don't see any using of this pointer (just use for draw)? this is my code for creating a vbo i downloaded from somewhere
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
// First see if the vertex array buffer has been created...
if(uiVertexArray == 0) { // Nope, we need to create it
glGenBuffers(1, &uiVertexArray);
glBindBuffer(GL_ARRAY_BUFFER, uiVertexArray);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * nNumVerts, NULL, GL_DYNAMIC_DRAW);
}
// Now see if it's already mapped, if not, map it
if(pVerts == NULL) {
glBindBuffer(GL_ARRAY_BUFFER, uiVertexArray);
pVerts = (M3DVector3f *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
}
// Ignore if we go past the end, keeps things from blowing up
if(nVertsBuilding >= nNumVerts)
return;
// Copy it in...
pVerts[nVertsBuilding][0] = x;
pVerts[nVertsBuilding][1] = y;
pVerts[nVertsBuilding][2] = z;
nVertsBuilding++;
for color and texture mapping its look like. which has better performance (GL_DYNAMIC_DRAW, GL_STREAM_DRAW)
2- this is a draw function? why use last line? for creating the next VBO and VAO?
// Set up the vertex array object
glBindVertexArray(vertexArrayObject);
glDrawArrays(primitiveType, 0, nNumVerts);
glBindVertexArray(0);
3- in the next time i want change a buffered VBO contents with new points i must do this all again for different number of points and if i do this i must delete last buffer of VBO or not?
// Vertex buffer objects
if(uiVertexArray != 0)
glDeleteBuffers(1, &uiVertexArray);
4- can i resize my VBOs and don't use recreate buffer VBO's?
5- How to copy data from buffer VBO's to draw VBO's?
this is my swap buffers with draw VBO's in main draw function, i recreate VBO data from first in swap function
for(int i=0; i<1000; i++)
if(must_show_buffer(i))
{
bufferVBO[i].draw();
drawedVBO[i].swap(bufferVBO[i]);
}
else drawedVBO[i].draw();
UPDATE:
i use this functions for swap data:
if(batch->cVerts != NULL)
{
CopyVertexData3f(batch->cVerts);
CopyColorData4f(batch->cColors);
}
End();
void GLBatch::CopyColorData4f(M3DVector4f *vColors)
{
// First time, create the buffer object, allocate the space
if(uiColorArray == 0) {
glGenBuffers(1, &uiColorArray);
glBindBuffer(GL_ARRAY_BUFFER, uiColorArray);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4 * nNumVerts, vColors, GL_DYNAMIC_DRAW);
}
else { // Just bind to existing object
glBindBuffer(GL_ARRAY_BUFFER, uiColorArray);
// Copy the data in
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * 4 * nNumVerts, vColors);
pColors = NULL;
}
}
this approach is work correct but glBufferSubData increase memory usage so fast and i get a memory out exception
and this approach
if(batch->uiVertexArray != NULL)
{
// First time, create the buffer object, allocate the space
if(uiVertexArray == 0) {
glGenBuffers(1, &uiVertexArray);
glBindBuffer(GL_ARRAY_BUFFER, uiVertexArray);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * nNumVerts, NULL, GL_DYNAMIC_DRAW);
}
// Fast copy data
glBindBuffer(GL_COPY_READ_BUFFER, batch->uiVertexArray);
glBindBuffer(GL_COPY_WRITE_BUFFER, uiVertexArray);
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, nNumVerts);
glBindBuffer(GL_COPY_READ_BUFFER, 0);
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
//copy color data
if(uiColorArray == 0) {
glGenBuffers(1, &uiColorArray);
glBindBuffer(GL_ARRAY_BUFFER, uiColorArray);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4 * nNumVerts, NULL, GL_DYNAMIC_DRAW);
}
glBindBuffer(GL_COPY_READ_BUFFER, batch->uiColorArray);
glBindBuffer(GL_COPY_WRITE_BUFFER, uiColorArray);
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, nNumVerts);
glBindBuffer(GL_COPY_READ_BUFFER, 0);
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
}
without memory allocation problem but doesn't clear before points
what i miss in each implementation?

VBOs are associated to the currently bound VAO during glVertexAttribPointer
unbinding buffers is a good practice, remember that bind state is global and the less global state you rely on the better
no need to delete the VBO you can just skip the glGenBuffers(1, &uiVertexArray); call
yes a new call to glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * nNumVerts, NULL, GL_DYNAMIC_DRAW); with the new nNumVerts will resize the buffer (and thrash any existing data)
you can do the following:
glBindBuffer(GL_COPY_READ_BUFFER, bufferVBO);
glBindBuffer(GL_COPY_WRITE_BUFFER, drawVBO);
glCopyBufferSubData(GL_COPY_READ_BUFFER​, GL_COPY_WRITE_BUFFER​, 0​, 0, size​);
glBindBuffer(GL_COPY_READ_BUFFER, 0);
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
I have written out 700k points/s to the screen no problem simply by uploading them all into a single VBO and drawing that.

Related

glBufferSubData setting data from 0 rather than supplied offset

Don't have a whole lot to preface with other than this is being used in/for a particle system that uses transform feedback. There are two vbos that I'm ping ponging between render loops. Here's the initial buffer setup:
glGenBuffers(1, &p_vbo_r); glBindBuffer(GL_ARRAY_BUFFER, p_vbo_r);
glBufferData(GL_ARRAY_BUFFER, (MAX_PARTICLES*2) * sizeof(particle), &pp[0], GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &p_vbo_w); glBindBuffer(GL_ARRAY_BUFFER, p_vbo_w);
glBufferData(GL_ARRAY_BUFFER, (MAX_PARTICLES*2) * sizeof(particle), &pp[0], GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
MAX_PARTICLES is set to 262144. particle is a struct that contains 2 glm::vec3 and 2 GLfloat totaling to 32 bytes, so my current setup creates buffers that are slightly over 16mb. pp is an array containing dummy particle data since straight 0s don't stick out as much when using gDebugger.
glBindBuffer(GL_ARRAY_BUFFER, p_vbo_r);
glBufferSubData(GL_ARRAY_BUFFER, total_part*sizeof(particle), pp.size() * sizeof(particle), &pp[0]);
glBindBuffer(GL_ARRAY_BUFFER, p_vbo_w);
glBufferSubData(GL_ARRAY_BUFFER, total_part*sizeof(particle), pp.size() * sizeof(particle), &pp[0]);
p.buf_start = total_part;
total_part += pp.size();
This happens every time a unique emitter is added to the particle system. All possible emitters in a given scene are added before any rendering is done(currently) and this actually works as intended! It stores the offset used when the emitter was added into p.buf_start when it's done and adds to the running offset.
Now, curiously enough using the same set up later during rendering/execution causes the glBufferSubData command to start from 0 rather than the supplied offset, as so:
glBindBuffer(GL_ARRAY_BUFFER, p_vbo_r);
glBufferSubData(GL_ARRAY_BUFFER, p_sys[i].buf_start * sizeof(particle), pp.size() * sizeof(particle), &pp[0]);
glBindBuffer(GL_ARRAY_BUFFER, p_vbo_w);
glBufferSubData(GL_ARRAY_BUFFER, p_sys[i].buf_start * sizeof(particle), pp.size() * sizeof(particle), &pp[0]);
This is done in a loop that checks if the buffer needs reset and does so. Any actor may request a reset at any time after rendering, and the buffer does the above code after the frame is finished but before the next frame starts rendering. The first emitter listed works as intended, but any emitter afterwards always overwrites the VBOs from 0 and not from the given offset. Am I unable to overwrite ping-ponging buffers involved in transform feedback? It's been a little tricky trying to figure this one out. Thanks!
EDIT: I've also tried GL_DYNAMIC_DRAW/GL_STATIC_DRAW for the initial buffer setup to no avail.

OpenGL will only update the last vertex array object

I am writing some code that generates some VAO and then when the physics have been updated a call is made to update the vertices inside the VAOs and then a call is made to redraw these objects.
The problem with my code is that only the last VAO is being updated by UpdateScene. The following two functions create the buffers.
void BuildBuffers(std::vector<demolish::Object>& objects)
{
VAO = new UINT[objects.size()];
glGenVertexArrays(objects.size(),VAO);
int counter = 0;
for(auto& o:objects)
{
if(o.getIsSphere())
{
BuildSphereBuffer(o.getRad(),o.getLocation(),counter);
counter++;
}
else
{
}
}
}
void BuildSphereBuffer(float radius,std::array<iREAL,3> position,int counter)
{
GeometryGenerator::MeshData meshObj;
geoGenObjects.push_back(meshObj);
geoGen.CreateSphere(radius,30,30,meshObj,position);
VAOIndexCounts.push_back(meshObj.Indices.size());
glGenBuffers(2,BUFFERS);
glBindVertexArray(VAO[counter]);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,BUFFERS[0]);
glBufferData(GL_ARRAY_BUFFER,
meshObj.Vertices.size()*sizeof(GLfloat)*11,
&meshObj.Vertices.front(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BUFFERS[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
meshObj.Indices.size() * sizeof(UINT),
&meshObj.Indices.front(), GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT,sizeof(GLfloat)*11, 0);
glNormalPointer(GL_FLOAT,sizeof(GLfloat)*11,(GLvoid*)(3*sizeof(GLfloat)));
}
Then the following function updates the buffers when it is called.
void UpdateScene(float dt, std::vector<demolish::Object>& objects)
{
float x = radius*sinf(phi)*cosf(theta);
float z = radius*sinf(phi)*sinf(theta);
float y = radius*cosf(phi);
AV4FLOAT position(x,y,z,1.0);
AV4FLOAT target(0.0,0.0,0.0,0.0);
AV4FLOAT up(0.0,1.0,0.0,0.0);
viewModelMatrix = formViewModelMatrix(position,target,up);
for(int i=0;i<objects.size();i++)
{
geoGen.CreateSphere(objects[i].getRad(),
30,
30,
geoGenObjects[i],
objects[i].getLocation());
VAOIndexCounts[i] = geoGenObjects[i].Indices.size();
glBindVertexArray(VAO[i]);
glBufferSubData(GL_ARRAY_BUFFER,
0,
geoGenObjects[i].Vertices.size()*sizeof(GLfloat)*11,
&geoGenObjects[i].Vertices.front());
}
RedrawTheWindow();
}
The problem with this code is that it is not updating all of the buffers, only the "last" one. For instance if objects has size 3 then even if the locations of all three objects change only the last buffer is being updated with the new vertices.
I have narrowed it down to OpenGL but I am not sure what I am doing wrong.
Binding the Vertex Array Object doesn't bind any array buffer object.
If you want to change the content of an array buffer, then you have to bind the array buffer:
GLuint VBO = .....; // VBO which corresponds to VAO[i]
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(
GL_ARRAY_BUFFER, 0,
geoGenObjects[i].Vertices.size()*sizeof(GLfloat)*11,
&geoGenObjects[i].Vertices.front());
Note, a vertex array object may refer to a different array buffer object, for each attribute. So which one should be bound?
Since OpenGL 4.5 you can do this by the direct state access version too.
See glNamedBufferSubData:
glNamedBufferSubData (
VBO, 0,
geoGenObjects[i].Vertices.size()*sizeof(GLfloat)*11,
&geoGenObjects[i].Vertices.front());
If the vertex array object is bound, then a named array buffer object which is bound to a binding point can be queried by glGetVertexAttribIuiv using the parameter GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
e.g.:
glBindVertexArray(VAO[i]);
GLuint VBO;
glGetVertexAttribIuiv(0, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &VBO);

OpenGL 3.3 Batch Rendering - Triangle doesn't show up

I'm trying to implement a batch-rendering system using OpenGL, but the triangle I'm trying to render doesn't show up.
In the constructor of my Renderer-class, I'm initializing the VBO and VAO and also load my shader program (this does work, so the error can't be found here). The VBO is supposed to be capable of holding the maximum amount of vertices I'll permit which is defined in the header to be 30000. The VAO contains the information about how the data that I'll store in that buffer is laid out - in this case I use a struct called VertexData which only contains a 3D-vector ('vertex'), but will also contain stuff like colors etc. later on. So I create the buffer with the size I already stated, don't fill in any content yet and provide the layout using 'glVertexAttribPointer'. The '_vertexCount', as the name implies, counts the amount of vertices currently stored inside that buffer for drawing purposes.
The constructor of my Renderer-class (note that every private member variable defined in the header file starts with an _ ):
Renderer::Renderer(std::string vertexShaderPath, std::string fragmentShaderPath) {
_shaderProgram = ShaderLoader::createProgram(vertexShaderPath, fragmentShaderPath);
glGenBuffers(1, &_vbo);
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, RENDERER_MAX_VERTICES * sizeof(VertexData), NULL, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) 0);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
_vertexCount = 0;
}
Once the initization is done, to render anything, the 'begin' procedure has to be called during the main-loop. This gets the current buffer with write permissions to fill in the vertices that should be rendered in the current frame:
void Renderer::begin() {
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
_buffer = (VertexData*) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
}
After beginning, the 'submit' procedure can be called to add vertices and their corrosponding data to the buffer. I add the data to the location in memory the buffer currently points to, then advance the buffer and increase the vertexcount:
void Renderer::submit(VertexData* data) {
_buffer = data;
_buffer++;
_vertexCount++;
}
Finally, once all vertices are pushed to the buffer, the 'end' procedure will unmap the buffer to enable the actual rendering of the vertices, bind the VAO, use the shader program, render the provided vertices as triangles, unbind the VAO and reset the vertex count:
void Renderer::end() {
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(_vao);
glUseProgram(_shaderProgram);
glDrawArrays(GL_TRIANGLES, 0, _vertexCount);
glBindVertexArray(0);
_vertexCount = 0;
}
In the main loop I'm beginning the rendering, submitting three vertices to render a simple triangle and ending the rendering process. This is the most important part of that file:
Renderer renderer("../sdr/basicVertex.glsl", "../sdr/basicFragment.glsl");
Renderer::VertexData one;
one.vertex = glm::vec3(-1.0f, 1.0f, 0.0f);
Renderer::VertexData two;
two.vertex = glm::vec3( 1.0f, 1.0f, 0.0f);
Renderer::VertexData three;
three.vertex = glm::vec3( 0.0f,-1.0f, 0.0f);
...
while (running) {
...
renderer.begin();
renderer.submit(&one);
renderer.submit(&two);
renderer.submit(&three);
renderer.end();
SDL_GL_SwapWindow(mainWindow);
}
This may not be the most efficient way of doing this and I'm open for criticism, but my biggest problem is that nothing appears at all. The problem has to lie within those code snippets, but I can't find it - I'm a newbie when it comes to OpenGL, so help is greatly appreciated. If full source code is required, I'll post it using pastebin, but I'm about 99% sure that I did something wrong in those code snippets.
Thank you very much!
You have the vertex attribute disabled when you make the draw call. This part of the setup code looks fine:
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, RENDERER_MAX_VERTICES * sizeof(VertexData), NULL, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) 0);
At this point, the attribute is set up and enabled. But this is followed by:
glDisableVertexAttribArray(0);
Now the attribute is disabled, and there's nothing else in the posted code that enables it again. So when you make the draw call, you don't have a vertex attribute that is actually enabled.
You can simply remove the glDisableVertexAttribArray() call to fix this.
Another problem in your code is the submit() method:
void Renderer::submit(VertexData* data) {
_buffer = data;
_buffer++;
_vertexCount++;
}
Both _buffer and data are pointers to a VertexData structure. So the assignment:
_buffer = data;
is a pointer assignment. Instead of copying the data into the buffer, it modifies the buffer pointer. This should be:
*_buffer = *data;
This will copy the vertex data into the buffer, and leave the buffer pointer unchanged until you explicitly increment it in the next statement.

OpenGL updating vertices array/buffer

When I first add some vertices to the buffer, these are the relevant functions I'm calling
// Create and bind the object's Vertex Array Object:
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);
// Create and load vertex data into a Vertex Buffer Object:
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW);
// Tells OpenGL that there is vertex data in this buffer object and what form that vertex data takes:
// Obtain attribute handles:
_posAttrib = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(_posAttrib);
glVertexAttribPointer(_posAttrib, // attribute handle
4, // number of scalars per vertex
GL_FLOAT, // scalar type
GL_FALSE,
0,
0);
// Unbind vertex array:
glBindVertexArray(0);
But later on in my program, I want to add some more vertices.
I do this by the following (within a separate function:
add_vertices(x,y); //adds the necessary vertices to the vector.
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, (TRIANGLE_AMOUNT+1)*4*_number_of_circles * sizeof(float), &vertices[0], GL_STATIC_DRAW);
Assuming the funky size in the 2nd argument of glBufferData is fine, am I missing anything? Are there any other OpenGL functions that need calling?
I'm not getting any errors, but when I'm trying to draw the extra shapes with the new vertices by looping over glDrawArrays with different subsets of the vertices, nothing happens. Only the first shape gets drawn.
I hope that this is semi-coherent...let me know if there's any info I haven't provided.
Cheers.
In OpenGL, changing the buffers and exchanging the buffers data are two different things which require different actions to be taken afterwards:
Exchanging the data
In this case a previously generated vbo is required. To upload new data, it is only required to bind the buffer and to buffer new data:
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, (TRIANGLE_AMOUNT+1)*4*_number_of_circles * sizeof(float),
&vertices[0], GL_STATIC_DRAW);
Creating a new buffer
In this case a new buffer is generated by glGenerateBuffers and (in addition to uploading the data) all VAO bindings have to be updated.
Sidenote: In the code shown above, you create a new buffer without deleting the previous buffer.

GL Error: Out of Memory when trying to render with VBO

I've been trying to use Vertex Buffer Objects to save vertex data on the GPU and reduce the overhead, but I cannot get it to work. The code is below.
From what I understand you generate the buffer with glGenBuffers, then you bind the buffer with glBindBuffer so it's ready to be used, then you write data to it with glBufferData and its done and can be unbinded and ready for use later with simply binding it again.
However the last part is what I'm having trouble with, when I bind it after I have created and loaded data to it and try to draw using it, it gives me lots of GL Error: Out of Memory.
I doubt that I am running out of memory for my simple mesh, so I must be doing something very wrong.
Thanks.
EDIT 1: I call glGetError after every frame, but since this is the only OpenGL I do in the entire program it shouldn't be a problem
//when loading the mesh we create the VBO
void createBuffer()
{
GLuint buf;
glGenBuffers(1, &buf);
glBindBuffer(GL_ARRAY_BUFFER, buf);
glBufferData(GL_ARRAY_BUFFER, vertNormalBuffer->size() * sizeof(GLfloat), (GLvoid*) bufferData, GL_STATIC_DRAW);
//EDIT 1: forgot to show how I handle the buffer
model->vertexNormalBuffer = &buf;
//Unbinds it
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Fighter::doRedraw(GLuint shaderProgram)
{
glm::mat4 transformationMatrix = getTransform();
GLuint loc = glGetUniformLocation(shaderProgram,"modelviewMatrix");
glUniformMatrix4fv(loc, 1, GL_FALSE, (GLfloat*) &transformationMatrix);
glBindBuffer(GL_ARRAY_BUFFER, *model->vertexNormalBuffer);
//If I uncomment this line below all works wonderfully, but isnt the purpose of VBO of not uploading the same data again and again?
//glBufferData(GL_ARRAY_BUFFER, model->vertAndNormalArraySize * sizeof(GLfloat), model->vertAndNormalArray, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(2);
renderChild(model, model);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Fighter::renderChild(ModelObject* model, ModelObject* parent)
{
//Recursively render the mesh children
for(int i = 0; i < model->nChildren; i++)
{
renderChild( dynamic_cast<ModelObject*>(model->children[i]), parent);
}
//Vertex and normal data are interlieved
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat),(void*)(model- >vertexDataPosition*sizeof(GLfloat)));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)((model->vertexDataPosition + 4)*sizeof(GLfloat)));
//Draws using two sets of indices
glDrawElements(GL_QUADS, model->nQuads * 4, GL_UNSIGNED_INT,(void*) model->quadsIndices);
glDrawElements(GL_TRIANGLES, model->nTriangles * 3, GL_UNSIGNED_INT, (void*) model->trisIndices);
}
This is your problem:
model->vertexNormalBuffer = &buf;
/* ... */
glBindBuffer(GL_ARRAY_BUFFER, *model->vertexNormalBuffer);
You're storing the address of your buf variable, rather than its contents, and then it falls out of scope when createBuffer returns, and is most likely overwritten with other data, so when you're later rendering, you're using an uninitialized buffer. Just store the contents of buf in your vertexNormalBuffer field instead.
I'll admit I don't know why OpenGL thinks it proper to say that it's "out of memory" just because of that, but perhaps you're just invoking undefined behavior. It does explain, however, why it starts working when you re-fill the buffer with data after you rebind it, because you then implicitly initialize the buffer that you just bound.