PDA

View Full Version : Some clarification about glBufferSubData and glMapBuffer



Lee_Jennifer_82
12-25-2015, 03:55 PM
Hello, I'm trying to update my object (vertices) using timerfunc. I have done the update using both glMapBuffer and glBufferSubData. Now here, vertex update is a function of time (sine function). From the output, I find, the result is not exactly the same for these two methods. Is time a factor here. But when I print the elapsed time for each update, it is using more or less the same time interval for update function. I used the same vertex update function for both cases. My question, why does the output look different for glMapBuffer and glBufferSubData. It'd be very kind of you to clarify this problem.
Thanks in advance!

Alfonse Reinheart
12-25-2015, 10:07 PM
If the time is "more or less the same time interval", then pretty much by definition it is not the same. So there's no reason to expect the same outputs when given different inputs.

If you want consistency, then you need to quantize your timesteps. Granted, I don't know why you need consistency to begin with, but whatever.

Lee_Jennifer_82
12-25-2015, 11:14 PM
Thanks for the reply. I've tried with same timesteps, but output not the same. I think, I could not update properly using glBufferSubData. But I couldn't figure out the errors. I have done as follows:




Initialize(){
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(2, vbo);

glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(vec3), NULL, GL_DYNAMIC_DRAW);

glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0); // position

glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, normals.size()*sizeof(vec3), &normals[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1); // normal

glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(int), &indices[0], GL_STATIC_DRAW);

glBindVertexArray(0);

}

void update(int n) //the "glutTimerFunc"
{
Float time2 = (float)timer.getElapsedTime();

if (update){


update(tempVert, normals, tempVert.size(), time2);


vertices.clear();
vertices.resize(tempVert.size());

for (int i = 0; i < vertices.size(); i++){
vertices[i].x = tempVert[i].x;
vertices[i].y = tempVert[i].y;
vertices[i].z = tempVert[i].z;
}

glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);

glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size()*sizeof(glm::vec3), &vertices[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);

}


glutPostRedisplay();
glutTimerFunc(500, update, n);

}

draw(){

glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);

glutSwapBuffers();


}




Please help me figure out what I'm doing wrong with glBufferSubData.

ajith.mk
12-25-2015, 11:42 PM
gMapBuffer maps the pointer from GPU memory to your application memory. So you write directly to GPU memory through the pointer provided to you. On the other hand, using glBufferSubData you copy into your buffer from application. This data in buffer is "later" copied into GPU memory by the opengl.

Having said that, I dont think your issue is related to glBufferSubData. Try narrowing down the issue. Like you can define a ofstream variable and modify the code in if(update) so that every time it updates, print them out in the file. See if you get consistent output there. By the way you cant precisely get the exact interval using TimerFunc. Not even if you use windows API directly.

Lee_Jennifer_82
12-26-2015, 11:17 AM
Thank you. I keep on trying, but still unclear what's going wrong. In some java program, I found that some 'flip' function is used for glBufferSubData. What does it mean in C or C++? I am worried I might be missing something. From my understanding both output should be the same.