-3

i figured out how to make the bar pieces but my problem is with the positioning of each piece's bottom edge, i can't figure out how to make them perfectly stacked on each other, so most of them end up being on top of the other, making the bar look wrong. here's my function:

void StackedBar(void)
{
// Clear the Screen
glClear(GL_COLOR_BUFFER_BIT);

float colors[6][3] = { { 1,0,0 },{ 1,1,0 },{ 0,1,0 },{ 0,1,1 },{ 1,0,1 },{0,0,1} };
float data[6] = { 20,66,42,28,71,23 };
float x = 0, y = 0;
float dy;

glBegin(GL_QUADS);
for (int i = 0; i < 6; i++) {
    glColor3f(colors[i][0], colors[i][1], colors[i][2]);
    dy = data[i];
    glVertex2f(x, y);
    glVertex2f(x + 5, y);
    glVertex2f(x + 5, y + dy);
    glVertex2f(x, y + dy);
    y  = dy;
    //x +=5; incremented x just to see how the pieces are positioned
}
glEnd();

// force execution of GL commands
glFlush();
}

output:

result

result with x incremented with 5 just to see the pieces are positioned

narlingz
  • 9
  • 4

1 Answers1

0

If you want to stack the bars on top of each other, just increment y in each iteration by dy.

for (int i = 0; i < 6; i++) {
    glColor3f(colors[i][0], colors[i][1], colors[i][2]);
    dy = data[i];
    glVertex2f(x, y);
    glVertex2f(x + 5, y);
    glVertex2f(x + 5, y + dy);
    glVertex2f(x, y + dy);
    y += dy;
}
BDL
  • 21,052
  • 22
  • 49
  • 55