I am using OPENGL ES for iOS, however this would presumably be applicable for any OPENGL scenario; I have an array of vertices but only want to draw some of them. In the following example, I want to draw a line from the first to the 2nd point, then the 4th to the 5th. However, the "inbetween" 3rd point is drawn, and not the 5th. I'm pretty sure I'm missing something obvious, I would appreciate a nudge in the right direction, thanks!
float xy[] = {
10.0f, 10.0f,
110.0f, 10.0f,
210.0f, 210.0f,
310.0f, 210.0f,
110.0f, 120.0f,
210.0f, 310.0f
};
glClearColor(0.2f, 0.5f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < 2; i++) {
glEnableClientState(GL_VERTEX_ARRAY);
int from = (i * 6);
glVertexPointer(2, GL_FLOAT, 0, &am开发者_运维问答p;xy[0]);
glDrawArrays(GL_LINE_STRIP, from, 4 );
glDisableClientState(GL_VERTEX_ARRAY);
};
In your code you only have to draw 2 vertices in each iteration, instead of 4. The last parameter to glDrawArrays is the vertex count, not the component (float) count.
Also, you can achieve the same results without a loop and with much less effort/code/time: Make an index array, containing the indices of the vertices you want to draw and then use glDrawElements instead of glDrawArrays.
float xy[] = { ... };
int indices[] = { 0, 1, 3, 4 };
glClearColor(...);
glClear(...);
glVertexPointer(...);
glEnableClientState(...);
glDrawElements(GL_LINES, 4, GL_UNSIGNED_INT, indices);
精彩评论