0

So, I have a matrix of pairs of x and y coordinates, each line in the matrix represents a route, which I would like to represent as a GL_LINE_STRIP in OpenGL. The thing is I would like to draw the lines with different colors each time. I thought my code would work, but somehow OpenGL keeps drawing the line_strips with the same color.

I thought this would do the work, xy is the matrix of pairs of coordinates:

static void Redraw(void)
{
    ...
    glClear(GL_COLOR_BUFFER_BIT);
   //drawing routes
    srand(time(NULL));
    for(int i = 0; i < xy.size(); i++)
    {   
        vector<pair<int, int>> route = xy[i];
        double r = ((double) rand() / (RAND_MAX));
        double g = ((double) rand() / (RAND_MAX));
        double b = ((double) rand() / (RAND_MAX));
        glColor3f(r,g,b);
        glLineWidth(2);
        glBegin(GL_LINE_STRIP);

        for(int j = 0; j < route.size();j++)
            glVertex2d(route[j].first, route[j].second);

        glEnd();
    }
    glFlush();
}

and my main:

int main(int argc,char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(1080,720);
    glutInitWindowPosition(0,0);
    glutCreateWindow("h_constante");
    gluOrtho2D(0,1000,0,1000);
    glutDisplayFunc(Redraw);
    glutMainLoop();


    return 0;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Actually, this should work. (OT: I would move the `srand(time(NULL));` to `main()`.) Have you tried to move the `glColor3f()` after the `glBegin()`? Though doc. for [`glColor()`](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glColor.xml) on khronos states that _The current color can be updated at any time._, but I haven't any better idea. – Scheff's Cat May 03 '19 at 05:25
  • 1
    To be clear: _I would like to draw the lines with different colors each time._ I assume you meant: I would like to draw the line**strips** with different colors each time? – Scheff's Cat May 03 '19 at 05:27
  • 1
    You're right, this part of the code is actually correct. I just figured it out, I was pushing all my nodes inside the first line in the matrix so I was actually drawing one big GL_LINE_STRIP. Thanks a lot for the help! – Arthur Cavalcante May 03 '19 at 05:30
  • Heads up. My OpenGL projects always start with a blue screen. (Clearing with blue color is the only thing, I get working from start...) ;-) (And, most time, these are issues like yours...) – Scheff's Cat May 03 '19 at 05:32

1 Answers1

0

I was pushing all my nodes inside the first line in the matrix so I was actually drawing one big GL_LINE_STRIP. Thanks a lot for all the help!