Reputation: 125
I have problem rendering many circles on the screen using these code.
float degree = 0;
unsigned int ctr = 0;
for(int xi = -3200; xi < 3200; xi+= 2*r)
{
for(int yi = 4800; yi > -4800; yi-= 2*r)
{
for(int i = 0; i < 360; ++i)
{
vertices.push_back(xi + r * cos(float(degree)));
vertices.push_back(yi + r * sin(float(degree)));
vertices.push_back(-8);
indices.push_back(i+ctr);
++degree;
}
ctr += 360;
degree = 0;
}
}
unsigned int i = 0;
for(i = 0; i < indices.size()/360; ++i)
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &vertices[i*360]);
glLineWidth(1);
glDrawElements(GL_LINE_LOOP, 360, GL_UNSIGNED_INT, &indices[i*360]);
glDisableClientState(GL_VERTEX_ARRAY);
}
Here is the result
In addition, the program crashes when I change xi value to [-6400, 6400]
Upvotes: 0
Views: 2702
Reputation: 35933
Leaving aside the questionable nature of this technique, you look to be accessing the indices incorrectly.
glVertexPointer(3, GL_FLOAT, 0, &vertices[i*360]);
glDrawElements(GL_LINE_LOOP, 360, GL_UNSIGNED_INT, &indices[i*360]);
The indices of glDrawElements specify an offset from the vertices at glVertexPointer. You've defined the indices as relative to the start of the vertex buffer:
indices.push_back(i+ctr);
But you're moving the buffer offset for each circle you draw. So in your indices buffer, the second circle starts at index 360. But when you draw the second circle, you also move the vertex pointer such that index 360 is the 0th element of the pointer.
Then when you try to access index 360, you're actually accessing element 720 (360 + start of buffer @360).
Upvotes: 4