OpenGL line drawing issue when using glDrawArrays

Viewed 105

When I am trying to draw a line using legacy openGL , lines are drawing fine.

glEnable(GL_LINE_STIPPLE);
glBegin(GL_LINE_LOOP);
for (size_t idx = 0; idx < m_spline_cvs.size(); idx++) {
    glVertex2f(m_cv_positions[0][idx].x,m_cv_positions[0][idx].y);
}
glEnd();
glDisable(GL_LINE_STIPPLE);

Correct Line Loop in Stipple mode

But when I am trying to use **glDrawArrays**. Something is going wrong?

std::vector <float> cv_tracker_target_line;

for (size_t idx = 0; idx < m_spline_cvs.size(); idx++) {
    cv_tracker_target_line.push_back(m_cv_positions[0][idx].x);
    cv_tracker_target_line.push_back(m_cv_positions[0][idx].y);
}
glEnable(GL_LINE_STIPPLE);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, &cv_tracker_target_line[0]);
glDrawArrays(GL_LINE_LOOP, 0, cv_tracker_target_line.size());
glDisable(GL_LINE_STIPPLE);
glDisableClientState(GL_VERTEX_ARRAY);

Wrong Lines drawn not in loop

What am I doing wrong here?

1 Answers

The 3rd argument in glDrawArrays is the number of vertices, but not the number of elements (floats) in the array. Each vertex coordinate consists of 2 components (x, y):

glDrawArrays(GL_LINE_LOOP, 0, cv_tracker_target_line.size());

glDrawArrays(GL_LINE_LOOP, 0, cv_tracker_target_line.size() / 2);
Related