Reputation: 4946
If I do this:
glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions1)+sizeof(vertexPositions2), vertexPositions1, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertexPositions1), sizeof(vertexPositions2), vertexPositions2);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
I get correct shapes displayed. Now, if I replace these lines by:
glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions1)+sizeof(vertexPositions2), 0, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
I still get two correct shapes (while nothing has been added into the buffer). I suppose it is because the memory allocated for the buffer in both cases is the same. So in case 2, it actually uses vertices stored during case 1.
To check that, I just comment the line glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
. The program crashes. Then I uncomment it and I get a black screen as expected.
So, is it really the memory initialized in case 1 that is actually used in case 2 (even if I did not initialized my buffer with any vertex)? Then, how could I avoid such side-effects to detect uninitilaized memore sooner?
EDIT 1: using GL_STREAM_DRAW produces the same behavior
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions1)+sizeof(vertexPositions2), vertexPositions1, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertexPositions1), sizeof(vertexPositions2), vertexPositions2);
EDIT 2: "similar" use of uninitialized memory on CPU (I am not asking why that or the differences between CPU and GPU. Just stating that random uninitialized memory would help too (if this is the actual problem here of course):
int a[2];
for (unsigned int i = 0; i < 2; ++i)
{
std::cout << a[i] << std::endl;
}
a[0] = 1234; a[1] = 5678;
for (unsigned int i = 0; i < 2; ++i)
{
std::cout << a[i] << std::endl;
}
2 executions in a row will produce:
-858993460
-858993460
1234
5678
Upvotes: 0
Views: 587
Reputation: 950
Using a debugger tool might help you ( gDEBugger maybe ). Uninitialized memory is pretty much the same on the graphic card as it is on the main RAM. You could get the same kind of artifact reading random memory.
Upvotes: 2